-
-
Notifications
You must be signed in to change notification settings - Fork 444
Rule: allow input nested in label (#1170) #1852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1b501b2
0756992
8ad7250
1bccbd0
6376fd3
29df719
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,13 @@ export default { | |
| col: number | ||
| forValue?: string | ||
| }> = [] | ||
| const inputTags: Array<{ event: Block; col: number; id?: string }> = [] | ||
| const inputTags: Array<{ | ||
| event: Block | ||
| col: number | ||
| id?: string | ||
| nested: boolean | ||
| }> = [] | ||
| let labelDepth = 0 | ||
|
|
||
| parser.addListener('tagstart', (event) => { | ||
| const tagName = event.tagName.toLowerCase() | ||
|
|
@@ -20,20 +26,36 @@ export default { | |
| if (tagName === 'input') { | ||
| // label is not required for hidden input | ||
| if (mapAttrs['type'] !== 'hidden') { | ||
| inputTags.push({ event: event, col: col, id: mapAttrs['id'] }) | ||
| inputTags.push({ | ||
| event: event, | ||
| col: col, | ||
| id: mapAttrs['id'], | ||
| nested: labelDepth > 0, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if (tagName === 'label') { | ||
| if ('for' in mapAttrs && mapAttrs['for'] !== '') { | ||
| // explicit label: associates with the referenced control, not nested ones | ||
| labelTags.push({ event: event, col: col, forValue: mapAttrs['for'] }) | ||
| } else if (!event.close) { | ||
| // implicit label (no `for`): nesting labels the input | ||
| // a self-closing <label/> opens no scope and emits no tagend | ||
| labelDepth++ | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| parser.addListener('tagend', (event) => { | ||
| if (event.tagName.toLowerCase() === 'label' && labelDepth > 0) { | ||
| labelDepth-- | ||
| } | ||
| }) | ||
|
|
||
| parser.addListener('end', () => { | ||
| inputTags.forEach((inputTag) => { | ||
| if (!hasMatchingLabelTag(inputTag)) { | ||
| if (!inputTag.nested && !hasMatchingLabelTag(inputTag)) { | ||
| reporter.warn( | ||
|
Comment on lines
57
to
59
|
||
| 'No matching [ label ] tag found.', | ||
| inputTag.event.line, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ Level: <Badge text="Warning" variant="caution" /> | |
| - `true`: enable rule | ||
| - `false`: disable rule | ||
|
|
||
| ### The following patterns are **not** considered rule violations | ||
| ### The following patterns are considered rule violations | ||
|
|
||
| ```html | ||
| <input type="password"> | ||
|
|
@@ -28,11 +28,12 @@ Level: <Badge text="Warning" variant="caution" /> | |
| <input type="password" /> <label for="something"/> | ||
| ``` | ||
|
|
||
| ### The following patterns are considered rule violations | ||
| ### The following patterns are **not** considered rule violations | ||
|
|
||
| ```html | ||
| <label for="some-id"/><input id="some-id" type="password" /> | ||
| <input id="some-id" type="password" /> <label for="some-id"/> | ||
| <label>Password <input type="password" /></label> | ||
| ``` | ||
|
Comment on lines
33
to
37
|
||
|
|
||
| ### Why this rule is important | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
labelDepthis incremented for every non-self-closing<label>, including labels with aforattribute. Per the HTML spec, implicit labeling via nesting only applies whenforis not specified; ifforexists, the label targets the referenced control and a nested<input>is not labeled by that<label>. This will incorrectly suppress warnings for<label for="other-id"><input ...></label>(and also forfor=""). Track depth only for labels that can provide implicit labeling (e.g.,fornot present), or store a stack of label scopes with a flag indicating whether they are implicit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. labelDepth now only increments for labels without a for attribute. Labels with for go to labelTags for explicit matching only, so correctly warns.