Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
fix: escape special characters to be used in a query selector string
Browse files Browse the repository at this point in the history
  • Loading branch information
joscha committed Dec 16, 2015
1 parent d1c98e8 commit 38f7e00
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/js/AccessibilityUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,17 @@ axs.utils.namedValues = function(obj) {
return values;
};

/**
* Escapes a given ID to be used in a CSS selector
*
* @private
* @param {!String} id The ID to be escaped
* @return {String} The escaped ID
*/
function escapeId(id) {
return id.replace(/[^a-zA-Z0-9_-]/g,function(match) { return '\\' + match; });
}

/** Gets a CSS selector text for a DOM object.
* @param {Node} obj The DOM object.
* @return {string} CSS selector text for the DOM object.
Expand All @@ -935,7 +946,7 @@ axs.utils.getQuerySelectorText = function(obj) {

if (obj.hasAttribute) {
if (obj.id) {
return '#' + obj.id;
return '#' + escapeId(obj.id);
}

if (obj.className) {
Expand Down
9 changes: 9 additions & 0 deletions test/js/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ test("nth-of-type does not refer to a selector but a tagName", function() {
equal(lastLi, document.querySelector(selector),
'selector "' + selector + '" does not match element');
});
test('special characters in IDs are properly escaped', function() {
var div = document.createElement('div');
div.id = 'some.id.with.special.chars';
this.fixture_.appendChild(div);
var selector = axs.utils.getQuerySelectorText(div);
equal(div, document.querySelector(selector),
'selector "' + selector + '" does not match element');
});


module("getIdReferrers", {
setup: function () {
Expand Down

0 comments on commit 38f7e00

Please sign in to comment.