Skip to content

Commit 4775a23

Browse files
authored
fix: Right trim URLs before outputting them in getSelector (#924)
Closes #788
1 parent 87e979f commit 4775a23

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/core/utils/get-friendly-uri-end.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function splitString (str, splitIndex) {
1919
return [str.substring(0, splitIndex), str.substring(splitIndex)];
2020
}
2121

22+
function trimRight (str) {
23+
return str.replace(/\s+$/, '')
24+
}
25+
2226
/**
2327
* Take a relative or absolute URL and pull it into it's indivisual pieces
2428
*
@@ -90,14 +94,14 @@ axe.utils.getFriendlyUriEnd = function getFriendlyUriEnd (uri = '', options = {}
9094

9195
if (hash) {
9296
if (pathEnd && (pathEnd + hash).length <= maxLength) {
93-
return pathEnd + hash;
97+
return trimRight(pathEnd + hash);
9498
} else if (pathEnd.length < 2 && hash.length > 2 && hash.length <= maxLength) {
95-
return hash;
99+
return trimRight(hash);
96100
} else {
97101
return;
98102
}
99103
} else if (domain && domain.length < maxLength && path.length <= 1) {// '' or '/'
100-
return domain + path;
104+
return trimRight(domain + path);
101105
}
102106

103107
// See if the domain should be returned
@@ -106,7 +110,7 @@ axe.utils.getFriendlyUriEnd = function getFriendlyUriEnd (uri = '', options = {}
106110
domain !== currentDomain &&
107111
(domain + path).length <= maxLength
108112
) {
109-
return domain + path;
113+
return trimRight(domain + path);
110114
}
111115

112116
const lastDotIndex = pathEnd.lastIndexOf('.');
@@ -119,6 +123,6 @@ axe.utils.getFriendlyUriEnd = function getFriendlyUriEnd (uri = '', options = {}
119123
// Exclude files that are likely to be database IDs
120124
!isMostlyNumbers(pathEnd)
121125
) {
122-
return pathEnd;
126+
return trimRight(pathEnd);
123127
}
124128
};

test/core/utils/get-friendly-uri-end.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ describe('axe.utils.getFriendlyUriEnd', function () {
1515
assert.equal('contact.html', getFriendlyUriEnd('/contact.html'));
1616
});
1717

18+
it('trims whitespace', function () {
19+
assert.equal(undefined, getFriendlyUriEnd(' '));
20+
assert.equal('start page', getFriendlyUriEnd('start page\t'));
21+
assert.equal('home#heading', getFriendlyUriEnd('home#heading '));
22+
});
23+
1824
it('returns a hash URI', function () {
1925
assert.equal('#footer', getFriendlyUriEnd('#footer'));
2026
assert.equal('contact.html#footer', getFriendlyUriEnd('/contact.html#footer'));
27+
assert.equal('home.html#main', getFriendlyUriEnd('/home.html#main '));
2128
});
2229

2330
it('returns undef when there is a query', function () {

0 commit comments

Comments
 (0)