diff --git a/src/main/webapp/scripts/xnat/url.js b/src/main/webapp/scripts/xnat/url.js index 06757cae2e2aec98a1ec88ec8d71ea314d247a44..280abd6ade08db549248a7fa6c24455fc4b85f44 100644 --- a/src/main/webapp/scripts/xnat/url.js +++ b/src/main/webapp/scripts/xnat/url.js @@ -19,23 +19,28 @@ var XNAT = getObject(XNAT||{}); // don't cache AJAX requests xhr.cache = firstDefined(xhr.cache||undefined, false); + // trim leading AND trailing slashes + function trimSlashes(str){ + return str.replace(/^\/+|\/+$/g,''); + } + + // chop off LEADING slashes + function chopSlashes(str){ + return str.replace(/^\/+/,''); + } + // fix potential duplicate url root prefixes function fixRoot(root, url){ - var rootParts, urlParts, newUrl; - // strips leading and trailing slashes - function trimSlashes(str){ - return str.replace(/^\/+|\/+$/g,''); - } - root = trimSlashes(root||''); - url = trimSlashes(url||''); - if (!root){ return '/' + url; } - rootParts = root.split('/'); - urlParts = url.split('/'); - while (rootParts.shift() === urlParts.shift()){ - // whittling away the arrays - newUrl = urlParts.join('/'); + var rootRegExp; + // remove slashes from both sides of root + root = trimSlashes(root); + // make sure url has only ONE leading slash + url = '/' + chopSlashes(url); + if (!root){ + return url; } - return '/' + (newUrl||url); + rootRegExp = new RegExp('^(\/' + root + ')+', 'i'); + return url.replace(rootRegExp, '/' + root); } // make sure the serverRoot string (and only ONE serverRoot string)