From 177572103ad42b1c935b038263b2c7a92c39ee9b Mon Sep 17 00:00:00 2001 From: "Mark M. Florida" <markflorida@wustl.edu> Date: Wed, 17 Feb 2016 14:28:48 -0600 Subject: [PATCH] Fixed root url processing. --- src/main/webapp/scripts/xnat/url.js | 33 +++++++++++++++++------------ 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main/webapp/scripts/xnat/url.js b/src/main/webapp/scripts/xnat/url.js index 06757cae..280abd6a 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) -- GitLab