From ae07b8e8144d342163510fdb19e028926b5a74f6 Mon Sep 17 00:00:00 2001 From: Marcy Sutton Date: Tue, 20 Mar 2018 14:42:17 -0700 Subject: [PATCH] fix(rule): improve messaging for hidden labels Closes https://github.com/dequelabs/axe-core/issues/242 --- lib/checks/label/explicit.js | 7 ++- lib/checks/label/hidden-explicit-label.js | 10 +++++ lib/checks/label/hidden-explicit-label.json | 11 +++++ lib/rules/label.json | 3 +- test/checks/label/explicit.js | 10 ++--- test/checks/label/hidden-explicit-label.js | 49 +++++++++++++++++++++ test/integration/rules/label/label.html | 1 + test/integration/rules/label/label.json | 1 + 8 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 lib/checks/label/hidden-explicit-label.js create mode 100644 lib/checks/label/hidden-explicit-label.json create mode 100644 test/checks/label/hidden-explicit-label.js diff --git a/lib/checks/label/explicit.js b/lib/checks/label/explicit.js index eee064f700..dc314576ea 100644 --- a/lib/checks/label/explicit.js +++ b/lib/checks/label/explicit.js @@ -4,7 +4,12 @@ if (node.getAttribute('id')) { const label = root.querySelector(`label[for="${id}"]`); if (label) { - return !!axe.commons.text.accessibleText(label); + // defer to hidden-explicit-label check for better messaging + if (!axe.commons.dom.isVisible(label)) { + return true; + } else { + return !!axe.commons.text.accessibleText(label); + } } } return false; diff --git a/lib/checks/label/hidden-explicit-label.js b/lib/checks/label/hidden-explicit-label.js new file mode 100644 index 0000000000..0fcdd0dd24 --- /dev/null +++ b/lib/checks/label/hidden-explicit-label.js @@ -0,0 +1,10 @@ +if (node.getAttribute('id')) { + const root = axe.commons.dom.getRootNode(node); + const id = axe.commons.utils.escapeSelector(node.getAttribute('id')); + const label = root.querySelector(`label[for="${id}"]`); + + if (label && !axe.commons.dom.isVisible(label)) { + return true; + } +} +return false; diff --git a/lib/checks/label/hidden-explicit-label.json b/lib/checks/label/hidden-explicit-label.json new file mode 100644 index 0000000000..70d342c103 --- /dev/null +++ b/lib/checks/label/hidden-explicit-label.json @@ -0,0 +1,11 @@ +{ + "id": "hidden-explicit-label", + "evaluate": "hidden-explicit-label.js", + "metadata": { + "impact": "critical", + "messages": { + "pass": "Form element has a visible explicit