diff --git a/package-lock.json b/package-lock.json
index ab4b20ed594..01e23422bbe 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -26,6 +26,7 @@
"acorn": "^8.4.1",
"agadoo": "^1.1.0",
"aria-query": "^5.0.0",
+ "axobject-query": "^3.0.1",
"code-red": "^0.2.5",
"css-tree": "^1.1.2",
"eslint": "^8.0.0",
@@ -812,6 +813,15 @@
"integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==",
"dev": true
},
+ "node_modules/axobject-query": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.0.1.tgz",
+ "integrity": "sha512-vy5JPSOibF9yAeC2PoemRdA1MuSXX7vX5osdoxKf/6OUeppAWekZ3JIJVNWFMH6wgj7uHYyqZUSqE/b/3JLV1A==",
+ "dev": true,
+ "engines": {
+ "node": ">=6.0"
+ }
+ },
"node_modules/balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@@ -5313,6 +5323,12 @@
"integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==",
"dev": true
},
+ "axobject-query": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.0.1.tgz",
+ "integrity": "sha512-vy5JPSOibF9yAeC2PoemRdA1MuSXX7vX5osdoxKf/6OUeppAWekZ3JIJVNWFMH6wgj7uHYyqZUSqE/b/3JLV1A==",
+ "dev": true
+ },
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
diff --git a/package.json b/package.json
index ade713f4e3f..9bdf7b3089d 100644
--- a/package.json
+++ b/package.json
@@ -132,6 +132,7 @@
"acorn": "^8.4.1",
"agadoo": "^1.1.0",
"aria-query": "^5.0.0",
+ "axobject-query": "^3.0.1",
"code-red": "^0.2.5",
"css-tree": "^1.1.2",
"eslint": "^8.0.0",
diff --git a/site/content/docs/05-accessibility-warnings.md b/site/content/docs/05-accessibility-warnings.md
index 438f2e47437..495d0107c4d 100644
--- a/site/content/docs/05-accessibility-warnings.md
+++ b/site/content/docs/05-accessibility-warnings.md
@@ -250,6 +250,17 @@ Some HTML elements have default ARIA roles. Giving these elements an ARIA role t
---
+### `a11y-no-interactive-element-to-noninteractive-role`
+
+[WAI-ARIA](https://www.w3.org/TR/wai-aria-1.1/#usage_intro) roles should not be used to convert an interactive element to a non-interactive element. Non-interactive ARIA roles include `article`, `banner`, `complementary`, `img`, `listitem`, `main`, `region` and `tooltip`.
+
+```sv
+
+
+```
+
+---
+
### `a11y-positive-tabindex`
Avoid positive `tabindex` property values. This will move elements out of the expected tab order, creating a confusing experience for keyboard users.
diff --git a/src/compiler/compile/compiler_warnings.ts b/src/compiler/compile/compiler_warnings.ts
index 0d703e0d8d9..5fc5faa8f12 100644
--- a/src/compiler/compile/compiler_warnings.ts
+++ b/src/compiler/compile/compiler_warnings.ts
@@ -115,6 +115,10 @@ export default {
code: 'a11y-no-redundant-roles',
message: `A11y: Redundant role '${role}'`
}),
+ a11y_no_interactive_element_to_noninteractive_role: (role: string | boolean, element: string) => ({
+ code: 'a11y-no-interactive-element-to-noninteractive-role',
+ message: `A11y: <${element}> cannot have role '${role}'`
+ }),
a11y_role_has_required_aria_props: (role: string, props: string[]) => ({
code: 'a11y-role-has-required-aria-props',
message: `A11y: Elements with the ARIA role "${role}" must have the following attributes defined: ${props.map(name => `"${name}"`).join(', ')}`
diff --git a/src/compiler/compile/nodes/Element.ts b/src/compiler/compile/nodes/Element.ts
index 760e229b0fd..e351a02499c 100644
--- a/src/compiler/compile/nodes/Element.ts
+++ b/src/compiler/compile/nodes/Element.ts
@@ -24,13 +24,14 @@ import { Literal } from 'estree';
import compiler_warnings from '../compiler_warnings';
import compiler_errors from '../compiler_errors';
import { ARIARoleDefintionKey, roles, aria, ARIAPropertyDefinition, ARIAProperty } from 'aria-query';
+import { is_interactive_element, is_non_interactive_roles, is_presentation_role } from '../utils/a11y';
const svg = /^(?:altGlyph|altGlyphDef|altGlyphItem|animate|animateColor|animateMotion|animateTransform|circle|clipPath|color-profile|cursor|defs|desc|discard|ellipse|feBlend|feColorMatrix|feComponentTransfer|feComposite|feConvolveMatrix|feDiffuseLighting|feDisplacementMap|feDistantLight|feDropShadow|feFlood|feFuncA|feFuncB|feFuncG|feFuncR|feGaussianBlur|feImage|feMerge|feMergeNode|feMorphology|feOffset|fePointLight|feSpecularLighting|feSpotLight|feTile|feTurbulence|filter|font|font-face|font-face-format|font-face-name|font-face-src|font-face-uri|foreignObject|g|glyph|glyphRef|hatch|hatchpath|hkern|image|line|linearGradient|marker|mask|mesh|meshgradient|meshpatch|meshrow|metadata|missing-glyph|mpath|path|pattern|polygon|polyline|radialGradient|rect|set|solidcolor|stop|svg|switch|symbol|text|textPath|tref|tspan|unknown|use|view|vkern)$/;
const aria_attributes = 'activedescendant atomic autocomplete busy checked colcount colindex colspan controls current describedby description details disabled dropeffect errormessage expanded flowto grabbed haspopup hidden invalid keyshortcuts label labelledby level live modal multiline multiselectable orientation owns placeholder posinset pressed readonly relevant required roledescription rowcount rowindex rowspan selected setsize sort valuemax valuemin valuenow valuetext'.split(' ');
const aria_attribute_set = new Set(aria_attributes);
-const aria_roles = 'alert alertdialog application article banner blockquote button caption cell checkbox code columnheader combobox complementary contentinfo definition deletion dialog directory document emphasis feed figure form generic graphics-document graphics-object graphics-symbol grid gridcell group heading img link list listbox listitem log main marquee math meter menu menubar menuitem menuitemcheckbox menuitemradio navigation none note option paragraph presentation progressbar radio radiogroup region row rowgroup rowheader scrollbar search searchbox separator slider spinbutton status strong subscript superscript switch tab table tablist tabpanel term textbox time timer toolbar tooltip tree treegrid treeitem'.split(' ');
+const aria_roles = roles.keys();
const aria_role_set = new Set(aria_roles);
const aria_role_abstract_set = new Set(roles.keys().filter(role => roles.get(role).abstract));
@@ -437,6 +438,11 @@ export default class Element extends Node {
validate_attributes_a11y() {
const { component, attributes } = this;
+ const attribute_map = new Map();
+ attributes.forEach(attribute => (
+ attribute_map.set(attribute.name, attribute)
+ ));
+
attributes.forEach(attribute => {
if (attribute.is_spread) return;
@@ -479,12 +485,11 @@ export default class Element extends Node {
component.warn(attribute, compiler_warnings.a11y_misplaced_role(this.name));
}
- const value = attribute.get_static_value();
+ const value = attribute.get_static_value() as ARIARoleDefintionKey;
- if (value && aria_role_abstract_set.has(value as ARIARoleDefintionKey)) {
+ if (value && aria_role_abstract_set.has(value)) {
component.warn(attribute, compiler_warnings.a11y_no_abstract_role(value));
- } else if (value && !aria_role_set.has(value as string)) {
- // @ts-ignore
+ } else if (value && !aria_role_set.has(value)) {
const match = fuzzymatch(value, aria_roles);
component.warn(attribute, compiler_warnings.a11y_unknown_role(value, match));
}
@@ -506,7 +511,7 @@ export default class Element extends Node {
}
// role-has-required-aria-props
- const role = roles.get(value as ARIARoleDefintionKey);
+ const role = roles.get(value);
if (role) {
const required_role_props = Object.keys(role.requiredProps);
const has_missing_props = required_role_props.some(prop => !attributes.find(a => a.name === prop));
@@ -515,6 +520,11 @@ export default class Element extends Node {
component.warn(attribute, compiler_warnings.a11y_role_has_required_aria_props(value as string, required_role_props));
}
}
+
+ // no-interactive-element-to-noninteractive-role
+ if (is_interactive_element(this.name, attribute_map) && (is_non_interactive_roles(value) || is_presentation_role(value))) {
+ component.warn(this, compiler_warnings.a11y_no_interactive_element_to_noninteractive_role(value, this.name));
+ }
}
// no-access-key
diff --git a/src/compiler/compile/utils/a11y.ts b/src/compiler/compile/utils/a11y.ts
new file mode 100644
index 00000000000..1e06608b54e
--- /dev/null
+++ b/src/compiler/compile/utils/a11y.ts
@@ -0,0 +1,137 @@
+import {
+ ARIARoleDefintionKey,
+ roles as roles_map,
+ elementRoles,
+ ARIARoleRelationConcept
+} from 'aria-query';
+import { AXObjects, elementAXObjects } from 'axobject-query';
+import Attribute from '../nodes/Attribute';
+
+const roles = [...roles_map.keys()];
+
+const non_interactive_roles = new Set(
+ roles
+ .filter((name) => {
+ const role = roles_map.get(name);
+ return (
+ !roles_map.get(name).abstract &&
+ // 'toolbar' does not descend from widget, but it does support
+ // aria-activedescendant, thus in practice we treat it as a widget.
+ name !== 'toolbar' &&
+ !role.superClass.some((classes) => classes.includes('widget'))
+ );
+ })
+ .concat(
+ // The `progressbar` is descended from `widget`, but in practice, its
+ // value is always `readonly`, so we treat it as a non-interactive role.
+ 'progressbar'
+ )
+);
+
+const interactive_roles = new Set(
+ roles
+ .filter((name) => {
+ const role = roles_map.get(name);
+ return (
+ !role.abstract &&
+ // The `progressbar` is descended from `widget`, but in practice, its
+ // value is always `readonly`, so we treat it as a non-interactive role.
+ name !== 'progressbar' &&
+ role.superClass.some((classes) => classes.includes('widget'))
+ );
+ })
+ .concat(
+ // 'toolbar' does not descend from widget, but it does support
+ // aria-activedescendant, thus in practice we treat it as a widget.
+ 'toolbar'
+ )
+);
+
+export function is_non_interactive_roles(role: ARIARoleDefintionKey) {
+ return non_interactive_roles.has(role);
+}
+
+const presentation_roles = new Set(['presentation', 'none']);
+
+export function is_presentation_role(role: ARIARoleDefintionKey) {
+ return presentation_roles.has(role);
+}
+
+const non_interactive_element_role_schemas: ARIARoleRelationConcept[] = [];
+
+elementRoles.entries().forEach(([schema, roles]) => {
+ if ([...roles].every((role) => non_interactive_roles.has(role))) {
+ non_interactive_element_role_schemas.push(schema);
+ }
+});
+
+const interactive_element_role_schemas: ARIARoleRelationConcept[] = [];
+
+elementRoles.entries().forEach(([schema, roles]) => {
+ if ([...roles].every((role) => interactive_roles.has(role))) {
+ interactive_element_role_schemas.push(schema);
+ }
+});
+
+const interactive_ax_objects = new Set(
+ [...AXObjects.keys()].filter((name) => AXObjects.get(name).type === 'widget')
+);
+
+const interactive_element_ax_object_schemas: ARIARoleRelationConcept[] = [];
+
+elementAXObjects.entries().forEach(([schema, ax_object]) => {
+ if ([...ax_object].every((role) => interactive_ax_objects.has(role))) {
+ interactive_element_ax_object_schemas.push(schema);
+ }
+});
+
+function match_schema(
+ schema: ARIARoleRelationConcept,
+ tag_name: string,
+ attribute_map: Map
+) {
+ if (schema.name !== tag_name) return false;
+ if (!schema.attributes) return true;
+ return schema.attributes.every((schema_attribute) => {
+ const attribute = attribute_map.get(schema_attribute.name);
+ if (!attribute) return false;
+ if (
+ schema_attribute.value &&
+ schema_attribute.value !== attribute.get_static_value()
+ ) {
+return false;
+}
+ return true;
+ });
+}
+
+export function is_interactive_element(
+ tag_name: string,
+ attribute_map: Map
+): boolean {
+ if (
+ interactive_element_role_schemas.some((schema) =>
+ match_schema(schema, tag_name, attribute_map)
+ )
+ ) {
+ return true;
+ }
+
+ if (
+ non_interactive_element_role_schemas.some((schema) =>
+ match_schema(schema, tag_name, attribute_map)
+ )
+ ) {
+ return false;
+ }
+
+ if (
+ interactive_element_ax_object_schemas.some((schema) =>
+ match_schema(schema, tag_name, attribute_map)
+ )
+ ) {
+ return true;
+ }
+
+ return false;
+}
diff --git a/test/validator/samples/a11y-no-interactive-element-to-noninteractive-role/input.svelte b/test/validator/samples/a11y-no-interactive-element-to-noninteractive-role/input.svelte
new file mode 100644
index 00000000000..80b4fd94102
--- /dev/null
+++ b/test/validator/samples/a11y-no-interactive-element-to-noninteractive-role/input.svelte
@@ -0,0 +1,150 @@
+
+link
+link
+link
+link
+link
+link
+link
+link
+link
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+title
+title
+title
+title
+title
+title
+
+
+
+
+
+
+
+
+ |
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/test/validator/samples/a11y-no-interactive-element-to-noninteractive-role/warnings.json b/test/validator/samples/a11y-no-interactive-element-to-noninteractive-role/warnings.json
new file mode 100644
index 00000000000..f702a4a31dd
--- /dev/null
+++ b/test/validator/samples/a11y-no-interactive-element-to-noninteractive-role/warnings.json
@@ -0,0 +1,887 @@
+[
+ {
+ "code": "a11y-no-interactive-element-to-noninteractive-role",
+ "end": {
+ "character": 49,
+ "column": 38,
+ "line": 2
+ },
+ "message": "A11y: cannot have role 'article'",
+ "pos": 11,
+ "start": {
+ "character": 11,
+ "column": 0,
+ "line": 2
+ }
+ },
+ {
+ "code": "a11y-no-interactive-element-to-noninteractive-role",
+ "end": {
+ "character": 87,
+ "column": 37,
+ "line": 3
+ },
+ "message": "A11y: cannot have role 'banner'",
+ "pos": 50,
+ "start": {
+ "character": 50,
+ "column": 0,
+ "line": 3
+ }
+ },
+ {
+ "code": "a11y-no-interactive-element-to-noninteractive-role",
+ "end": {
+ "character": 132,
+ "column": 44,
+ "line": 4
+ },
+ "message": "A11y: cannot have role 'complementary'",
+ "pos": 88,
+ "start": {
+ "character": 88,
+ "column": 0,
+ "line": 4
+ }
+ },
+ {
+ "code": "a11y-no-interactive-element-to-noninteractive-role",
+ "end": {
+ "character": 167,
+ "column": 34,
+ "line": 5
+ },
+ "message": "A11y: cannot have role 'img'",
+ "pos": 133,
+ "start": {
+ "character": 133,
+ "column": 0,
+ "line": 5
+ }
+ },
+ {
+ "code": "a11y-no-interactive-element-to-noninteractive-role",
+ "end": {
+ "character": 207,
+ "column": 39,
+ "line": 6
+ },
+ "message": "A11y: cannot have role 'listitem'",
+ "pos": 168,
+ "start": {
+ "character": 168,
+ "column": 0,
+ "line": 6
+ }
+ },
+ {
+ "code": "a11y-no-interactive-element-to-noninteractive-role",
+ "end": {
+ "character": 243,
+ "column": 35,
+ "line": 7
+ },
+ "message": "A11y: cannot have role 'main'",
+ "pos": 208,
+ "start": {
+ "character": 208,
+ "column": 0,
+ "line": 7
+ }
+ },
+ {
+ "code": "a11y-no-interactive-element-to-noninteractive-role",
+ "end": {
+ "character": 281,
+ "column": 37,
+ "line": 8
+ },
+ "message": "A11y: cannot have role 'region'",
+ "pos": 244,
+ "start": {
+ "character": 244,
+ "column": 0,
+ "line": 8
+ }
+ },
+ {
+ "code": "a11y-no-interactive-element-to-noninteractive-role",
+ "end": {
+ "character": 320,
+ "column": 38,
+ "line": 9
+ },
+ "message": "A11y: cannot have role 'tooltip'",
+ "pos": 282,
+ "start": {
+ "character": 282,
+ "column": 0,
+ "line": 9
+ }
+ },
+ {
+ "code": "a11y-no-interactive-element-to-noninteractive-role",
+ "end": {
+ "character": 414,
+ "column": 38,
+ "line": 13
+ },
+ "message": "A11y: