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

[feat] a11y click events have key events #7081

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/compile/compiler_warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export default {
code: 'a11y-unknown-role',
message: `A11y: Unknown role '${role}'` + (suggestion ? ` (did you mean '${suggestion}'?)` : '')
}),
a11y_click_events_have_key_events: () => ({
code: 'a11y-unknown-role',
message: 'A11y: on:click should be accompanied by one of the following: on:keyup, on:keydown or on:keypress'
}),
a11y_accesskey: {
code: 'a11y-accesskey',
message: 'A11y: Avoid using accesskey'
Expand Down
20 changes: 20 additions & 0 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export default class Element extends Node {
this.validate_bindings_foreign();
} else {
this.validate_attributes_a11y();
this.validate_handlers_a11y();
this.validate_special_cases();
this.validate_bindings();
this.validate_content();
Expand Down Expand Up @@ -311,6 +312,25 @@ export default class Element extends Node {
});
}

validate_handlers_a11y() {
const { component, handlers } = this;

const handlers_map = new Map();
handlers.forEach(handler => (
handlers_map.set(handler.name, handler)
));

const up_press_down = (
handlers_map.has('keyup') ||
handlers_map.has('keydown') ||
handlers_map.has('keypress')
);

if (handlers_map.has('click') && !up_press_down) {
return component.warn(this, compiler_warnings.a11y_click_events_have_key_events());
}
}

validate_attributes_a11y() {
const { component } = this;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<button on:click={ () => {} }></button>
<div on:click={ () => {} }></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"code": "a11y-unknown-role",
"end": {
"character": 39,
"column": 39,
"line": 1
},
"message": "A11y: on:click should be accompanied by one of the following: on:keyup, on:keydown or on:keypress",
"pos": 0,
"start": {
"character": 0,
"column": 0,
"line": 1
}
},
{
"code": "a11y-unknown-role",
"end": {
"character": 73,
"column": 33,
"line": 2
},
"message": "A11y: on:click should be accompanied by one of the following: on:keyup, on:keydown or on:keypress",
"pos": 40,
"start": {
"character": 40,
"column": 0,
"line": 2
}
}
]