forked from squizlabs/HTML_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLCS.Util.js
1316 lines (1155 loc) · 45.8 KB
/
HTMLCS.Util.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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* +--------------------------------------------------------------------+
* | 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.util = function() {
var self = {};
/**
* Trim off excess spaces on either side.
*
* @param {String} string The string with potentially extraneous whitespace.
*
* @returns {String}
*/
self.trim = function(string) {
return string.replace(/^\s*(.*)\s*$/g, '$1');
};
/**
* Returns true if the string is "empty" according to WCAG standards.
*
* We can test for whether the string is entirely composed of whitespace, but
* WCAG standards explicitly state that non-breaking spaces ( ,  )
* are not considered "empty". So we need this function to filter out that
* situation.
*
* @param {String} string The potentially empty string.
*
* @returns {Boolean}
*/
self.isStringEmpty = function(string) {
if (typeof string !== 'string') {
return true;
}
var empty = true;
if (string.indexOf(String.fromCharCode(160)) !== -1) {
// Has an NBSP, therefore cannot be empty.
empty = false;
} else if (/^\s*$/.test(string) === false) {
// Not spacing.
empty = false;
}
return empty;
};
/**
* Get the document type being tested.
*
* Possible values: html5, xhtml5, xhtml11, xhtml10, html401, html40
* ... or empty string if it couldn't work out the doctype.
*
* This will only give the thumbs-up to the "strict" doctypes.
*
* @param {Document} The document being tested.
*
* @return {String}
*/
self.getDocumentType = function(document)
{
var retval = null;
var doctype = document.doctype;
if (doctype) {
var doctypeName = doctype.name;
var publicId = doctype.publicId;
var systemId = doctype.systemId;
if (doctypeName === null) {
doctypeName = '';
}
if (systemId === null) {
systemId = '';
}
if (publicId === null) {
publicId = '';
}
if (doctypeName.toLowerCase() === 'html') {
if (publicId === '' && systemId === '') {
retval = 'html5';
} else if (publicId.indexOf('//DTD HTML 4.01') !== -1 || systemId.indexOf('w3.org/TR/html4/strict.dtd') !== -1) {
retval = 'html401';
} else if (publicId.indexOf('//DTD HTML 4.0') !== -1 || systemId.indexOf('w3.org/TR/REC-html40/strict.dtd') !== -1) {
retval = 'html40';
} else if (publicId.indexOf('//DTD XHTML 1.0 Strict') !== -1 && systemId.indexOf('w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd') !== -1) {
retval = 'xhtml10';
} else if (publicId.indexOf('//DTD XHTML 1.1') !== -1 && systemId.indexOf('w3.org/TR/xhtml11/DTD/xhtml11.dtd') !== -1) {
retval = 'xhtml11';
} if (systemId.indexOf('about:legacy-compat') !== -1) {
// Some tools don't like the lack of doctype for XHTML5 so permit
// an "about:legacy-compat" SYSTEM doctype.
if (document.contentType === 'application/xhtml+xml') {
var htmlElement = document.querySelector('html');
if (htmlElement.getAttribute('xmlns') === 'http://www.w3.org/1999/xhtml') {
retval = 'xhtml5';
}
}
}
}
} else {
// XHTML5 has no doctype (at all) normally, but it only counts if the
// content type it was sent as is set correctly
if (document.contentType === 'application/xhtml+xml') {
var htmlElement = document.querySelector('html');
if (htmlElement.getAttribute('xmlns') === 'http://www.w3.org/1999/xhtml') {
retval = 'xhtml5';
}
}
}
return retval;
};//end getDocumentType()
/**
* Get the window object relating to the passed element.
*
* @param {Node|Document} element The element (or document) to pass.
*
* @returns {Window}
*/
self.getElementWindow = function(element)
{
if (element.ownerDocument) {
var doc = element.ownerDocument;
} else {
var doc = element;
}
var window = null;
if (doc.defaultView) {
window = doc.defaultView;
} else {
window = doc.parentWindow;
}
return window;
};
/**
* Returns true if the element has a valid aria label.
*
* @param {Node} element The element we are checking.
*
* @return {Boolean}
*/
self.hasValidAriaLabel = function(element)
{
var found = false;
if (element.hasAttribute('aria-labelledby') === true) {
// Checking aria-labelled by where the label exists AND it has text available
// to an accessibility API.
var labelledByIds = element.getAttribute('aria-labelledby').split(/\s+/);
labelledByIds.forEach(function(id) {
var elem = document.getElementById(id);
if (elem) {
var text = self.getElementTextContent(elem);
if (/^\s*$/.test(text) === false) {
found = true;
}
}
});
} else if (element.hasAttribute('aria-label') === true) {
var text = element.getAttribute('aria-label');
if (/^\s*$/.test(text) === false) {
found = true;
}
}
return found;
};
/**
* Return the appropriate computed style object for an element.
*
* It's accessed in different ways depending on whether it's IE or not.
*
* @param {Node} element An element with style.
*
* @returns {Object}
*/
self.style = function(element, pseudo) {
var computedStyle = null;
var window = self.getElementWindow(element);
var pseudo = pseudo || null;
if (element.currentStyle) {
computedStyle = element.currentStyle;
} else if (window.getComputedStyle) {
computedStyle = window.getComputedStyle(element, pseudo);
}
return computedStyle;
};
/**
* Return true if an element is hidden visually.
*
* If the computed style of an element cannot be determined for some reason,
* it is presumed it is NOT hidden.
*
* @param {Node} element The element that is hiding, or not.
*
* @returns {Boolean}
*/
self.isVisuallyHidden = function(element) {
var hidden = false;
// Handle titles in svg as a special visually hidden case (hidden by browsers but
// available to accessibility apis.
if (element.nodeName.toLowerCase() === 'title' && self.findParentNode(element, 'svg') !== null) {
return true;
}
// Do not point to elem if its hidden. Use computed styles.
var style = self.style(element);
if (style !== null) {
if ((style.visibility === 'hidden') || (style.display === 'none')) {
hidden = true;
}
if ((parseInt(style.left, 10) + parseInt(style.width, 10)) < 0) {
hidden = true;
}
if ((parseInt(style.top, 10) + parseInt(style.height, 10)) < 0) {
hidden = true;
}
}
return hidden;
};
/**
* Returns true if the element is deliberately hidden from Accessibility APIs using ARIA hidden.
*
* Not: This is separate to isAccessibilityHidden() due to a need to check specifically for aria hidden.
*
* @param {Node} element The element to check.
*
* @return {Boolean}
*/
self.isAriaHidden = function(element) {
do {
// WAI-ARIA hidden attribute.
if (element.hasAttribute('aria-hidden') && element.getAttribute('aria-hidden') === 'true') {
return true;
}
} while (element = element.parentElement);
return false;
};
/**
* Returns true if the element is deliberately hidden from Accessibility APIs.
*
* @param {Node} element The element to check.
*
* @return {Boolean}
*/
self.isAccessibilityHidden = function(element) {
do {
// WAI-ARIA presentation role.
if (element.hasAttribute('role') && element.getAttribute('role') === 'presentation') {
return true;
}
// WAI-ARIA hidden attribute.
if (element.hasAttribute('aria-hidden') && element.getAttribute('aria-hidden') === 'true') {
return true;
}
} while (element = element.parentElement);
return false;
};
/**
* Returns TRUE if the element is able to be focused .
*
* @param {Node} element DOM Node to test.
*
* @return {Boolean}
*/
self.isFocusable = function(element)
{
var nodeName = element.nodeName.toLowerCase();
if (self.isDisabled(element) === true) {
return false;
}
if (self.isVisuallyHidden(element) === true) {
return false;
}
// Form elements.
if (/^(input|select|textarea|button|object)$/.test(nodeName)) {
return true;
}
// Hyperlinks without empty hrefs are focusable.
if (nodeName === 'a' && element.hasAttribute('href') && /^\s*$/.test(element.getAttribute('href')) === false) {
return true;
}
return false;
};
/**
* Returns TRUE if the element is able to be focused by keyboard tabbing.
*
* @param {Node} element DOM Node to test.
*
* @return {Boolean}
*/
self.isKeyboardTabbable = function(element)
{
if (element.hasAttribute('tabindex') === true) {
var index = element.getAttribute('tabindex');
if (index === "-1") {
return false;
} else {
return true;
}
}
return self.isFocusable(element);
};
/**
* Returns TRUE if the element is able to be accessed via the keyboard.
*
* @param {Node} element DOM Node to test.
*
* @return {Boolean}
*/
self.isKeyboardNavigable = function(element)
{
if (element.hasAttribute('accesskey') && /^\s*$/.test(element.getAttribute('accesskey')) === false) {
return true;
}
return self.isKeyboardTabbable(element);
};
/**
* Return true if an element is disabled.
*
* If the computed style of an element cannot be determined for some reason,
* it is presumed it is NOT hidden.
*
* @param {Node} element The element that is hiding, or not.
*
* @returns {Boolean}
*/
self.isDisabled = function(element) {
var disabled = false;
// Do not point to elem if its hidden. Use computed styles.
if ((element.disabled === true) || (element.getAttribute('aria-disabled') === 'true')) {
disabled = true;
}
return disabled;
};
/**
* Return true if an element is in a document.
*
* @param {Node} element The element that is in a doc, or not.
*
* @returns {Boolean}
*/
self.isInDocument = function(element) {
// Check whether the element is in the document, by looking up its
// DOM tree for a document object.
var parent = element.parentNode;
while (parent && parent.ownerDocument) {
parent = parent.parentNode;
}//end while
// If we didn't hit a document, the element must not be in there.
if (parent === null) {
return false;
}
return true;
};
/**
* Returns all elements that are visible to the accessibility API.
*
* @param {Node} element The parent element to search.
* @param {String} selector Optional selector to pass to
*
* @return {Array}
*/
self.getAllElements = function(element, selector) {
element = element || document;
selector = selector || '*';
var elements = Array.prototype.slice.call(element.querySelectorAll(selector));
var visible = elements.filter(function(elem) {
return HTMLCS.util.isAccessibilityHidden(elem) === false;
});
// We shouldn't be testing elements inside the injected auditor code if it's present.
var auditor = document.getElementById('HTMLCS-wrapper');
if (auditor) {
visible = visible.filter(function(elem) {
return auditor.contains(elem) === false;
});
}
return visible;
};
/**
* Returns true if the passed child is contained by the passed parent.
*
* Uses either the IE contains() method or the W3C compareDocumentPosition()
* method, as appropriate.
*
* @param {Node|Document} parent The parent element or document.
* @param {Node|Document} child The child.
*
* @returns {Boolean}
*/
self.contains = function(parent, child) {
var contained = false;
// If the parent and the child are the same, they can't contain each
// other.
if (parent !== child) {
if (!parent.ownerDocument) {
// Parent is the document. Short-circuiting because contains()
// doesn't exist on the document element.
// We check whether the child can be contained, and whether the
// child is in the same document as the parent.
if ((child.ownerDocument) && (child.ownerDocument === parent)) {
contained = true;
}
} else {
if ((parent.contains) && (parent.contains(child) === true)) {
contained = true;
} else if ((parent.compareDocumentPosition) && ((parent.compareDocumentPosition(child) & 16) > 0)) {
contained = true;
}
}//end if
}//end if
return contained;
};
/**
* Returns true if the table passed is a layout table.
*
* If the passed table contains headings - through the use of the th
* element - HTML_CodeSniffer will assume it is a data table. This is in line
* with most other online checkers.
*
* @param {Node} table The table to check.
*
* @returns {Boolean}
*/
self.isLayoutTable = function(table) {
var th = table.querySelector('th');
if (th === null) {
return true;
}
return false;
};
/**
* Calculate the contrast ratio between two colours.
*
* Colours should be in rgb() or 3/6-digit hex format; order does not matter
* (ie. it doesn't matter which is the lighter and which is the darker).
* Values should be in the range [1.0, 21.0]... a ratio of 1.0 means "they're
* exactly the same contrast", 21.0 means it's white-on-black or v.v.
* Formula as per WCAG 2.0 definitions.
*
* @param {String} colour1 The first colour to compare.
* @param {String} colour2 The second colour to compare.
*
* @returns {Number}
*/
self.contrastRatio = function(colour1, colour2) {
var ratio = (0.05 + self.relativeLum(colour1)) / (0.05 + self.relativeLum(colour2));
if (ratio < 1) {
ratio = 1 / ratio;
}
return ratio;
};
/**
* Calculate relative luminescence for a colour in the sRGB colour profile.
*
* Supports rgb() and hex colours. rgba() also supported but the alpha
* channel is currently ignored.
* Hex colours can have an optional "#" at the front, which is stripped.
* Relative luminescence formula is defined in the definitions of WCAG 2.0.
* It can be either three or six hex digits, as per CSS conventions.
* It should return a value in the range [0.0, 1.0].
*
* @param {String} colour The colour to calculate from.
*
* @returns {Number}
*/
self.relativeLum = function(colour) {
if (colour.charAt) {
var colour = self.colourStrToRGB(colour);
}
var transformed = {};
for (var x in colour) {
if (colour[x] <= 0.03928) {
transformed[x] = colour[x] / 12.92;
} else {
transformed[x] = Math.pow(((colour[x] + 0.055) / 1.055), 2.4);
}
}//end for
var lum = ((transformed.red * 0.2126) + (transformed.green * 0.7152) + (transformed.blue * 0.0722));
return lum;
};
/**
* Convert a colour string to a structure with red/green/blue/alpha elements.
*
* Supports rgb() and hex colours (3, 4, 6 or 8 hex digits, optional "#").
* Each red/green/blue element is in the range [0.0, 1.0].
*
* @param {String} colour The colour to convert.
*
* @returns {Object}
*/
self.colourStrToRGB = function(colour) {
colour = colour.toLowerCase();
if (colour.substring(0, 3) === 'rgb') {
// rgb[a](0, 0, 0[, 0]) format.
var matches = /^rgba?\s*\((\d+),\s*(\d+),\s*(\d+)([^)]*)\)$/.exec(colour);
colour = {
red: (matches[1] / 255),
green: (matches[2] / 255),
blue: (matches[3] / 255),
alpha: 1.0
};
if (matches[4]) {
colour.alpha = parseFloat(/^,\s*(.*)$/.exec(matches[4])[1]);
}
} else {
// Hex digit format.
if (colour.charAt(0) === '#') {
colour = colour.substr(1);
}
if (colour.length === 3) {
colour = colour.replace(/^(.)(.)(.)$/, '$1$1$2$2$3$3');
}
if (colour.length === 4) {
colour = colour.replace(/^(.)(.)(.)(.)$/, '$1$1$2$2$3$3$4$4');
}
var alpha = 1; // Default if alpha is not specified
if (colour.length === 8) {
alpha = parseInt(colour.substr(6, 2), 16) / 255;
}
colour = {
red: (parseInt(colour.substr(0, 2), 16) / 255),
green: (parseInt(colour.substr(2, 2), 16) / 255),
blue: (parseInt(colour.substr(4, 2), 16) / 255),
alpha: alpha,
};
}
return colour;
};
/**
* Convert an RGB colour structure to a hex colour.
*
* The red/green/blue colour elements should be on a [0.0, 1.0] scale.
* Colours that can be converted into a three Hex-digit string will be
* converted as such (eg. rgb(34,34,34) => #222). Others will be converted
* to a six-digit string (eg. rgb(48,48,48) => #303030).
*
* @param {Object} colour Structure with "red", "green" and "blue" elements.
*
* @returns {String}
*/
self.RGBtoColourStr = function(colour) {
colourStr = '#';
colour.red = Math.round(colour.red * 255);
colour.green = Math.round(colour.green * 255);
colour.blue = Math.round(colour.blue * 255);
if ((colour.red % 17 === 0) && (colour.green % 17 === 0) && (colour.blue % 17 === 0)) {
// Reducible to three hex digits.
colourStr += (colour.red / 17).toString(16);
colourStr += (colour.green / 17).toString(16);
colourStr += (colour.blue / 17).toString(16);
} else {
if (colour.red < 16) {
colourStr += '0';
}
colourStr += colour.red.toString(16);
if (colour.green < 16) {
colourStr += '0';
}
colourStr += colour.green.toString(16);
if (colour.blue < 16) {
colourStr += '0';
}
colourStr += colour.blue.toString(16);
}
return colourStr;
};
/**
* Convert an RGB colour into hue-saturation-value.
*
* This is used for calculations changing the colour (for colour contrast
* purposes) to ensure that the hue is maintained.
* The parameter accepts either a string (hex or rgb() format) or a
* red/green/blue structure.
* The returned structure has hue, saturation, and value components: the
* latter two are in the range [0.0, 1.0]; hue is in degrees,
* range [0.0, 360.0).
* If there is no saturation then hue is technically undefined.
*
* @param {String|Object} colour A colour to convert.
*
* @returns {Object}
*/
self.sRGBtoHSV = function(colour) {
// If this is a string, then convert to a colour structure.
if (colour.charAt) {
colour = self.colourStrToRGB(colour);
}
var hsvColour = {
hue: 0,
saturation: 0,
value: 0
};
var maxColour = Math.max(colour.red, colour.green, colour.blue);
var minColour = Math.min(colour.red, colour.green, colour.blue);
var chroma = maxColour - minColour;
if (chroma === 0) {
hsvColour.value = colour.red;
} else {
hsvColour.value = maxColour;
if (maxColour === colour.red) {
hsvColour.hue = ((colour.green - colour.blue) / chroma);
} else if (maxColour === colour.green) {
hsvColour.hue = (2.0 + ((colour.blue - colour.red) / chroma));
} else {
hsvColour.hue = (4.0 + ((colour.red - colour.green) / chroma));
}//end if
hsvColour.hue = (hsvColour.hue * 60.0);
if (hsvColour.hue >= 360.0) {
hsvColour.hue -= 360.0;
}
hsvColour.saturation = chroma / hsvColour.value;
}//end if
return hsvColour;
};
/**
* Convert a hue-saturation-value structure into an RGB structure.
*
* The hue element should be a degree value in the region of [0.0, 360.0).
* The saturation and value elements should be in the range [0.0, 1.0].
* Use RGBtoColourStr to convert back into a hex colour.
*
* @param {Object} hsvColour A HSV structure to convert.
*
* @returns {Object}
*/
self.HSVtosRGB = function(hsvColour) {
var colour = {
red: 0,
green: 0,
blue: 0
};
if (hsvColour.saturation === 0) {
colour.red = hsvColour.value;
colour.green = hsvColour.value;
colour.blue = hsvColour.value;
} else {
var chroma = hsvColour.value * hsvColour.saturation;
var minColour = hsvColour.value - chroma;
var interHue = hsvColour.hue / 60.0;
var interHueMod = interHue - 2 * (Math.floor(interHue / 2));
var interCol = chroma * (1 - Math.abs(interHueMod - 1));
switch(Math.floor(interHue)) {
case 0:
colour.red = chroma;
colour.green = interCol;
break;
case 1:
colour.green = chroma;
colour.red = interCol;
break;
case 2:
colour.green = chroma;
colour.blue = interCol;
break;
case 3:
colour.blue = chroma;
colour.green = interCol;
break;
case 4:
colour.blue = chroma;
colour.red = interCol;
break;
case 5:
colour.red = chroma;
colour.blue = interCol;
break;
}//end switch
colour.red = (colour.red + minColour);
colour.green = (colour.green + minColour);
colour.blue = (colour.blue + minColour);
}//end if
return colour;
};
/**
* Gets the text contents of an element.
*
* @param {Node} element The element being inspected.
* @param {Boolean} [includeAlt=true] Include alt text from images.
*
* @returns {String} The text contents.
*/
self.getElementTextContent = function(element, includeAlt)
{
if (includeAlt === undefined) {
includeAlt = true;
}
var element = element.cloneNode(true);
var nodes = [];
for (var i = 0; i < element.childNodes.length; i++) {
nodes.push(element.childNodes[i]);
}
var text = [element.textContent];
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 an image, include the alt text unless we are blocking it.
if ((includeAlt === true) && (node.hasAttribute('alt') === true)) {
text.push(node.getAttribute('alt'));
}
} else {
for (var i = 0; i < node.childNodes.length; i++) {
nodes.push(node.childNodes[i]);
}
}
} else if (node.nodeType === 3) {
// Text node.
text.push(node.nodeValue);
}
}
// Push the text nodes together and trim.
text = text.join('').replace(/^\s+|\s+$/g,'');
return text;
};
/**
* Find a parent node matching a selector.
*
* @param {DOMNode} node Node to search from.
* @param {String} selector The selector to search.
*
* @return DOMNode|null
*/
self.findParentNode = function(node, selector) {
if (node && node.matches && node.matches(selector)) {
return node;
}
while (node && node.parentNode) {
node = node.parentNode;
if (node && node.matches && node.matches(selector)) {
return node;
}
}
return null;
};
/**
* Iterate parent nodes of an element.
*
* @param {DOMNode} node Node to search from.
* @param {Function} cb Callback function providing each parent node.
*
* @return void
*/
self.eachParentNode = function(node, cb) {
while (node && node.parentNode) {
cb(node);
node = node.parentNode;
}
};
/**
* Returns TRUE if the provided node name is not a valid phrasing node.
*
* @param {String} nodeName The node name to test.
*
* @return {Boolean}
*/
self.isPhrasingNode = function(nodeName) {
var nodeNames = [ 'abbr', 'audio', 'b', 'bdo', 'br', 'button', 'canvas', 'cite', 'code', 'command', 'data',
'datalist', 'dfn', 'em', 'embed', 'i', 'iframe', 'img', 'input', 'kbd', 'keygen', 'label', 'mark', 'math',
'meter', 'noscript', 'object', 'output', 'progress', 'q', 'ruby', 'samp', 'script', 'select', 'small',
'span', 'strong', 'sub', 'sup', 'svg', 'textarea', 'time', 'var', 'video', 'wbr'];
return nodeNames.indexOf(nodeName.toLowerCase()) !== -1;
};
self.getChildrenForTable = function(table, childNodeName)
{
if (table.nodeName.toLowerCase() !== 'table') {
return null;
}
var rows = [];
var allRows = table.getElementsByTagName(childNodeName);
// Filter out rows that don't belong to this table.
for (var i = 0, l = allRows.length; i<l; i++) {
if (self.findParentNode(allRows[i], 'table') === table) {
rows.push(allRows[i]);
}
}
return rows;
};
/**
* Test for the correct headers attributes on table cell elements.
*
* Return value contains the following elements:
* - required (Boolean): Whether header association at all is required.
* - used (Boolean): Whether headers attribute has been used on at least
* one table data (td) cell.
* - allowScope (Boolean): Whether scope is allowed to satisfy the association
* requirement (ie. max one row/one column).
* - correct (Boolean): Whether headers have been correctly used.
* - missingThId (Array): Array of th elements without IDs.
* - missingTd (Array): Array of elements without headers attribute.
* - wrongHeaders (Array): Array of elements where headers attr is incorrect.
* Each is a structure with following keys: element,
* expected [headers attr], actual [headers attr].
*
* @param {DOMNode} element Table element to test upon.
*
* @return {Object} The above return value structure.
*/
self.testTableHeaders = function(element)
{
var retval = {
required: true,
used: false,
correct: true,
allowScope: true,
missingThId: [],
missingTd: [],
wrongHeaders: []
};
var rows = self.getChildrenForTable(element, 'tr');
var tdCells = {};
var skipCells = [];
// Header IDs already used.
var headerIds = {
rows: [],
cols: []
};
var multiHeaders = {
rows: 0,
cols: 0
};
var missingIds = false;
for (var rownum = 0; rownum < rows.length; rownum++) {
var row = rows[rownum];
var colnum = 0;
for (var item = 0; item < row.childNodes.length; item++) {
var cell = row.childNodes[item];
if (cell.nodeType === 1) {
// Skip columns that are skipped due to rowspan.
if (skipCells[rownum]) {
while (skipCells[rownum][0] === colnum) {
skipCells[rownum].shift();
colnum++;
}
}
var nodeName = cell.nodeName.toLowerCase();
var rowspan = Number(cell.getAttribute('rowspan')) || 1;
var colspan = Number(cell.getAttribute('colspan')) || 1;
// If rowspanned, mark columns as skippable in the following
// row(s).
if (rowspan > 1) {
for (var i = rownum + 1; i < rownum + rowspan; i++) {
if (!skipCells[i]) {
skipCells[i] = [];
}
for (var j = colnum; j < colnum + colspan; j++) {
skipCells[i].push(j);
}
}
}
if (nodeName === 'th') {
var id = (cell.getAttribute('id') || '');
// Save the fact that we have a missing ID on the header.
if (id === '') {
retval.correct = false;
retval.missingThId.push(cell);
}
if ((rowspan > 1) && (colspan > 1)) {
// Multi-column AND multi-row header. Abandon all hope,
// As it must span across more than one row+column
retval.allowScope = false;
} else if (retval.allowScope === true) {
// If we haven't had a th in this column (row) yet,
// record it. if we find another th in this column (row),
// record that has multi-ths. If we already have a column
// (row) with multi-ths, we cannot use scope.
if (headerIds.cols[colnum] === undefined) {
headerIds.cols[colnum] = 0;
}
if (headerIds.rows[rownum] === undefined) {
headerIds.rows[rownum] = 0;
}
headerIds.rows[rownum] += colspan;
headerIds.cols[colnum] += rowspan;
}//end if
} else if ((nodeName === 'td')) {