Skip to content

Commit a96f4d0

Browse files
committed
Check required props for ARIA roles
1 parent 6aaa105 commit a96f4d0

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/compiler/compile/nodes/Element.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ const a11y_required_attributes = {
3939
object: ['title', 'aria-label', 'aria-labelledby']
4040
};
4141

42+
const a11y_required_role_props = {
43+
checkbox: ['aria-checked'],
44+
combobox: ['aria-controls', 'aria-expanded'],
45+
heading: ['aria-level'],
46+
menuitemcheckbox: ['aria-checked'],
47+
menuitemradio: ['aria-checked'],
48+
meter: ['aria-valuemax', 'aria-valuemin', 'aria-valuenow'],
49+
option: ['aria-selected'],
50+
radio: ['aria-checked'],
51+
scrollbar: ['aria-controls', 'aria-valuenow'],
52+
slider: ['aria-valuenow'],
53+
switch: ['aria-checked']
54+
};
55+
4256
const a11y_distracting_elements = new Set([
4357
'blink',
4458
'marquee'
@@ -304,11 +318,11 @@ export default class Element extends Node {
304318
}
305319

306320
validate_attributes() {
307-
const { component, parent } = this;
321+
const { component, parent, attributes } = this;
308322

309323
const attribute_map = new Map();
310324

311-
this.attributes.forEach(attribute => {
325+
attributes.forEach(attribute => {
312326
if (attribute.is_spread) return;
313327

314328
const name = attribute.name.toLowerCase();
@@ -365,6 +379,21 @@ export default class Element extends Node {
365379
code: 'a11y-unknown-role',
366380
message
367381
});
382+
} else {
383+
// @ts-ignore
384+
const required_role_props = a11y_required_role_props[value];
385+
386+
// role-has-required-aria-props
387+
if (required_role_props) {
388+
const has_missing_props = required_role_props.some(prop => !attributes.find(a => a.name === prop));
389+
390+
if (has_missing_props) {
391+
component.warn(attribute, {
392+
code: 'a11y-role-has-required-aria-props',
393+
message: `A11y: Elements with the ARIA role "${value}" must have the following attributes defined: ${String(required_role_props)}`
394+
});
395+
}
396+
}
368397
}
369398
}
370399

0 commit comments

Comments
 (0)