diff --git a/lib/core/utils/get-friendly-uri-end.js b/lib/core/utils/get-friendly-uri-end.js index 69b6dbe8c8..842cf3040d 100644 --- a/lib/core/utils/get-friendly-uri-end.js +++ b/lib/core/utils/get-friendly-uri-end.js @@ -19,6 +19,10 @@ function splitString (str, splitIndex) { return [str.substring(0, splitIndex), str.substring(splitIndex)]; } +function trimRight (str) { + return str.replace(/\s+$/, '') +} + /** * Take a relative or absolute URL and pull it into it's indivisual pieces * @@ -90,14 +94,14 @@ axe.utils.getFriendlyUriEnd = function getFriendlyUriEnd (uri = '', options = {} if (hash) { if (pathEnd && (pathEnd + hash).length <= maxLength) { - return pathEnd + hash; + return trimRight(pathEnd + hash); } else if (pathEnd.length < 2 && hash.length > 2 && hash.length <= maxLength) { - return hash; + return trimRight(hash); } else { return; } } else if (domain && domain.length < maxLength && path.length <= 1) {// '' or '/' - return domain + path; + return trimRight(domain + path); } // See if the domain should be returned @@ -106,7 +110,7 @@ axe.utils.getFriendlyUriEnd = function getFriendlyUriEnd (uri = '', options = {} domain !== currentDomain && (domain + path).length <= maxLength ) { - return domain + path; + return trimRight(domain + path); } const lastDotIndex = pathEnd.lastIndexOf('.'); @@ -119,6 +123,6 @@ axe.utils.getFriendlyUriEnd = function getFriendlyUriEnd (uri = '', options = {} // Exclude files that are likely to be database IDs !isMostlyNumbers(pathEnd) ) { - return pathEnd; + return trimRight(pathEnd); } }; diff --git a/test/core/utils/get-friendly-uri-end.js b/test/core/utils/get-friendly-uri-end.js index 49a1f959dd..d2cc9342e7 100644 --- a/test/core/utils/get-friendly-uri-end.js +++ b/test/core/utils/get-friendly-uri-end.js @@ -15,9 +15,16 @@ describe('axe.utils.getFriendlyUriEnd', function () { assert.equal('contact.html', getFriendlyUriEnd('/contact.html')); }); + it('trims whitespace', function () { + assert.equal(undefined, getFriendlyUriEnd(' ')); + assert.equal('start page', getFriendlyUriEnd('start page\t')); + assert.equal('home#heading', getFriendlyUriEnd('home#heading ')); + }); + it('returns a hash URI', function () { assert.equal('#footer', getFriendlyUriEnd('#footer')); assert.equal('contact.html#footer', getFriendlyUriEnd('/contact.html#footer')); + assert.equal('home.html#main', getFriendlyUriEnd('/home.html#main ')); }); it('returns undef when there is a query', function () {