From 9c7349b7860f318beaf24447e0e3d77fe95986a7 Mon Sep 17 00:00:00 2001 From: Niko Simonson Date: Fri, 28 Aug 2020 22:40:51 -0700 Subject: [PATCH] Recursively check label children for input control --- src/compiler/compile/nodes/Element.ts | 21 +++++++++++++++++-- .../input.svelte | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/compiler/compile/nodes/Element.ts b/src/compiler/compile/nodes/Element.ts index 760e229b0f..e9a17af857 100644 --- a/src/compiler/compile/nodes/Element.ts +++ b/src/compiler/compile/nodes/Element.ts @@ -616,8 +616,25 @@ export default class Element extends Node { } if (this.name === 'label') { - const has_input_child = this.children.some(i => (i instanceof Element && a11y_labelable.has(i.name) )); - if (!attribute_map.has('for') && !has_input_child) { + const has_input_child = (children) => { + const has_input = children.some(i => (i instanceof Element && a11y_labelable.has(i.name))); + if (has_input) { + return true; + } + + for (const c of children) { + if (!c.children || c.children.length === 0) { + continue; + } + if (has_input_child(c.children)) { + return true; + } + } + + return false; + }; + + if (!attribute_map.has('for') && !has_input_child(this.children)) { component.warn(this, compiler_warnings.a11y_label_has_associated_control); } } diff --git a/test/validator/samples/a11y-label-has-associated-control/input.svelte b/test/validator/samples/a11y-label-has-associated-control/input.svelte index 43304689dc..acae1bcddf 100644 --- a/test/validator/samples/a11y-label-has-associated-control/input.svelte +++ b/test/validator/samples/a11y-label-has-associated-control/input.svelte @@ -4,3 +4,4 @@ +