-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): Require unique aria labels in checkboxgroup & radiogroup (#…
- Loading branch information
1 parent
6f649d6
commit c9b310d
Showing
8 changed files
with
245 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,61 @@ | ||
this.data({ | ||
name: node.getAttribute('name'), | ||
type: node.getAttribute('type') | ||
}); | ||
const { dom, text, utils } = axe.commons; | ||
|
||
const type = utils.escapeSelector(node.type); | ||
const name = utils.escapeSelector(node.name); | ||
const doc = dom.getRootNode(node); | ||
const data = { | ||
name: node.name, | ||
type: node.type | ||
}; | ||
|
||
var doc = axe.commons.dom.getRootNode(node); | ||
var matchingNodes = doc.querySelectorAll( | ||
'input[type="' + | ||
axe.commons.utils.escapeSelector(node.type) + | ||
'"][name="' + | ||
axe.commons.utils.escapeSelector(node.name) + | ||
'"]' | ||
const matchingNodes = Array.from( | ||
doc.querySelectorAll(`input[type="${type}"][name="${name}"]`) | ||
); | ||
// There is only one node with this name & type, so there's no need for a group | ||
if (matchingNodes.length <= 1) { | ||
this.data(data); | ||
return true; | ||
} | ||
|
||
// Check to see if there's an aria-labelledby value that all nodes have in common | ||
return ( | ||
[].map | ||
.call(matchingNodes, function(m) { | ||
var l = m.getAttribute('aria-labelledby'); | ||
return l ? l.split(/\s+/) : []; | ||
}) | ||
.reduce(function(prev, curr) { | ||
return prev.filter(function(n) { | ||
return curr.includes(n); | ||
}); | ||
}) | ||
.filter(function(n) { | ||
var labelNode = doc.getElementById(n); | ||
return labelNode && axe.commons.text.accessibleText(labelNode, true); | ||
}).length !== 0 | ||
let sharedLabels = dom.idrefs(node, 'aria-labelledby').filter(label => !!label); // Filter for "null" labels | ||
let uniqueLabels = sharedLabels.slice(); | ||
|
||
// Figure out which labels are unique, which are shared by all items, or neither | ||
matchingNodes.forEach(groupItem => { | ||
if (groupItem === node) { | ||
return; | ||
} | ||
// Find new labels associated with current groupItem | ||
const labels = dom | ||
.idrefs(groupItem, 'aria-labelledby') | ||
.filter(newLabel => newLabel); | ||
|
||
sharedLabels = sharedLabels.filter(sharedLabel => | ||
labels.includes(sharedLabel) | ||
); | ||
uniqueLabels = uniqueLabels.filter( | ||
uniqueLabel => !labels.includes(uniqueLabel) | ||
); | ||
}); | ||
|
||
// filter items with no accessible name, do this last for performance reasons | ||
uniqueLabels = uniqueLabels.filter(labelNode => | ||
text.accessibleText(labelNode, true) | ||
); | ||
sharedLabels = sharedLabels.filter(labelNode => | ||
text.accessibleText(labelNode, true) | ||
); | ||
|
||
if (uniqueLabels.length > 0 && sharedLabels.length > 0) { | ||
this.data(data); | ||
return true; | ||
} | ||
|
||
if (uniqueLabels.length > 0 && sharedLabels.length === 0) { | ||
data.failureCode = 'no-shared-label'; | ||
} else if (uniqueLabels.length === 0 && sharedLabels.length > 0) { | ||
data.failureCode = 'no-unique-label'; | ||
} | ||
|
||
this.data(data); | ||
return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.