From d65df7beef35bd7c1ba31b709a24d4fe78e95d8e Mon Sep 17 00:00:00 2001 From: Marcy Sutton Date: Tue, 7 Nov 2017 18:23:15 -0800 Subject: [PATCH] fix(color-contrast): allow disabled label children Closes https://github.com/dequelabs/axe-core/issues/596 --- lib/rules/color-contrast-matches.js | 6 +++++- test/rule-matches/color-contrast-matches.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/rules/color-contrast-matches.js b/lib/rules/color-contrast-matches.js index 6377556636..7fbf47be76 100644 --- a/lib/rules/color-contrast-matches.js +++ b/lib/rules/color-contrast-matches.js @@ -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); @@ -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; diff --git a/test/rule-matches/color-contrast-matches.js b/test/rule-matches/color-contrast-matches.js index 77979cad30..a18d6a9ec5 100644 --- a/test/rule-matches/color-contrast-matches.js +++ b/test/rule-matches/color-contrast-matches.js @@ -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 = ''; + 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