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: escape href attribute when creating a CSS selector #1366

Merged
merged 3 commits into from
Feb 18, 2019
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
6 changes: 5 additions & 1 deletion lib/core/utils/get-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ function getAttributeNameValue(node, at) {
return;
straker marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
atnv = escapeSelector(at.name) + '="' + node.getAttribute(name) + '"';
atnv =
escapeSelector(at.name) +
'="' +
escapeSelector(node.getAttribute(name)) +
'"';
}
} else {
atnv = escapeSelector(name) + '="' + escapeSelector(at.value) + '"';
Expand Down
21 changes: 19 additions & 2 deletions test/core/utils/get-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,20 @@ describe('axe.utils.getSelector', function() {
assert.equal(axe.utils.getSelector(img2), 'img[src$="logo.png"]');
});

it('should escape href attributes', function() {
var link1 = document.createElement('a');
link1.setAttribute('href', '//deque.com/about/');

var link2 = document.createElement('a');
link2.setAttribute('href', '//deque.com/child/ \n\n\n');

fixtureSetup([link1, link2]);
assert.equal(
axe.utils.getSelector(link2),
'a[href="\\/\\/deque\\.com\\/child\\/\\ \\a \\a \\a "]'
);
});

it('should not generate universal selectors', function() {
var node = document.createElement('div');
node.setAttribute('role', 'menuitem');
Expand All @@ -516,8 +530,11 @@ describe('axe.utils.getSelector', function() {
node2.setAttribute('href', href2);
fixtureSetup([node1, node2]);

assert.include(axe.utils.getSelector(node1), href1);
assert.include(axe.utils.getSelector(node2), href2);
assert.include(axe.utils.getSelector(node1), 'mars2\\.html\\?a\\=be_bold');
assert.include(
axe.utils.getSelector(node2),
'mars2\\.html\\?a\\=be_italic'
);
});

// shadow DOM v1 - note: v0 is compatible with this code, so no need
Expand Down