Skip to content

Commit

Permalink
fix: Right trim URLs before outputting them in getSelector (#924)
Browse files Browse the repository at this point in the history
Closes #788
  • Loading branch information
WilcoFiers authored Jun 4, 2018
1 parent 87e979f commit 4775a23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/core/utils/get-friendly-uri-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand All @@ -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('.');
Expand All @@ -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);
}
};
7 changes: 7 additions & 0 deletions test/core/utils/get-friendly-uri-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 4775a23

Please sign in to comment.