forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-require-valid-alt-text.js
More file actions
203 lines (181 loc) · 6.27 KB
/
template-require-valid-alt-text.js
File metadata and controls
203 lines (181 loc) · 6.27 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const REDUNDANT_WORDS = ['image', 'photo', 'picture', 'logo', 'spacer'];
function findAttr(node, name) {
return node.attributes?.find((a) => a.name === name);
}
function hasAttr(node, name) {
return node.attributes?.some((a) => a.name === name);
}
function hasAnyAttr(node, names) {
return names.some((name) => hasAttr(node, name));
}
function getTextValue(attr) {
if (!attr?.value) {
return undefined;
}
if (attr.value.type === 'GlimmerTextNode') {
return attr.value.chars;
}
return undefined;
}
function getNormalizedAltText(altAttr) {
if (!altAttr?.value) {
return null;
}
if (altAttr.value.type === 'GlimmerTextNode') {
return altAttr.value.chars.trim().toLowerCase();
}
if (altAttr.value.type === 'GlimmerConcatStatement') {
const parts = (altAttr.value.parts || [])
.filter((p) => p.type === 'GlimmerTextNode')
.map((p) => p.chars)
.join(' ')
.trim()
.toLowerCase();
return parts === '' ? null : parts;
}
return null;
}
function hasChildren(node) {
return (
node.children &&
node.children.some((child) => {
if (child.type === 'GlimmerTextNode') {
return child.chars.trim().length > 0;
}
return true;
})
);
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require valid alt text for images and other elements',
category: 'Accessibility',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-valid-alt-text.md',
templateMode: 'both',
},
schema: [],
messages: {
imgMissing: 'All `<img>` tags must have an alt attribute',
imgRedundant:
'Invalid alt attribute. Words such as `image`, `photo,` or `picture` are already announced by screen readers.',
imgAltEqualsSrc: 'The alt text must not be the same as the image source',
imgNumericAlt: 'A number is not valid alt text',
imgRolePresentation:
'The `alt` attribute should be empty if `<img>` has `role` of `none` or `presentation`',
inputImage:
'All <input> elements with type="image" must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` attribute.',
objectMissing:
'Embedded <object> elements must have alternative text by providing inner text, aria-label or aria-labelledby attributes.',
areaMissing:
'Each area of an image map must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` attribute.',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/require-valid-alt-text.js',
docs: 'docs/rule/require-valid-alt-text.md',
tests: 'test/unit/rules/require-valid-alt-text-test.js',
},
},
create(context) {
return {
// eslint-disable-next-line complexity
GlimmerElementNode(node) {
// Skip hidden elements
if (hasAttr(node, 'hidden')) {
return;
}
const ariaHidden = findAttr(node, 'aria-hidden');
if (ariaHidden) {
const val = getTextValue(ariaHidden);
if (val === 'true') {
return;
}
}
// Skip elements with ...attributes (splattributes)
if (hasAttr(node, '...attributes')) {
return;
}
const tag = node.tag;
switch (tag) {
case 'img': {
const altAttr = findAttr(node, 'alt');
const roleAttr = findAttr(node, 'role');
const srcAttr = findAttr(node, 'src');
// Check role=none/presentation with non-empty alt
if (altAttr && roleAttr) {
const roleValue = getTextValue(roleAttr);
const altValue = getTextValue(altAttr);
if (
roleValue &&
['none', 'presentation'].includes(roleValue.trim().toLowerCase()) &&
altValue !== ''
) {
context.report({ node, messageId: 'imgRolePresentation' });
}
}
if (!altAttr) {
context.report({ node, messageId: 'imgMissing' });
return;
}
// Check alt === src
const altValue = getTextValue(altAttr);
const srcValue = getTextValue(srcAttr);
if (altValue !== undefined && srcValue !== undefined && altValue === srcValue) {
context.report({ node, messageId: 'imgAltEqualsSrc' });
return;
}
// Check numeric-only alt and redundant words
const normalizedAlt = getNormalizedAltText(altAttr);
if (normalizedAlt !== null) {
if (/^\d+$/.test(normalizedAlt)) {
context.report({ node, messageId: 'imgNumericAlt' });
} else {
const words = normalizedAlt.split(' ');
const hasRedundant = REDUNDANT_WORDS.some((w) => words.includes(w));
if (hasRedundant) {
context.report({ node, messageId: 'imgRedundant' });
}
}
}
break;
}
case 'input': {
// Only check input type="image"
const typeAttr = findAttr(node, 'type');
const typeVal = getTextValue(typeAttr);
if (typeVal !== 'image') {
return;
}
if (!hasAnyAttr(node, ['aria-label', 'aria-labelledby', 'alt'])) {
context.report({ node, messageId: 'inputImage' });
}
break;
}
case 'object': {
const roleAttr = findAttr(node, 'role');
const roleValue = getTextValue(roleAttr);
if (
hasAnyAttr(node, ['aria-label', 'aria-labelledby', 'title']) ||
hasChildren(node) ||
(roleValue && ['presentation', 'none'].includes(roleValue))
) {
return;
}
context.report({ node, messageId: 'objectMissing' });
break;
}
case 'area': {
if (!hasAnyAttr(node, ['aria-label', 'aria-labelledby', 'alt'])) {
context.report({ node, messageId: 'areaMissing' });
}
break;
}
// No default
}
},
};
},
};