mirror of https://github.com/sveltejs/svelte
commit
ad59771bbc
@ -1,15 +0,0 @@
|
||||
import { Node } from '../../interfaces';
|
||||
|
||||
export default function getStaticAttributeValue(node: Node, name: string) {
|
||||
const attribute = node.attributes.find(
|
||||
(attr: Node) => attr.name.toLowerCase() === name
|
||||
);
|
||||
if (!attribute) return null;
|
||||
|
||||
if (attribute.value.length !== 1 || attribute.value[0].type !== 'Text') {
|
||||
// TODO catch this in validation phase, give a more useful error (with location etc)
|
||||
throw new Error(`'${name}' must be a static attribute`);
|
||||
}
|
||||
|
||||
return attribute.value[0].data;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
import { Node } from '../interfaces';
|
||||
|
||||
export default function getStaticAttributeValue(node: Node, name: string) {
|
||||
const attribute = node.attributes.find(
|
||||
(attr: Node) => attr.name.toLowerCase() === name
|
||||
);
|
||||
|
||||
if (!attribute) return null;
|
||||
|
||||
if (attribute.value.length === 0) return '';
|
||||
|
||||
if (attribute.value.length === 1 && attribute.value[0].type === 'Text') {
|
||||
return attribute.value[0].data;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
@ -0,0 +1,171 @@
|
||||
import * as namespaces from '../../utils/namespaces';
|
||||
import getStaticAttributeValue from '../../utils/getStaticAttributeValue';
|
||||
import fuzzymatch from '../utils/fuzzymatch';
|
||||
import validateEventHandler from './validateEventHandler';
|
||||
import { Validator } from '../index';
|
||||
import { Node } from '../../interfaces';
|
||||
|
||||
const ariaAttributes = 'activedescendant atomic autocomplete busy checked controls describedby disabled dropeffect expanded flowto grabbed haspopup hidden invalid label labelledby level live multiline multiselectable orientation owns posinset pressed readonly relevant required selected setsize sort valuemax valuemin valuenow valuetext'.split(' ');
|
||||
const ariaAttributeSet = new Set(ariaAttributes);
|
||||
|
||||
const ariaRoles = 'alert alertdialog application article banner button checkbox columnheader combobox command complementary composite contentinfo definition dialog directory document form grid gridcell group heading img input landmark link list listbox listitem log main marquee math menu menubar menuitem menuitemcheckbox menuitemradio navigation note option presentation progressbar radio radiogroup range region roletype row rowgroup rowheader scrollbar search section sectionhead select separator slider spinbutton status structure tab tablist tabpanel textbox timer toolbar tooltip tree treegrid treeitem widget window'.split(' ');
|
||||
const ariaRoleSet = new Set(ariaRoles);
|
||||
|
||||
const invisibleElements = new Set(['meta', 'html', 'script', 'style']);
|
||||
|
||||
export default function a11y(
|
||||
validator: Validator,
|
||||
node: Node,
|
||||
elementStack: Node[]
|
||||
) {
|
||||
if (node.type === 'Text') {
|
||||
// accessible-emoji
|
||||
return;
|
||||
}
|
||||
|
||||
if (node.type !== 'Element') return;
|
||||
|
||||
const attributeMap = new Map();
|
||||
node.attributes.forEach((attribute: Node) => {
|
||||
const name = attribute.name.toLowerCase();
|
||||
|
||||
// aria-props
|
||||
if (name.startsWith('aria-')) {
|
||||
if (invisibleElements.has(node.name)) {
|
||||
// aria-unsupported-elements
|
||||
validator.warn(`A11y: <${node.name}> should not have aria-* attributes`, attribute.start);
|
||||
}
|
||||
|
||||
const type = name.slice(5);
|
||||
if (!ariaAttributeSet.has(type)) {
|
||||
const match = fuzzymatch(type, ariaAttributes);
|
||||
let message = `A11y: Unknown aria attribute 'aria-${type}'`;
|
||||
if (match) message += ` (did you mean '${match}'?)`;
|
||||
|
||||
validator.warn(message, attribute.start);
|
||||
}
|
||||
}
|
||||
|
||||
// aria-role
|
||||
if (name === 'role') {
|
||||
if (invisibleElements.has(node.name)) {
|
||||
// aria-unsupported-elements
|
||||
validator.warn(`A11y: <${node.name}> should not have role attribute`, attribute.start);
|
||||
}
|
||||
|
||||
const value = getStaticAttributeValue(node, 'role');
|
||||
if (value && !ariaRoleSet.has(value)) {
|
||||
const match = fuzzymatch(value, ariaRoles);
|
||||
let message = `A11y: Unknown role '${value}'`;
|
||||
if (match) message += ` (did you mean '${match}'?)`;
|
||||
|
||||
validator.warn(message, attribute.start);
|
||||
}
|
||||
}
|
||||
|
||||
// no-access-key
|
||||
if (name === 'accesskey') {
|
||||
validator.warn(`A11y: Avoid using accesskey`, attribute.start);
|
||||
}
|
||||
|
||||
// no-autofocus
|
||||
if (name === 'autofocus') {
|
||||
validator.warn(`A11y: Avoid using autofocus`, attribute.start);
|
||||
}
|
||||
|
||||
// scope
|
||||
if (name === 'scope' && node.name !== 'th') {
|
||||
validator.warn(`A11y: The scope attribute should only be used with <th> elements`, attribute.start);
|
||||
}
|
||||
|
||||
// tabindex-no-positive
|
||||
if (name === 'tabindex') {
|
||||
const value = getStaticAttributeValue(node, 'tabindex');
|
||||
if (!isNaN(value) && +value > 0) {
|
||||
validator.warn(`A11y: avoid tabindex values above zero`, attribute.start);
|
||||
}
|
||||
}
|
||||
|
||||
attributeMap.set(attribute.name, attribute);
|
||||
});
|
||||
|
||||
function shouldHaveAttribute(attributes: string[], name = node.name) {
|
||||
if (attributes.length === 0 || !attributes.some((name: string) => attributeMap.has(name))) {
|
||||
const article = /^[aeiou]/.test(attributes[0]) ? 'an' : 'a';
|
||||
const sequence = attributes.length > 1 ?
|
||||
attributes.slice(0, -1).join(', ') + ` or ${attributes[attributes.length - 1]}` :
|
||||
attributes[0];
|
||||
|
||||
validator.warn(`A11y: <${name}> element should have ${article} ${sequence} attribute`, node.start);
|
||||
}
|
||||
}
|
||||
|
||||
function shouldHaveContent() {
|
||||
if (node.children.length === 0) {
|
||||
validator.warn(`A11y: <${node.name}> element should have child content`, node.start);
|
||||
}
|
||||
}
|
||||
|
||||
if (node.name === 'a') {
|
||||
// anchor-is-valid
|
||||
const href = attributeMap.get('href');
|
||||
if (attributeMap.has('href')) {
|
||||
const value = getStaticAttributeValue(node, 'href');
|
||||
if (value === '' || value === '#') {
|
||||
validator.warn(`A11y: '${value}' is not a valid href attribute`, href.start);
|
||||
}
|
||||
} else {
|
||||
validator.warn(`A11y: <a> element should have an href attribute`, node.start);
|
||||
}
|
||||
|
||||
// anchor-has-content
|
||||
shouldHaveContent();
|
||||
}
|
||||
|
||||
if (node.name === 'img') shouldHaveAttribute(['alt']);
|
||||
if (node.name === 'area') shouldHaveAttribute(['alt', 'aria-label', 'aria-labelledby']);
|
||||
if (node.name === 'object') shouldHaveAttribute(['title', 'aria-label', 'aria-labelledby']);
|
||||
if (node.name === 'input' && getStaticAttributeValue(node, 'type') === 'image') {
|
||||
shouldHaveAttribute(['alt', 'aria-label', 'aria-labelledby'], 'input type="image"');
|
||||
}
|
||||
|
||||
// heading-has-content
|
||||
if (/^h[1-6]$/.test(node.name)) {
|
||||
shouldHaveContent();
|
||||
|
||||
if (attributeMap.has('aria-hidden')) {
|
||||
validator.warn(`A11y: <${node.name}> element should not be hidden`, attributeMap.get('aria-hidden').start);
|
||||
}
|
||||
}
|
||||
|
||||
// iframe-has-title
|
||||
if (node.name === 'iframe') {
|
||||
shouldHaveAttribute(['title']);
|
||||
}
|
||||
|
||||
// no-distracting-elements
|
||||
if (node.name === 'marquee' || node.name === 'blink') {
|
||||
validator.warn(`A11y: Avoid <${node.name}> elements`, node.start);
|
||||
}
|
||||
|
||||
if (node.name === 'figcaption') {
|
||||
const parent = elementStack[elementStack.length - 1];
|
||||
if (parent) {
|
||||
if (parent.name !== 'figure') {
|
||||
validator.warn(`A11y: <figcaption> must be an immediate child of <figure>`, node.start);
|
||||
} else {
|
||||
const index = parent.children.indexOf(node);
|
||||
if (index !== 0 && index !== parent.children.length - 1) {
|
||||
validator.warn(`A11y: <figcaption> must be first or last child of <figure>`, node.start);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getValue(attribute: Node) {
|
||||
if (attribute.value.length === 0) return '';
|
||||
if (attribute.value.length === 1 && attribute.value[0].type === 'Text') return attribute.value[0].data;
|
||||
|
||||
return null;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<img src='foo.jpg'>
|
||||
|
||||
<map>
|
||||
<area>
|
||||
</map>
|
||||
|
||||
<object></object>
|
||||
|
||||
<input type='image'>
|
@ -0,0 +1,37 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: <img> element should have an alt attribute",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 0
|
||||
},
|
||||
|
||||
{
|
||||
"message": "A11y: <area> element should have an alt, aria-label or aria-labelledby attribute",
|
||||
"loc": {
|
||||
"line": 4,
|
||||
"column": 1
|
||||
},
|
||||
"pos": 28
|
||||
},
|
||||
|
||||
{
|
||||
"message": "A11y: <object> element should have a title, aria-label or aria-labelledby attribute",
|
||||
"loc": {
|
||||
"line": 7,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 43
|
||||
},
|
||||
|
||||
{
|
||||
"message": "A11y: <input type=\"image\"> element should have an alt, aria-label or aria-labelledby attribute",
|
||||
"loc": {
|
||||
"line": 9,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 62
|
||||
}
|
||||
]
|
@ -0,0 +1 @@
|
||||
<a href='/foo'></a>
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "A11y: <a> element should have child content",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 0
|
||||
}]
|
@ -0,0 +1,3 @@
|
||||
<a>not actually a link</a>
|
||||
<a href=''>invalid</a>
|
||||
<a href='#'>invalid</a>
|
@ -0,0 +1,26 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: <a> element should have an href attribute",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 0
|
||||
},
|
||||
{
|
||||
"message": "A11y: '' is not a valid href attribute",
|
||||
"loc": {
|
||||
"line": 2,
|
||||
"column": 3
|
||||
},
|
||||
"pos": 30
|
||||
},
|
||||
{
|
||||
"message": "A11y: '#' is not a valid href attribute",
|
||||
"loc": {
|
||||
"line": 3,
|
||||
"column": 3
|
||||
},
|
||||
"pos": 53
|
||||
}
|
||||
]
|
@ -0,0 +1 @@
|
||||
<input type="image" aria-labeledby="foo">
|
@ -0,0 +1,19 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: Unknown aria attribute 'aria-labeledby' (did you mean 'labelledby'?)",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
},
|
||||
"pos": 20
|
||||
},
|
||||
|
||||
{
|
||||
"message": "A11y: <input type=\"image\"> element should have an alt, aria-label or aria-labelledby attribute",
|
||||
"loc": {
|
||||
"column": 0,
|
||||
"line": 1
|
||||
},
|
||||
"pos": 0
|
||||
}
|
||||
]
|
@ -0,0 +1 @@
|
||||
<div role="toooltip"></div>
|
@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: Unknown role 'toooltip' (did you mean 'tooltip'?)",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"pos": 5
|
||||
}
|
||||
]
|
@ -0,0 +1,2 @@
|
||||
<meta aria-hidden="false">
|
||||
<meta role="tooltip">
|
@ -0,0 +1,19 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: <meta> should not have aria-* attributes",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
},
|
||||
"pos": 6
|
||||
},
|
||||
|
||||
{
|
||||
"message": "A11y: <meta> should not have role attribute",
|
||||
"loc": {
|
||||
"line": 2,
|
||||
"column": 6
|
||||
},
|
||||
"pos": 33
|
||||
}
|
||||
]
|
@ -0,0 +1,19 @@
|
||||
<figure>
|
||||
<img src='foo.jpg' alt='a picture of a foo'>
|
||||
|
||||
<figcaption>
|
||||
a foo in its natural habitat
|
||||
</figcaption>
|
||||
|
||||
<p>this should not be here</p>
|
||||
</figure>
|
||||
|
||||
<figure>
|
||||
<img src='foo.jpg' alt='a picture of a foo'>
|
||||
|
||||
<div class='markup-for-styling'>
|
||||
<figcaption>
|
||||
this element should be a child of the figure
|
||||
</figcaption>
|
||||
</div>
|
||||
</figure>
|
@ -0,0 +1,18 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: <figcaption> must be first or last child of <figure>",
|
||||
"loc": {
|
||||
"line": 4,
|
||||
"column": 1
|
||||
},
|
||||
"pos": 57
|
||||
},
|
||||
{
|
||||
"message": "A11y: <figcaption> must be an immediate child of <figure>",
|
||||
"loc": {
|
||||
"line": 15,
|
||||
"column": 2
|
||||
},
|
||||
"pos": 252
|
||||
}
|
||||
]
|
@ -0,0 +1,2 @@
|
||||
<h1></h1>
|
||||
<h2 aria-hidden>invisible header</h2>
|
@ -0,0 +1,19 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: <h1> element should have child content",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 0
|
||||
},
|
||||
|
||||
{
|
||||
"message": "A11y: <h2> element should not be hidden",
|
||||
"loc": {
|
||||
"line": 2,
|
||||
"column": 4
|
||||
},
|
||||
"pos": 14
|
||||
}
|
||||
]
|
@ -0,0 +1 @@
|
||||
<iframe src='{{src}}'></iframe>
|
@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: <iframe> element should have a title attribute",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 0
|
||||
}
|
||||
]
|
@ -0,0 +1 @@
|
||||
<div accessKey='z'></div>
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "A11y: Avoid using accesskey",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"pos": 5
|
||||
}]
|
@ -0,0 +1 @@
|
||||
<div autofocus></div>
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "A11y: Avoid using autofocus",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"pos": 5
|
||||
}]
|
@ -0,0 +1,2 @@
|
||||
<marquee/>
|
||||
<blink/>
|
@ -0,0 +1,19 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: Avoid <marquee> elements",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 0
|
||||
},
|
||||
|
||||
{
|
||||
"message": "A11y: Avoid <blink> elements",
|
||||
"loc": {
|
||||
"line": 2,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 11
|
||||
}
|
||||
]
|
@ -0,0 +1 @@
|
||||
<div scope/>
|
@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: The scope attribute should only be used with <th> elements",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"pos": 5
|
||||
}
|
||||
]
|
@ -0,0 +1,4 @@
|
||||
<div tabindex='-1'/>
|
||||
<div tabindex='0'/>
|
||||
<div tabindex='1'/>
|
||||
<div tabindex='{{foo}}'/>
|
@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"message": "A11y: avoid tabindex values above zero",
|
||||
"loc": {
|
||||
"line": 3,
|
||||
"column": 5
|
||||
},
|
||||
"pos": 46
|
||||
}
|
||||
]
|
Loading…
Reference in new issue