Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 17223cd

Browse files
authoredJul 3, 2020
Merge pull request #27 from vue-a11y/label-has-for-nesting
For the `label-has-for` rule, validate proper nesting
2 parents 557f7c2 + 8dd7e54 commit 17223cd

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed
 

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
99
### Changed
1010

1111
- For the `anchor-has-content` rule, allow anchors with explicit aria labels to pass.
12+
- For the `label-has-for` rule when validating that there is correct nesting, ensure that we're actually checking for form controls and not just checking for VElement nodes.
1213

1314
## [0.2.0] - 2020-06-26
1415

‎src/rules/__tests__/label-has-for.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ makeRuleTester("label-has-for", rule, {
55
valid: [
66
"<label for='id'><input type='text' /></label>",
77
"<label :for='id'><input type='text' :id='id' /></label>",
8+
"<label for='id'><span><input type='text' /></span></label>",
89
{
910
code: "<label for='id' /><input type='text' />",
1011
options: [{ required: { some: ["nesting", "id"] } }]
1112
},
1213
{
13-
code: "<label>name</label>",
14-
options: [{ required: { some: ["nesting", "id"] }, allowChildren: true }]
14+
code: "<label for='id'><slot /></label>",
15+
options: [{ allowChildren: true }]
1516
}
1617
],
1718
invalid: [
1819
"<label for='id' />",
1920
"<label><input type='text' /></label>",
21+
"<label for='id'><slot /></label>",
22+
"<label for='id'><div /></label>",
2023
{
2124
code: "<Label for='id' />",
2225
options: [{ components: ["Label"] }],

‎src/rules/label-has-for.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,38 @@ const {
22
defineTemplateBodyVisitor,
33
getElementAttributeValue,
44
getElementType,
5-
hasAccessibleChild,
5+
isHiddenFromScreenReader,
66
makeDocsURL
77
} = require("../utils");
88

9-
const validate = (node, rule, allowChildren) => {
10-
if (allowChildren === true) {
11-
return hasAccessibleChild(node);
12-
}
9+
const controlTypes = ["input", "meter", "progress", "select", "textarea"];
1310

14-
if (rule === "nesting") {
15-
return node.children.some((child) => child.type === "VElement");
16-
}
11+
const validateNesting = (node, allowChildren) =>
12+
node.children.some((child) => {
13+
if (child.rawName === "slot") {
14+
return allowChildren;
15+
}
16+
17+
if (child.type === "VElement") {
18+
return (
19+
!isHiddenFromScreenReader(child) &&
20+
(controlTypes.includes(getElementType(child)) ||
21+
validateNesting(child, allowChildren))
22+
);
23+
}
1724

18-
return getElementAttributeValue(node, "for");
25+
return false;
26+
});
27+
28+
const validate = (node, rule, allowChildren) => {
29+
switch (rule) {
30+
case "nesting":
31+
return validateNesting(node, allowChildren);
32+
case "id":
33+
return getElementAttributeValue(node, "for");
34+
default:
35+
return false;
36+
}
1937
};
2038

2139
const isValidLabel = (node, required, allowChildren) => {

0 commit comments

Comments
 (0)
Please sign in to comment.