Skip to content

Commit

Permalink
chore: small refactor of escapeSelector calls
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers committed Jun 28, 2017
1 parent 0b2d6be commit 428eea2
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 38 deletions.
7 changes: 2 additions & 5 deletions lib/checks/aria/required-parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ function getAriaOwners(element) {

while (element) {
if (element.getAttribute('id')) {
o = document.querySelector(
'[aria-owns~=' +
axe.commons.utils.escapeSelector(element.getAttribute('id')) +
']'
);
const id = axe.commons.utils.escapeSelector(element.getAttribute('id'));
o = document.querySelector(`[aria-owns~=${id}]`);
if (o) { owners.push(o); }
}
element = element.parentElement;
Expand Down
8 changes: 3 additions & 5 deletions lib/checks/label/explicit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
if (node.getAttribute('id')) {
var label = document.querySelector(
'label[for="' +
axe.commons.utils.escapeSelector(node.getAttribute('id')) +
'"]'
);
const id = axe.commons.utils.escapeSelector(node.getAttribute('id'));
const label = document.querySelector(`label[for="${id}"]`);

if (label) {
return !!axe.commons.text.accessibleText(label);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/checks/label/multiple-label.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var labels = [].slice.call(document.querySelectorAll('label[for="' +
axe.commons.utils.escapeSelector(node.getAttribute('id')) + '"]')),
parent = node.parentNode;
const id = axe.commons.utils.escapeSelector(node.getAttribute('id'));
let labels = Array.from(document.querySelectorAll(`label[for="${id}"]`));
let parent = node.parentNode;

if (labels.length) {
// filter out hidden labels because they're fine
Expand Down
8 changes: 3 additions & 5 deletions lib/checks/shared/duplicate-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ if (!node.getAttribute('id').trim()) {
return true;
}

var matchingNodes = document.querySelectorAll(
'[id="' +
axe.commons.utils.escapeSelector(node.getAttribute('id')) +
'"]'
);
const id = axe.commons.utils.escapeSelector(node.getAttribute('id'));
var matchingNodes = document.querySelectorAll(`[id="${id}"]`);
var related = [];

for (var i = 0; i < matchingNodes.length; i++) {
if (matchingNodes[i] !== node) {
related.push(matchingNodes[i]);
Expand Down
7 changes: 2 additions & 5 deletions lib/commons/table/is-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ table.isHeader = function (cell) {
}

if (cell.getAttribute('id')) {
return !!document.querySelector(
'[headers~="' +
axe.utils.escapeSelector(cell.getAttribute('id')) +
'"]'
);
const id = axe.utils.escapeSelector(cell.getAttribute('id'));
return !!document.querySelector(`[headers~="${id}"]`);
}

return false;
Expand Down
7 changes: 2 additions & 5 deletions lib/commons/text/accessible-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ var phrasingElements = ['A', 'EM', 'STRONG', 'SMALL', 'MARK', 'ABBR', 'DFN', 'I'
function findLabel(element) {
var ref = null;
if (element.getAttribute('id')) {
ref = document.querySelector(
'label[for="' +
axe.utils.escapeSelector(element.getAttribute('id')) +
'"]'
);
const id = axe.utils.escapeSelector(element.getAttribute('id'));
ref = document.querySelector(`label[for="${id}"]`);
if (ref) {
return ref;
}
Expand Down
7 changes: 2 additions & 5 deletions lib/commons/text/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ text.label = function (node) {

// explicit label
if (node.getAttribute('id')) {
ref = document.querySelector(
'label[for="' +
axe.utils.escapeSelector(node.getAttribute('id')) +
'"]'
);
const id = axe.commons.utils.escapeSelector(node.getAttribute('id'));
ref = document.querySelector(`label[for="${id}"]`);
candidate = ref && text.visible(ref, true);
if (candidate) {
return candidate;
Expand Down
7 changes: 2 additions & 5 deletions lib/rules/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ if (nodeName === 'LABEL' || nodeParentLabel) {
// label of disabled control associated w/ aria-labelledby
if (node.getAttribute('id')) {
// TODO: This should check if ALL elements are disabled, not just the first
var candidate = doc.querySelector(
'[aria-labelledby~=' +
axe.commons.utils.escapeSelector(node.getAttribute('id')) +
']'
);
const id = axe.commons.utils.escapeSelector(node.getAttribute('id'));
const candidate = doc.querySelector(`[aria-labelledby~="${id}"]`);
if (candidate && candidate.hasAttribute('disabled')) {
return false;
}
Expand Down

0 comments on commit 428eea2

Please sign in to comment.