Skip to content

Commit e4bb4d6

Browse files
melnarytanhauhau
authored andcommitted
Check required props for ARIA roles
1 parent 52153db commit e4bb4d6

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
@@ -45,6 +45,20 @@ const a11y_required_attributes = {
4545
object: ['title', 'aria-label', 'aria-labelledby']
4646
};
4747

48+
const a11y_required_role_props = {
49+
checkbox: ['aria-checked'],
50+
combobox: ['aria-controls', 'aria-expanded'],
51+
heading: ['aria-level'],
52+
menuitemcheckbox: ['aria-checked'],
53+
menuitemradio: ['aria-checked'],
54+
meter: ['aria-valuemax', 'aria-valuemin', 'aria-valuenow'],
55+
option: ['aria-selected'],
56+
radio: ['aria-checked'],
57+
scrollbar: ['aria-controls', 'aria-valuenow'],
58+
slider: ['aria-valuenow'],
59+
switch: ['aria-checked']
60+
};
61+
4862
const a11y_distracting_elements = new Set([
4963
'blink',
5064
'marquee'
@@ -407,9 +421,9 @@ export default class Element extends Node {
407421
}
408422

409423
validate_attributes_a11y() {
410-
const { component } = this;
424+
const { component, attributes } = this;
411425

412-
this.attributes.forEach(attribute => {
426+
attributes.forEach(attribute => {
413427
if (attribute.is_spread) return;
414428

415429
const name = attribute.name.toLowerCase();
@@ -462,6 +476,21 @@ export default class Element extends Node {
462476
component.warn(attribute, compiler_warnings.a11y_no_redundant_roles(value));
463477
}
464478
}
479+
480+
// @ts-ignore
481+
const required_role_props = a11y_required_role_props[value];
482+
483+
// role-has-required-aria-props
484+
if (required_role_props) {
485+
const has_missing_props = required_role_props.some(prop => !attributes.find(a => a.name === prop));
486+
487+
if (has_missing_props) {
488+
component.warn(attribute, {
489+
code: 'a11y-role-has-required-aria-props',
490+
message: `A11y: Elements with the ARIA role "${value}" must have the following attributes defined: ${String(required_role_props)}`
491+
});
492+
}
493+
}
465494
}
466495

467496
// no-access-key

0 commit comments

Comments
 (0)