-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy path1_1_1.js
More file actions
482 lines (419 loc) · 18.8 KB
/
1_1_1.js
File metadata and controls
482 lines (419 loc) · 18.8 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**
* +--------------------------------------------------------------------+
* | This HTML_CodeSniffer file is Copyright (c) |
* | Squiz Pty Ltd (ABN 77 084 670 600) |
* +--------------------------------------------------------------------+
* | IMPORTANT: Your use of this Software is subject to the terms of |
* | the Licence provided in the file licence.txt. If you cannot find |
* | this file please contact Squiz (www.squiz.com.au) so we may |
* | provide you a copy. |
* +--------------------------------------------------------------------+
*
*/
_global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_1_1_1_1 = {
/**
* Determines the elements to register for processing.
*
* Each element of the returned array can either be an element name, or "_top"
* which is the top element of the tested code.
*
* @returns {Array} The list of elements.
*/
register: function()
{
return [
'_top',
'img'
];
},
/**
* Process the registered element.
*
* @param {DOMNode} element The element registered.
* @param {DOMNode} top The top element of the tested code.
*/
process: function(element, top)
{
if (element === top) {
this.addNullAltTextResults(top);
this.addMediaAlternativesResults(top);
} else {
var nodeName = element.nodeName.toLowerCase();
switch (nodeName) {
case 'img':
this.testLinkStutter(element);
this.testLongdesc(element);
break;
}//end if
}//end if
},
/**
* Driver function for the null alt text tests.
*
* This takes the generic result given by the alt text testing functions,
* and converts them into WCAG 2.1-specific messages.
*
* @param {DOMNode} element The element to test.
*/
addNullAltTextResults: function(top)
{
var errors = this.testNullAltText(top);
for (var i = 0; i < errors.img.emptyAltInLink.length; i++) {
HTMLCS.addMessage(HTMLCS.ERROR, errors.img.emptyAltInLink[i], _global.HTMLCS.getTranslation("1_1_1_H30.2"), 'H30.2');
}
for (var i = 0; i < errors.img.nullAltWithTitle.length; i++) {
HTMLCS.addMessage(HTMLCS.ERROR, errors.img.nullAltWithTitle[i], _global.HTMLCS.getTranslation("1_1_1_H67.1"), 'H67.1');
}
for (var i = 0; i < errors.img.ignored.length; i++) {
HTMLCS.addMessage(HTMLCS.WARNING, errors.img.ignored[i], _global.HTMLCS.getTranslation("1_1_1_H67.2"), 'H67.2');
}
for (var i = 0; i < errors.img.missingAlt.length; i++) {
HTMLCS.addMessage(HTMLCS.ERROR, errors.img.missingAlt[i], _global.HTMLCS.getTranslation("1_1_1_H37"), 'H37');
}
for (var i = 0; i < errors.img.generalAlt.length; i++) {
HTMLCS.addMessage(HTMLCS.NOTICE, errors.img.generalAlt[i], _global.HTMLCS.getTranslation("1_1_1_G94.Image"), 'G94.Image');
}
for (var i = 0; i < errors.inputImage.missingAlt.length; i++) {
HTMLCS.addMessage(HTMLCS.ERROR, errors.inputImage.missingAlt[i], _global.HTMLCS.getTranslation("1_1_1_H36"), 'H36');
}
for (var i = 0; i < errors.inputImage.generalAlt.length; i++) {
HTMLCS.addMessage(HTMLCS.NOTICE, errors.inputImage.generalAlt[i], _global.HTMLCS.getTranslation("1_1_1_G94.Button"), 'G94.Button');
}
for (var i = 0; i < errors.area.missingAlt.length; i++) {
HTMLCS.addMessage(HTMLCS.ERROR, errors.area.missingAlt[i], _global.HTMLCS.getTranslation("1_1_1_H24"), 'H24');
}
for (var i = 0; i < errors.area.generalAlt.length; i++) {
HTMLCS.addMessage(HTMLCS.NOTICE, errors.area.generalAlt[i], _global.HTMLCS.getTranslation("1_1_1_H24.2"), 'H24.2');
}
},
/**
* Test for missing or null alt text in certain elements.
*
* Tested elements are:
* - IMG elements
* - INPUT elements with type="image" (ie. image submit buttons).
* - AREA elements (ie. in client-side image maps).
*
* @param {DOMNode} element The element to test.
*
* @returns {Object} A structured list of errors.
*/
testNullAltText: function(top)
{
var errors = {
img: {
generalAlt: [],
missingAlt: [],
ignored: [],
nullAltWithTitle: [],
emptyAltInLink: []
},
inputImage: {
generalAlt: [],
missingAlt: []
},
area: {
generalAlt: [],
missingAlt: []
}
};
var elements = HTMLCS.util.getAllElements(top, 'img, area, input[type="image"]');
for (var el = 0; el < elements.length; el++) {
var element = elements[el];
var nodeName = element.nodeName.toLowerCase();
var linkOnlyChild = false;
var missingAlt = false;
var nullAlt = false;
if (element.parentNode.nodeName.toLowerCase() === 'a') {
var prevNode = HTMLCS.util.getPreviousSiblingElement(element, null);
var nextNode = HTMLCS.util.getNextSiblingElement(element, null);
if ((prevNode === null) && (nextNode === null)) {
var textContent = element.parentNode.textContent;
if (element.parentNode.textContent !== undefined) {
var textContent = element.parentNode.textContent;
} else {
// Keep IE8 happy.
var textContent = element.parentNode.innerText;
}
if (HTMLCS.util.isStringEmpty(textContent) === true) {
linkOnlyChild = true;
}
}
}//end if
if (element.hasAttribute('alt') === false) {
missingAlt = true;
} else if (!element.getAttribute('alt') || HTMLCS.util.isStringEmpty(element.getAttribute('alt')) === true) {
nullAlt = true;
}
// Now determine which test(s) should fire.
switch (nodeName) {
case 'img':
if ((linkOnlyChild === true) && ((missingAlt === true) || (nullAlt === true))) {
// Img tags cannot have an empty alt text if it is the
// only content in a link (as the link would not have a text
// alternative).
errors.img.emptyAltInLink.push(element.parentNode);
} else if (missingAlt === true) {
errors.img.missingAlt.push(element);
} else if (nullAlt === true) {
if ((element.hasAttribute('title') === true) && (HTMLCS.util.isStringEmpty(element.getAttribute('title')) === false)) {
// Title attribute present and not empty. This is wrong when
// an image is marked as ignored.
errors.img.nullAltWithTitle.push(element);
} else {
errors.img.ignored.push(element);
}
} else {
errors.img.generalAlt.push(element);
}
break;
case 'input':
// Image submit buttons.
if ((missingAlt === true) || (nullAlt === true)) {
errors.inputImage.missingAlt.push(element);
} else {
errors.inputImage.generalAlt.push(element);
}
break;
case 'area':
// Area tags in a client-side image map.
if ((missingAlt === true) || (nullAlt === true)) {
errors.area.missingAlt.push(element);
} else {
errors.inputImage.generalAlt.push(element);
}
break;
default:
// No other tags defined.
break;
}//end switch
}//end for
return errors;
},
/**
* Test for longdesc attributes on images (technique H45).
*
* We throw a notice to ensure that a longdesc is available in an accessible
* way - ie. using body text or a link. Longdesc is specifically ignored as it
* is not accessible to sighted users.
*
* @param {DOMNode} element The element to test.
*
* @returns void
*/
testLongdesc: function(element)
{
HTMLCS.addMessage(HTMLCS.NOTICE, element, _global.HTMLCS.getTranslation("1_1_1_G73,G74"), 'G73,G74');
},
/**
* Test for link stutter with adjacent images and text (technique H2).
*
* Only runs on IMG elements contained inside an anchor (A) element. We test that
* its alt text does not duplicate the text content of a link directly beside it.
* We also test that the technique hasn't been applied incorrectly (Failure
* Examples 4 and 5 in technique H2).
*
* Error messages are given codes in the form "H2.EG5", meaning it is a case of
* the applicable failure example (3, 4, or 5).
*
* @param {DOMNode} element The image element to test.
*/
testLinkStutter: function(element)
{
if (element.parentNode.nodeName.toLowerCase() === 'a') {
var anchor = element.parentNode;
// If contained by an "a" link, check that the alt text does not duplicate
// the link text, or if no link text, check an adjacent link does not
// duplicate it.
var nodes = {
anchor: {
href: anchor.getAttribute('href'),
text: HTMLCS.util.getElementTextContent(anchor, false),
alt: this._getLinkAltText(anchor)
}
};
if (nodes.anchor.alt === null) {
nodes.anchor.alt = '';
}
if ((nodes.anchor.alt !== null) && (nodes.anchor.alt !== '')) {
if (HTMLCS.util.trim(nodes.anchor.alt).toLowerCase() === HTMLCS.util.trim(nodes.anchor.text).toLowerCase()) {
// H2 "Failure Example 5": they're in one link, but the alt text
// duplicates the link text. Trimmed and lowercased because they
// would sound the same to a screen reader.
HTMLCS.addMessage(HTMLCS.ERROR, element, _global.HTMLCS.getTranslation("1_1_1_H2.EG5"), 'H2.EG5');
}
}
// If there is no supplementary text, try to catch H2 "Failure Examples"
// in cases where there are adjacent links with the same href:
// 3 - img text that duplicates link text in an adjacent link. (Screen
// readers will stutter.)
// 4 - img text is blank when another link adjacent contains link text.
// (This leaves one link with no text at all - the two should be
// combined into one link.)
if (nodes.anchor.text === '') {
var prevLink = HTMLCS.util.getPreviousSiblingElement(anchor, 'a', true);
var nextLink = HTMLCS.util.getNextSiblingElement(anchor, 'a', true);
if (prevLink !== null) {
nodes.previous = {
href: prevLink.getAttribute('href'),
text: HTMLCS.util.getElementTextContent(prevLink, false),
alt: this._getLinkAltText(prevLink)
};
if (nodes.previous.alt === null) {
nodes.previous.alt = '';
}
}
if (nextLink !== null) {
nodes.next = {
href: nextLink.getAttribute('href'),
text: HTMLCS.util.getElementTextContent(nextLink, false),
alt: this._getLinkAltText(nextLink)
};
if (nodes.next.alt === null) {
nodes.next.alt = '';
}
}
// Test against the following link, if any.
if (nodes.next && (nodes.next.href !== '') && (nodes.next.href !== null) && (nodes.anchor.href === nodes.next.href)) {
if ((nodes.next.text !== '') && (nodes.anchor.alt === '')) {
HTMLCS.addMessage(HTMLCS.ERROR, element, _global.HTMLCS.getTranslation("1_1_1_H2.EG4"), 'H2.EG4');
} else if (nodes.next.text.toLowerCase() === nodes.anchor.alt.toLowerCase()) {
HTMLCS.addMessage(HTMLCS.ERROR, element, _global.HTMLCS.getTranslation("1_1_1_H2.EG3"), 'H2.EG3');
}
}
// Test against the preceding link, if any.
if (nodes.previous && (nodes.previous.href !== '') && (nodes.previous.href !== null) && (nodes.anchor.href === nodes.previous.href)) {
if ((nodes.previous.text !== '') && (nodes.anchor.alt === '')) {
HTMLCS.addMessage(HTMLCS.ERROR, element, _global.HTMLCS.getTranslation("1_1_1_H2.EG4"), 'H2.EG4');
} else if (nodes.previous.text.toLowerCase() === nodes.anchor.alt.toLowerCase()) {
HTMLCS.addMessage(HTMLCS.ERROR, element, _global.HTMLCS.getTranslation("1_1_1_H2.EG3"), 'H2.EG3');
}
}
}//end if
}//end if
},
/**
* Driver function for the media alternative (object/applet) tests.
*
* This takes the generic result given by the media alternative testing function,
* and converts them into WCAG 2.1-specific messages.
*
* @param {DOMNode} element The element to test.
*/
addMediaAlternativesResults: function(top)
{
var errors = this.testMediaTextAlternatives(top);
for (var i = 0; i < errors.object.missingBody.length; i++) {
HTMLCS.addMessage(HTMLCS.ERROR, errors.object.missingBody[i], _global.HTMLCS.getTranslation("1_1_1_H53,ARIA6"), 'H53,ARIA6');
}
for (var i = 0; i < errors.object.generalAlt.length; i++) {
HTMLCS.addMessage(HTMLCS.NOTICE, errors.object.generalAlt[i], _global.HTMLCS.getTranslation("1_1_1_G94,G92.Object,ARIA6"), 'G94,G92.Object,ARIA6');
}
for (var i = 0; i < errors.applet.missingBody.length; i++) {
HTMLCS.addMessage(HTMLCS.ERROR, errors.applet.missingBody[i], _global.HTMLCS.getTranslation("1_1_1_H35.3"), 'H35.3');
}
for (var i = 0; i < errors.applet.missingAlt.length; i++) {
HTMLCS.addMessage(HTMLCS.ERROR, errors.applet.missingAlt[i], _global.HTMLCS.getTranslation("1_1_1_H35.2"), 'H35.2');
}
for (var i = 0; i < errors.applet.generalAlt.length; i++) {
HTMLCS.addMessage(HTMLCS.NOTICE, errors.applet.generalAlt[i], _global.HTMLCS.getTranslation("1_1_1_G94,G92.Applet"), 'G94,G92.Applet');
}
},
testMediaTextAlternatives: function(top)
{
var errors = {
object: {
missingBody: [],
generalAlt: []
},
applet: {
missingBody: [],
missingAlt: [],
generalAlt: []
}
};
var elements = HTMLCS.util.getAllElements(top, 'object');
for (var el = 0; el < elements.length; el++) {
var element = elements[el];
var childObject = element.querySelector('object');
// If we have an object as our alternative, skip it. Pass the blame onto
// the child.
if (childObject === null) {
if (HTMLCS.util.isStringEmpty(HTMLCS.util.getElementTextContent(element, true)) === true) {
if (HTMLCS.util.hasValidAriaLabel(element) === false) {
errors.object.missingBody.push(element);
}
} else {
if (HTMLCS.util.hasValidAriaLabel(element) === false) {
errors.object.generalAlt.push(element);
}
}
}//end if
}//end if
var elements = HTMLCS.util.getAllElements(top, 'applet');
for (var el = 0; el < elements.length; el++) {
// Test firstly for whether we have an object alternative.
var childObject = element.querySelector('object');
var hasError = false;
// If we have an object as our alternative, skip it. Pass the blame onto
// the child. (This is a special case: those that don't understand APPLET
// may understand OBJECT, but APPLET shouldn't be nested.)
if (childObject === null) {
var textAlt = HTMLCS.util.getElementTextContent(element, true);
if (HTMLCS.util.isStringEmpty(textAlt) === true) {
errors.applet.missingBody.push(element);
hasError = true;
}
}//end if
var altAttr = element.getAttribute('alt') || '';
if (HTMLCS.util.isStringEmpty(altAttr) === true) {
errors.applet.missingAlt.push(element);
hasError = true;
}
// Catch anything with a valid aria label.
if (HTMLCS.util.hasValidAriaLabel(element) === true) {
hasError = false;
}
if (hasError === false) {
// No error? Remind of obligations about equivalence of alternatives.
errors.applet.generalAlt.push(element);
}
}//end if
return errors;
},
/**
* Gets just the alt text from any images on a link.
*
* @param {DOMNode} anchor The link element being inspected.
*
* @returns {String} The alt text.
*/
_getLinkAltText: function(anchor)
{
var anchor = anchor.cloneNode(true);
var nodes = [];
for (var i = 0; i < anchor.childNodes.length; i++) {
nodes.push(anchor.childNodes[i]);
}
var alt = null;
while (nodes.length > 0) {
var node = nodes.shift();
// If it's an element, add any sub-nodes to the process list.
if (node.nodeType === 1) {
if (node.nodeName.toLowerCase() === 'img') {
if (node.hasAttribute('alt') === true) {
alt = node.getAttribute('alt');
if (!alt) {
alt = '';
} else {
// Trim the alt text.
alt = alt.replace(/^\s+|\s+$/g,'');
}
break;
}
}
}
}
return alt;
}
};