Skip to content

Commit

Permalink
fix(explicit-label): work with multiple labels (#2573)
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Oct 21, 2020
1 parent 5000aa6 commit d26f106
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/checks/label/explicit-evaluate.js
Expand Up @@ -7,15 +7,17 @@ function explicitEvaluate(node, options, virtualNode) {
if (virtualNode.attr('id')) {
const root = getRootNode(virtualNode.actualNode);
const id = escapeSelector(virtualNode.attr('id'));
const label = root.querySelector(`label[for="${id}"]`);
const labels = Array.from(root.querySelectorAll(`label[for="${id}"]`));

if (label) {
// defer to hidden-explicit-label check for better messaging
if (!isVisible(label)) {
return true;
} else {
return !!accessibleText(label);
}
if (labels.length) {
return labels.some(label => {
// defer to hidden-explicit-label check for better messaging
if (!isVisible(label)) {
return true;
} else {
return !!accessibleText(label);
}
});
}
}
return false;
Expand Down
9 changes: 9 additions & 0 deletions test/checks/label/explicit.js
Expand Up @@ -44,6 +44,15 @@ describe('explicit-label', function() {
);
});

it('should work for multiple labels', function() {
var vNode = queryFixture(
'<label for="target"></label><label for="target">Text</label><input type="text" id="target">'
);
assert.isTrue(
axe.testUtils.getCheckEvaluate('explicit-label')(null, {}, vNode)
);
});

(shadowSupport.v1 ? it : xit)(
'should return true if input and label are in the same shadow root',
function() {
Expand Down

0 comments on commit d26f106

Please sign in to comment.