This repository was archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest-slowparse.js
More file actions
857 lines (753 loc) · 33.1 KB
/
test-slowparse.js
File metadata and controls
857 lines (753 loc) · 33.1 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
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
module.exports = function(Slowparse, window, document, validators) {
var Node = require('../src/Node');
var ok = validators.ok;
var equal = validators.equal;
var test = validators.test;
var asyncTest = validators.asyncTest;
var start = validators.start;
var documentFragmentHTML = validators.documentFragmentHTML;
var assertParseIntervals = validators.assertParseIntervals;
var parseWithoutErrors = validators.parseWithoutErrors;
var testManySnippets = validators.testManySnippets;
var testStyleSheet = validators.testStyleSheet;
var parse = function(html) { return Slowparse.HTML(document, html); };
test("Stream.match()", function() {
var stream = new Slowparse.Stream("blArgle");
ok(stream.match("blArgle"));
equal(stream.pos, 0);
ok(!stream.match("blargle"));
equal(stream.pos, 0);
ok(stream.match("blargle", false, true));
equal(stream.pos, 0);
ok(stream.match("bla", true, true));
equal(stream.pos, 3);
ok(stream.match("rgle", true));
equal(stream.pos, 7);
});
test("Stream.eat() works at EOF", function() {
var stream = new Slowparse.Stream("");
ok(!stream.eat(/blah/));
});
test("parsing of valid DOCTYPE", function() {
var html = '<!DOCTYPE html><p>hi</p>';
var doc = parseWithoutErrors(html);
assertParseIntervals(html, doc, "document", {
'parseInfo.doctype': '<!DOCTYPE html>'
});
});
test("parsing of misplaced DOCTYPE", function() {
var html = '<p>hi</p><!DOCTYPE html>';
var result = parse(html);
equal(result.error, {
"openTag": {
"end": 10,
"name": "",
"start": 9
},
cursor: 9,
"type": "INVALID_TAG_NAME"
});
});
test("parsing of HTML comments", function() {
var html = 'hi<!--testing-->there';
var doc = parseWithoutErrors(html);
assertParseIntervals(html, doc.childNodes[1], "comment", {
'parseInfo': '<!--testing-->'
});
});
test("UNQUOTED_ATTR_VALUE in <h2><span start=</h2>", function() {
// https://github.com/mozilla/slowparse/issues/6
var err = Slowparse.HTML(document, '<h2><span start=</h2>').error;
equal(err.type, "UNQUOTED_ATTR_VALUE");
});
test("parsing of elements with boolean attributes", function() {
var html = '<a href></a>';
var doc = parseWithoutErrors(html);
var attr = doc.childNodes[0].attributes[0];
equal(attr.nodeName, 'href');
equal(attr.nodeValue, '');
assertParseIntervals(html, attr, "attr", {
'parseInfo.name': 'href'
});
var result = Slowparse.HTML(document, '<a href+></a>');
var expected = {
type: 'INVALID_ATTR_NAME',
start: 3,
end: 8,
attribute: { name: { value: "+" }},
cursor: 3
};
equal(result.error, expected);
html = '<a href class="foo"></a>';
doc = parseWithoutErrors(html);
var attr1 = doc.childNodes[0].attributes[0];
var attr2 = doc.childNodes[0].attributes[1];
// Apparently NamedNodeMap entries are not in any particular order:
// https://developer.mozilla.org/en/DOM/NamedNodeMap
//
// So, we'll swap these values if they're not in the order we expect
// them to be in, which is the case in IE9, at the very least.
if (attr1.nodeName == 'class') {
var temp = attr1;
attr1 = attr2;
attr2 = temp;
}
equal(attr1.nodeName, 'href');
equal(attr1.nodeValue, '');
equal(attr2.nodeName, 'class');
equal(attr2.nodeValue, 'foo');
assertParseIntervals(html, attr1, "attr1", {
'parseInfo.name': 'href'
});
ok(attr1.parseInfo.value === undefined);
assertParseIntervals(html, attr2, "attr2", {
'parseInfo.name': 'class',
'parseInfo.value': '"foo"'
});
});
test("parsing of <script> tags", function() {
var html = '<script>x < 3;</script>';
var doc = parseWithoutErrors(html);
equal(doc.childNodes.length, 1, "document has one child");
var script = doc.childNodes[0];
equal(script.nodeName, "SCRIPT",
"first child of generated DOM is <script>");
equal(script.childNodes.length, 1, "<script> has one child");
var textNode = script.childNodes[0];
equal(textNode.nodeType, Node.TEXT_NODE,
"<script>'s child is a text node.");
assertParseIntervals(html, textNode, "textNode", {
'parseInfo': 'x < 3;'
});
equal(documentFragmentHTML(doc), html,
"serialization of generated DOM matches original HTML");
});
test("parsing of valid HTML", function() {
var html = '<p class="foo">hello there</p>';
var doc = parseWithoutErrors(html);
equal(doc.childNodes.length, 1, "document has one child");
var p = doc.childNodes[0];
equal(p.nodeName, "P", "first child of generated DOM is <p>");
assertParseIntervals(html, p, "p", {
'parseInfo.openTag': '<p class="foo">',
'parseInfo.closeTag': '</p>'
});
equal(p.childNodes.length, 1, "<p> has one child");
equal(p.attributes.length, 1, "<p> has one attribute");
var textNode = p.childNodes[0];
equal(textNode.nodeType, Node.TEXT_NODE, "<p>'s child is a text node.");
assertParseIntervals(html, textNode, "textNode", {
'parseInfo': 'hello there'
});
assertParseIntervals(html, p.attributes[0], "attr", {
'parseInfo.name': 'class',
'parseInfo.value': '"foo"'
});
equal(documentFragmentHTML(doc), html,
"serialization of generated DOM matches original HTML");
});
test("parsing <img src='bogus' onerror='prompt(document.domain)'> (active attributes should be left intact)", function() {
// https://github.com/mozilla/slowparse/issues/6
var html = "<img src='bogus' onerror='prompt(document.domain)'>";
var doc = parseWithoutErrors(html);
var attr = doc.childNodes[0].attributes[0];
equal(attr.nodeName, 'src');
equal(attr.nodeValue, 'bogus');
attr = doc.childNodes[0].attributes[1];
equal(attr.nodeName, 'onerror');
equal(attr.nodeValue, 'prompt(document.domain)');
});
test("parsing <img src='bogus' onerror='prompt(document.domain)'> (active attributes should become \"active\"", function() {
// https://github.com/mozilla/slowparse/issues/6
var html = "<img src='bogus' onerror='prompt(document.domain)'>";
var doc = parseWithoutErrors(html, { disallowActiveAttributes: true });
var attr = doc.childNodes[0].attributes[0];
equal(attr.nodeName, 'src');
equal(attr.nodeValue, 'bogus');
attr = doc.childNodes[0].attributes[1];
equal(attr.nodeName, 'onerror');
equal(attr.nodeValue, '');
});
test("parsing of HTML comments with '--' in them", function() {
var html = '<!-- allow\n--\nin comments plz -->';
var doc = parseWithoutErrors(html);
equal(documentFragmentHTML(doc), html,
"serialization of generated DOM matches original HTML");
});
test("parsing of CDATA in <textarea> elements", function() {
var text = "\nThis is CDATA with <p>, <i> and" +
" <script> in it.\nThis should not trigger errors.";
var html = "<textarea>" + text + "</textarea>";
var doc = parseWithoutErrors(html);
equal(doc.childNodes.length, 1, "doc has one child node");
equal(doc.childNodes[0].nodeName, "TEXTAREA", "child node is <textarea>");
equal(doc.childNodes[0].childNodes.length, 1, "textarea has one child");
equal(doc.childNodes[0].childNodes[0].nodeValue, text,
"textarea contents are ok");
});
test("parsing of HTML is case-insensitive", function() {
testManySnippets("parsing of HTML is case-insensitive", [
'<P CLASS="FOO">hi</P>',
'<P class="FOO">hi</P>',
'<p class="FOO">hi</P>',
'<P class="FOO">hi</p>'
], function(html, doc) {
equal(doc.childNodes[0].nodeName, 'P');
equal(doc.childNodes[0].getAttribute('class'), 'FOO');
});
});
test("parsing of SVG elements", function() {
var d = "M 0 0 L 100 0 L 100 100 L 0 100 Z";
var html = "<svg width='100' height='100' viewbox='0 0 100 100'><path d='"+d+"'/></svg>";
var doc = parseWithoutErrors(html);
equal(doc.childNodes.length, 1, "doc has one child node");
equal(doc.childNodes[0].nodeName, "SVG", "child node is <svg>");
equal(doc.childNodes[0].childNodes.length, 1, "svg element has one child");
equal(doc.childNodes[0].childNodes[0].nodeName, "PATH", "svg child node is <path>");
equal(doc.childNodes[0].childNodes[0].getAttribute('d'), d, "path outline data is correct");
});
/*
// Commented off because of a bug in jsdom, see https://github.com/tmpvar/jsdom/issues/705
test("verifying SVG namespace", function() {
var d = "M 0 0 L 100 0 100 100 0 100 Z";
var html = "<html><body><p>test</p><svg width='100' height='100' viewbox='0 0 100 100'><path d='"+d+"'/></svg><p>test</p></body></html>";
var doc = parseWithoutErrors(html);
var htmlns = "http://www.w3.org/1999/xhtml",
svgns = "http://www.w3.org/2000/svg";
equal(doc.childNodes.length, 1, "doc has one child node");
equal(doc.childNodes[0].nodeName, "HTML", "top element is <html>");
equal(doc.childNodes[0].childNodes[0].nodeName, "BODY", "contained element is <body>");
equal(doc.childNodes[0].childNodes[0].childNodes[0].nodeName, "P", "first content node is <p>");
equal(doc.childNodes[0].childNodes[0].childNodes[0].namespaceURI.toLowerCase(), htmlns, "p element uses the correct namespace");
equal(doc.childNodes[0].childNodes[0].childNodes[1].nodeName, "svg", "second content node is <svg>");
equal(doc.childNodes[0].childNodes[0].childNodes[1].namespaceURI.toLowerCase(), svgns, "svg element uses the correct namespace");
equal(doc.childNodes[0].childNodes[0].childNodes[2].nodeName, "P", "third content node is <p>");
equal(doc.childNodes[0].childNodes[0].childNodes[2].namespaceURI.toLowerCase(), htmlns, "p element uses the correct namespace");
});
*/
test("verifying out-of-svg error", function() {
var html = "<html><body><svg><rect/></svg><path>error</path></body></html>";
var error = Slowparse.HTML(document, html).error;
equal(error.type, "INVALID_TAG_NAME");
});
test("parsing of HTML with void elements", function(){
testManySnippets("parsing of HTML with void elements", [
'<br>',
'<img src="http://www.mozilla.org/favicon.ico">'
], function(html, doc) {
equal(documentFragmentHTML(doc), html);
});
});
test("parsing of text content w/ newlines", function(){
testManySnippets("parsing of text content w/ newlines", [
'<p>hello\nthere</p>',
'<p>\n hello there</p>'
], function(html, doc) {
equal(documentFragmentHTML(doc), html);
});
});
test("parsing of valid HTML w/ whitespace", function(){
testManySnippets("parsing of valid HTML w/ whitespace", [
'<p class = "foo">hello there</p><p>u</p>',
'<p class="foo" >hello there</p><p>u</p>',
'<p \nclass="foo">hello there</p><p>u</p>',
'<p class="foo">hello there</p ><p>u</p>'
], function(html, doc) {
var canonicalHTML = '<p class="foo">hello there</p><p>u</p>';
var p = doc.childNodes[0];
assertParseIntervals(html, p.childNodes[0], "textNode", {
'parseInfo': 'hello there'
});
assertParseIntervals(html, p.attributes[0], "attr", {
'parseInfo.name': 'class',
'parseInfo.value': '"foo"'
});
equal(documentFragmentHTML(doc), canonicalHTML,
"Document fragment is correct.");
});
});
test("parsing of CSS rule w/ one decl, no semicolon", function(){
testStyleSheet("parsing of CSS rule w/ one decl, no semicolon",
"body { color: pink }",
function(html, css, styleContents) {
equal(styleContents.parseInfo.rules.length, 1);
equal(styleContents.parseInfo.rules[0].declarations.properties.length, 1);
assertParseIntervals(html, styleContents, "style", {
'parseInfo': 'body { color: pink }',
'parseInfo.rules[0].selector': 'body',
'parseInfo.rules[0].declarations': '{ color: pink }',
'parseInfo.rules[0].declarations.properties[0].name': 'color',
'parseInfo.rules[0].declarations.properties[0].value': 'pink'
});
});
});
test("parsing of CSS rule w/ one decl and semicolon", function(){
testStyleSheet("parsing of CSS rule w/ one decl and semicolon",
"body { color: pink; }",
function(html, css, styleContents) {
equal(styleContents.parseInfo.rules.length, 1);
equal(styleContents.parseInfo.rules[0].declarations.properties.length, 1);
assertParseIntervals(html, styleContents, "style", {
'parseInfo': 'body { color: pink; }',
'parseInfo.rules[0].selector': 'body',
'parseInfo.rules[0].declarations': '{ color: pink; }',
'parseInfo.rules[0].declarations.properties[0].name': 'color',
'parseInfo.rules[0].declarations.properties[0].value': 'pink'
});
});
});
test("parsing of empty CSS rule", function(){
testStyleSheet("parsing of empty CSS rule",
"body {}",
function(html, css, styleContents) {
equal(styleContents.parseInfo.rules.length, 1);
equal(styleContents.parseInfo.rules[0].declarations.properties.length, 0);
assertParseIntervals(html, styleContents, "style", {
'parseInfo': 'body {}',
'parseInfo.rules[0].selector': 'body',
'parseInfo.rules[0].declarations': '{}'
});
});
});
test("parsing of CSS rule w/ funky whitespace", function(){
testStyleSheet("parsing of CSS rule w/ funky whitespace",
["body\n { color: pink; }",
"body\n {\n color: pink; }",
"body\n {\n color: \npink; }",
"body\n {\n color: \npink\n\n }",
"body\n {\n color : \npink\n\n }",
"body\n {\n color: \npink \n; }",
"body\n {\n color: \npink; }\n"],
function(html, css, styleContents) {
equal(styleContents.parseInfo.rules.length, 1);
equal(styleContents.parseInfo.rules[0].declarations.properties.length, 1);
assertParseIntervals(html, styleContents, "style", {
'parseInfo.rules[0].selector': 'body',
'parseInfo.rules[0].declarations.properties[0].name': 'color',
'parseInfo.rules[0].declarations.properties[0].value': 'pink'
});
});
});
test("parsing of empty CSS sheet w/ comment", function(){
testStyleSheet("parsing of empty CSS sheet w/ comment",
"/* nothing to see here. */",
function(html, css, styleContents) {
equal(styleContents.parseInfo.rules.length, 0);
});
});
test("parsing of empty CSS rule w/ comment", function(){
testStyleSheet("parsing of empty CSS rule w/ comment",
"p { /* nothing to see here. */ }",
function(html, css, styleContents) {
equal(styleContents.parseInfo.rules.length, 1);
});
});
test("parsing of CSS rule w/ comments", function(){
testStyleSheet("parsing of CSS rule w/ comments",
"/** comment 1 **/ bo/* comment 2 */dy /*comment 3*/{ /* c4 */ co/*c5*/lor/*c6*/: /*c7*/pi/*c8*/nk/*c9****** c9 *****/}",
function(html, css, styleContents) {
equal(styleContents.parseInfo.rules.length, 1);
equal(styleContents.parseInfo.rules[0].declarations.properties.length, 1);
assertParseIntervals(html, styleContents, "style", {
'parseInfo': '/** comment 1 **/ bo/* comment 2 */dy /*comment 3*/{ /* c4 */ co/*c5*/lor/*c6*/: /*c7*/pi/*c8*/nk/*c9****** c9 *****/}',
'parseInfo.rules[0].selector': 'bo/* comment 2 */dy',
'parseInfo.rules[0].declarations': '{ /* c4 */ co/*c5*/lor/*c6*/: /*c7*/pi/*c8*/nk/*c9****** c9 *****/}',
'parseInfo.rules[0].declarations.properties[0].name': 'co/*c5*/lor',
'parseInfo.rules[0].declarations.properties[0].value': 'pi/*c8*/nk'
});
equal(styleContents.parseInfo.rules[0].selector.value, "body");
equal(styleContents.parseInfo.rules[0].declarations.properties[0].name.value, "color");
equal(styleContents.parseInfo.rules[0].declarations.properties[0].value.value, "pink");
equal(styleContents.parseInfo.comments.length, 9);
assertParseIntervals(html, styleContents, "style", {
'parseInfo.comments[0]': '/** comment 1 **/',
'parseInfo.comments[1]': '/* comment 2 */',
'parseInfo.comments[2]': '/*comment 3*/',
'parseInfo.comments[3]': '/* c4 */',
'parseInfo.comments[4]': '/*c5*/',
'parseInfo.comments[5]': '/*c6*/',
'parseInfo.comments[6]': '/*c7*/',
'parseInfo.comments[7]': '/*c8*/',
'parseInfo.comments[8]': '/*c9****** c9 *****/'
});
});
});
test("parsing of CSS rule w/ vendor prefixes", function(){
testStyleSheet("parsing of CSS rule w/ vendor prefixes",
".vendors { -o-border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; -ms-border-radius: 5px; border-radius: 5px; }",
function(html, css, styleContents) {
equal(styleContents.parseInfo.rules.length, 1);
equal(styleContents.parseInfo.rules[0].declarations.properties.length, 5);
assertParseIntervals(html, styleContents, "style", {
'parseInfo': '.vendors { -o-border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; -ms-border-radius: 5px; border-radius: 5px; }',
'parseInfo.rules[0].selector': '.vendors',
'parseInfo.rules[0].declarations': '{ -o-border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; -ms-border-radius: 5px; border-radius: 5px; }',
'parseInfo.rules[0].declarations.properties[0].name': '-o-border-radius',
'parseInfo.rules[0].declarations.properties[0].value': '5px',
'parseInfo.rules[0].declarations.properties[1].name': '-moz-border-radius',
'parseInfo.rules[0].declarations.properties[1].value': '5px',
'parseInfo.rules[0].declarations.properties[2].name': '-webkit-border-radius',
'parseInfo.rules[0].declarations.properties[2].value': '5px',
'parseInfo.rules[0].declarations.properties[3].name': '-ms-border-radius',
'parseInfo.rules[0].declarations.properties[3].value': '5px',
'parseInfo.rules[0].declarations.properties[4].name': 'border-radius',
'parseInfo.rules[0].declarations.properties[4].value': '5px'
});
});
});
test("replaceEntityRefs", function() {
[
["<", "<"],
[">", ">"],
["&", "&"],
[""", '"'],
[""", '"', "matches are case-insensitive"],
["<p> tag", "<p> tag", "multiple refs are replaced"],
["hello &garbage;", "hello &garbage;", "replacer is forgiving"]
].forEach(function(arg) {
equal(Slowparse.replaceEntityRefs(arg[0]), arg[1],
"replaceEntityRefs(" + JSON.stringify(arg[0]) + ") == " +
JSON.stringify(arg[1]) + (arg[2] ? " (" + arg[2] + ")" : ""));
});
});
test("parsing of self-closing void elements works", function() {
var html = 'hello<br/>';
var doc = parseWithoutErrors(html);
assertParseIntervals(html, doc.childNodes[1], "brNode", {
'parseInfo.openTag': '<br/>'
});
});
test("parsing of self-closing void elements w/ spaces works", function() {
var html = 'hello<br />';
var doc = parseWithoutErrors(html);
assertParseIntervals(html, doc.childNodes[1], "brNode", {
'parseInfo.openTag': '<br />'
});
});
test("parsing of text content w/ HTML entities", function() {
var html = '<p><p></p>';
var doc = parseWithoutErrors(html);
var textNode = doc.childNodes[0].childNodes[0];
equal(textNode.nodeValue, '<p>');
assertParseIntervals(html, textNode, "textNode", {
'parseInfo': '<p>'
});
});
test("parsing of <script> tags doesn't execute the code", function(){
asyncTest("parsing of <script> tags doesn't execute the code", function() {
var html = '<script>alert("PWNED"); window.PWNED = true;</script>';
var doc = parseWithoutErrors(html);
setTimeout(function() {
equal(window.PWNED, undefined);
start();
}, 100);
});
});
test("parsing of attr content w/ HTML entities", function() {
var html = '<p class="1 < 2 < 3"></p>';
var doc = parseWithoutErrors(html);
var attrNode = doc.childNodes[0].attributes[0];
equal(attrNode.nodeValue, '1 < 2 < 3');
assertParseIntervals(html, attrNode, "attr", {
'parseInfo.value': '"1 < 2 < 3"'
});
});
test("INVALID_TAG_NAME raised by < at EOF", function() {
var error = Slowparse.HTML(document, '<').error;
equal(error.type, "INVALID_TAG_NAME");
});
test("MISSING_CSS_SELECTOR works after comment", function() {
var html = '<style>/* hello */ {</style>';
var error = Slowparse.HTML(document, html).error;
equal(error.type, "MISSING_CSS_SELECTOR");
});
test("UNTERMINATED_ATTR_VALUE works at end of stream", function() {
var html = '<a href="';
var error = Slowparse.HTML(document, html).error;
equal(error.type, "UNTERMINATED_ATTR_VALUE");
equal(error.attribute.value.start, html.length-1);
});
test("UNQUOTED_ATTR_VALUE works at end of stream", function() {
var html = '<a href=';
var error = Slowparse.HTML(document, html).error;
equal(error.type, "UNQUOTED_ATTR_VALUE");
equal(error.start, html.length);
});
test("UNTERMINATED_CLOSE_TAG works at end of stream", function() {
var html = "<span>test</span";
var error = Slowparse.HTML(document, html).error;
equal(error.type, "UNTERMINATED_CLOSE_TAG");
equal(error.closeTag.end, html.length);
});
test("ATTRIBUTE_IN_CLOSING_TAG works when attribute is included in closing tag", function() {
var html = "<a href=\"http://www.foo.com/\">Foo</a href>";
var error = Slowparse.HTML(document, html).error;
equal(error.type, "ATTRIBUTE_IN_CLOSING_TAG", "error.type should be ATTRIBUTE_IN_CLOSING_TAG, but was " + error.type);
});
test("Space at the end of a closing tag (with no attributes) produces no error", function() {
var html = "<a href=\"http://www.foo.com/\">Foo</a >";
var error = Slowparse.HTML(document, html).error;
equal(error, null, "Valid closing tag with extra space should produce no error.");
});
test("Slowparse.HTML_ELEMENT_NAMES", function() {
ok(Slowparse.HTML_ELEMENT_NAMES.indexOf("p") != -1);
});
test("Slowparse.CSS_PROPERTY_NAMES", function() {
ok(Slowparse.CSS_PROPERTY_NAMES.indexOf("color") != -1);
});
test("parsing elements with optional close tags: <p>", function() {
var html = '<div><p>text\n<p>more text</div>';
var result = parse(html);
ok(!result.error, "no error on omitted </p>");
});
test("parsing elements with nested optional close tags: <li><p></li>", function() {
var html = '<ul><li><p></li></ul>';
var result = parse(html);
ok(!result.error, "no error on omitted </p>");
});
test("parsing elements with nested optional close tags: <li><p>x</li>", function() {
var html = '<ul><li><p>x</li></ul>';
var result = parse(html);
ok(!result.error, "no error on omitted </p>");
});
test("parsing elements with nested optional close tags: <li><p>x</p></li>", function() {
var html = '<ul><li><p>x</p></li></ul>';
var result = parse(html);
ok(!result.error, "no error on omitted </p>");
});
test("parsing elements with nested optional close tags: <li><p>x</p>m</li>", function() {
var html = '<ul><li><p>x</p>m</li></ul>';
var result = parse(html);
ok(!result.error, "no error on omitted </p>");
});
test("parsing elements with nested optional close tags: <li><p>x<p>y</li>", function() {
var html = '<ul><li><p>x<p>y</li></ul>';
var result = parse(html);
ok(!result.error, "no error on omitted </p>");
});
test("parsing elements with optional close tags: <select><optgroup label='X'><option>V<option>S<optgroup label='Z'><option>M<option>A</select>", function() {
var html = '<select><optgroup label="X"><option>V<option>S<optgroup label="Z"><option>M<option>A</select>';
var result = parse(html);
ok(!result.error, "no error on omitted </option> and </optgroup>");
});
test("parsing elements with optional close tags: <datalist><option>a<option>b</datalist>", function() {
var html = '<datalist><option>a<option>b</datalist>';
var result = parse(html);
ok(!result.error, "no error on omitted </option>");
});
test("parsing elements with optional close tags: <table><tr><td></td><tr><td></td></tr></table>", function() {
var html = '<table><tr><td></td><tr><td></td></tr></table>';
var result = parse(html);
ok(!result.error, "no error on omitted </tr> before <tr>");
});
test("parsing elements with optional close tags: <table><tr><td></td><tr><td></td></table>", function() {
var html = '<table><tr><td></td><tr><td></td></table>';
var result = parse(html);
ok(!result.error, "no error on omitted </tr>");
});
test("parsing elements with optional close tags: <table><tr><td><tr><td></table>", function() {
var html = '<table><tr><td><tr><td></table>';
var result = parse(html);
ok(!result.error, "no error on omitted </tr> and </td>");
});
test("parsing elements with optional close tags: <table><thead><tr><td><tbody><tr><td><tr><td><tfoot></table>", function() {
var html = '<table><thead><tr><td><tbody><tr><td><tr><td><tfoot></table>';
var result = parse(html);
ok(!result.error, "no error on omitted </tr> and </td>");
});
test("parsing elements with optional close tags: <dl><dt>Coffee<dd>Black hot drink<dt>Milk<dd>White cold drink</dl>", function() {
var html = '<dl><dt>Coffee<dd>Black hot drink<dt>Milk<dd>White cold drink</dl>';
var result = parse(html);
ok(!result.error, "no error on omitted </dt> and </dd>");
});
test("parsing elements with optional close tags: <ruby>漢<rp>(<rt>Kan<rp>)</rp>字<rp>(<rt>ji<rp>)</ruby>", function() {
var html = '<ruby>漢<rp>(<rt>Kan<rp>)</rp>字<rp>(<rt>ji<rp>)</ruby>';
var result = parse(html);
ok(!result.error, "no error on omitted </rp> and </rt>");
});
test("parsing elements with optional close tags: <ruby><rb>10<rb>31<rb>2002<rtc><rt>Month<rt>Day<rt>Year<rtc><rt>Expiration Date</ruby>", function() {
var html = '<ruby><rb>10<rb>31<rb>2002<rtc><rt>Month<rt>Day<rt>Year<rtc><rt>Expiration Date</ruby>';
var result = parse(html);
ok(!result.error, "no error on omitted </rb> , </rt> ,and </rtc>");
});
test("intentional fail for optional close tag (incorrect use). pass = not accepted", function() {
var html = '<div><p>text\n<a>more text</a></div>';
var result = parse(html);
var expected = {
type: 'MISMATCHED_CLOSE_TAG',
openTag: {
name: 'p',
start: 5,
end: 8
},
closeTag: {
name: 'div',
start: 29,
end: 34
},
cursor: 29
};
equal(result.error, expected, "bad omission error for <p>");
});
test("testing CSS 'content' property values with semi-colons", function() {
var html = "<style>div:before { content: '<' attr(test) 'test'; content: \"<\"; }</style>";
var result = parse(html);
ok(!result.error, "semi-colons accepted");
});
test("testing CSS 'content' property values with semi-colons inside nested quotes", function() {
var html = "<style>div:before { content: 'let\\'s try \";\", eh?'; }</style>";
var result = parse(html);
ok(!result.error, "semi-colons accepted");
});
test("@keyframes css block", function() {
var html = "<style>@keyframes { 0% { opacity: 0; } 100% { opacity: 1.0; } } .test { opacity: 0; }</style>";
var result = parse(html);
ok(!result.error, "@keyframes accepted");
});
test("@keyframes css block with named frame", function() {
var html = "<style>@keyframes animation1 { 0% { left: 260px; top: -10%; } 100% { left: 260px; top: 100%; } }</style>";
var result = parse(html);
ok(!result.error, "@keyframes accepted");
});
test("@keyframes css block with typo (@keyfarmes). pass = not accepted", function() {
var html = "<style>@keyfarmes { 0% { opacity: 0; } 100% { opacity: 1.0; } } .test { opacity: 0; }</style>";
var result = parse(html);
var expected = {
type: 'UNKOWN_CSS_KEYWORD',
cssKeyword: {
start: 7,
end: 18,
value: "@keyfarmes"
},
cursor: 7
};
equal(result.error, expected, "keyfarmes is not accepted as @keyword");
});
test("@-moz-keyframes css block", function() {
var html = "<style>@-moz-keyframes { 0% { opacity: 0; } 100% { opacity: 1.0; } } .test { opacity: 0; }</style>";
var result = parse(html);
ok(!result.error, "@-*-keyframes accepted");
});
test("@-webkit-keyframes css block", function() {
var html = "<style>@-webkit-keyframes { 0% { opacity: 0; } 100% { opacity: 1.0; } } .test { opacity: 0; }</style>";
var result = parse(html);
ok(!result.error, "@-*-keyframes accepted");
});
test("@keyframes css block with leading block comment", function() {
var html = "<style>/*\n keyframe test\n*/\n@keyframes { 0% { opacity: 0; } 100% { opacity: 1.0; } } .test { opacity: 0; }</style>";
var result = parse(html);
ok(!result.error, "@keyframes accepted");
});
test("@keyframes css block with trailing block comment", function() {
var html = "<style>\n@keyframes { 0% { opacity: 0; } 100% { opacity: 1.0; } } .test { opacity: 0; }\n/*\n keyframe test\n*/\n</style>";
var result = parse(html);
ok(!result.error, "@keyframes accepted");
});
test("@keyframes css block with leading and trailing block comment", function() {
var html = "<style>/*\n keyframe test\n*/\n@keyframes { 0% { opacity: 0; } 100% { opacity: 1.0; } } .test { opacity: 0; }\n/*\n keyframe test\n*/\n</style>";
var result = parse(html);
ok(!result.error, "@keyframes accepted");
});
test("@media rule", function() {
var html = "<style>@media (max-width: 100px) { .class { background: white; } }</style>";
var result = parse(html);
ok(!result.error, "@media accepted");
});
test("@media rule (complex)", function() {
var html = "<style>@media (min-width: 700px), handheld and (orientation: landscape) { .class { background: white; } }</style>";
var result = parse(html);
ok(!result.error, "@media accepted");
});
test("@font-face rule", function() {
var html = "<style>@font-face { font-family: 'snickerdoodle'; } .test { opacity: 0; }</style>";
var result = parse(html);
ok(!result.error, "@font-face accepted");
});
test("@font-face rule with typo (@font-faec). pass = not accepted", function() {
var html = "<style>@font-faec { font-family: 'snickerdoodle'; } .test { opacity: 0; }</style>";
var result = parse(html);
var expected = {
type: 'UNKOWN_CSS_KEYWORD',
cssKeyword: {
start: 7,
end: 18,
value: "@font-faec"
},
cursor: 7
};
equal(result.error, expected, "font-faec is not accepted as @keyword");
});
test("Custom Elements are allowed", function () {
var html = "<p-q style=\"color: green;\">hello</p-q>";
var result = parse(html);
ok(!result.error, "custom element p-q accepted");
});
test("Custom Elements with attributes are allowed", function () {
var html = "<p-q style=\"color: green;\">hello</p-q>";
var result = parse(html);
ok(!result.error, "custom element p-q with attribtues accepted");
});
test("Custom Element tag names must be <[a-z]-[a-z]>", function () {
var html = "<-></->";
var result = parse(html);
equal(result.error, {
type: 'INVALID_TAG_NAME',
openTag: { name: '-', start: 0, end: 2 },
cursor: 0
});
});
test("HTML with SVG that uses xlink:* attributes", function () {
var html = "<svg><use xlink:href='#triangle' transform='scale(-1,1)'/></svg>";
var result = parse(html);
ok(!result.error, "xlink:* attributes accepted");
});
test("HTML that uses xml:* attributes", function () {
var html = "<p xml:spaces='2'>test</p>";
var result = parse(html);
ok(!result.error, "xml:* attributes accepted");
});
test("HTML with nonsense:attribute should not be rejected", function () {
var html = "<p nonsense:attribute='triangle'>test</p>";
var result = parse(html);
ok(result.error, {
type: 'UNSUPPORTED_ATTR_NAMESPACE',
start: 3,
end: 21,
attribute: {
name: {
value: 'nonsense:attribute'
}
},
cursor: 3
});
});
test("Attributes with multiple namespaces should be rejected", function () {
var html = "<svg><rect xml:xlink:href='#triangle' /></svg>";
var result = parse(html);
equal(result.error, {
type: 'MULTIPLE_ATTR_NAMESPACES',
start: 11,
end: 25,
attribute: {
name: {
value: 'xml:xlink:href'
}
},
cursor: 11
});
});
test("Rogue \"value\" entries inside the opening tag should be flagged as bad", function () {
var html = '<body><img src="https://example.org/test.png" alt="test image" width "207"></body>';
var result = parse(html);
equal(result.error, {
type: 'UNBOUND_ATTRIBUTE_VALUE',
value: '"207"',
interval: {
start: 69,
end: 74
},
cursor: 69
});
});
return validators.getFailCount();
};