Skip to content

Commit 419641b

Browse files
nsimonsontanhauhau
andauthored
Recursively check label children for input control (#5323)
* Recursively check label children for input control * Add another test case * Update snapshot * clean up test Co-authored-by: tanhauhau <[email protected]>
1 parent 5b29124 commit 419641b

File tree

5 files changed

+58
-32
lines changed

5 files changed

+58
-32
lines changed

src/compiler/compile/nodes/Element.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,24 @@ export default class Element extends Node {
626626
}
627627

628628
if (this.name === 'label') {
629-
const has_input_child = this.children.some(i => (i instanceof Element && a11y_labelable.has(i.name) ));
630-
if (!attribute_map.has('for') && !has_input_child) {
629+
const has_input_child = (children: INode[]) => {
630+
if (children.some(child => (child instanceof Element && (a11y_labelable.has(child.name) || child.name === 'slot')))) {
631+
return true;
632+
}
633+
634+
for (const child of children) {
635+
if (!('children' in child) || child.children.length === 0) {
636+
continue;
637+
}
638+
if (has_input_child(child.children)) {
639+
return true;
640+
}
641+
}
642+
643+
return false;
644+
};
645+
646+
if (!attribute_map.has('for') && !has_input_child(this.children)) {
631647
component.warn(this, compiler_warnings.a11y_label_has_associated_control);
632648
}
633649
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<label>
2+
<slot />
3+
</label>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
<script>
2+
import LabelComponent from './label.svelte'
3+
</script>
4+
15
<label>A</label>
26
<label for="id">B</label>
37

48
<label>C <input type="text" /></label>
59
<label>D <button>D</button></label>
610
<label>E <span></span></label>
11+
<label>F {#if true}<input type="text" />{/if}</label>
12+
<LabelComponent>G <input type="text" /></LabelComponent>
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
[
2-
{
3-
"code": "a11y-label-has-associated-control",
4-
"end": {
5-
"character": 16,
6-
"column": 16,
7-
"line": 1
8-
},
9-
"message": "A11y: A form label must be associated with a control.",
10-
"pos": 0,
11-
"start": {
12-
"character": 0,
13-
"column": 0,
14-
"line": 1
15-
}
16-
},
17-
{
18-
"code": "a11y-label-has-associated-control",
19-
"end": {
20-
"character": 149,
21-
"column": 30,
22-
"line": 6
23-
},
24-
"message": "A11y: A form label must be associated with a control.",
25-
"pos": 119,
26-
"start": {
27-
"character": 119,
28-
"column": 0,
29-
"line": 6
30-
}
31-
}
2+
{
3+
"code": "a11y-label-has-associated-control",
4+
"end": {
5+
"character": 82,
6+
"column": 16,
7+
"line": 5
8+
},
9+
"message": "A11y: A form label must be associated with a control.",
10+
"pos": 66,
11+
"start": {
12+
"character": 66,
13+
"column": 0,
14+
"line": 5
15+
}
16+
},
17+
{
18+
"code": "a11y-label-has-associated-control",
19+
"end": {
20+
"character": 215,
21+
"column": 30,
22+
"line": 10
23+
},
24+
"message": "A11y: A form label must be associated with a control.",
25+
"pos": 185,
26+
"start": {
27+
"character": 185,
28+
"column": 0,
29+
"line": 10
30+
}
31+
}
3232
]

0 commit comments

Comments
 (0)