Skip to content

Commit a113555

Browse files
committed
fix: make getSelector work with URIs that cannot be shortened
1 parent a5ea385 commit a113555

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

lib/core/utils/get-selector.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ function getAttributeNameValue(node, at) {
2222
let atnv;
2323

2424
if (name.indexOf('href') !== -1 || name.indexOf('src') !== -1) {
25-
let value = encodeURI(axe.utils.getFriendlyUriEnd(node.getAttribute(name)));
26-
if (value) {
27-
atnv = escapeSelector(at.name) + '$="' + value + '"';
28-
} else {
29-
return;
30-
}
25+
let friendly = axe.utils.getFriendlyUriEnd(node.getAttribute(name));
26+
if (friendly) {
27+
let value = encodeURI(friendly);
28+
if (value) {
29+
atnv = escapeSelector(at.name) + '$="' + value + '"';
30+
} else {
31+
return;
32+
}
33+
} else {
34+
atnv = escapeSelector(at.name) + '="' + node.getAttribute(name) + '"';
35+
}
3136
} else {
3237
atnv = escapeSelector(name) + '="' + escapeSelector(at.value) + '"';
3338
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@
9191
"standard-version": "^4.2.0"
9292
},
9393
"dependencies": {}
94-
}
94+
}

test/core/utils/get-selector.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,16 @@ describe('axe.utils.getSelector', function () {
551551
);
552552
});
553553

554+
it('should work correctly when a URL attribute cannot be shortened', function () {
555+
var href = 'mars2.html?a=be_bold';
556+
var node = document.createElement('a');
557+
node.setAttribute('href', href);
558+
fixture.appendChild(node);
559+
axe._tree = axe.utils.getFlattenedTree(document.documentElement);
560+
561+
assert.include(axe.utils.getSelector(node), href);
562+
});
563+
554564
it('no options: should work with shadow DOM', function () {
555565
var shadEl;
556566

0 commit comments

Comments
 (0)