Skip to content

Commit 9573fec

Browse files
committed
fix: take event attributes into account when checking a11y
also adjusted the warning messages slightly so they make sense for both variants of event handlers fixes #9654
1 parent 405e9da commit 9573fec

File tree

9 files changed

+46
-22
lines changed

9 files changed

+46
-22
lines changed

.changeset/polite-pumpkins-guess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: take event attributes into account when checking a11y

packages/svelte/src/compiler/phases/2-analyze/a11y.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '../patterns.js';
1010
import { warn } from '../../warnings.js';
1111
import fuzzymatch from '../1-parse/utils/fuzzymatch.js';
12-
import { is_text_attribute } from '../../utils/ast.js';
12+
import { is_event_attribute, is_text_attribute } from '../../utils/ast.js';
1313
import { ContentEditableBindings } from '../constants.js';
1414
import { walk } from 'zimmerframe';
1515

@@ -704,10 +704,14 @@ function check_element(node, state, path) {
704704
} else if (attribute.type === 'OnDirective') {
705705
handlers.add(attribute.name);
706706
} else if (attribute.type === 'Attribute') {
707-
attributes.push(attribute);
708-
attribute_map.set(attribute.name, attribute);
709-
if (attribute.name === 'contenteditable') {
710-
has_contenteditable_attr = true;
707+
if (is_event_attribute(attribute)) {
708+
handlers.add(attribute.name.slice(2));
709+
} else {
710+
attributes.push(attribute);
711+
attribute_map.set(attribute.name, attribute);
712+
if (attribute.name === 'contenteditable') {
713+
has_contenteditable_attr = true;
714+
}
711715
}
712716
} else if (
713717
attribute.type === 'BindDirective' &&

packages/svelte/src/compiler/warnings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const a11y = {
115115
'a11y-misplaced-scope': () => 'A11y: The scope attribute should only be used with <th> elements',
116116
'a11y-positive-tabindex': () => 'A11y: avoid tabindex values above zero',
117117
'a11y-click-events-have-key-events': () =>
118-
'A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type="button"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.',
118+
'A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type="button"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.',
119119
'a11y-no-noninteractive-tabindex': () =>
120120
'A11y: noninteractive element cannot have nonnegative tabIndex value',
121121
/**
@@ -180,7 +180,7 @@ const a11y = {
180180
* @param {string} accompanied_by
181181
*/
182182
'a11y-mouse-events-have-key-events': (event, accompanied_by) =>
183-
`A11y: on:${event} must be accompanied by on:${accompanied_by}`,
183+
`A11y: '${event}' event must be accompanied by '${accompanied_by}' event`,
184184
/** @param {string} name */
185185
'a11y-missing-content': (name) => `A11y: <${name}> element should have child content`
186186
};

packages/svelte/tests/validator/samples/a11y-click-events-have-key-events/input.svelte

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
<header on:click={noop} />
2525
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
2626
<footer on:click={noop} />
27+
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
28+
<footer onclick={noop} />
2729

2830
<!-- should not warn -->
2931
<div class="foo" />
@@ -66,6 +68,7 @@
6668
<div on:click={noop} role="presentation" />
6769
<div on:click={noop} role="none" />
6870
<div on:click={noop} role={dynamicRole} />
71+
<div onclick={noop} role={dynamicRole} />
6972

7073
<!-- svelte-ignore a11y-no-static-element-interactions -->
7174
<svelte:element this={Math.random() ? 'button' : 'div'} on:click={noop} />

packages/svelte/tests/validator/samples/a11y-click-events-have-key-events/warnings.json

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "a11y-click-events-have-key-events",
4-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
4+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
55
"start": {
66
"line": 13,
77
"column": 0
@@ -13,7 +13,7 @@
1313
},
1414
{
1515
"code": "a11y-click-events-have-key-events",
16-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
16+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
1717
"start": {
1818
"line": 15,
1919
"column": 0
@@ -25,7 +25,7 @@
2525
},
2626
{
2727
"code": "a11y-click-events-have-key-events",
28-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
28+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
2929
"start": {
3030
"line": 18,
3131
"column": 0
@@ -37,7 +37,7 @@
3737
},
3838
{
3939
"code": "a11y-click-events-have-key-events",
40-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
40+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
4141
"start": {
4242
"line": 20,
4343
"column": 0
@@ -49,7 +49,7 @@
4949
},
5050
{
5151
"code": "a11y-click-events-have-key-events",
52-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
52+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
5353
"start": {
5454
"line": 22,
5555
"column": 0
@@ -61,7 +61,7 @@
6161
},
6262
{
6363
"code": "a11y-click-events-have-key-events",
64-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
64+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
6565
"start": {
6666
"line": 24,
6767
"column": 0
@@ -73,7 +73,7 @@
7373
},
7474
{
7575
"code": "a11y-click-events-have-key-events",
76-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
76+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
7777
"start": {
7878
"line": 26,
7979
"column": 0
@@ -82,5 +82,17 @@
8282
"line": 26,
8383
"column": 26
8484
}
85+
},
86+
{
87+
"code": "a11y-click-events-have-key-events",
88+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
89+
"start": {
90+
"line": 28,
91+
"column": 0
92+
},
93+
"end": {
94+
"line": 28,
95+
"column": 25
96+
}
8597
}
8698
]

packages/svelte/tests/validator/samples/a11y-interactive-supports-focus/warnings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
"column": 44,
234234
"line": 29
235235
},
236-
"message": "A11y: on:mouseout must be accompanied by on:blur",
236+
"message": "A11y: 'mouseout' event must be accompanied by 'blur' event",
237237
"start": {
238238
"column": 0,
239239
"line": 29
@@ -257,7 +257,7 @@
257257
"column": 48,
258258
"line": 30
259259
},
260-
"message": "A11y: on:mouseover must be accompanied by on:focus",
260+
"message": "A11y: 'mouseover' event must be accompanied by 'focus' event",
261261
"start": {
262262
"column": 0,
263263
"line": 30

packages/svelte/tests/validator/samples/a11y-mouse-events-have-key-events/warnings.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"column": 35,
66
"line": 11
77
},
8-
"message": "A11y: on:mouseover must be accompanied by on:focus",
8+
"message": "A11y: 'mouseover' event must be accompanied by 'focus' event",
99
"start": {
1010
"column": 0,
1111
"line": 11
@@ -17,7 +17,7 @@
1717
"column": 51,
1818
"line": 15
1919
},
20-
"message": "A11y: on:mouseover must be accompanied by on:focus",
20+
"message": "A11y: 'mouseover' event must be accompanied by 'focus' event",
2121
"start": {
2222
"column": 0,
2323
"line": 15
@@ -29,7 +29,7 @@
2929
"column": 34,
3030
"line": 17
3131
},
32-
"message": "A11y: on:mouseout must be accompanied by on:blur",
32+
"message": "A11y: 'mouseout' event must be accompanied by 'blur' event",
3333
"start": {
3434
"column": 0,
3535
"line": 17
@@ -41,7 +41,7 @@
4141
"column": 50,
4242
"line": 21
4343
},
44-
"message": "A11y: on:mouseout must be accompanied by on:blur",
44+
"message": "A11y: 'mouseout' event must be accompanied by 'blur' event",
4545
"start": {
4646
"column": 0,
4747
"line": 21

packages/svelte/tests/validator/samples/slot-warning/warnings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "a11y-click-events-have-key-events",
4-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
4+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
55
"start": {
66
"column": 1,
77
"line": 7

packages/svelte/tests/validator/samples/slot-warning2/warnings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "a11y-click-events-have-key-events",
4-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
4+
"message": "A11y: visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
55
"start": {
66
"column": 1,
77
"line": 8

0 commit comments

Comments
 (0)