Skip to content

Commit

Permalink
fix(color-contrast): allow disabled label children
Browse files Browse the repository at this point in the history
Closes #596
  • Loading branch information
Marcy Sutton committed Nov 24, 2017
1 parent c473d97 commit d65df7b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rules/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ if (nodeName === 'FIELDSET' && node.disabled || axe.commons.dom.findUp(node, 'fi
var nodeParentLabel = axe.commons.dom.findUp(node, 'label');
if (nodeName === 'LABEL' || nodeParentLabel) {
var relevantNode = node;
var relevantVirtualNode = virtualNode;

if (nodeParentLabel) {
relevantNode = nodeParentLabel;
// we need an input candidate from a parent to account for label children
relevantVirtualNode = axe.utils.getNodeFromTree(axe._tree[0], nodeParentLabel);
}
// explicit label of disabled input
let doc = axe.commons.dom.getRootNode(relevantNode);
Expand All @@ -45,7 +49,7 @@ if (nodeName === 'LABEL' || nodeParentLabel) {
return false;
}

var candidate = axe.utils.querySelectorAll(virtualNode, 'input:not([type="hidden"]):not([type="image"])' +
var candidate = axe.utils.querySelectorAll(relevantVirtualNode, 'input:not([type="hidden"]):not([type="image"])' +
':not([type="button"]):not([type="submit"]):not([type="reset"]), select, textarea');
if (candidate.length && candidate[0].actualNode.disabled) {
return false;
Expand Down
11 changes: 11 additions & 0 deletions test/rule-matches/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,18 @@ describe('color-contrast-matches', function () {
var target = fixture.querySelector('input');
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
assert.isFalse(rule.matches(target, axe.utils.getNodeFromTree(tree[0], target)));
});

it('should not match a disabled implicit label child', function () {
fixture.innerHTML = '<label>' +
'<input type="checkbox" style="position: absolute;display: inline-block;width: 1.5rem;height: 1.5rem;opacity: 0;" disabled checked>' +
'<span style="background-color:rgba(0, 0, 0, 0.26);display:inline-block;width: 1.5rem;height: 1.5rem;" aria-hidden="true"></span>' +
'<span style="color:rgba(0, 0, 0, 0.38);" id="target">Baseball</span>' +
'</label>';
var target = fixture.querySelector('#target');
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
var result = rule.matches(target, axe.utils.getNodeFromTree(tree[0], target));
assert.isFalse(result);
});

it('should not match <textarea disabled>', function () {
Expand Down

0 comments on commit d65df7b

Please sign in to comment.