Skip to content

Commit

Permalink
swtich to use aria-query
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Jul 10, 2022
1 parent a85e5d3 commit 622bfc1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 26 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -124,12 +124,14 @@
"@rollup/plugin-typescript": "^2.0.1",
"@rollup/plugin-virtual": "^2.0.0",
"@sveltejs/eslint-config": "github:sveltejs/eslint-config#v5.8.0",
"@types/aria-query": "^5.0.0",
"@types/mocha": "^7.0.0",
"@types/node": "^8.10.53",
"@typescript-eslint/eslint-plugin": "^5.22.0",
"@typescript-eslint/parser": "^5.22.0",
"acorn": "^8.4.1",
"agadoo": "^1.1.0",
"aria-query": "^5.0.0",
"code-red": "^0.2.5",
"css-tree": "^1.1.2",
"eslint": "^8.0.0",
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/compile/compiler_warnings.ts
Expand Up @@ -80,6 +80,10 @@ export default {
code: 'a11y-no-redundant-roles',
message: `A11y: Redundant 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(', ')}`
}),
a11y_accesskey: {
code: 'a11y-accesskey',
message: 'A11y: Avoid using accesskey'
Expand Down
27 changes: 5 additions & 22 deletions src/compiler/compile/nodes/Element.ts
Expand Up @@ -23,6 +23,7 @@ import { string_literal } from '../utils/stringify';
import { Literal } from 'estree';
import compiler_warnings from '../compiler_warnings';
import compiler_errors from '../compiler_errors';
import { ARIARoleDefintionKey, roles } from 'aria-query';

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)$/;

Expand All @@ -45,20 +46,6 @@ const a11y_required_attributes = {
object: ['title', 'aria-label', 'aria-labelledby']
};

const a11y_required_role_props = {
checkbox: ['aria-checked'],
combobox: ['aria-controls', 'aria-expanded'],
heading: ['aria-level'],
menuitemcheckbox: ['aria-checked'],
menuitemradio: ['aria-checked'],
meter: ['aria-valuemax', 'aria-valuemin', 'aria-valuenow'],
option: ['aria-selected'],
radio: ['aria-checked'],
scrollbar: ['aria-controls', 'aria-valuenow'],
slider: ['aria-valuenow'],
switch: ['aria-checked']
};

const a11y_distracting_elements = new Set([
'blink',
'marquee'
Expand Down Expand Up @@ -477,18 +464,14 @@ export default class Element extends Node {
}
}

// @ts-ignore
const required_role_props = a11y_required_role_props[value];

// role-has-required-aria-props
if (required_role_props) {
const role = roles.get(value as ARIARoleDefintionKey);
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));

if (has_missing_props) {
component.warn(attribute, {
code: 'a11y-role-has-required-aria-props',
message: `A11y: Elements with the ARIA role "${value}" must have the following attributes defined: ${String(required_role_props)}`
});
component.warn(attribute, compiler_warnings.a11y_role_has_required_aria_props(value as string, required_role_props));
}
}
}
Expand Down
@@ -1,7 +1,9 @@
<div role="heading"></div>
<span role="checkbox"></span>
<div role="meter"></div>
<div role="scrollbar"></div>

<div role="heading" aria-level="1"></div>
<span role="checkbox" aria-checked="false"></span>
<div role="meter" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100"></div>
<div role="meter" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100"></div>
<div role="scrollbar" aria-controls="panel" aria-valuenow="50"></div>
@@ -1,7 +1,7 @@
[
{
"code": "a11y-role-has-required-aria-props",
"message": "A11y: Elements with the ARIA role \"heading\" must have the following attributes defined: aria-level",
"message": "A11y: Elements with the ARIA role \"heading\" must have the following attributes defined: \"aria-level\"",
"start": {
"line": 1,
"column": 5,
Expand All @@ -16,7 +16,7 @@
},
{
"code": "a11y-role-has-required-aria-props",
"message": "A11y: Elements with the ARIA role \"checkbox\" must have the following attributes defined: aria-checked",
"message": "A11y: Elements with the ARIA role \"checkbox\" must have the following attributes defined: \"aria-checked\"",
"start": {
"line": 2,
"column": 6,
Expand All @@ -31,7 +31,7 @@
},
{
"code": "a11y-role-has-required-aria-props",
"message": "A11y: Elements with the ARIA role \"meter\" must have the following attributes defined: aria-valuemax,aria-valuemin,aria-valuenow",
"message": "A11y: Elements with the ARIA role \"meter\" must have the following attributes defined: \"aria-valuenow\"",
"start": {
"line": 3,
"column": 5,
Expand All @@ -43,5 +43,20 @@
"character": 74
},
"pos": 62
},
{
"code": "a11y-role-has-required-aria-props",
"message": "A11y: Elements with the ARIA role \"scrollbar\" must have the following attributes defined: \"aria-controls\", \"aria-valuenow\"",
"start": {
"character": 87,
"column": 5,
"line": 4
},
"end": {
"character": 103,
"column": 21,
"line": 4
},
"pos": 87
}
]

0 comments on commit 622bfc1

Please sign in to comment.