-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
1776 lines (1650 loc) · 45.1 KB
/
index.ts
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
class Unreachable extends Error {
name = "Unreachable";
constructor() {
super("Parser reached an unreachable state! This is a bug.");
}
}
export function unreachable(): never {
throw new Unreachable();
}
export class Position {
constructor(public offset = -1, public line = -1, public column = -1) {}
clone() {
return new Position(this.offset, this.line, this.column);
}
}
export class Debug {
public from: Position;
public to: Position;
constructor(from?: Position, to?: Position) {
// prevent reuse
this.from = from ? from.clone() : new Position();
this.to = to ? to.clone() : new Position();
}
clone() {
return new Debug(this.from, this.to);
}
}
const dbg = (from?: Position, to?: Position) => new Debug(from, to);
function char(str: string) {
return str.charCodeAt(0);
}
function def(code?: number): code is number {
return code !== null;
}
function between(num: number | undefined, first: number, last: number): num is number {
return def(num) && num >= first && num <= last;
}
function digit(code: number) {
return between(code, 0x30, 0x39);
}
function hexdigit(code: number): code is number {
if (code == undefined) return false;
return digit(code) || between(code, 0x41, 0x46) || between(code, 0x61, 0x66);
}
function uppercaseletter(code: number) {
return between(code, 0x41, 0x5a);
}
function lowercaseletter(code: number) {
return between(code, 0x61, 0x7a);
}
function letter(code: number) {
return uppercaseletter(code) || lowercaseletter(code);
}
function nonascii(code: number) {
return (
code == 0xb7 ||
between(code, 0xc0, 0xd6) ||
between(code, 0xd8, 0xf6) ||
between(code, 0xf8, 0x37d) ||
between(code, 0x37f, 0x1fff) ||
code == 0x200c ||
code == 0x200d ||
code == 0x203f ||
code == 0x2040 ||
between(code, 0x2070, 0x218f) ||
between(code, 0x2c00, 0x2fef) ||
between(code, 0x3001, 0xd7ff) ||
between(code, 0xf900, 0xfdcf) ||
between(code, 0xfdf0, 0xfffd) ||
code >= 0x10000
);
}
function namestartchar(code: number) {
return def(code) && (letter(code) || nonascii(code) || code == 0x5f);
}
function namechar(code: number) {
return namestartchar(code) || digit(code) || code == 0x2d;
}
function nonprintable(code: number) {
return between(code, 0, 8) || code == 0xb || between(code, 0xe, 0x1f) || code == 0x7f;
}
function newline(code: number) {
return code == 0xa;
}
function whitespace(code: number) {
return newline(code) || code == 9 || code == 0x20;
}
function badescape(code: number) {
return newline(code) || isNaN(code);
}
function surrogate(code: number) {
return between(code, 0xd800, 0xdfff);
}
const maximumallowedcodepoint = 0x10ffff;
class InvalidCharacterError extends Error {
constructor(public message: string) {
super();
this.name = "InvalidCharacterError";
}
}
/** @see https://drafts.csswg.org/css-syntax/#input-preprocessing */
function preprocess(str: string) {
// Turn a string into an array of code points,
// following the preprocessing cleanup rules.
const codepoints = [];
for (let i = 0; i < str.length; i++) {
let code = str.charCodeAt(i);
if (code == 0xd && str.charCodeAt(i + 1) == 0xa) {
code = 0xa;
i++;
}
if (code == 0xd || code == 0xc) code = 0xa;
if (code == 0x0) code = 0xfffd;
if (between(code, 0xd800, 0xdbff) && between(str.charCodeAt(i + 1), 0xdc00, 0xdfff)) {
// Decode a surrogate pair into an astral codepoint.
const lead = code - 0xd800;
const trail = str.charCodeAt(i + 1) - 0xdc00;
code = Math.pow(2, 16) + lead * Math.pow(2, 10) + trail;
i++;
}
codepoints.push(code);
}
return codepoints;
}
function asciiCaselessMatch(s1: string, s2: string) {
return s1.toLowerCase() == s2.toLowerCase();
}
/** @see https://drafts.csswg.org/css-syntax/#tokenization */
function tokenise(str: string, opts?: ParseOpts) {
const codepoints = preprocess(str);
let offset = -1;
const tokens: CSSParserToken[] = [];
let code: number;
// Line number information.
const position = new Position(0, 1, 1);
// The only use of lastLineLength is in reconsume().
let lastLineLength = 0;
function incrLineno() {
position.line += 1;
lastLineLength = position.column;
position.column = 0;
}
// unsure why this exists
const locstart: Position = position.clone();
function codepoint(i: number): number {
if (i >= codepoints.length) return -1;
// verified above that codepoints cannot return undefined
return codepoints[i] as number;
}
function next(num: number = 1) {
if (num > 3) {
throw "Spec Error: no more than three codepoints of lookahead.";
}
return codepoint(offset + num);
}
function consume(num: number = 1) {
offset += num;
position.offset = offset;
code = codepoint(offset);
if (newline(code)) incrLineno();
else position.column += num;
//console.log('Consume '+i+' '+String.fromCharCode(code) + ' 0x' + code.toString(16));
return true;
}
function reconsume() {
offset -= 1;
position.offset = offset;
if (newline(code)) {
position.line -= 1;
position.column = lastLineLength;
} else {
position.column -= 1;
}
locstart.line = position.line;
locstart.column = position.column;
return true;
}
function eof(codepoint?: number) {
if (codepoint === undefined) codepoint = code;
return codepoint == -1;
}
function donothing() {}
function parseerror() {
if (code == null) {
console.log("Parse error at index " + offset + ", processing codepoint undefined.");
} else {
console.log("Parse error at index " + offset + ", processing codepoint 0x" + code.toString(16) + ".");
}
return true;
}
/** @see https://drafts.csswg.org/css-syntax/#consume-token */
function consumeAToken(opts?: ParseOpts) {
consumeComments();
const from = position.clone();
consume();
if (whitespace(code)) {
while (whitespace(next())) consume();
const to = position.clone();
return new WhitespaceToken(dbg(from, to));
} else if (code == 0x22) return consumeAStringToken(from);
else if (code == 0x23) {
if (namechar(next()) || areAValidEscape(next(1), next(2))) {
const isIdent = wouldStartAnIdentifier(next(1), next(2), next(3));
const name = consumeAName();
const to = position.clone();
return new HashToken(name, isIdent, dbg(from, to));
} else return new DelimToken(code, dbg(from));
} else if (code == 0x27) return consumeAStringToken(from);
else if (code == 0x28) {
const to = position.clone();
return new OpenParenToken(dbg(from, to));
} else if (code == 0x29) {
const to = position.clone();
return new CloseParenToken(dbg(from, to));
} else if (code == 0x2b) {
if (startsWithANumber()) {
reconsume();
return consumeANumericToken(from);
} else {
const to = position.clone();
return new DelimToken(code, dbg(from, to));
}
} else if (code == 0x2c) {
const to = position.clone();
return new CommaToken(dbg(from, to));
} else if (code == 0x2d) {
if (startsWithANumber()) {
reconsume();
return consumeANumericToken(from);
} else if (next(1) == 0x2d && next(2) == 0x3e) {
consume(2);
const to = position.clone();
return new CDCToken(dbg(from, to));
} else if (startsWithAnIdentifier()) {
reconsume();
return consumeAnIdentlikeToken(from);
} else {
const to = position.clone();
return new DelimToken(code, dbg(from, to));
}
} else if (code == 0x2e) {
if (startsWithANumber()) {
reconsume();
return consumeANumericToken(from);
} else {
const to = position.clone();
return new DelimToken(code, dbg(from, to));
}
} else if (code == 0x3a) {
const to = position.clone();
return new ColonToken(dbg(from, to));
} else if (code == 0x3b) {
const to = position.clone();
return new SemicolonToken(dbg(from, to));
} else if (code == 0x3c) {
if (next(1) == 0x21 && next(2) == 0x2d && next(3) == 0x2d) {
consume(3);
const to = position.clone();
return new CDOToken(dbg(from, to));
} else {
const to = position.clone();
return new DelimToken(code, dbg(from, to));
}
} else if (code == 0x40) {
if (wouldStartAnIdentifier(next(1), next(2), next(3))) {
const name = consumeAName();
const to = position.clone();
return new AtKeywordToken(name, dbg(from, to));
} else {
const to = position.clone();
return new DelimToken(code, dbg(from, to));
}
} else if (code == 0x5b) {
const to = position.clone();
return new OpenSquareToken(dbg(from, to));
} else if (code == 0x5c) {
if (startsWithAValidEscape()) {
reconsume();
return consumeAnIdentlikeToken(from);
} else {
parseerror();
const to = position.clone();
return new DelimToken(code, dbg(from, to));
}
} else if (code == 0x5d) {
const to = position.clone();
return new CloseSquareToken(dbg(from, to));
} else if (code == 0x7b) {
const to = position.clone();
return new OpenCurlyToken(dbg(from, to));
} else if (code == 0x7d) {
const to = position.clone();
return new CloseCurlyToken(dbg(from, to));
} else if (digit(code)) {
reconsume();
return consumeANumericToken(from);
} else if (opts?.unicodeRangesAllowed && (code == 0x55 || code == 0x75)) {
if (wouldStartAUnicodeRange(code, next(1), next(2))) {
reconsume();
return consumeAUnicodeRangeToken(from);
}
reconsume();
return consumeAnIdentlikeToken(from);
} else if (namestartchar(code)) {
reconsume();
return consumeAnIdentlikeToken(from);
} else if (eof()) {
const to = position.clone();
return new EOFToken(dbg(from, to));
} else {
const to = position.clone();
return new DelimToken(code, dbg(from, to));
}
}
/** @see https://drafts.csswg.org/css-syntax/#consume-comment */
function consumeComments() {
while (next(1) == 0x2f && next(2) == 0x2a) {
consume(2);
while (true) {
consume();
if (code == 0x2a && next() == 0x2f) {
consume();
break;
} else if (eof()) {
parseerror();
return;
}
}
}
}
/** @see https://drafts.csswg.org/css-syntax/#consume-numeric-token */
function consumeANumericToken(from: Position) {
const { value, type, sign } = consumeANumber();
if (wouldStartAnIdentifier(next(1), next(2), next(3))) {
const unit = consumeAName();
const to = position.clone();
return new DimensionToken(value, unit, sign, dbg(from, to));
} else if (next() == 0x25) {
consume();
const to = position.clone();
return new PercentageToken(value, sign, dbg(from, to));
} else {
const to = position.clone();
return new NumberToken(value, type, sign, dbg(from, to));
}
}
/** @see https://drafts.csswg.org/css-syntax/#consume-ident-like-token */
function consumeAnIdentlikeToken(from: Position) {
const str = consumeAName();
if (str.toLowerCase() == "url" && next() == 0x28) {
consume();
while (whitespace(next(1)) && whitespace(next(2))) consume();
if (next() == 0x22 || next() == 0x27) {
const to = position.clone();
return new FunctionToken(str, dbg(from, to));
} else if (whitespace(next()) && (next(2) == 0x22 || next(2) == 0x27)) {
const to = position.clone();
return new FunctionToken(str, dbg(from, to));
} else {
return consumeAURLToken(from);
}
} else if (next() == 0x28) {
consume();
const to = position.clone();
return new FunctionToken(str, dbg(from, to));
} else {
const to = position.clone();
return new IdentToken(str, dbg(from, to));
}
}
/** @see https://drafts.csswg.org/css-syntax/#consume-string-token */
function consumeAStringToken(from: Position, endingCodePoint = code) {
let string = "";
while (consume()) {
if (code == endingCodePoint || eof() || code == null) {
const to = position.clone();
return new StringToken(string, dbg(from, to));
} else if (newline(code)) {
parseerror();
reconsume();
const to = position.clone();
return new BadStringToken(dbg(from, to));
} else if (code == 0x5c) {
if (eof(next())) {
donothing();
} else if (newline(next())) {
consume();
} else {
string += String.fromCodePoint(consumeEscape());
}
} else {
string += String.fromCodePoint(code);
}
}
unreachable();
}
/** @see https://drafts.csswg.org/css-syntax/#consume-url-token */
function consumeAURLToken(from: Position) {
const to = position.clone();
const token = new URLToken("", dbg(from, to));
while (whitespace(next())) consume();
if (eof(next())) return token;
while (consume()) {
if (code == 0x29 || eof()) {
const to = position.clone();
token.debug.to = to;
return token;
} else if (whitespace(code)) {
while (whitespace(next())) consume();
if (next() == 0x29 || eof(next())) {
consume();
const to = position.clone();
token.debug.to = to;
return token;
} else {
consumeTheRemnantsOfABadURL();
const to = position.clone();
return new BadURLToken(dbg(from, to));
}
} else if (code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code)) {
parseerror();
consumeTheRemnantsOfABadURL();
const to = position.clone();
return new BadURLToken(dbg(from, to));
} else if (code == 0x5c) {
if (startsWithAValidEscape()) {
token.value += String.fromCodePoint(consumeEscape());
} else {
parseerror();
consumeTheRemnantsOfABadURL();
const to = position.clone();
return new BadURLToken(dbg(from, to));
}
} else {
token.value += String.fromCodePoint(code);
}
}
unreachable();
}
/** @see https://drafts.csswg.org/css-syntax/#consume-escaped-code-point */
function consumeEscape() {
// Assume the the current character is the \
// and the next code point is not a newline.
consume();
if (hexdigit(code)) {
// Consume 1-6 hex digits
const digits = [code];
for (let total = 0; total < 5; total++) {
if (hexdigit(next())) {
consume();
digits.push(code);
} else {
break;
}
}
if (whitespace(next())) consume();
let value = parseInt(
digits
.map(function (x) {
return String.fromCharCode(x);
})
.join(""),
16,
);
if (value > maximumallowedcodepoint) value = 0xfffd;
return value;
} else if (eof()) {
return 0xfffd;
} else {
return code;
}
}
/** @see https://drafts.csswg.org/css-syntax/#starts-with-a-valid-escape */
function areAValidEscape(c1: number, c2: number) {
if (c1 != 0x5c) return false;
if (newline(c2)) return false;
return true;
}
function startsWithAValidEscape() {
return areAValidEscape(code, next());
}
/** @see https://drafts.csswg.org/css-syntax/#would-start-an-identifier */
function wouldStartAnIdentifier(c1: number, c2: number, c3: number) {
if (c1 == 0x2d) {
return namestartchar(c2) || c2 == 0x2d || areAValidEscape(c2, c3);
} else if (namestartchar(c1)) {
return true;
} else if (c1 == 0x5c) {
return areAValidEscape(c1, c2);
} else {
return false;
}
}
function startsWithAnIdentifier() {
return wouldStartAnIdentifier(code, next(1), next(2));
}
/** @see https://drafts.csswg.org/css-syntax/#starts-with-a-number */
function wouldStartANumber(c1: number, c2: number, c3: number) {
if (c1 == 0x2b || c1 == 0x2d) {
if (digit(c2)) return true;
if (c2 == 0x2e && digit(c3)) return true;
return false;
} else if (c1 == 0x2e) {
if (digit(c2)) return true;
return false;
} else if (digit(c1)) {
return true;
} else {
return false;
}
}
function startsWithANumber() {
return wouldStartANumber(code, next(1), next(2));
}
/** @see https://drafts.csswg.org/css-syntax/#starts-a-unicode-range */
function wouldStartAUnicodeRange(c1: number, c2: number, c3: number) {
if (c1 == 0x55 || c1 == 0x75) if (c2 == 0x2b) if (c3 == 0x3f || hexdigit(c3)) return true;
return false;
}
/** @see https://drafts.csswg.org/css-syntax/#consume-name */
function consumeAName() {
let result = "";
while (consume()) {
if (namechar(code)) {
result += String.fromCodePoint(code);
} else if (startsWithAValidEscape()) {
result += String.fromCodePoint(consumeEscape());
} else {
reconsume();
return result;
}
}
unreachable();
}
/** @see https://drafts.csswg.org/css-syntax/#consume-number */
function consumeANumber() {
let type: NumberToken["type"] = "integer";
let sign;
let numberPart = "";
let exponentPart = "";
if (next() == 0x2b || next() == 0x2d) {
consume();
sign = String.fromCodePoint(code);
numberPart += sign;
}
while (digit(next())) {
consume();
numberPart += String.fromCodePoint(code);
}
if (next(1) == 0x2e && digit(next(2))) {
consume();
numberPart += ".";
while (digit(next())) {
consume();
numberPart += String.fromCodePoint(code);
}
type = "number";
}
const [c1, c2, c3] = [next(1), next(2), next(3)];
const eDigit = (c1 == 0x45 || c1 == 0x65) && digit(c2);
const eSignDigit = (c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3);
if (eDigit || eSignDigit) {
consume();
if (eSignDigit) {
consume();
exponentPart += String.fromCodePoint(code);
}
while (digit(next())) {
consume();
exponentPart += String.fromCodePoint(code);
}
type = "number";
}
let value = +numberPart;
if (exponentPart) value = value * Math.pow(10, +exponentPart);
return { value, type, sign };
}
/** @see https://drafts.csswg.org/css-syntax/#consume-a-unicode-range-token */
function consumeAUnicodeRangeToken(from: Position) {
let firstSegment = "";
let start = "";
let end = "";
consume(2);
while (hexdigit(next()) && firstSegment.length <= 6) {
consume();
firstSegment += String.fromCodePoint(code);
}
if (firstSegment.length < 6 && next() == 0x3f) {
let wildcardLen = 0;
while (next() == 0x3f && firstSegment.length <= 6) {
consume();
wildcardLen++;
}
start = firstSegment + String.fromCodePoint(0x30).repeat(wildcardLen);
end = firstSegment + String.fromCodePoint(0x46).repeat(wildcardLen);
const to = position.clone();
return new UnicodeRangeToken(start, end, dbg(from, to));
}
start = firstSegment;
if (next(1) == 0x2d && hexdigit(next(2))) {
consume();
while (hexdigit(next()) && end.length <= 6) {
consume();
end += String.fromCodePoint(code);
}
const to = position.clone();
return new UnicodeRangeToken(start, end, dbg(from, to));
}
const to = position.clone();
return new UnicodeRangeToken(start, start, dbg(from, to));
}
/** @see https://drafts.csswg.org/css-syntax/#consume-remnants-of-bad-url */
function consumeTheRemnantsOfABadURL() {
while (consume()) {
if (code == 0x29 || eof()) {
return;
} else if (startsWithAValidEscape()) {
consumeEscape();
donothing();
} else {
donothing();
}
}
}
let iterationCount = 0;
while (!eof(next())) {
tokens.push(consumeAToken(opts));
iterationCount++;
if (iterationCount > codepoints.length * 2) throw "I'm infinite-looping!";
}
return tokens;
}
class CSSParserToken {
public value: unknown;
constructor(public kind: string, public debug: Debug = dbg()) {}
toJSON() {
return {
kind: this.kind,
value: this.value,
debug: this.debug,
};
}
toString() {
return this.kind;
}
toSource(): string {
throw new TypeError("Not implemented.");
}
}
class BadStringToken extends CSSParserToken {
constructor(debug?: Debug) {
super("BADSTRING", debug);
}
toSource() {
return '"\n"';
}
}
class BadURLToken extends CSSParserToken {
public tokenType: string = "BADURL";
constructor(debug?: Debug) {
super("BADURL", debug);
}
toSource() {
return "url(BADURL '')";
}
}
class WhitespaceToken extends CSSParserToken {
constructor(debug?: Debug) {
super("WHITESPACE", debug);
}
toString() {
return "WS";
}
toSource() {
return " ";
}
}
class CDOToken extends CSSParserToken {
constructor(debug?: Debug) {
super("CDO", debug);
}
toSource() {
return "<!--";
}
}
class CDCToken extends CSSParserToken {
constructor(debug?: Debug) {
super("CDC", debug);
}
toSource() {
return "-->";
}
}
/** @see https://drafts.csswg.org/css-syntax/#typedef-unicode-range-token */
class UnicodeRangeToken extends CSSParserToken {
constructor(public start: string, public end: string, debug?: Debug) {
super("UNICODE-RANGE", debug);
}
toSource(): string {
if (this.start === this.end) return "U+" + this.start;
return `U+${this.start}-${this.end}`;
}
}
class ColonToken extends CSSParserToken {
constructor(debug?: Debug) {
super("COLON", debug);
}
toSource() {
return ":";
}
}
class SemicolonToken extends CSSParserToken {
constructor(debug?: Debug) {
super("SEMICOLON", debug);
}
toSource() {
return ";";
}
}
class CommaToken extends CSSParserToken {
constructor(debug?: Debug) {
super("COMMA", debug);
}
toSource() {
return ",";
}
}
class OpenCurlyToken extends CSSParserToken {
public grouping = true;
public mirror: typeof CloseCurlyToken;
constructor(debug?: Debug) {
super("OPEN-CURLY", debug);
this.mirror = CloseCurlyToken;
}
toSource() {
return "{";
}
}
class CloseCurlyToken extends CSSParserToken {
constructor(debug?: Debug) {
super("CLOSE-CURLY", debug);
}
toSource() {
return "}";
}
}
class OpenSquareToken extends CSSParserToken {
public grouping = true;
public mirror: typeof CloseSquareToken;
constructor(debug?: Debug) {
super("OPEN-SQUARE", debug);
this.mirror = CloseSquareToken;
}
toSource() {
return "[";
}
}
class CloseSquareToken extends CSSParserToken {
constructor(debug?: Debug) {
super("CLOSE-SQUARE", debug);
}
toSource() {
return "]";
}
}
class OpenParenToken extends CSSParserToken {
public grouping = true;
public mirror: typeof CloseParenToken;
constructor(debug?: Debug) {
super("OPEN-PAREN", debug);
this.mirror = CloseParenToken;
}
toSource() {
return "(";
}
}
class CloseParenToken extends CSSParserToken {
constructor(debug?: Debug) {
super("CLOSE-PAREN", debug);
}
toSource() {
return ")";
}
}
class EOFToken extends CSSParserToken {
constructor(debug?: Debug) {
super("EOF", debug);
}
toSource() {
return "";
}
}
class DelimToken extends CSSParserToken {
public value: string;
constructor(val: number | string, debug?: Debug) {
super("DELIM", debug);
if (typeof val == "number") {
val = String.fromCodePoint(val);
} else {
val = String(val);
}
this.value = val;
}
toString() {
return `DELIM(${this.value})`;
}
toSource() {
if (this.value == "\\") return "\\\n";
return this.value;
}
}
class IdentToken extends CSSParserToken {
constructor(public value: string, debug?: Debug) {
super("IDENT", debug);
}
toString() {
return `IDENT(${this.value})`;
}
toSource() {
return escapeIdent(this.value);
}
}
class FunctionToken extends CSSParserToken {
public mirror: typeof CloseParenToken;
constructor(public value: string, debug?: Debug) {
super("FUNCTION", debug);
this.mirror = CloseParenToken;
}
toString() {
return `FUNCTION(${this.value})`;
}
toSource() {
return escapeIdent(this.value) + "(";
}
}
class AtKeywordToken extends CSSParserToken {
constructor(public value: string, debug?: Debug) {
super("AT-KEYWORD", debug);
}
toString() {
return `AT(${this.value})`;
}
toSource() {
return "@" + escapeIdent(this.value);
}
}
class HashToken extends CSSParserToken {
constructor(public value: string, public isIdent: boolean, debug?: Debug) {
super("HASH", debug);
}
toString() {
return `HASH(${this.value})`;
}
toJSON() {
return {
kind: this.kind,
value: this.value,
isIdent: this.isIdent,
debug: this.debug,
};
}
toSource() {
if (this.isIdent) {
return "#" + escapeIdent(this.value);
}
return "#" + escapeHash(this.value);
}
}
class StringToken extends CSSParserToken {
constructor(public value: string, debug?: Debug) {
super("STRING", debug);
}
toString() {
return `STRING(${this.value})`;
}
toSource() {
return `"${escapeString(this.value)}"`;
}
}
class URLToken extends CSSParserToken {
constructor(public value: string, debug?: Debug) {
super("URL", debug);
}
toString() {
return `URL(${this.value})`;
}
toSource() {
return `url("${escapeString(this.value)}")`;
}
}
class NumberToken extends CSSParserToken {
constructor(public value: number, public type: "integer" | "number", public sign?: string, debug?: Debug) {
super("NUMBER", debug);
}
toString() {
const name = this.type === "integer" ? "INT" : "NUMBER";
const sign = this.sign == "+" ? "+" : "";
return `${name}(${sign}${this.value})`;
}
toJSON() {
return {
kind: this.kind,
value: this.value,
type: this.type,
sign: this.sign,
debug: this.debug,
};
}
toSource() {
return formatNumber(this.value, this.sign);
}
}
class PercentageToken extends CSSParserToken {
constructor(public value: number, public sign?: string, debug?: Debug) {
super("PERCENTAGE", debug);
}
toString() {
const sign = this.sign == "+" ? "+" : "";
return `PERCENTAGE(${sign}${this.value})`;
}
toJSON() {
return {
kind: this.kind,
value: this.value,
sign: this.sign,
debug: this.debug,
};
}
toSource() {
return `${formatNumber(this.value, this.sign)}%`;
}
}