From 6fd0915db9a955ab53e811377cf127588c01b429 Mon Sep 17 00:00:00 2001 From: Paul Grenier Date: Wed, 22 May 2019 14:44:49 -0400 Subject: [PATCH] fix(multiple-label): considers explicit labels in the same shadow tree --- lib/checks/label/multiple-label.js | 11 ++++++++++- test/checks/label/multiple-label.js | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/checks/label/multiple-label.js b/lib/checks/label/multiple-label.js index b8c156cf79..e487d4c754 100644 --- a/lib/checks/label/multiple-label.js +++ b/lib/checks/label/multiple-label.js @@ -1,12 +1,21 @@ const id = axe.utils.escapeSelector(node.getAttribute('id')); -let labels = Array.from(document.querySelectorAll(`label[for="${id}"]`)); let parent = node.parentNode; +let root = document; +if (!axe.utils.contains(node.ownerDocument.body, node)) { + while (parent.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) { + parent = parent.parentNode; + } + root = parent; +} + +let labels = Array.from(root.querySelectorAll(`label[for="${id}"]`)); if (labels.length) { // filter out CSS hidden labels because they're fine labels = labels.filter(label => axe.commons.dom.isVisible(label)); } +parent = node.parentNode; while (parent) { if ( parent.nodeName.toUpperCase() === 'LABEL' && diff --git a/test/checks/label/multiple-label.js b/test/checks/label/multiple-label.js index 98bc10330f..834b6d7f2c 100644 --- a/test/checks/label/multiple-label.js +++ b/test/checks/label/multiple-label.js @@ -2,7 +2,7 @@ describe('multiple-label', function() { 'use strict'; var fixture = document.getElementById('fixture'); - + var shadowSupported = axe.testUtils.shadowSupport.v1; var checkContext = axe.testUtils.MockCheckContext(); afterEach(function() { @@ -176,4 +176,23 @@ describe('multiple-label', function() { var target = fixture.querySelector('#Q'); assert.isTrue(checks['multiple-label'].evaluate.call(checkContext, target)); }); + + (shadowSupported ? it : xit)( + 'should consider labels in the same document/shadow tree', + function() { + fixture.innerHTML = '
'; + + var target = document.querySelector('#target'); + var shadowRoot = target.attachShadow({ mode: 'open' }); + shadowRoot.innerHTML = + ''; + var shadowTarget = target.shadowRoot; + assert.isFalse( + checks['multiple-label'].evaluate.call( + checkContext, + shadowTarget.firstElementChild + ) + ); + } + ); });