From d26f10699d099c8a4f2aacf180862890d8dbbe18 Mon Sep 17 00:00:00 2001
From: Steven Lambert <2433219+straker@users.noreply.github.com>
Date: Wed, 21 Oct 2020 08:14:40 -0600
Subject: [PATCH] fix(explicit-label): work with multiple labels (#2573)
---
lib/checks/label/explicit-evaluate.js | 18 ++++++++++--------
test/checks/label/explicit.js | 9 +++++++++
2 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/lib/checks/label/explicit-evaluate.js b/lib/checks/label/explicit-evaluate.js
index fadb1af36c..5f45d9c158 100644
--- a/lib/checks/label/explicit-evaluate.js
+++ b/lib/checks/label/explicit-evaluate.js
@@ -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;
diff --git a/test/checks/label/explicit.js b/test/checks/label/explicit.js
index d47eab5d31..a7069cdf04 100644
--- a/test/checks/label/explicit.js
+++ b/test/checks/label/explicit.js
@@ -44,6 +44,15 @@ describe('explicit-label', function() {
);
});
+ it('should work for multiple labels', function() {
+ var vNode = queryFixture(
+ ''
+ );
+ 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() {