Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(explicit-label): work with multiple labels #2573

Merged
merged 1 commit into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/checks/label/explicit-evaluate.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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