-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathTokenStream.js
executable file
·1047 lines (906 loc) · 33.4 KB
/
TokenStream.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";
module.exports = TokenStream;
var TokenStreamBase = require("../util/TokenStreamBase");
var PropertyValuePart = require("./PropertyValuePart");
var Tokens = require("./Tokens");
var h = /^[0-9a-fA-F]$/,
nonascii = /^[\u00A0-\uFFFF]$/,
nl = /\n|\r\n|\r|\f/,
whitespace = /\u0009|\u000a|\u000c|\u000d|\u0020/;
//-----------------------------------------------------------------------------
// Helper functions
//-----------------------------------------------------------------------------
function isHexDigit(c) {
return c !== null && h.test(c);
}
function isDigit(c) {
return c !== null && /\d/.test(c);
}
function isWhitespace(c) {
return c !== null && whitespace.test(c);
}
function isNewLine(c) {
return c !== null && nl.test(c);
}
function isNameStart(c) {
return c !== null && /[a-z_\u00A0-\uFFFF\\]/i.test(c);
}
function isNameChar(c) {
return c !== null && (isNameStart(c) || /[0-9\-\\]/.test(c));
}
function isIdentStart(c) {
return c !== null && (isNameStart(c) || /-\\/.test(c));
}
function mix(receiver, supplier) {
for (var prop in supplier) {
if (Object.prototype.hasOwnProperty.call(supplier, prop)) {
receiver[prop] = supplier[prop];
}
}
return receiver;
}
//-----------------------------------------------------------------------------
// CSS Token Stream
//-----------------------------------------------------------------------------
/**
* A token stream that produces CSS tokens.
* @param {String|Reader} input The source of text to tokenize.
* @constructor
* @class TokenStream
* @namespace parserlib.css
*/
function TokenStream(input) {
TokenStreamBase.call(this, input, Tokens);
}
TokenStream.prototype = mix(new TokenStreamBase(), {
/**
* Overrides the TokenStreamBase method of the same name
* to produce CSS tokens.
* @return {Object} A token object representing the next token.
* @method _getToken
* @private
*/
_getToken: function() {
var c,
reader = this._reader,
token = null,
startLine = reader.getLine(),
startCol = reader.getCol();
c = reader.read();
while (c) {
switch (c) {
/*
* Potential tokens:
* - COMMENT
* - SLASH
* - CHAR
*/
case "/":
if (reader.peek() === "*") {
token = this.commentToken(c, startLine, startCol);
} else {
token = this.charToken(c, startLine, startCol);
}
break;
/*
* Potential tokens:
* - DASHMATCH
* - INCLUDES
* - PREFIXMATCH
* - SUFFIXMATCH
* - SUBSTRINGMATCH
* - CHAR
*/
case "|":
case "~":
case "^":
case "$":
case "*":
if (reader.peek() === "=") {
token = this.comparisonToken(c, startLine, startCol);
} else {
token = this.charToken(c, startLine, startCol);
}
break;
/*
* Potential tokens:
* - STRING
* - INVALID
*/
case "\"":
case "'":
token = this.stringToken(c, startLine, startCol);
break;
/*
* Potential tokens:
* - HASH
* - CHAR
*/
case "#":
if (isNameChar(reader.peek())) {
token = this.hashToken(c, startLine, startCol);
} else {
token = this.charToken(c, startLine, startCol);
}
break;
/*
* Potential tokens:
* - DOT
* - NUMBER
* - DIMENSION
* - PERCENTAGE
*/
case ".":
if (isDigit(reader.peek())) {
token = this.numberToken(c, startLine, startCol);
} else {
token = this.charToken(c, startLine, startCol);
}
break;
/*
* Potential tokens:
* - CDC
* - MINUS
* - NUMBER
* - DIMENSION
* - PERCENTAGE
*/
case "-":
if (reader.peek() === "-") { // could be closing HTML-style comment
token = this.htmlCommentEndToken(c, startLine, startCol);
} else if (isNameStart(reader.peek())) {
token = this.identOrFunctionToken(c, startLine, startCol);
} else {
token = this.charToken(c, startLine, startCol);
}
break;
/*
* Potential tokens:
* - IMPORTANT_SYM
* - CHAR
*/
case "!":
token = this.importantToken(c, startLine, startCol);
break;
/*
* Any at-keyword or CHAR
*/
case "@":
token = this.atRuleToken(c, startLine, startCol);
break;
/*
* Potential tokens:
* - NOT
* - CHAR
*/
case ":":
token = this.notToken(c, startLine, startCol);
break;
/*
* Potential tokens:
* - CDO
* - CHAR
*/
case "<":
token = this.htmlCommentStartToken(c, startLine, startCol);
break;
/*
* Potential tokens:
* - IDENT
* - CHAR
*/
case "\\":
if (/[^\r\n\f]/.test(reader.peek())) {
token = this.identOrFunctionToken(this.readEscape(c, true), startLine, startCol);
} else {
token = this.charToken(c, startLine, startCol);
}
break;
/*
* Potential tokens:
* - UNICODE_RANGE
* - URL
* - CHAR
*/
case "U":
case "u":
if (reader.peek() === "+") {
token = this.unicodeRangeToken(c, startLine, startCol);
break;
}
/* falls through */
default:
/*
* Potential tokens:
* - NUMBER
* - DIMENSION
* - LENGTH
* - FREQ
* - TIME
* - EMS
* - EXS
* - ANGLE
*/
if (isDigit(c)) {
token = this.numberToken(c, startLine, startCol);
} else
/*
* Potential tokens:
* - S
*/
if (isWhitespace(c)) {
token = this.whitespaceToken(c, startLine, startCol);
} else
/*
* Potential tokens:
* - IDENT
*/
if (isIdentStart(c)) {
token = this.identOrFunctionToken(c, startLine, startCol);
} else {
/*
* Potential tokens:
* - CHAR
* - PLUS
*/
token = this.charToken(c, startLine, startCol);
}
}
// make sure this token is wanted
// TODO: check channel
break;
}
if (!token && c === null) {
token = this.createToken(Tokens.EOF, null, startLine, startCol);
}
return token;
},
//-------------------------------------------------------------------------
// Methods to create tokens
//-------------------------------------------------------------------------
/**
* Produces a token based on available data and the current
* reader position information. This method is called by other
* private methods to create tokens and is never called directly.
* @param {int} tt The token type.
* @param {String} value The text value of the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @param {Object} options (Optional) Specifies a channel property
* to indicate that a different channel should be scanned
* and/or a hide property indicating that the token should
* be hidden.
* @return {Object} A token object.
* @method createToken
*/
createToken: function(tt, value, startLine, startCol, options) {
var reader = this._reader;
options = options || {};
return {
value: value,
type: tt,
channel: options.channel,
endChar: options.endChar,
hide: options.hide || false,
startLine: startLine,
startCol: startCol,
endLine: reader.getLine(),
endCol: reader.getCol()
};
},
//-------------------------------------------------------------------------
// Methods to create specific tokens
//-------------------------------------------------------------------------
/**
* Produces a token for any at-rule. If the at-rule is unknown, then
* the token is for a single "@" character.
* @param {String} first The first character for the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method atRuleToken
*/
atRuleToken: function(first, startLine, startCol) {
var rule = first,
reader = this._reader,
tt = Tokens.CHAR,
ident;
/*
* First, mark where we are. There are only four @ rules,
* so anything else is really just an invalid token.
* Basically, if this doesn't match one of the known @
* rules, just return '@' as an unknown token and allow
* parsing to continue after that point.
*/
reader.mark();
// try to find the at-keyword
ident = this.readName();
rule = first + ident;
tt = Tokens.type(rule.toLowerCase());
// if it's not valid, use the first character only and reset the reader
if (tt === Tokens.CHAR || tt === Tokens.UNKNOWN) {
if (rule.length > 1) {
tt = Tokens.UNKNOWN_SYM;
} else {
tt = Tokens.CHAR;
rule = first;
reader.reset();
}
}
return this.createToken(tt, rule, startLine, startCol);
},
/**
* Produces a character token based on the given character
* and location in the stream. If there's a special (non-standard)
* token name, this is used; otherwise CHAR is used.
* @param {String} c The character for the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method charToken
*/
charToken: function(c, startLine, startCol) {
var tt = Tokens.type(c);
var opts = {};
if (tt === -1) {
tt = Tokens.CHAR;
} else {
opts.endChar = Tokens[tt].endChar;
}
return this.createToken(tt, c, startLine, startCol, opts);
},
/**
* Produces a character token based on the given character
* and location in the stream. If there's a special (non-standard)
* token name, this is used; otherwise CHAR is used.
* @param {String} first The first character for the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method commentToken
*/
commentToken: function(first, startLine, startCol) {
var comment = this.readComment(first);
return this.createToken(Tokens.COMMENT, comment, startLine, startCol);
},
/**
* Produces a comparison token based on the given character
* and location in the stream. The next character must be
* read and is already known to be an equals sign.
* @param {String} c The character for the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method comparisonToken
*/
comparisonToken: function(c, startLine, startCol) {
var reader = this._reader,
comparison = c + reader.read(),
tt = Tokens.type(comparison) || Tokens.CHAR;
return this.createToken(tt, comparison, startLine, startCol);
},
/**
* Produces a hash token based on the specified information. The
* first character provided is the pound sign (#) and then this
* method reads a name afterward.
* @param {String} first The first character (#) in the hash name.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method hashToken
*/
hashToken: function(first, startLine, startCol) {
var name = this.readName(first);
return this.createToken(Tokens.HASH, name, startLine, startCol);
},
/**
* Produces a CDO or CHAR token based on the specified information. The
* first character is provided and the rest is read by the function to determine
* the correct token to create.
* @param {String} first The first character in the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method htmlCommentStartToken
*/
htmlCommentStartToken: function(first, startLine, startCol) {
var reader = this._reader,
text = first;
reader.mark();
text += reader.readCount(3);
if (text === "<!--") {
return this.createToken(Tokens.CDO, text, startLine, startCol);
} else {
reader.reset();
return this.charToken(first, startLine, startCol);
}
},
/**
* Produces a CDC or CHAR token based on the specified information. The
* first character is provided and the rest is read by the function to determine
* the correct token to create.
* @param {String} first The first character in the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method htmlCommentEndToken
*/
htmlCommentEndToken: function(first, startLine, startCol) {
var reader = this._reader,
text = first;
reader.mark();
text += reader.readCount(2);
if (text === "-->") {
return this.createToken(Tokens.CDC, text, startLine, startCol);
} else {
reader.reset();
return this.charToken(first, startLine, startCol);
}
},
/**
* Produces an IDENT or FUNCTION token based on the specified information. The
* first character is provided and the rest is read by the function to determine
* the correct token to create.
* @param {String} first The first character in the identifier.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method identOrFunctionToken
*/
identOrFunctionToken: function(first, startLine, startCol) {
var reader = this._reader,
ident = this.readName(first),
tt = Tokens.IDENT,
uriFns = ["url(", "url-prefix(", "domain("],
uri;
// if there's a left paren immediately after, it's a URI or function
if (reader.peek() === "(") {
ident += reader.read();
if (uriFns.indexOf(ident.toLowerCase()) > -1) {
reader.mark();
uri = this.readURI(ident);
if (uri === null) {
// didn't find a valid URL or there's no closing paren
reader.reset();
tt = Tokens.FUNCTION;
} else {
tt = Tokens.URI;
ident = uri;
}
} else {
tt = Tokens.FUNCTION;
}
} else if (reader.peek() === ":") { // might be an IE function
// IE-specific functions always begin with progid:
if (ident.toLowerCase() === "progid") {
ident += reader.readTo("(");
tt = Tokens.IE_FUNCTION;
}
}
return this.createToken(tt, ident, startLine, startCol);
},
/**
* Produces an IMPORTANT_SYM or CHAR token based on the specified information. The
* first character is provided and the rest is read by the function to determine
* the correct token to create.
* @param {String} first The first character in the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method importantToken
*/
importantToken: function(first, startLine, startCol) {
var reader = this._reader,
important = first,
tt = Tokens.CHAR,
temp,
c;
reader.mark();
c = reader.read();
while (c) {
// there can be a comment in here
if (c === "/") {
// if the next character isn't a star, then this isn't a valid !important token
if (reader.peek() !== "*") {
break;
} else {
temp = this.readComment(c);
if (temp === "") { // broken!
break;
}
}
} else if (isWhitespace(c)) {
important += c + this.readWhitespace();
} else if (/i/i.test(c)) {
temp = reader.readCount(8);
if (/mportant/i.test(temp)) {
important += c + temp;
tt = Tokens.IMPORTANT_SYM;
}
break; // we're done
} else {
break;
}
c = reader.read();
}
if (tt === Tokens.CHAR) {
reader.reset();
return this.charToken(first, startLine, startCol);
} else {
return this.createToken(tt, important, startLine, startCol);
}
},
/**
* Produces a NOT or CHAR token based on the specified information. The
* first character is provided and the rest is read by the function to determine
* the correct token to create.
* @param {String} first The first character in the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method notToken
*/
notToken: function(first, startLine, startCol) {
var reader = this._reader,
text = first;
reader.mark();
text += reader.readCount(4);
if (text.toLowerCase() === ":not(") {
return this.createToken(Tokens.NOT, text, startLine, startCol);
} else {
reader.reset();
return this.charToken(first, startLine, startCol);
}
},
/**
* Produces a number token based on the given character
* and location in the stream. This may return a token of
* NUMBER, EMS, EXS, LENGTH, ANGLE, TIME, FREQ, DIMENSION,
* or PERCENTAGE.
* @param {String} first The first character for the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method numberToken
*/
numberToken: function(first, startLine, startCol) {
var reader = this._reader,
value = this.readNumber(first),
ident,
tt = Tokens.NUMBER,
c = reader.peek();
if (isIdentStart(c)) {
ident = this.readName(reader.read());
value += ident;
if (/^em$|^ex$|^px$|^gd$|^rem$|^vw$|^vh$|^vmax$|^vmin$|^ch$|^cm$|^mm$|^in$|^pt$|^pc$/i.test(ident)) {
tt = Tokens.LENGTH;
} else if (/^deg|^rad$|^grad$|^turn$/i.test(ident)) {
tt = Tokens.ANGLE;
} else if (/^ms$|^s$/i.test(ident)) {
tt = Tokens.TIME;
} else if (/^hz$|^khz$/i.test(ident)) {
tt = Tokens.FREQ;
} else if (/^dpi$|^dpcm$/i.test(ident)) {
tt = Tokens.RESOLUTION;
} else {
tt = Tokens.DIMENSION;
}
} else if (c === "%") {
value += reader.read();
tt = Tokens.PERCENTAGE;
}
return this.createToken(tt, value, startLine, startCol);
},
/**
* Produces a string token based on the given character
* and location in the stream. Since strings may be indicated
* by single or double quotes, a failure to match starting
* and ending quotes results in an INVALID token being generated.
* The first character in the string is passed in and then
* the rest are read up to and including the final quotation mark.
* @param {String} first The first character in the string.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method stringToken
*/
stringToken: function(first, startLine, startCol) {
var delim = first,
string = first,
reader = this._reader,
tt = Tokens.STRING,
c = reader.read(),
i;
while (c) {
string += c;
if (c === "\\") {
c = reader.read();
if (c === null) {
break; // premature EOF after backslash
} else if (/[^\r\n\f0-9a-f]/i.test(c)) {
// single-character escape
string += c;
} else {
// read up to six hex digits
for (i = 0; isHexDigit(c) && i < 6; i++) {
string += c;
c = reader.read();
}
// swallow trailing newline or space
if (c === "\r" && reader.peek() === "\n") {
string += c;
c = reader.read();
}
if (isWhitespace(c)) {
string += c;
} else {
// This character is null or not part of the escape;
// jump back to the top to process it.
continue;
}
}
} else if (c === delim) {
break; // delimiter found.
} else if (isNewLine(reader.peek())) {
// newline without an escapement: it's an invalid string
tt = Tokens.INVALID;
break;
}
c = reader.read();
}
// if c is null, that means we're out of input and the string was never closed
if (c === null) {
tt = Tokens.INVALID;
}
return this.createToken(tt, string, startLine, startCol);
},
unicodeRangeToken: function(first, startLine, startCol) {
var reader = this._reader,
value = first,
temp,
tt = Tokens.CHAR;
// then it should be a unicode range
if (reader.peek() === "+") {
reader.mark();
value += reader.read();
value += this.readUnicodeRangePart(true);
// ensure there's an actual unicode range here
if (value.length === 2) {
reader.reset();
} else {
tt = Tokens.UNICODE_RANGE;
// if there's a ? in the first part, there can't be a second part
if (value.indexOf("?") === -1) {
if (reader.peek() === "-") {
reader.mark();
temp = reader.read();
temp += this.readUnicodeRangePart(false);
// if there's not another value, back up and just take the first
if (temp.length === 1) {
reader.reset();
} else {
value += temp;
}
}
}
}
}
return this.createToken(tt, value, startLine, startCol);
},
/**
* Produces a S token based on the specified information. Since whitespace
* may have multiple characters, this consumes all whitespace characters
* into a single token.
* @param {String} first The first character in the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method whitespaceToken
*/
whitespaceToken: function(first, startLine, startCol) {
var value = first + this.readWhitespace();
return this.createToken(Tokens.S, value, startLine, startCol);
},
//-------------------------------------------------------------------------
// Methods to read values from the string stream
//-------------------------------------------------------------------------
readUnicodeRangePart: function(allowQuestionMark) {
var reader = this._reader,
part = "",
c = reader.peek();
// first read hex digits
while (isHexDigit(c) && part.length < 6) {
reader.read();
part += c;
c = reader.peek();
}
// then read question marks if allowed
if (allowQuestionMark) {
while (c === "?" && part.length < 6) {
reader.read();
part += c;
c = reader.peek();
}
}
// there can't be any other characters after this point
return part;
},
readWhitespace: function() {
var reader = this._reader,
whitespace = "",
c = reader.peek();
while (isWhitespace(c)) {
reader.read();
whitespace += c;
c = reader.peek();
}
return whitespace;
},
readNumber: function(first) {
var reader = this._reader,
number = first,
hasDot = first === ".",
c = reader.peek();
while (c) {
if (isDigit(c)) {
number += reader.read();
} else if (c === ".") {
if (hasDot) {
break;
} else {
hasDot = true;
number += reader.read();
}
} else {
break;
}
c = reader.peek();
}
return number;
},
// returns null w/o resetting reader if string is invalid.
readString: function() {
var token = this.stringToken(this._reader.read(), 0, 0);
return token.type === Tokens.INVALID ? null : token.value;
},
// returns null w/o resetting reader if URI is invalid.
readURI: function(first) {
var reader = this._reader,
uri = first,
inner = "",
c = reader.peek();
// skip whitespace before
while (c && isWhitespace(c)) {
reader.read();
c = reader.peek();
}
// it's a string
if (c === "'" || c === "\"") {
inner = this.readString();
if (inner !== null) {
inner = PropertyValuePart.parseString(inner);
}
} else {
inner = this.readUnquotedURL();
}
c = reader.peek();
// skip whitespace after
while (c && isWhitespace(c)) {
reader.read();
c = reader.peek();
}
// if there was no inner value or the next character isn't closing paren, it's not a URI
if (inner === null || c !== ")") {
uri = null;
} else {
// Ensure argument to URL is always double-quoted
// (This simplifies later processing in PropertyValuePart.)
uri += PropertyValuePart.serializeString(inner) + reader.read();
}
return uri;
},
// This method never fails, although it may return an empty string.
readUnquotedURL: function(first) {
var reader = this._reader,
url = first || "",
c;
for (c = reader.peek(); c; c = reader.peek()) {
// Note that the grammar at
// https://www.w3.org/TR/CSS2/grammar.html#scanner
// incorrectly includes the backslash character in the
// `url` production, although it is correctly omitted in
// the `baduri1` production.
if (nonascii.test(c) || /^[-!#$%&*-[\]-~]$/.test(c)) {
url += c;
reader.read();
} else if (c === "\\") {
if (/^[^\r\n\f]$/.test(reader.peek(2))) {
url += this.readEscape(reader.read(), true);
} else {
break; // bad escape sequence.
}
} else {
break; // bad character
}
}
return url;
},
readName: function(first) {
var reader = this._reader,
ident = first || "",
c;
for (c = reader.peek(); c; c = reader.peek()) {
if (c === "\\") {
if (/^[^\r\n\f]$/.test(reader.peek(2))) {
ident += this.readEscape(reader.read(), true);
} else {
// Bad escape sequence.
break;
}
} else if (isNameChar(c)) {
ident += reader.read();
} else {
break;
}
}
return ident;
},
readEscape: function(first, unescape) {
var reader = this._reader,
cssEscape = first || "",
i = 0,
c = reader.peek();
if (isHexDigit(c)) {
do {
cssEscape += reader.read();
c = reader.peek();
} while (c && isHexDigit(c) && ++i < 6);
}
if (cssEscape.length === 1) {
if (/^[^\r\n\f0-9a-f]$/.test(c)) {
reader.read();
if (unescape) {
return c;
}
} else {
// We should never get here (readName won't call readEscape