-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathtemplate-require-valid-alt-text.js
More file actions
231 lines (207 loc) · 7.34 KB
/
template-require-valid-alt-text.js
File metadata and controls
231 lines (207 loc) · 7.34 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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));
}
/**
* Returns true if the named attribute is present with a non-empty, non-whitespace
* static value, OR present with a dynamic (mustache/concat) value. Dynamic values
* are assumed to resolve to a meaningful name at runtime (we can't verify at lint
* time). Static empty-string / whitespace-only values return false — per ACCNAME 1.2
* §4.3.2 step 2D.
*
* NOTE: This does not validate that aria-labelledby IDREFs reference existing IDs.
* Rule consumers should layer that check separately if needed.
*/
function hasNonEmptyTextAttr(node, name) {
const attr = findAttr(node, name);
if (!attr?.value) {
return false;
}
if (attr.value.type === 'GlimmerTextNode') {
return attr.value.chars.trim() !== '';
}
// Mustache / concat — dynamic; assume truthy.
return true;
}
function hasAnyNonEmptyTextAttr(node, names) {
return names.some((name) => hasNonEmptyTextAttr(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;
}
// Empty-string aria-label/aria-labelledby/alt provides no accessible
// name — require a non-empty fallback value.
if (!hasAnyNonEmptyTextAttr(node, ['aria-label', 'aria-labelledby', 'alt'])) {
context.report({ node, messageId: 'inputImage' });
}
break;
}
case 'object': {
const roleAttr = findAttr(node, 'role');
const roleValue = getTextValue(roleAttr);
if (
hasAnyNonEmptyTextAttr(node, ['aria-label', 'aria-labelledby', 'title']) ||
hasChildren(node) ||
(roleValue && ['presentation', 'none'].includes(roleValue))
) {
return;
}
context.report({ node, messageId: 'objectMissing' });
break;
}
case 'area': {
if (!hasAnyNonEmptyTextAttr(node, ['aria-label', 'aria-labelledby', 'alt'])) {
context.report({ node, messageId: 'areaMissing' });
}
break;
}
// No default
}
},
};
},
};