Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(get-selector): do not URL encode or token escape attribute selectors #3215

Merged
merged 4 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions lib/core/utils/get-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ function getAttributeNameValue(node, at) {
if (name.indexOf('href') !== -1 || name.indexOf('src') !== -1) {
const friendly = getFriendlyUriEnd(node.getAttribute(name));
if (friendly) {
const value = encodeURI(friendly);
if (value) {
atnv = escapeSelector(at.name) + '$="' + escapeSelector(value) + '"';
} else {
return;
}
atnv = escapeSelector(at.name) + '$="' + friendly + '"';
straker marked this conversation as resolved.
Show resolved Hide resolved
} else {
atnv =
escapeSelector(at.name) +
Expand All @@ -54,7 +49,7 @@ function getAttributeNameValue(node, at) {
'"';
}
} else {
atnv = escapeSelector(name) + '="' + escapeSelector(at.value) + '"';
atnv = escapeSelector(name) + '="' + at.value + '"';
straker marked this conversation as resolved.
Show resolved Hide resolved
}
return atnv;
}
Expand Down
15 changes: 13 additions & 2 deletions test/core/utils/get-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ describe('axe.utils.getSelector', function() {
img2.setAttribute('src', '//deque.com/logo.png');

fixtureSetup([link1, link2, img1, img2]);
assert.equal(axe.utils.getSelector(link2), 'a[href$="about\\/"]');
assert.equal(axe.utils.getSelector(img2), 'img[src$="logo\\.png"]');
assert.equal(axe.utils.getSelector(link2), 'a[href$="about/"]');
assert.equal(axe.utils.getSelector(img2), 'img[src$="logo.png"]');
});

it('should escape href attributes', function() {
Expand All @@ -512,6 +512,17 @@ describe('axe.utils.getSelector', function() {
);
});

it('should not URL encode or token escape href attribute', function() {
straker marked this conversation as resolved.
Show resolved Hide resolved
var link1 = document.createElement('a');
link1.setAttribute('href', '3 Seater');

var link2 = document.createElement('a');
link2.setAttribute('href', '1 Seater');

fixtureSetup([link1, link2]);
assert.equal(axe.utils.getSelector(link2), 'a[href$="1 Seater"]');
});

it('should not generate universal selectors', function() {
var node = document.createElement('div');
node.setAttribute('role', 'menuitem');
Expand Down