-
Notifications
You must be signed in to change notification settings - Fork 126
/
img-uses-alt.js
69 lines (63 loc) · 1.84 KB
/
img-uses-alt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
devices,
hiddenFromAT,
hasProp
} from '../util';
// test will be run in order
export default [{
tagName: 'img',
msg: 'The img does not have an `alt` prop, screen-readers will not know what it is',
url: 'https://dequeuniversity.com/rules/axe/2.1/image-alt',
test(tagName, props) {
const hidden = hiddenFromAT(props);
const alt = hasProp(props, 'alt');
return hidden || alt;
},
affects: [
devices.screenReaders
]
},
{
tagName: 'img',
msg: 'The `alt` prop cannot be empty string if role="presentation" is not set.',
url: 'https://www.w3.org/WAI/PF/aria/roles#presentation',
test(tagName, props) {
const hidden = hiddenFromAT(props);
const empty = props.alt === '';
const pres = props.role === 'presentation';
return hidden || !empty || pres;
},
affects: [
devices.screenReaders
]
}];
export const pass = [
{
when: 'the img has an `alt`',
render: React => <img src="foo" alt="nice" />
},
{
when: 'the img has an empty `alt` and role="presentation"',
render: React => <img src="foo" alt="" role="presentation" />
},
{
when: 'the img is aria-hidden',
// eslint-disable-next-line jsx-a11y/img-has-alt
render: React => <img src="foo" aria-hidden />
}
];
export const fail = [
{
when: 'the img doesn\'t have an `alt`',
// eslint-disable-next-line jsx-a11y/img-has-alt
render: React => <img src="foo" />
},
{
when: 'the img has alt="" but no role="presentation"',
render: React => <img src="foo" alt="" />
}
];
export const description = `
Enforce that an \`img\` element contains the \`alt\` prop. The \`alt\` attribute specifies
an alternate text for an image, if the image cannot be displayed.
`;