From 44b033c51619294a8d85c26aa6c703f57db90a59 Mon Sep 17 00:00:00 2001 From: Steven Lambert <2433219+straker@users.noreply.github.com> Date: Mon, 20 Jul 2020 07:49:22 -0600 Subject: [PATCH] fix(label): work with virtual nodes (#2354) * fix(label): work with virtual nodes * last two checks * Update lib/commons/text/label-virtual.js Co-authored-by: Wilco Fiers * integration * finalize * fix tests Co-authored-by: Wilco Fiers --- lib/checks/label/explicit-evaluate.js | 28 +-- lib/checks/label/explicit.json | 3 +- .../label/hidden-explicit-label-evaluate.js | 22 ++- lib/checks/label/hidden-explicit-label.json | 3 +- lib/checks/label/implicit-evaluate.js | 16 +- lib/checks/label/implicit.json | 3 +- lib/commons/aria/label-virtual.js | 10 +- lib/commons/dom/is-visible.js | 7 +- lib/commons/standards/implicit-html-roles.js | 13 +- lib/commons/text/form-control-value.js | 77 ++++---- lib/commons/text/label-virtual.js | 24 ++- lib/commons/text/visible-virtual.js | 19 +- .../virtual-node/abstract-virtual-node.js | 2 +- lib/core/base/virtual-node/virtual-node.js | 12 +- lib/core/utils/closest.js | 6 + lib/core/utils/get-flattened-tree.js | 6 +- lib/rules/label-matches.js | 8 +- .../checks/aria/no-implicit-explicit-label.js | 16 +- test/checks/label/explicit.js | 72 +++++--- test/checks/label/hidden-explicit-label.js | 27 +++ test/checks/label/implicit.js | 37 +++- test/core/utils/closest.js | 22 +++ test/core/utils/flattened-tree.js | 5 + .../virtual-rules/aria-input-field-name.js | 11 +- .../virtual-rules/aria-toggle-field-name.js | 7 +- test/integration/virtual-rules/index.html | 1 + test/integration/virtual-rules/label.js | 165 ++++++++++++++++++ test/rule-matches/label-matches.js | 49 ++---- 28 files changed, 516 insertions(+), 155 deletions(-) create mode 100644 test/integration/virtual-rules/label.js diff --git a/lib/checks/label/explicit-evaluate.js b/lib/checks/label/explicit-evaluate.js index d3410c2124..fadb1af36c 100644 --- a/lib/checks/label/explicit-evaluate.js +++ b/lib/checks/label/explicit-evaluate.js @@ -2,22 +2,26 @@ import { getRootNode, isVisible } from '../../commons/dom'; import { accessibleText } from '../../commons/text'; import { escapeSelector } from '../../core/utils'; -function explicitEvaluate(node) { - if (node.getAttribute('id')) { - const root = getRootNode(node); - const id = escapeSelector(node.getAttribute('id')); - const label = root.querySelector(`label[for="${id}"]`); +function explicitEvaluate(node, options, virtualNode) { + try { + if (virtualNode.attr('id')) { + const root = getRootNode(virtualNode.actualNode); + const id = escapeSelector(virtualNode.attr('id')); + const label = root.querySelector(`label[for="${id}"]`); - if (label) { - // defer to hidden-explicit-label check for better messaging - if (!isVisible(label)) { - return true; - } else { - return !!accessibleText(label); + if (label) { + // defer to hidden-explicit-label check for better messaging + if (!isVisible(label)) { + return true; + } else { + return !!accessibleText(label); + } } } + return false; + } catch (e) { + return undefined; } - return false; } export default explicitEvaluate; diff --git a/lib/checks/label/explicit.json b/lib/checks/label/explicit.json index defced373b..511b68c4d0 100644 --- a/lib/checks/label/explicit.json +++ b/lib/checks/label/explicit.json @@ -5,7 +5,8 @@ "impact": "critical", "messages": { "pass": "Form element has an explicit