Skip to content
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

fix(820): label has associated control #5074

Merged
merged 2 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ const a11y_no_onchange = new Set([
'option'
]);

const a11y_labelable = new Set([
"button",
"input",
"keygen",
"meter",
"output",
"progress",
"select",
"textarea"
]);

const invisible_elements = new Set(['meta', 'html', 'script', 'style']);

const valid_modifiers = new Set([
Expand Down Expand Up @@ -507,6 +518,16 @@ 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) ));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ignores the possibility of elements having the role attribure.

if (!attribute_map.has('for') && !has_input_child) {
component.warn(this, {
code: `a11y-label-has-associated-control`,
message: `A11y: A form label must be associated with a control.`
});
}
}

if (a11y_no_onchange.has(this.name)) {
if (handlers_map.has('change') && !handlers_map.has('blur')) {
component.warn(this, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<label>A</label>
<label for="id">B</label>

<label>C <input type="text" /></label>
<label>D <button>D</button></label>
<label>E <span></span></label>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"code": "a11y-label-has-associated-control",
"end": {
"character": 16,
"column": 16,
"line": 1
},
"message": "A11y: A form label must be associated with a control.",
"pos": 0,
"start": {
"character": 0,
"column": 0,
"line": 1
}
},
{
"code": "a11y-label-has-associated-control",
"end": {
"character": 149,
"column": 30,
"line": 6
},
"message": "A11y: A form label must be associated with a control.",
"pos": 119,
"start": {
"character": 119,
"column": 0,
"line": 6
}
}
]