forked from mozilla/slowparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slowparse.js
1402 lines (1324 loc) · 53.2 KB
/
slowparse.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
"use strict";
// Slowparse is a token stream parser for HTML and CSS text,
// recording regions of interest during the parse run and
// signaling any errors detected accompanied by relevant
// regions in the text stream, to make debugging easy. Each
// error type is documented in the [error specification][].
//
// Slowparse also builds a DOM as it goes, attaching metadata
// to each node build that points to where it came from in
// the original source.
//
// For more information on the rationale behind Slowparse, as
// well as its design goals, see the [README][].
//
// If [RequireJS] is detected, this file is defined as a module via
// `define()`. Otherwise, a global called `Slowparse` is exposed.
//
// ## Implementation
//
// Slowparse is effectively a finite state machine for
// HTML and CSS strings, and will switch between the HTML
// and CSS parsers while maintaining a single token stream.
//
// [RequireJS]: http://requirejs.org/
// [error specification]: spec/
// [README]: https://github.com/mozilla/slowparse#readme
var Slowparse = (function() {
// ### Character Entity Parsing
//
// We currently only parse the most common named character entities.
var CHARACTER_ENTITY_REFS = {
lt: "<",
gt: ">",
apos: "'",
quot: '"',
amp: "&"
};
// `replaceEntityRefs()` will replace named character entity references
// (e.g. `<`) in the given text string and return the result. If an
// entity name is unrecognized, don't replace it at all. Writing HTML
// would be surprisingly painful without this forgiving behavior.
//
// This function does not currently replace numeric character entity
// references (e.g., ` `).
function replaceEntityRefs(text) {
return text.replace(/&([A-Za-z]+);/g, function(ref, name) {
name = name.toLowerCase();
if (name in CHARACTER_ENTITY_REFS)
return CHARACTER_ENTITY_REFS[name];
return ref;
});
}
// ### Errors
//
// `ParseError` is an internal error class used to indicate a parsing error.
// It never gets seen by Slowparse clients, as parse errors are an
// expected occurrence. However, they are used internally to simplify
// flow control.
//
// The first argument is the name of an error type, followed by
// arbitrary positional arguments specific to that error type. Every
// instance has a `parseInfo` property which contains the error
// object that will be exposed to Slowparse clients when parsing errors
// occur.
function ParseError(type) {
this.name = "ParseError";
if (!(type in ParseErrorBuilders))
throw new Error("Unknown ParseError type: " + type);
var args = [];
for (var i = 1; i < arguments.length; i++)
args.push(arguments[i]);
var parseInfo = ParseErrorBuilders[type].apply(ParseErrorBuilders, args);
/* This may seem a weird way of setting an attribute, but we want
* to make the JSON serialize so the 'type' appears first, as it
* makes our documentation read better. */
parseInfo = ParseErrorBuilders._combine({
type: type
}, parseInfo);
this.message = type;
this.parseInfo = parseInfo;
}
ParseError.prototype = Error.prototype;
// `ParseErrorBuilders` contains Factory functions for all our types of
// parse errors, indexed by error type.
//
// Each public factory function returns a `parseInfo` object, sans the
// `type` property. For more information on each type of error,
// see the [error specification][].
//
// [error specification]: spec/
var ParseErrorBuilders = {
/* Create a new object that has the properties of both arguments
* and return it. */
_combine: function(a, b) {
var obj = {}, name;
for (name in a) {
obj[name] = a[name];
}
for (name in b) {
obj[name] = b[name];
}
return obj;
},
// These are HTML errors.
UNCLOSED_TAG: function(parser) {
return {
openTag: this._combine({
name: parser.domBuilder.currentNode.nodeName.toLowerCase()
}, parser.domBuilder.currentNode.parseInfo.openTag)
};
},
INVALID_TAG_NAME: function(tagName, token) {
return {
openTag: this._combine({
name: tagName
}, token.interval)
};
},
UNEXPECTED_CLOSE_TAG: function(parser, closeTagName, token) {
return {
closeTag: this._combine({
name: closeTagName
}, token.interval)
};
},
MISMATCHED_CLOSE_TAG: function(parser, openTagName, closeTagName, token) {
return {
openTag: this._combine({
name: openTagName
}, parser.domBuilder.currentNode.parseInfo.openTag),
closeTag: this._combine({
name: closeTagName
}, token.interval)
};
},
CLOSE_TAG_FOR_VOID_ELEMENT: function(parser, closeTagName, token) {
return {
closeTag: this._combine({
name: closeTagName
}, token.interval)
};
},
UNTERMINATED_COMMENT: function(token) {
return {
start: token.interval.start
};
},
UNTERMINATED_ATTR_VALUE: function(parser, nameTok) {
return {
openTag: this._combine({
name: parser.domBuilder.currentNode.nodeName.toLowerCase()
}, parser.domBuilder.currentNode.parseInfo.openTag),
attribute: {
name: {
value: nameTok.value,
start: nameTok.interval.start,
end: nameTok.interval.end
},
value: {
start: parser.stream.makeToken().interval.start
}
},
};
},
UNQUOTED_ATTR_VALUE: function(parser) {
var pos = parser.stream.pos;
if (!parser.stream.end())
pos = parser.stream.makeToken().interval.start;
return {
start: pos
};
},
UNTERMINATED_OPEN_TAG: function(parser) {
return {
openTag: {
start: parser.domBuilder.currentNode.parseInfo.openTag.start,
end: parser.stream.pos,
name: parser.domBuilder.currentNode.nodeName.toLowerCase()
}
};
},
SELF_CLOSING_NON_VOID_ELEMENT: function(parser, tagName) {
return {
name: tagName,
start: parser.domBuilder.currentNode.parseInfo.openTag.start,
end: parser.stream.makeToken().interval.end
};
},
UNTERMINATED_CLOSE_TAG: function(parser) {
var end = parser.stream.pos;
if (!parser.stream.end())
end = parser.stream.makeToken().interval.start;
return {
closeTag: {
name: parser.domBuilder.currentNode.nodeName.toLowerCase(),
start: parser.domBuilder.currentNode.parseInfo.closeTag.start,
end: end
}
};
},
// These are CSS errors.
MISSING_CSS_SELECTOR: function(parser, start, end) {
return {
cssBlock: {
start: start,
end: end
}
};
},
UNFINISHED_CSS_SELECTOR: function(parser, start, end, selector) {
return {
cssSelector: {
start: start,
end: end,
selector: selector
}
};
},
MISSING_CSS_BLOCK_OPENER: function(parser, start, end, selector) {
return {
cssSelector: {
start: start,
end: end,
selector: selector
}
};
},
INVALID_CSS_PROPERTY_NAME: function(parser, start, end, property) {
return {
cssProperty: {
start: start,
end: end,
property: property
}
};
},
MISSING_CSS_PROPERTY: function(parser, start, end, selector) {
return {
cssSelector: {
start: start,
end: end,
selector: selector
}
};
},
UNFINISHED_CSS_PROPERTY: function(parser, start, end, property) {
return {
cssProperty: {
start: start,
end: end,
property: property
}
};
},
MISSING_CSS_VALUE: function(parser, start, end, property) {
return {
cssProperty: {
start: start,
end: end,
property: property
}
};
},
UNFINISHED_CSS_VALUE: function(parser, start, end, value) {
return {
cssValue: {
start: start,
end: end,
value: value
}
};
},
MISSING_CSS_BLOCK_CLOSER: function(parser, start, end, value) {
return {
cssValue: {
start: start,
end: end,
value: value
}
};
},
UNCAUGHT_CSS_PARSE_ERROR: function(parser, start, end, msg) {
return {
error: {
start: start,
end: end,
msg: msg
}
};
},
UNTERMINATED_CSS_COMMENT: function(start) {
return {
start: start
};
},
HTML_CODE_IN_CSS_BLOCK: function(parser, start, end) {
return {
html: {
start: start,
end: end
}
}
}
};
// ### Streams
//
// `Stream` is an internal class used for tokenization. The interface for
// this class is inspired by the analogous class in [CodeMirror][].
//
// [CodeMirror]: http://codemirror.net/doc/manual.html#modeapi
function Stream(text) {
this.text = text;
this.pos = 0;
this.tokenStart = 0;
}
Stream.prototype = {
// `Stream.peek()` returns the next character in the stream without
// advancing it. It will return `undefined` at the end of the text.
peek: function() {
return this.text[this.pos];
},
// `Stream.substream(len)` returns a substream from the stream
// without advancing it, with length `len`.
substream: function(len) {
return this.text.substring(this.pos, this.pos + len);
},
// `Stream.next()` returns the next character in the stream and advances
// it. It also returns `undefined` when no more characters are available.
next: function() {
if (!this.end())
return this.text[this.pos++];
},
// `Stream.end()` returns true only if the stream is at the end of the
// text.
end: function() {
return (this.pos == this.text.length);
},
// `Stream.eat()` takes a regular expression. If the next character in
// the stream matches the given argument, it is consumed and returned.
// Otherwise, `undefined` is returned.
eat: function(match) {
if (!this.end() && this.peek().match(match))
return this.next();
},
// `Stream.eatWhile()` repeatedly calls `eat()` with the given argument,
// until it fails. Returns `true` if any characters were eaten.
eatWhile: function(matcher) {
var wereAnyEaten = false;
while (!this.end()) {
if (this.eat(matcher))
wereAnyEaten = true;
else
return wereAnyEaten;
}
},
// `Stream.eatSpace()` is a shortcut for `eatWhile()` when matching
// white-space (including newlines).
eatSpace: function() {
return this.eatWhile(/[\s\n]/);
},
// `Stream.eatCSSWhile()` is like `eatWhile()`, but it
// automatically deals with eating block comments like `/* foo */`.
eatCSSWhile: function(matcher) {
var wereAnyEaten = false,
chr = '',
peek = '',
next = '';
while (!this.end()) {
chr = this.eat(matcher);
if (chr)
wereAnyEaten = true;
else
return wereAnyEaten;
if (chr === '/') {
peek = this.peek();
if (peek === '*') {
/* Block comment found. Gobble until resolved. */
while(next !== '/' && !this.end()) {
this.eatWhile(/[^*]/);
this.next();
next = this.next();
}
next = '';
}
}
}
},
// `Stream.markTokenStart()` will set the start for the next token to
// the current stream position (i.e., "where we are now").
markTokenStart: function() {
this.tokenStart = this.pos;
},
// `Stream.markTokenStartAfterSpace()` is a wrapper function for eating
// up space, then marking the start for a new token.
markTokenStartAfterSpace: function() {
this.eatSpace();
this.markTokenStart();
},
// `Stream.makeToken()` generates a JSON-serializable token object
// representing the interval of text between the end of the last
// generated token and the current stream position.
makeToken: function() {
if (this.pos == this.tokenStart)
return null;
var token = {
value: this.text.slice(this.tokenStart, this.pos),
interval: {
start: this.tokenStart,
end: this.pos
}
};
this.tokenStart = this.pos;
return token;
},
// `Stream.match()` acts like a multi-character eat—if *consume* is `true`
// or not given—or a look-ahead that doesn't update the stream
// position—if it is `false`. *string* must be a string. *caseFold* can
// be set to `true` to make the match case-insensitive.
match: function(string, consume, caseFold) {
var substring = this.text.slice(this.pos, this.pos + string.length);
if (caseFold) {
string = string.toLowerCase();
substring = substring.toLowerCase();
}
if (string == substring) {
if (consume)
this.pos += string.length;
return true;
}
return false;
}
};
// ### CSS Parsing
//
// `CSSParser` is our internal CSS token stream parser object. This object
// has references to the stream, as well as the HTML DOM builder that is
// used by the HTML parser.
function CSSParser(stream, domBuilder) {
this.stream = stream;
this.domBuilder = domBuilder;
}
CSSParser.prototype = {
// We keep a list of all currently valid CSS properties (CSS1-CSS3).
// This list does not contain vendor prefixes.
cssProperties: [
"alignment-adjust","alignment-baseline","animation","animation-delay",
"animation-direction","animation-duration","animation-iteration-count",
"animation-name","animation-play-state","animation-timing-function",
"appearance","azimuth","backface-visibility","background",
"background-attachment","background-clip","background-color",
"background-image","background-origin","background-position",
"background-repeat","background-size","baseline-shift","binding",
"bleed","bookmark-label","bookmark-level","bookmark-state",
"bookmark-target","border","border-bottom","border-bottom-color",
"border-bottom-left-radius","border-bottom-right-radius",
"border-bottom-style","border-bottom-width","border-collapse",
"border-color","border-image","border-image-outset",
"border-image-repeat","border-image-slice","border-image-source",
"border-image-width","border-left","border-left-color",
"border-left-style","border-left-width","border-radius","border-right",
"border-right-color","border-right-style","border-right-width",
"border-spacing","border-style","border-top","border-top-color",
"border-top-left-radius","border-top-right-radius","border-top-style",
"border-top-width","border-width","bottom","box-decoration-break",
"box-shadow","box-sizing","break-after","break-before","break-inside",
"caption-side","clear","clip","color","color-profile","column-count",
"column-fill","column-gap","column-rule","column-rule-color",
"column-rule-style","column-rule-width","column-span","column-width",
"columns","content","counter-increment","counter-reset","crop","cue",
"cue-after","cue-before","cursor","direction","display",
"dominant-baseline","drop-initial-after-adjust",
"drop-initial-after-align","drop-initial-before-adjust",
"drop-initial-before-align","drop-initial-size","drop-initial-value",
"elevation","empty-cells","filter","fit","fit-position","flex-align",
"flex-flow","flex-line-pack","flex-order","flex-pack","float","float-offset",
"font","font-family","font-size","font-size-adjust","font-stretch",
"font-style","font-variant","font-weight","grid-columns","grid-rows",
"hanging-punctuation","height","hyphenate-after","hyphenate-before",
"hyphenate-character","hyphenate-lines","hyphenate-resource","hyphens",
"icon","image-orientation","image-rendering","image-resolution",
"inline-box-align","left","letter-spacing","line-break","line-height",
"line-stacking","line-stacking-ruby","line-stacking-shift",
"line-stacking-strategy","list-style","list-style-image",
"list-style-position","list-style-type","margin","margin-bottom",
"margin-left","margin-right","margin-top","marker-offset","marks",
"marquee-direction","marquee-loop","marquee-play-count","marquee-speed",
"marquee-style","max-height","max-width","min-height","min-width",
"move-to","nav-down","nav-index","nav-left","nav-right","nav-up",
"opacity","orphans","outline","outline-color","outline-offset",
"outline-style","outline-width","overflow","overflow-style",
"overflow-wrap","overflow-x","overflow-y","padding","padding-bottom",
"padding-left","padding-right","padding-top","page","page-break-after",
"page-break-before","page-break-inside","page-policy","pause",
"pause-after","pause-before","perspective","perspective-origin",
"phonemes","pitch","pitch-range","play-during","pointer-events",
"position",
"presentation-level","punctuation-trim","quotes","rendering-intent",
"resize","rest","rest-after","rest-before","richness","right",
"rotation","rotation-point","ruby-align","ruby-overhang",
"ruby-position","ruby-span","src","size","speak","speak-header",
"speak-numeral","speak-punctuation","speech-rate","stress","string-set",
"tab-size","table-layout","target","target-name","target-new",
"target-position","text-align","text-align-last","text-decoration",
"text-decoration-color","text-decoration-line","text-decoration-skip",
"text-decoration-style","text-emphasis","text-emphasis-color",
"text-emphasis-position","text-emphasis-style","text-height",
"text-indent","text-justify","text-outline","text-shadow",
"text-space-collapse","text-transform","text-underline-position",
"text-wrap","top","transform","transform-origin","transform-style",
"transition","transition-delay","transition-duration",
"transition-property","transition-timing-function","unicode-bidi",
"vertical-align","visibility","voice-balance","voice-duration",
"voice-family","voice-pitch","voice-pitch-range","voice-rate",
"voice-stress","voice-volume","volume","white-space","widows","width",
"word-break","word-spacing","word-wrap","z-index"],
// This helper verifies that a specific string is a known CSS property.
// We include vendor-prefixed known CSS properties, like `-o-transition`.
_knownCSSProperty: function(propertyName) {
propertyName = propertyName.replace(/^-.+?-/,'');
return this.cssProperties.indexOf(propertyName) > -1;
},
// #### The CSS Master Parse Function
//
// Here we process the token stream, assumed to have its pointer inside a
// CSS element, and will try to parse the content inside it as CSS until
// we hit the end of the CSS element.
//
// Any parse errors along the way will result in a `ParseError`
// being thrown.
parse: function() {
// We'll use some instance variables to keep track of our parse
// state:
// * A list of the CSS rulesets for the CSS block.
this.rules = [];
// * A list of comment blocks inside the CSS.
this.comments = [];
// Parsing is based on finite states, and a call
// to `_parseSelector()` will run through any number
// of states until it either throws an error,
// or terminates cleanly.
var sliceStart = this.stream.pos;
this.stream.markTokenStartAfterSpace();
this._parseSelector();
var sliceEnd = this.stream.pos;
// If we get here, the CSS block has no errors,
// and we report the start/end of the CSS block
// in the stream, as well as the rules/comments
// for the calling `HTMLparser` instance to work with.
var cssBlock = {
value: this.stream.text.slice(sliceStart, sliceEnd),
parseInfo: {
start: sliceStart,
end: sliceEnd,
rules: this.rules,
comments: this.comments
}
};
this.rules = null;
this.comments = null;
return cssBlock;
},
// #### CSS Comment Parsing
//
// Here we record the position of comments in *term* in the instance's
// comment list, and return *term* with all its comments stripped.
stripComments: function(term, startPos) {
var pos,
last = term.length,
commentStart, commentEnd,
prev, next,
stripped = "";
for (pos=0; pos < last; pos++) {
if (term[pos] === '/' && pos<last-1 && term[pos+1] === '*') {
commentStart = startPos + pos;
pos += 3;
while(pos < last-1 && term.substr(pos-1,2) !== "*/") {
pos++;
}
if (pos >= last-1 && term.substr(pos-1,2) !== "*/")
throw new ParseError("UNTERMINATED_CSS_COMMENT", commentStart);
commentEnd = startPos + pos + 1;
this.comments.push({start: commentStart, end: commentEnd});
} else {
stripped += term[pos];
}
}
return stripped.trim();
},
// #### CSS Comment Filtering
//
// Here we filter a token so that its start and end positions
// point to the content without leading and trailing comments,
// with comments in the token.value completely removed.
filterComments: function(token) {
var text = token.value,
tsize = text.length,
ntsize,
stripped = this.stripComments(text, token.interval.start);
// strip leading comments
text = text.replace(/^\s+/,"");
text = text.replace(/^\/\*[\w\W]*?\*\/\s*/,'');
ntsize = text.length;
token.interval.start += tsize - ntsize;
// strip trailing comments (=reverse and repeat previous)
tsize = ntsize;
text = text.split('').reverse().join('');
text = text.replace(/^\s+/,"");
text = text.replace(/^\/\*[\w\W]*?\*\/\s*/,'');
// FIXME: this still fails comments like this: /* ... /* ... */,
// which is a single block. The problems is that in the
// reversed string this looks like /* ... */ ... */ which
// counts as one block plus left-over junk.
ntsize = text.length;
token.interval.end -= tsize - ntsize;
// commit text change
token.value = stripped;
},
// #### CSS Selector Parsing
//
// A selector is a string, and terminates on `{`, which signals
// the start of a CSS property/value pair (which may be empty).
//
// There are a few characters in selectors that are an immediate error:
//
// * `;` Rule terminator (ERROR: missing block opener)
// * `}` End of css block (ERROR: missing block opener)
// * `<` End of `<style>` element, start of `</style>`
// (ERROR: css declaration has no body)
//
// Note that we cannot flag `:` as an error because pseudo-classes use
// it as their prefix.
_parseSelector: function() {
// Depending on our state, we may be coming from having just parsed
// a rule. If that's the case, add it to our list of rules.
if (this.currentRule) {
this.rules.push(this.currentRule);
this.currentRule = null;
}
// Gobble all characters that could be part of the selector.
this.stream.eatCSSWhile(/[^\{;\}<]/);
var token = this.stream.makeToken(),
peek = this.stream.peek();
// If there was nothing to select, we're either done,
// or an error occurred.
if (token === null) {
if (!this.stream.end() && this.stream.peek() === '<') {
// if this is the start of <!-- make sure to throw an error
if (this.stream.substream(2) !== "</") {
throw new ParseError("HTML_CODE_IN_CSS_BLOCK", this, this.stream.pos-1,
this.stream.pos);
}
return;
}
throw new ParseError("MISSING_CSS_SELECTOR", this, this.stream.pos-1,
this.stream.pos);
}
// If we get here, we have a selector string.
// Filter the token for comments before continueing.
this.filterComments(token);
var selector = token.value,
selectorStart = token.interval.start,
selectorEnd = token.interval.end;
if (selector === '') {
this._parseSelector();
return;
}
// Now we'll set up a ruleset object for this selector.
this.currentRule = {
selector: {
value: selector,
start: selectorStart,
end: selectorEnd
},
declarations: {
start: null,
end: null,
properties: []
}
};
// Now we start to analyse whether we can continue,
// or whether we're in a terminal state, based on the
// next character in the stream.
if (this.stream.end() || peek === '<') {
throw new ParseError("UNFINISHED_CSS_SELECTOR", this, selectorStart,
selectorEnd, selector);
}
if (!this.stream.end()) {
var next = this.stream.next(),
errorMsg = "[_parseSelector] Expected {, }, ; or :, " +
"instead found " + next;
if (next === '{') {
// The only legal continuation after a selector is the opening
// `{` character. If that's the character we see, we can mark the
// start of the declarations block and start parsing them.
this.currentRule.declarations.start = this.stream.pos-1;
this._parseDeclaration(selector, selectorStart);
} else if (next === ';' || next === '}') {
// Otherwise, this is a parse error; we should have seen `{`
// instead.
throw new ParseError("MISSING_CSS_BLOCK_OPENER", this,
selectorStart, selectorEnd, selector);
} else {
// We get here if an unexpected character was found.
throw new ParseError("UNCAUGHT_CSS_PARSE_ERROR", this,
token.interval.start, token.interval.end,
errorMsg);
}
} else {
// If the stream ended after the selector, we want the user to follow
// up with `{`.
throw new ParseError("MISSING_CSS_BLOCK_OPENER", this, selectorStart,
selectorEnd, selector);
}
},
// #### CSS Declaration Parsing
//
// A declaration is a `property: value;` pair. It can be empty,
// in which case the next character must be `}`.
_parseDeclaration: function(selector, selectorStart, value) {
// First, we forward the stream to the next non-space character.
this.stream.markTokenStartAfterSpace();
var peek = this.stream.peek();
if (peek === '}') {
// If the next character is `}` then this is an empty block, and we
// should move on to trying to read a new selector ruleset.
this.stream.next();
this.currentRule.declarations.end = this.stream.pos;
this.stream.markTokenStartAfterSpace();
this._parseSelector();
}
// Administratively important: there are two ways for this function
// to have been called. One is from `_parseSelector()`, which is
// "the normal way", the other from `_parseValue()`, after finding a
// properly closed `property:value;` pair. In this case *value* will be
// the last declaration's value, which will let us throw a sensible
// debug error in case the stream is empty at this point, or points to
// `</style>`.
else if (value && (this.stream.end() || peek === '<')) {
throw new ParseError("MISSING_CSS_BLOCK_CLOSER", this, selectorStart,
selectorStart+value.length, value);
}
// If we're still in this function at this point, all is well
// and we can move on to property parsing.
else {
this._parseProperty(selector, selectorStart);
}
},
// #### CSS Property Parsing
// There is a fixed list of CSS properties, and we must check two things:
//
// 1. Does the token string contain a syntax-legal property?
// 2. Is that property in the set of known ones?
//
// Properties are terminated by `:`, but we might also see the following
// characters, which should signal an error:
//
// * `;` rule terminator (ERROR: missing value)
// * `}` end of CSS block (ERROR: missing value)
// * `<` end of `<style>` element, start of `</style>`
// (ERROR: missing value)
_parseProperty: function(selector, selectorStart) {
var property = this.stream.eatCSSWhile(/[^\{\}<;:]/),
token = this.stream.makeToken();
if (token === null) {
throw new ParseError("MISSING_CSS_PROPERTY", this, selectorStart,
selectorStart + selector.length, selector);
}
this.filterComments(token)
var property = token.value,
propertyStart = token.interval.start,
propertyEnd = token.interval.end;
if (property === '') {
this._parseDeclaration(selector, selectorStart);
return;
}
var next = this.stream.next(),
errorMsg = "[_parseProperty] Expected }, {, <, ; or :, " +
"instead found " + next;
if (next === '{') {
throw new ParseError("MISSING_CSS_BLOCK_CLOSER", this, selectorStart,
propertyStart, selector);
}
if ((this.stream.end() && next !== ':') || next === '<' ||
next === '}') {
throw new ParseError("UNFINISHED_CSS_PROPERTY", this, propertyStart,
propertyEnd, property);
}
// We record `property: value` pairs as we run through the stream,
// which are added to the set of `property: value` pairs in the
// instance's `rules.properties` array. The push happens when we have a
// clean run in `_parseValue()`.
this.currentProperty = {
name: {
value: property,
start: propertyStart,
end: propertyEnd
}
};
// If we find a colon, we have a property and now need a value to go
// along with it.
if (next === ':') {
// Before we continue, we must make sure the string we found is a real
// CSS property.
if (!( property && property.match(/^[a-z\-]+$/)) ||
!this._knownCSSProperty(property))
throw new ParseError("INVALID_CSS_PROPERTY_NAME", this,
propertyStart, propertyEnd, property);
this.stream.markTokenStartAfterSpace();
this._parseValue(selector, selectorStart, property, propertyStart);
}
// Otherwise, anything else at this point constitutes an error.
else if (next === ';') {
throw new ParseError("MISSING_CSS_VALUE", this, propertyStart,
propertyEnd, property);
}
else {
throw new ParseError("UNCAUGHT_CSS_PARSE_ERROR", this,
token.interval.start, token.interval.end,
errorMsg);
}
},
// #### CSS Value Parsing
//
// A value must end either in `;` or in `}`. However, we may also find:
//
// * `<` end of `<style>` element, start of `</style>`
// (ERROR: missing block closer)
_parseValue: function(selector, selectorStart, property, propertyStart) {
var rule = this.stream.eatCSSWhile(/[^}<;]/),
token = this.stream.makeToken();
if(token === null) {
throw new ParseError("MISSING_CSS_VALUE", this, propertyStart,
propertyStart+property.length, property);
}
var next = (!this.stream.end() ? this.stream.next() : "end of stream"),
errorMsg = "[_parseValue] Expected }, <, or ;, instead found "+next;
this.filterComments(token);
var value = token.value,
valueStart = token.interval.start,
valueEnd = token.interval.end;
if (value === '') {
throw new ParseError("MISSING_CSS_VALUE", this, this.stream.pos-1,
this.stream.pos);
}
// At this point we can fill in the *value* part of the current
// `property: value;` pair. However, we hold off binding it until
// we are sure there are no parse errors.
this.currentProperty.value = {
value: value,
start: valueStart,
end: valueEnd
}
if ((this.stream.end() && next !== ';') || next === '<') {
throw new ParseError("UNFINISHED_CSS_VALUE", this, valueStart,
valueEnd, value);
}
if (next === ';') {
// This is normal CSS rule termination; try to read a new
// property/value pair.
this._bindCurrentRule();
this.stream.markTokenStartAfterSpace();
this._parseDeclaration(selector, valueStart, value);
}
else if (next === '}') {
// This is block level termination; try to read a new selector.
this.currentRule.declarations.end = this.stream.pos;
this._bindCurrentRule();
this.stream.markTokenStartAfterSpace();
this._parseSelector();
}
else {
throw new ParseError("UNCAUGHT_CSS_PARSE_ERROR", this,
token.interval.start, token.interval.end,
errorMsg);
}
},
// This helper function binds the currrent `property: value` object
// in the current ruleset, and resets it for the next selector block.
_bindCurrentRule: function() {
this.currentRule.declarations.properties.push(this.currentProperty);
this.currentProperty = null;
}
}
// ### HTML Parsing
//
// The HTML token stream parser object has references to the stream,
// as well as a DOM builder that is used to construct the DOM while we
// run through the token stream.
function HTMLParser(stream, domBuilder) {
this.stream = stream;
this.domBuilder = domBuilder;
this.cssParser = new CSSParser(stream, domBuilder);
}
HTMLParser.prototype = {
// since SVG requires a slightly different code path,
// we need to track whether we're in HTML or SVG mode.
parsingSVG: false,
// For SVG DOM elements, we need to know the SVG namespace.
svgNameSpace: "http://www.w3.org/2000/svg",
// HTML5 documents have a special doctype that we must use
html5Doctype: "<!DOCTYPE html>",
// Void HTML elements are the ones that don't need to have a closing tag.
voidHtmlElements: ["area", "base", "br", "col", "command", "embed", "hr",
"img", "input", "keygen", "link", "meta", "param",
"source", "track", "wbr"],
// We keep a list of all valid HTML5 elements.
htmlElements: ["a", "abbr", "address", "area", "article", "aside",
"audio", "b", "base", "bdi", "bdo", "bgsound", "blink",
"blockquote", "body", "br", "button", "canvas", "caption",
"cite", "code", "col", "colgroup", "command", "datalist",
"dd", "del", "details", "dfn", "div", "dl", "dt", "em",
"embed", "fieldset", "figcaption", "figure", "footer",
"form", "frame", "frameset", "h1", "h2", "h3", "h4", "h5",
"h6", "head", "header", "hgroup", "hr", "html", "i",
"iframe", "img", "input", "ins", "kbd", "keygen", "label",
"legend", "li", "link", "map", "mark", "marquee", "menu",
"meta", "meter", "nav", "nobr", "noscript", "object", "ol",
"optgroup", "option", "output", "p", "param", "pre",
"progress", "q", "rp", "rt", "ruby", "samp", "script",
"section", "select", "small", "source", "spacer", "span",
"strong", "style", "sub", "summary", "sup", "svg", "table",
"tbody", "td", "textarea", "tfoot", "th", "thead", "time",
"title", "tr", "track", "u", "ul", "var", "video", "wbr"],
// HTML5 allows SVG elements
svgElements: ["a", "altglyph", "altglyphdef", "altglyphitem", "animate",
"animatecolor", "animatemotion", "animatetransform", "circle",
"clippath", "color-profile", "cursor", "defs", "desc",
"ellipse", "feblend", "fecolormatrix", "fecomponenttransfer",
"fecomposite", "feconvolvematrix", "fediffuselighting",
"fedisplacementmap", "fedistantlight", "feflood", "fefunca",
"fefuncb", "fefuncg", "fefuncr", "fegaussianblur", "feimage",
"femerge", "femergenode", "femorphology", "feoffset",
"fepointlight", "fespecularlighting", "fespotlight",
"fetile", "feturbulence", "filter", "font", "font-face",
"font-face-format", "font-face-name", "font-face-src",
"font-face-uri", "foreignobject", "g", "glyph", "glyphref",
"hkern", "image", "line", "lineargradient", "marker", "mask",
"metadata", "missing-glyph", "mpath", "path", "pattern",
"polygon", "polyline", "radialgradient", "rect", "script",
"set", "stop", "style", "svg", "switch", "symbol", "text",
"textpath", "title", "tref", "tspan", "use", "view", "vkern"],
// We also keep a list of HTML elements that are now obsolete, but
// may still be encountered in the wild on popular sites.
obsoleteHtmlElements: ["acronym", "applet", "basefont", "big", "center",
"dir", "font", "isindex", "listing", "noframes",
"plaintext", "s", "strike", "tt", "xmp"],
// This is a helper function to determine whether a given string
// is a legal HTML5 element tag.