Skip to content

a11y: add mouse-events-have-key-events rule #4874

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,20 @@ export default class Element extends Node {
});
}
}

if (handlers_map.has('mouseover') && !handlers_map.has('focus')) {
component.warn(this, {
code: `a11y-mouse-events-have-key-events`,
message: `A11y: on:mouseover must be accompanied by on:focus for accessibility`
});
}

if (handlers_map.has('mouseout') && !handlers_map.has('blur')) {
component.warn(this, {
code: `a11y-mouse-events-have-key-events`,
message: `A11y: on:mouseout must be accompanied by on:blur for accessibility`
});
}
}

validate_bindings() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
function noop() {}
</script>

<!-- should warn -->
<div on:mouseover={noop} />
<div on:mouseout={noop} />
<div on:mouseover={noop} on:blur={noop} />
<div on:mouseout={noop} on:focus={noop} />

<!-- should not warn -->
<div on:mouseover={noop} on:focus={noop} />
<div on:mouseout={noop} on:blur={noop} />
<div on:mouseover={noop} on:mouseout={noop} on:blur={noop} on:focus={noop}/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"code": "a11y-mouse-events-have-key-events",
"message": "A11y: on:mouseover must be accompanied by on:focus for accessibility",
"end": {
"character": 91,
"column": 27,
"line": 6
},
"start": {
"character": 64,
"column": 0,
"line": 6
},
"pos": 64
},
{
"code": "a11y-mouse-events-have-key-events",
"message": "A11y: on:mouseout must be accompanied by on:blur for accessibility",
"end": {
"character": 118,
"column": 26,
"line": 7
},
"start": {
"character": 92,
"column": 0,
"line": 7
},
"pos": 92
},
{
"code": "a11y-mouse-events-have-key-events",
"message": "A11y: on:mouseover must be accompanied by on:focus for accessibility",
"end": {
"character": 161,
"column": 42,
"line": 8
},
"start": {
"character": 119,
"column": 0,
"line": 8
},
"pos": 119
},
{
"code": "a11y-mouse-events-have-key-events",
"message": "A11y: on:mouseout must be accompanied by on:blur for accessibility",
"end": {
"character": 204,
"column": 42,
"line": 9
},
"start": {
"character": 162,
"column": 0,
"line": 9
},
"pos": 162
}
]