-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathtemplate-no-invalid-role.js
More file actions
196 lines (187 loc) · 4.7 KB
/
template-no-invalid-role.js
File metadata and controls
196 lines (187 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const { roles } = require('aria-query');
// Valid ARIA roles = concrete (non-abstract) entries from aria-query, plus the
// WAI-ARIA 1.3 draft roles that aria-query 5.3.2 doesn't yet ship. The
// ARIA 1.2 base roles, DPUB-ARIA (doc-*), and Graphics-ARIA (graphics-*) all
// come from aria-query. `associationlist*`, `comment`, and `suggestion` are in
// the current ARIA 1.3 editor's draft (https://w3c.github.io/aria/) but not
// yet in aria-query, so they're listed here until the next aria-query release
// adds them.
const ARIA_13_DRAFT_ROLES = [
'associationlist',
'associationlistitemkey',
'associationlistitemvalue',
'comment',
'suggestion',
];
const VALID_ROLES = new Set([
...[...roles.keys()].filter((role) => !roles.get(role).abstract),
...ARIA_13_DRAFT_ROLES,
]);
// Elements with semantic meaning that should not be given role="presentation" or role="none"
// List from https://developer.mozilla.org/en-US/docs/Web/HTML/Element
const SEMANTIC_ELEMENTS = new Set([
'a',
'abbr',
'applet',
'area',
'audio',
'b',
'bdi',
'bdo',
'blockquote',
'br',
'button',
'caption',
'cite',
'code',
'col',
'colgroup',
'data',
'datalist',
'dd',
'del',
'details',
'dfn',
'dialog',
'dir',
'dl',
'dt',
'em',
'embed',
'fieldset',
'figcaption',
'figure',
'form',
'hr',
'i',
'iframe',
'input',
'ins',
'kbd',
'label',
'legend',
'main',
'map',
'mark',
'menu',
'menuitem',
'meter',
'noembed',
'object',
'ol',
'optgroup',
'option',
'output',
'p',
'param',
'pre',
'progress',
'q',
'rb',
'rp',
'rt',
'rtc',
'ruby',
's',
'samp',
'select',
'small',
'source',
'strong',
'sub',
'summary',
'sup',
'table',
'tbody',
'td',
'textarea',
'tfoot',
'th',
'thead',
'time',
'tr',
'track',
'tt',
'u',
'ul',
'var',
'video',
'wbr',
]);
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow invalid ARIA roles',
category: 'Accessibility',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-invalid-role.md',
templateMode: 'both',
},
fixable: null,
schema: [
{
type: 'object',
properties: {
catchNonexistentRoles: { type: 'boolean' },
},
additionalProperties: false,
},
],
messages: {
invalid: "Invalid ARIA role '{{role}}'. Must be a valid ARIA role.",
presentationOnSemantic:
'The role "{{role}}" should not be used on the semantic element <{{tag}}>.',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-invalid-role.js',
docs: 'docs/rule/no-invalid-role.md',
tests: 'test/unit/rules/no-invalid-role-test.js',
},
},
create(context) {
const options = context.options[0] || {};
const catchNonexistentRoles = options.catchNonexistentRoles !== false; // default true
return {
GlimmerElementNode(node) {
const roleAttr = node.attributes?.find((a) => a.name === 'role');
if (!roleAttr || roleAttr.value?.type !== 'GlimmerTextNode') {
return;
}
const raw = roleAttr.value.chars.trim();
if (!raw) {
return;
}
// ARIA role attribute is a whitespace-separated list of tokens
// (role-fallback pattern per ARIA 1.2 §5.4). Validate each token.
const tokens = raw.split(/\s+/u).map((t) => t.toLowerCase());
if (catchNonexistentRoles) {
const invalidToken = tokens.find((token) => !VALID_ROLES.has(token));
if (invalidToken) {
context.report({
node: roleAttr,
messageId: 'invalid',
data: { role: invalidToken },
});
return;
}
}
// Check for presentation/none role on semantic elements (case-insensitive per WAI-ARIA 1.2:
// "Case-sensitivity of the comparison inherits from the case-sensitivity of the host language"
// and HTML is case-insensitive — https://www.w3.org/TR/wai-aria-1.2/#document-handling_author-errors_roles)
const offendingToken = tokens.find((t) => t === 'presentation' || t === 'none');
if (offendingToken && SEMANTIC_ELEMENTS.has(node.tag)) {
context.report({
node: roleAttr,
messageId: 'presentationOnSemantic',
// Report the specific offending token, not the whole raw role
// string — e.g. for role="presentation foo" we point at
// 'presentation' rather than the full attribute value.
data: { role: offendingToken, tag: node.tag },
});
}
},
};
},
};