Skip to content

Commit

Permalink
fix(multiple-label): considers explicit labels in the same shadow tree
Browse files Browse the repository at this point in the history
  • Loading branch information
AutoSponge committed May 23, 2019
1 parent 93d59f4 commit e878f00
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/checks/label/multiple-label.js
Original file line number Diff line number Diff line change
@@ -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, 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' &&
Expand Down
18 changes: 17 additions & 1 deletion test/checks/label/multiple-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -176,4 +176,20 @@ 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 = '<div id="target"></div>';

var target = document.querySelector('#target');
var shadowRoot = target.attachShadow({ mode: 'open' });
shadowRoot.innerHTML =
'<label for="myinput">normal</label><input id="myinput"></input>';
var shadowTarget = target.shadowRoot;
assert.isFalse(
checks['multiple-label'].evaluate.call(checkContext, shadowTarget)
);
}
);
});

0 comments on commit e878f00

Please sign in to comment.