@tbela99/css-parser 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/CHANGELOG.md +86 -1
  2. package/README.md +15 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +4848 -3750
  6. package/dist/index-umd-web.js +18234 -9498
  7. package/dist/index.d.ts +1251 -373
  8. package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
  9. package/dist/{src/lib → lib}/ast/arena.js +1034 -655
  10. package/dist/{src/lib → lib}/ast/cache.js +36 -31
  11. package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
  12. package/dist/{src/lib → lib}/ast/features/if.js +24 -19
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
  21. package/dist/{src/lib → lib}/ast/math/math.js +8 -8
  22. package/dist/{src/lib → lib}/ast/minify.js +932 -354
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +7 -15
  37. package/dist/{src/lib → lib}/ast/walk.js +19 -19
  38. package/dist/{src/lib → lib}/fs/resolve.js +15 -15
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
  42. package/dist/lib/parser/input.js +85 -0
  43. package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
  44. package/dist/{src/lib → lib}/parser/parse.js +852 -619
  45. package/dist/lib/parser/source.js +71 -0
  46. package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
  64. package/dist/lib/parser/utils/selector.js +720 -0
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
  68. package/dist/{src/lib → lib}/printer/render.js +536 -243
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
  77. package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
  99. package/dist/{src/lib → lib}/syntax/constants.js +30 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
  101. package/dist/lib/validation/config.js +173 -0
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +1256 -579
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +92 -80
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +13 -8
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +80 -75
  113. package/package.json +13 -13
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -87
  116. package/dist/src/lib/ast/features/transform.js +0 -85
  117. package/dist/src/lib/ast/transform/compute.js +0 -358
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/source.js +0 -127
  126. package/dist/src/lib/parser/utils/cache.js +0 -54
  127. package/dist/src/lib/parser/utils/selector.js +0 -440
  128. package/dist/src/lib/validation/config.js +0 -120
@@ -1,147 +1,218 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken, ColorType } from '../ast/types.js';
9
- import { pseudoElements, containerFunc, urlFunc, gridTemplateFunc, imageFunc, timelineFunc, supportFunc, timingFunc, colorsFunc, mathFuncs, transformFunctions, whenElseFunc, wildCardFuncs } from '../syntax/constants.js';
10
- import { isWhiteSpace, isNewLine, isDigit, isLetter, isIdentStart, isIdentCodepoint, isNonPrintable, flexUnits, dimensionUnits, resolutionUnits, angleUnits, timeUnits, frequencyUnits } from '../syntax/syntax.js';
9
+ import { validationUnits, pseudoElements, containerFunc, urlFunc, gridTemplateFunc, imageFunc, timelineFunc, supportFunc, timingFunc, colorsFunc, mathFuncs, transformFunctions, whenElseFunc, wildCardFuncs } from '../syntax/constants.js';
10
+ import { isNewLine, isWhiteSpace, isLetter, isIdentStart, isIdentCodepoint, isNonPrintable, isDigit, flexUnits, dimensionUnits, resolutionUnits, angleUnits, timeUnits, frequencyUnits } from '../syntax/syntax.js';
11
11
  import { SourceFile } from './source.js';
12
+ import { isCssInput, toCodeUnits } from './input.js';
13
+ import { hashRangeLower, lowerAscii } from './utils/codes.js';
14
+ import { canonicalStart, canonicalEnd, canonicalCodes, registerCanonical } from './utils/canonical.js';
12
15
 
13
- const SymbolsMapTokens = Object.create(null);
14
- const SymbolsMapTokensLower = Object.create(null);
15
- // Regex for escape sequence decoding - compile once, reuse many times
16
- const ESCAPE_SEQUENCE_REGEX = /\\((\n|\r|\f|\v|\u2028|\u2029|([0-9a-fA-F]{1,6})) ?)/gms;
17
- function decodeEscapeSequences(value) {
18
- return value.replace(ESCAPE_SEQUENCE_REGEX, (_, sequence) => {
19
- // \n \r \f \v
20
- switch (sequence.charCodeAt(0)) {
21
- case 0xa:
22
- case 0xb:
23
- case 0xc:
24
- case 0xd:
25
- case 0x2028:
26
- case 0x2029:
27
- return "";
28
- }
29
- const codepoint = parseInt(sequence, 16);
30
- if (codepoint == 0 ||
31
- // leading surrogate
32
- (0xd800 <= codepoint && codepoint <= 0xdbff) ||
33
- // trailing surrogate
34
- (0xdc00 <= codepoint && codepoint <= 0xdfff) ||
35
- codepoint > 0x10ffff) {
36
- return "\uFFFD";
37
- }
38
- return String.fromCodePoint(codepoint);
39
- });
16
+ /**
17
+ * decoded code units of the last value decoded by {@link decodeEscapes}
18
+ */
19
+ let decoded = new Uint16Array(256);
20
+ /**
21
+ * decode the escape sequences of a range of code units into {@link decoded}: a backslash followed by a new line, or by
22
+ * 1 to 6 hexadecimal digits, and an optional space. a new line is removed, the digits are replaced by the character
23
+ * (U+FFFD for 0, the surrogates and the values larger than U+10FFFF). the other backslashes are kept.
24
+ * no string is created
25
+ * @param codes
26
+ * @param start
27
+ * @param end
28
+ * @returns the number of decoded code units
29
+ */
30
+ function decodeEscapes(codes, start, end) {
31
+ // the decoded value is not longer than the value
32
+ if (decoded.length < end - start) {
33
+ decoded = new Uint16Array(end - start);
34
+ }
35
+ const output = decoded;
36
+ let length = 0;
37
+ let i = start;
38
+ while (i < end) {
39
+ const code = codes[i];
40
+ if (code !== 0x5c || i + 1 >= end) {
41
+ output[length++] = code;
42
+ i++;
43
+ continue;
44
+ }
45
+ let next = codes[i + 1];
46
+ // \n \r \f \v U+2028 U+2029: removed
47
+ if (next === 0xa || next === 0xb || next === 0xc || next === 0xd || next === 0x2028 || next === 0x2029) {
48
+ i += 2;
49
+ }
50
+ else if (isHexDigit(next)) {
51
+ let codepoint = 0;
52
+ let k = i + 1;
53
+ while (k < end && k < i + 7 && isHexDigit((next = codes[k]))) {
54
+ codepoint = codepoint * 16 + (next <= 0x39 ? next - 0x30 : (next | 0x20) - 0x57);
55
+ k++;
56
+ }
57
+ i = k;
58
+ if (codepoint === 0 ||
59
+ // surrogates
60
+ (codepoint >= 0xd800 && codepoint <= 0xdfff) ||
61
+ codepoint > 0x10ffff) {
62
+ output[length++] = 0xfffd;
63
+ }
64
+ else if (codepoint > 0xffff) {
65
+ // surrogate pair
66
+ codepoint -= 0x10000;
67
+ output[length++] = 0xd800 + (codepoint >> 10);
68
+ output[length++] = 0xdc00 + (codepoint & 0x3ff);
69
+ }
70
+ else {
71
+ output[length++] = codepoint;
72
+ }
73
+ }
74
+ else {
75
+ // not an escape sequence: the backslash is kept, the next character is read again
76
+ output[length++] = code;
77
+ i++;
78
+ continue;
79
+ }
80
+ // an optional space ends the escape sequence
81
+ if (i < end && codes[i] === 0x20) {
82
+ i++;
83
+ }
84
+ }
85
+ return length;
40
86
  }
41
87
  /**
42
- * hash table of ascii strings, looked up by a range of the css source without allocating a string.
43
- * the keys are lowercase, the lookups ignore the case of ascii letters
88
+ * the lowercase code units of an ascii string
89
+ * @param str
44
90
  */
45
- class RangeTable {
91
+ function lowerCodesOf(str) {
92
+ const codes = new Uint16Array(str.length);
93
+ for (let i = 0; i < str.length; i++) {
94
+ codes[i] = lowerAscii(str.charCodeAt(i));
95
+ }
96
+ return codes;
97
+ }
98
+ /**
99
+ * hash table of ascii strings keyed by their hash. a range of the code units of the css source is looked up by its
100
+ * hash, without creating a string: the keys are lowercase, the lookups ignore the case (see {@link hashRangeLower}).
101
+ * when the hashes are equal, the code units are compared
102
+ */
103
+ class HashTable {
104
+ /**
105
+ * lowercase code units of the keys, by entry
106
+ */
46
107
  keys = [];
108
+ /**
109
+ * values, by entry
110
+ */
47
111
  values = [];
48
- slots = new Int32Array(1024).fill(-1);
49
112
  /**
50
- * hash of a range of a string, ascii letters are lowercased. -1 when the range contains a non ascii character
51
- * @param str
113
+ * hash of the keys, by entry
114
+ */
115
+ hashes = [];
116
+ /**
117
+ * entry + 1 by slot, 0 when the slot is empty
118
+ */
119
+ slots = new Int32Array(2048);
120
+ /**
121
+ * the value of a range of code units, ignoring the case
122
+ * @param codes
52
123
  * @param start
53
124
  * @param end
54
125
  */
55
- static hash(str, start, end) {
56
- let hash = 0x811c9dc5;
57
- for (let i = start; i < end; i++) {
58
- let code = str.charCodeAt(i);
59
- if (code > 0x7f) {
60
- return -1;
61
- }
62
- if (code >= 65 && code <= 90) {
63
- code += 32;
64
- }
65
- hash = Math.imul(hash ^ code, 0x01000193);
126
+ get(codes, start, end) {
127
+ const hash = hashRangeLower(codes, start, end);
128
+ if (hash === -1) {
129
+ return undefined;
130
+ }
131
+ const entry = this.slots[this.find(codes, start, end, hash)];
132
+ return entry === 0 ? undefined : this.values[entry - 1];
133
+ }
134
+ /**
135
+ * the value of a key, ignoring the case
136
+ * @param key
137
+ */
138
+ getKey(key) {
139
+ return this.get(lowerCodesOf(key), 0, key.length);
140
+ }
141
+ /**
142
+ * set the value of a key. the key is lowercased
143
+ * @param key
144
+ * @param value
145
+ */
146
+ set(key, value) {
147
+ const codes = lowerCodesOf(key);
148
+ const hash = hashRangeLower(codes, 0, codes.length);
149
+ const slot = this.find(codes, 0, codes.length, hash);
150
+ if (this.slots[slot] === 0) {
151
+ this.keys.push(codes);
152
+ this.values.push(value);
153
+ this.hashes.push(hash);
154
+ this.slots[slot] = this.keys.length;
155
+ }
156
+ else {
157
+ this.values[this.slots[slot] - 1] = value;
66
158
  }
67
- return hash >>> 0;
68
159
  }
69
160
  /**
70
- * slot of a key. the table is never full: it is sized for the static tables of the tokenizer
71
- * @param str
161
+ * slot of a key: the slot of the entry, or the empty slot where it is inserted. the table is never full: it is
162
+ * sized for the static tables of the tokenizer
163
+ * @param codes
72
164
  * @param start
73
165
  * @param end
74
- * @param hash
166
+ * @param hash hash of the key
75
167
  */
76
- find(str, start, end, hash) {
168
+ find(codes, start, end, hash) {
77
169
  const mask = this.slots.length - 1;
78
170
  const length = end - start;
79
171
  let slot = hash & mask;
80
172
  for (;;) {
81
- const index = this.slots[slot];
82
- if (index == -1) {
173
+ const entry = this.slots[slot];
174
+ if (entry === 0) {
83
175
  return slot;
84
176
  }
85
- const key = this.keys[index];
86
- if (key.length == length) {
87
- let k = 0;
88
- while (k < length) {
89
- let code = str.charCodeAt(start + k);
90
- if (code >= 65 && code <= 90) {
91
- code += 32;
177
+ // the hashes are compared first: the code units are compared when they are equal
178
+ if (this.hashes[entry - 1] === hash) {
179
+ const key = this.keys[entry - 1];
180
+ if (key.length === length) {
181
+ let k = 0;
182
+ while (k < length && lowerAscii(codes[start + k]) === key[k]) {
183
+ k++;
92
184
  }
93
- if (code != key.charCodeAt(k)) {
94
- break;
185
+ if (k === length) {
186
+ return slot;
95
187
  }
96
- k++;
97
- }
98
- if (k == length) {
99
- return slot;
100
188
  }
101
189
  }
102
190
  slot = (slot + 1) & mask;
103
191
  }
104
192
  }
105
- get(str, start, end) {
106
- const hash = RangeTable.hash(str, start, end);
107
- if (hash == -1) {
108
- return undefined;
109
- }
110
- const index = this.slots[this.find(str, start, end, hash)];
111
- return index == -1 ? undefined : this.values[index];
112
- }
113
- set(key, value) {
114
- const slot = this.find(key, 0, key.length, RangeTable.hash(key, 0, key.length));
115
- if (this.slots[slot] == -1) {
116
- this.slots[slot] = this.keys.length;
117
- this.keys.push(key);
118
- this.values.push(value);
119
- }
120
- else {
121
- this.values[this.slots[slot]] = value;
122
- }
123
- }
124
193
  }
125
194
  /**
126
- * value of a number: the digits are read from the source without creating a string when the result is exact
127
- * (at most 15 significant digits, no exponent), parseFloat() otherwise
128
- * @param stream
195
+ * value of a number: the digits are read from the code units of the source, no string is created. the result is the
196
+ * same as parseFloat() (the longest prefix that is a number): the value is exact when it has at most 15 significant
197
+ * digits and a power of 10 between -22 and 22, it is computed with BigInt otherwise (see {@link exactNumber})
198
+ * @param codes the code units of the source
129
199
  * @param start
130
200
  * @param end
131
201
  */
132
- function parseNumber(stream, start, end) {
202
+ function parseNumber(codes, start, end) {
133
203
  let i = start;
134
- let code = stream.charCodeAt(i);
204
+ let code = codes[i];
135
205
  let negative = false;
136
- if (code == 0x2d || code == 0x2b) {
137
- negative = code == 0x2d;
138
- code = stream.charCodeAt(++i);
206
+ if (code === 0x2d || code === 0x2b) {
207
+ negative = code === 0x2d;
208
+ code = codes[++i];
139
209
  }
210
+ const digitsStart = i;
140
211
  let mantissa = 0;
141
212
  let count = 0;
142
213
  let digits = 0;
143
214
  let decimals = -1;
144
- for (; i < end; code = stream.charCodeAt(++i)) {
215
+ for (; i < end; code = codes[++i]) {
145
216
  if (code >= 0x30 && code <= 0x39) {
146
217
  mantissa = mantissa * 10 + (code - 0x30);
147
218
  count++;
@@ -152,21 +223,119 @@ function parseNumber(stream, start, end) {
152
223
  decimals++;
153
224
  }
154
225
  }
155
- else if (code == 0x2e && decimals == -1) {
226
+ else if (code === 0x2e && decimals === -1) {
156
227
  decimals = 0;
157
228
  }
158
229
  else {
159
230
  break;
160
231
  }
161
232
  }
162
- // exponent, too many digits or not a number: the (correctly rounded) result of parseFloat()
163
- if (i != end || count == 0 || digits > 15 || decimals > 22 || decimals == 0) {
164
- return parseFloat(stream.slice(start, end));
233
+ if (count === 0) {
234
+ return NaN;
235
+ }
236
+ const digitsEnd = i;
237
+ // power of 10 of the value: minus the number of decimals, plus the exponent
238
+ let exponent = decimals > 0 ? -decimals : 0;
239
+ // exponent: `1e3`, `1.5E-2`. ignored when it has no digit (the prefix before `e` is the number)
240
+ if (i < end && (code === 0x65 || code === 0x45)) {
241
+ let k = i + 1;
242
+ let sign = 1;
243
+ let value = 0;
244
+ if (k < end && (codes[k] === 0x2d || codes[k] === 0x2b)) {
245
+ sign = codes[k] === 0x2d ? -1 : 1;
246
+ k++;
247
+ }
248
+ if (k < end && codes[k] >= 0x30 && codes[k] <= 0x39) {
249
+ for (; k < end && (code = codes[k]) >= 0x30 && code <= 0x39; k++) {
250
+ // larger exponents are Infinity or 0
251
+ if (value < 100000) {
252
+ value = value * 10 + (code - 0x30);
253
+ }
254
+ }
255
+ exponent += sign * value;
256
+ }
257
+ }
258
+ if (mantissa === 0) {
259
+ return negative ? -0 : 0;
260
+ }
261
+ if (digits > 15 || exponent > 22 || exponent < -22) {
262
+ const value = exactNumber(codes, digitsStart, digitsEnd, exponent);
263
+ return negative ? -value : value;
165
264
  }
166
- // mantissa and 10^decimals are exact: the division is correctly rounded like parseFloat()
167
- const value = decimals > 0 ? mantissa / powersOf10[decimals] : mantissa;
265
+ // the mantissa (less than 2^53) and 10^exponent are exact: the product or the division is correctly rounded like
266
+ // parseFloat()
267
+ const value = exponent > 0 ? mantissa * powersOf10[exponent] : exponent < 0 ? mantissa / powersOf10[-exponent] : mantissa;
168
268
  return negative ? -value : value;
169
269
  }
270
+ /**
271
+ * correctly rounded value of `digits * 10^exponent`, computed with BigInt. the digits may contain a dot, it is ignored
272
+ * (the exponent takes the decimals into account)
273
+ * @param codes
274
+ * @param start start of the digits
275
+ * @param end end of the digits
276
+ * @param exponent power of 10
277
+ */
278
+ function exactNumber(codes, start, end, exponent) {
279
+ let mantissa = 0n;
280
+ let chunk = 0;
281
+ let chunkDigits = 0;
282
+ let digits = 0;
283
+ for (let i = start; i < end; i++) {
284
+ const code = codes[i];
285
+ if (code === 0x2e) {
286
+ continue;
287
+ }
288
+ chunk = chunk * 10 + (code - 0x30);
289
+ chunkDigits++;
290
+ if (digits > 0 || code !== 0x30) {
291
+ digits++;
292
+ }
293
+ if (chunkDigits === 15) {
294
+ mantissa = mantissa * 1000000000000000n + BigInt(chunk);
295
+ chunk = 0;
296
+ chunkDigits = 0;
297
+ }
298
+ }
299
+ if (chunkDigits > 0) {
300
+ mantissa = mantissa * 10n ** BigInt(chunkDigits) + BigInt(chunk);
301
+ }
302
+ if (exponent >= 0) {
303
+ return digits + exponent > 310 ? Infinity : Number(mantissa * 10n ** BigInt(exponent));
304
+ }
305
+ if (digits + exponent < -330) {
306
+ return 0;
307
+ }
308
+ // the quotient has at least 64 bits: the lowest bit marks an inexact division, the rounding of Number() is correct
309
+ let shift = Math.max(0, Math.ceil((1 - exponent - digits) * 3.321928094887362) + 66);
310
+ const divisor = 10n ** BigInt(-exponent);
311
+ const scaled = mantissa << BigInt(shift);
312
+ let quotient = scaled / divisor;
313
+ if (quotient * divisor !== scaled) {
314
+ quotient |= 1n;
315
+ }
316
+ // subnormal result: rounded to a multiple of 2^-1074 (rounding the quotient then scaling would round twice)
317
+ if (shift > 1074) {
318
+ const drop = BigInt(shift - 1074);
319
+ let units = quotient >> drop;
320
+ const remainder = quotient - (units << drop);
321
+ const half = 1n << (drop - 1n);
322
+ // round half to even
323
+ if (remainder > half || (remainder === half && (units & 1n) === 1n)) {
324
+ units++;
325
+ }
326
+ // the result is exact: less than 2^53 units of 2^-1074
327
+ if (units < 9007199254740992n) {
328
+ return Number(units) * 2 ** -1074;
329
+ }
330
+ }
331
+ let value = Number(quotient);
332
+ // value * 2^-shift: exact, the powers of 2 are exact
333
+ while (shift > 1000) {
334
+ value *= 2 ** -1e3;
335
+ shift -= 1000;
336
+ }
337
+ return value * 2 ** -shift;
338
+ }
170
339
  const powersOf10 = [
171
340
  1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20,
172
341
  1e21, 1e22,
@@ -174,63 +343,77 @@ const powersOf10 = [
174
343
  /**
175
344
  * token type of a symbol, unit or function name (the name followed by `(`), ignoring the case
176
345
  */
177
- const symbolTable = new RangeTable();
346
+ const symbolTable = new HashTable();
178
347
  /**
179
348
  * units and function names of each token type, by lowercase name: the spelling of the first matching item
180
349
  */
181
- const itemTable = new RangeTable();
350
+ const itemTable = new HashTable();
351
+ /**
352
+ * symbols, units and function names: the definitions of {@link symbolTable}. only used to build the table
353
+ */
354
+ const symbolDefinitions = new Map();
182
355
  function assignTokenMap(entries, tokenType, suffix = "", lowercase = false) {
183
356
  for (const entry of entries) {
184
357
  const key = (lowercase ? entry.toLowerCase() : entry) + suffix;
185
- SymbolsMapTokens[key] = tokenType;
186
- SymbolsMapTokensLower[key.toLowerCase()] = tokenType;
187
- let items = itemTable.get(entry, 0, entry.length);
358
+ symbolDefinitions.set(key, tokenType);
359
+ let items = itemTable.getKey(entry);
188
360
  if (items == null) {
189
361
  items = new Map();
190
362
  itemTable.set(entry.toLowerCase(), items);
191
363
  }
192
- // the first item wins, like a search of the array
364
+ // the first item wins, like a search of the array. the spelling is a range of the canonical text
193
365
  if (!items.has(tokenType)) {
194
- items.set(tokenType, entry);
366
+ items.set(tokenType, registerCanonical(entry));
195
367
  }
196
368
  }
197
369
  }
198
- SymbolsMapTokens["+"] = EnumToken.Plus;
199
- SymbolsMapTokens["="] = EnumToken.DelimTokenType;
200
- SymbolsMapTokens["|"] = EnumToken.PipeTokenType;
201
- SymbolsMapTokens["||"] = EnumToken.ColumnCombinatorTokenType;
202
- SymbolsMapTokens["|="] = EnumToken.DashMatchTokenType;
203
- SymbolsMapTokens["&"] = EnumToken.NestingSelectorTokenType;
204
- SymbolsMapTokens["*"] = EnumToken.Star;
205
- SymbolsMapTokens["*="] = EnumToken.ContainMatchTokenType;
206
- SymbolsMapTokens["~"] = EnumToken.Tilda;
207
- SymbolsMapTokens["~="] = EnumToken.IncludeMatchTokenType;
208
- SymbolsMapTokens["^="] = EnumToken.StartMatchTokenType;
209
- SymbolsMapTokens["$="] = EnumToken.EndMatchTokenType;
210
- SymbolsMapTokens[","] = EnumToken.Comma;
211
- SymbolsMapTokens[":"] = EnumToken.ColonTokenType;
212
- SymbolsMapTokens["::"] = EnumToken.DoubleColonTokenType;
213
- SymbolsMapTokens[";"] = EnumToken.SemiColonTokenType;
214
- SymbolsMapTokens["("] = EnumToken.StartParensTokenType;
215
- SymbolsMapTokens[")"] = EnumToken.EndParensTokenType;
216
- SymbolsMapTokens["["] = EnumToken.AttrStartTokenType;
217
- SymbolsMapTokens["]"] = EnumToken.AttrEndTokenType;
218
- SymbolsMapTokens["{"] = EnumToken.BlockStartTokenType;
219
- SymbolsMapTokens["}"] = EnumToken.BlockEndTokenType;
220
- SymbolsMapTokens["<="] = EnumToken.LteTokenType;
221
- SymbolsMapTokens[">"] = EnumToken.GtTokenType;
222
- SymbolsMapTokens[">="] = EnumToken.GteTokenType;
223
- SymbolsMapTokens[" "] = EnumToken.Whitespace;
224
- SymbolsMapTokens["\t"] = EnumToken.Whitespace;
225
- SymbolsMapTokens["\r"] = EnumToken.Whitespace;
226
- SymbolsMapTokens["\n"] = EnumToken.Whitespace;
227
- SymbolsMapTokens["\f"] = EnumToken.Whitespace;
370
+ symbolDefinitions.set("+", EnumToken.Plus);
371
+ symbolDefinitions.set("=", EnumToken.DelimTokenType);
372
+ symbolDefinitions.set("|", EnumToken.PipeTokenType);
373
+ symbolDefinitions.set("||", EnumToken.ColumnCombinatorTokenType);
374
+ symbolDefinitions.set("|=", EnumToken.DashMatchTokenType);
375
+ symbolDefinitions.set("&", EnumToken.NestingSelectorTokenType);
376
+ symbolDefinitions.set("*", EnumToken.Star);
377
+ symbolDefinitions.set("*=", EnumToken.ContainMatchTokenType);
378
+ symbolDefinitions.set("~", EnumToken.Tilda);
379
+ symbolDefinitions.set("~=", EnumToken.IncludeMatchTokenType);
380
+ symbolDefinitions.set("^=", EnumToken.StartMatchTokenType);
381
+ symbolDefinitions.set("$=", EnumToken.EndMatchTokenType);
382
+ symbolDefinitions.set(",", EnumToken.Comma);
383
+ symbolDefinitions.set(":", EnumToken.ColonTokenType);
384
+ symbolDefinitions.set("::", EnumToken.DoubleColonTokenType);
385
+ symbolDefinitions.set(";", EnumToken.SemiColonTokenType);
386
+ symbolDefinitions.set("(", EnumToken.StartParensTokenType);
387
+ symbolDefinitions.set(")", EnumToken.EndParensTokenType);
388
+ symbolDefinitions.set("[", EnumToken.AttrStartTokenType);
389
+ symbolDefinitions.set("]", EnumToken.AttrEndTokenType);
390
+ symbolDefinitions.set("{", EnumToken.BlockStartTokenType);
391
+ symbolDefinitions.set("}", EnumToken.BlockEndTokenType);
392
+ symbolDefinitions.set("<=", EnumToken.LteTokenType);
393
+ symbolDefinitions.set(">", EnumToken.GtTokenType);
394
+ symbolDefinitions.set(">=", EnumToken.GteTokenType);
395
+ symbolDefinitions.set(" ", EnumToken.Whitespace);
396
+ symbolDefinitions.set("\t", EnumToken.Whitespace);
397
+ symbolDefinitions.set("\r", EnumToken.Whitespace);
398
+ symbolDefinitions.set("\n", EnumToken.Whitespace);
399
+ symbolDefinitions.set("\f", EnumToken.Whitespace);
228
400
  assignTokenMap(flexUnits, EnumToken.FlexTokenType);
229
401
  assignTokenMap(dimensionUnits, EnumToken.LengthTokenType);
230
402
  assignTokenMap(resolutionUnits, EnumToken.ResolutionTokenType);
231
403
  assignTokenMap(angleUnits, EnumToken.AngleTokenType);
232
404
  assignTokenMap(timeUnits, EnumToken.TimeTokenType);
233
405
  assignTokenMap(frequencyUnits, EnumToken.FrequencyTokenType);
406
+ // the units of the other dimensions are rendered in their canonical spelling: `6DB` is rendered `6dB`
407
+ for (const unit of validationUnits) {
408
+ let items = itemTable.getKey(unit);
409
+ if (items == null) {
410
+ items = new Map();
411
+ itemTable.set(unit.toLowerCase(), items);
412
+ }
413
+ if (!items.has(EnumToken.DimensionTokenType)) {
414
+ items.set(EnumToken.DimensionTokenType, registerCanonical(unit));
415
+ }
416
+ }
234
417
  assignTokenMap(pseudoElements, EnumToken.PseudoElementTokenType);
235
418
  assignTokenMap(containerFunc, EnumToken.ContainerFunctionTokenDefType, "(");
236
419
  assignTokenMap(urlFunc, EnumToken.UrlFunctionTokenDefType, "(");
@@ -244,9 +427,11 @@ assignTokenMap(mathFuncs, EnumToken.MathFunctionTokenDefType, "(");
244
427
  assignTokenMap(transformFunctions, EnumToken.TransformFunctionTokenDefType, "(", true);
245
428
  assignTokenMap(whenElseFunc, EnumToken.WhenElseFunctionTokenDefType, "(");
246
429
  assignTokenMap(wildCardFuncs, EnumToken.WildCardFunctionTokenDefType, "(");
247
- for (const key of Object.keys(SymbolsMapTokens)) {
248
- symbolTable.set(key.toLowerCase(), SymbolsMapTokens[key]);
430
+ // the table is keyed by the hash of the lowercase symbols
431
+ for (const [key, type] of symbolDefinitions) {
432
+ symbolTable.set(key, type);
249
433
  }
434
+ symbolDefinitions.clear();
250
435
  // do not capture the value
251
436
  const hintsEnum = new Set([
252
437
  EnumToken.CommaTokenType,
@@ -259,9 +444,54 @@ const hintsEnum = new Set([
259
444
  EnumToken.ColonTokenType,
260
445
  EnumToken.EOFTokenType,
261
446
  ]);
447
+ /**
448
+ * field of the token the value of a token is stored in
449
+ */
450
+ var TokenValueField;
451
+ (function (TokenValueField) {
452
+ /**
453
+ * no value
454
+ */
455
+ TokenValueField[TokenValueField["None"] = 0] = "None";
456
+ /**
457
+ * numeric value
458
+ */
459
+ TokenValueField[TokenValueField["Number"] = 1] = "Number";
460
+ /**
461
+ * name: at-rule
462
+ */
463
+ TokenValueField[TokenValueField["Name"] = 2] = "Name";
464
+ /**
465
+ * string value
466
+ */
467
+ TokenValueField[TokenValueField["Value"] = 3] = "Value";
468
+ /**
469
+ * unit of a dimension, with the numeric value
470
+ */
471
+ TokenValueField[TokenValueField["Unit"] = 4] = "Unit";
472
+ })(TokenValueField || (TokenValueField = {}));
473
+ /**
474
+ * where the string value of a token is read from
475
+ */
476
+ var TokenValueKind;
477
+ (function (TokenValueKind) {
478
+ /**
479
+ * a range of the source
480
+ */
481
+ TokenValueKind[TokenValueKind["Source"] = 0] = "Source";
482
+ /**
483
+ * a range of the canonical text
484
+ */
485
+ TokenValueKind[TokenValueKind["Canonical"] = 1] = "Canonical";
486
+ /**
487
+ * a range of the source that contains escape sequences
488
+ */
489
+ TokenValueKind[TokenValueKind["Escaped"] = 2] = "Escaped";
490
+ })(TokenValueKind || (TokenValueKind = {}));
262
491
  var TokenMap;
263
492
  (function (TokenMap) {
264
493
  TokenMap[TokenMap["EXCLAMATION"] = 33] = "EXCLAMATION";
494
+ TokenMap[TokenMap["QUESTION"] = 63] = "QUESTION";
265
495
  TokenMap[TokenMap["SLASH"] = 47] = "SLASH";
266
496
  TokenMap[TokenMap["LOWERTHAN"] = 60] = "LOWERTHAN";
267
497
  TokenMap[TokenMap["HASH"] = 35] = "HASH";
@@ -292,29 +522,19 @@ var TokenMap;
292
522
  TokenMap[TokenMap["PERCENTAGE"] = 37] = "PERCENTAGE";
293
523
  })(TokenMap || (TokenMap = {}));
294
524
  function getSymbolHint(parseInfo, start, end) {
295
- const stream = parseInfo.stream;
296
- if (end > stream.length) {
297
- end = stream.length;
525
+ const codes = parseInfo.codes;
526
+ if (end > parseInfo.length) {
527
+ end = parseInfo.length;
298
528
  }
299
529
  if (end - start <= 0) {
300
530
  return null;
301
531
  }
302
- // ascii names are looked up without allocating a string
303
- const type = symbolTable.get(stream, start, end);
304
- if (type !== undefined) {
305
- return type;
306
- }
307
- for (let i = start; i < end; i++) {
308
- // the unicode case mapping of toLowerCase() may produce ascii letters
309
- if (stream.charCodeAt(i) > 0x7f) {
310
- const value = stream.slice(start, end);
311
- return SymbolsMapTokens[value] ?? SymbolsMapTokensLower[value.toLowerCase()] ?? null;
312
- }
313
- }
314
- return null;
532
+ // the range is looked up by its hash: no string is created
533
+ return symbolTable.get(codes, start, end) ?? null;
315
534
  }
316
535
  /**
317
- * spelling of a unit or a function name of the given token type, ignoring the case
536
+ * spelling of a unit or a function name of the given token type, ignoring the case: a range of the canonical text
537
+ * (see {@link registerCanonical}), -1 when the name is not known
318
538
  * @param type
319
539
  * @param parseInfo
320
540
  * @param start
@@ -322,41 +542,96 @@ function getSymbolHint(parseInfo, start, end) {
322
542
  */
323
543
  function findItem(type, parseInfo, start, end) {
324
544
  if (end - start <= 0) {
325
- return null;
545
+ return -1;
326
546
  }
327
- return itemTable.get(parseInfo.stream, start, end)?.get(type) ?? null;
547
+ return itemTable.get(parseInfo.codes, start, end)?.get(type) ?? -1;
548
+ }
549
+ /**
550
+ * true if the character is a hexadecimal digit
551
+ * @param codepoint
552
+ */
553
+ function isHexDigit(codepoint) {
554
+ return ((codepoint >= 0x30 && codepoint <= 0x39) ||
555
+ (codepoint >= 0x41 && codepoint <= 0x46) ||
556
+ (codepoint >= 0x61 && codepoint <= 0x66));
557
+ }
558
+ /**
559
+ * true if the character may follow a numeric token: the end of the stream, a whitespace, `)`, `;`, `!`
560
+ * (`0!important`), `}`, `/`, `*`, `,`, and `+` after a dimension (`2n+1`)
561
+ * @param codepoint
562
+ * @param isDimension
563
+ */
564
+ function isNumericTokenEnd(codepoint, isDimension) {
565
+ return (
566
+ // eof
567
+ codepoint === undefined ||
568
+ isWhiteSpace(codepoint) ||
569
+ codepoint === 123 /* TokenMap.LEFT_BRACE */ ||
570
+ codepoint === 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
571
+ codepoint === 59 /* TokenMap.SEMICOLON */ ||
572
+ codepoint === 33 /* TokenMap.EXCLAMATION */ ||
573
+ codepoint === 125 /* TokenMap.RIGHT_BRACE */ ||
574
+ codepoint === 47 /* TokenMap.SLASH */ ||
575
+ codepoint === 42 /* TokenMap.STAR */ ||
576
+ codepoint === 44 /* TokenMap.COMMA */ ||
577
+ (isDimension && codepoint === 43 /* TokenMap.PLUS */));
578
+ }
579
+ /**
580
+ * the parse info of a css input: the input is converted to code units, see {@link Tokenizer.load}. a stream is read
581
+ * by {@link Tokenizer.tokenizeStream}
582
+ * @param input the css: a string, an ArrayBuffer, a view of an ArrayBuffer or a stream
583
+ * @param source the source file of the css
584
+ */
585
+ function createParseInfo(input, source) {
586
+ const parseInfo = {
587
+ codes: new Uint8Array(0),
588
+ length: 0,
589
+ source,
590
+ offset: 0,
591
+ time: 0,
592
+ position: 0,
593
+ currentPosition: 0,
594
+ };
595
+ if (!(input instanceof ReadableStream)) {
596
+ Tokenizer.load(parseInfo, input);
597
+ }
598
+ return parseInfo;
328
599
  }
329
600
  /**
330
601
  * tokenizer class
331
602
  */
332
603
  class Tokenizer {
333
- parseInfo;
334
604
  input;
335
605
  /**
336
606
  * token type
337
607
  */
338
608
  typ = null;
339
- /**
340
- * the identifier read by the last call of consumeIdentToken() has non ascii characters: they may be new lines
341
- * (U+2028, U+2029)
342
- */
343
- identNonAscii = false;
344
609
  /**
345
610
  * token kind
346
611
  */
347
612
  kin = null;
348
613
  /**
349
- * token name
614
+ * numeric value of the number, dimension and percentage tokens
615
+ */
616
+ num = 0;
617
+ /**
618
+ * field of the token the value is stored in: the numeric value ({@link num}), the name of an at-rule, the string
619
+ * value, the unit of a dimension (with the numeric value)
350
620
  */
351
- nam = null;
621
+ field = 0 /* TokenValueField.None */;
352
622
  /**
353
- * token value
623
+ * where the string value is read from: a range of the source, a range of the canonical text (the spelling of a
624
+ * known unit or function name) or a range of the source that contains escape sequences
354
625
  */
355
- val = null;
626
+ valueKind = 0 /* TokenValueKind.Source */;
356
627
  /**
357
- * token unit
628
+ * start of the string value: in the code units of the source, or in the canonical text
358
629
  */
359
- unit = null;
630
+ valueStart = 0;
631
+ /**
632
+ * end of the string value: in the code units of the source, or in the canonical text
633
+ */
634
+ valueEnd = 0;
360
635
  /**
361
636
  * source id
362
637
  */
@@ -373,10 +648,6 @@ class Tokenizer {
373
648
  * bytes in
374
649
  */
375
650
  bytesIn = null;
376
- /**
377
- * decode string
378
- */
379
- decodeString = null;
380
651
  /**
381
652
  * token slice
382
653
  */
@@ -385,11 +656,46 @@ class Tokenizer {
385
656
  * source file
386
657
  */
387
658
  source = null;
659
+ /**
660
+ * the identifier read by the last call of consumeIdentToken() has non ascii characters: they may be new lines
661
+ * (U+2028, U+2029)
662
+ */
663
+ identNonAscii = false;
388
664
  /**
389
665
  * token hint
390
666
  */
391
667
  hint = null;
392
668
  state = null;
669
+ parseInfo;
670
+ /**
671
+ * @param parseInfo the parse info, or the css: a string, an ArrayBuffer or a view of an ArrayBuffer
672
+ * @param input a stream of css
673
+ */
674
+ constructor(parseInfo, input = null) {
675
+ this.input = input;
676
+ if (isCssInput(parseInfo)) {
677
+ this.parseInfo = createParseInfo(parseInfo, new SourceFile("", [], ""));
678
+ }
679
+ else {
680
+ // the parse info of a stream is loaded by tokenizeStream()
681
+ if (parseInfo.codes == null && input == null) {
682
+ Tokenizer.load(parseInfo, parseInfo.source.content);
683
+ }
684
+ this.parseInfo = parseInfo;
685
+ }
686
+ }
687
+ /**
688
+ * set the css read by the tokenizer: the code units of the css (an ascii ArrayBuffer is read without copy). the
689
+ * text of the css is the content of the source file: the offsets of the tokens are offsets of the content
690
+ * @param parseInfo
691
+ * @param input
692
+ */
693
+ static load(parseInfo, input) {
694
+ const { codes, text } = toCodeUnits(input);
695
+ parseInfo.codes = codes;
696
+ parseInfo.length = codes.length;
697
+ parseInfo.source.content = text;
698
+ }
393
699
  /**
394
700
  * id of the source file being tokenized
395
701
  */
@@ -397,26 +703,10 @@ class Tokenizer {
397
703
  return this.parseInfo.source.id;
398
704
  }
399
705
  /**
400
- * length of the css input. 0 when the input is a stream
706
+ * length of the css input, in code units. 0 when the input is a stream
401
707
  */
402
708
  get streamLength() {
403
- const stream = this.parseInfo.stream;
404
- return typeof stream == "string" ? stream.length : 0;
405
- }
406
- constructor(parseInfo, input = null) {
407
- this.parseInfo = parseInfo;
408
- this.input = input;
409
- this.parseInfo =
410
- typeof parseInfo == "string"
411
- ? {
412
- stream: parseInfo,
413
- source: new SourceFile(parseInfo, [], ""),
414
- offset: 0,
415
- time: 0,
416
- position: 0,
417
- currentPosition: 0,
418
- }
419
- : parseInfo;
709
+ return this.parseInfo.length;
420
710
  }
421
711
  /**
422
712
  *
@@ -427,48 +717,30 @@ class Tokenizer {
427
717
  const quote = this.advanceCode(parseInfo);
428
718
  let charCode;
429
719
  let decodeSegments = false;
430
- while ((charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) == charCode) {
431
- if (charCode == 92 /* TokenMap.REVERSE_SOLIDUS */) {
432
- if (charCode == parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1)) {
720
+ while ((charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset]) !== undefined) {
721
+ if (charCode === 92 /* TokenMap.REVERSE_SOLIDUS */) {
722
+ if (charCode === parseInfo.codes[parseInfo.currentPosition - parseInfo.offset + 1]) {
433
723
  this.advance(parseInfo, 2);
434
724
  continue;
435
725
  }
436
- charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1);
726
+ charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset + 1];
437
727
  // \n \r \f \v
438
- if (charCode == 0xa ||
439
- charCode == 0xb ||
440
- charCode == 0xc ||
441
- charCode == 0xd ||
442
- charCode == 0x2028 ||
443
- charCode == 0x2029) {
728
+ if (charCode === 0xa ||
729
+ charCode === 0xb ||
730
+ charCode === 0xc ||
731
+ charCode === 0xd ||
732
+ charCode === 0x2028 ||
733
+ charCode === 0x2029) {
444
734
  this.advance(parseInfo, 2);
445
735
  decodeSegments = true;
446
736
  continue;
447
737
  }
448
- const sequence = this.peek(parseInfo, 7);
449
- let escapeSequence = "";
450
- let codepoint;
451
- let i;
452
- for (i = 1; i < sequence.length; i++) {
453
- codepoint = sequence.charCodeAt(i);
454
- if (codepoint == 0x20 ||
455
- (codepoint >= 0x61 && codepoint <= 0x66) ||
456
- (codepoint >= 0x41 && codepoint <= 0x46) ||
457
- (codepoint >= 0x30 && codepoint <= 0x39)) {
458
- escapeSequence += sequence[i];
459
- if (codepoint == 0x20) {
460
- break;
461
- }
462
- continue;
463
- }
464
- break;
465
- }
466
- if (escapeSequence.trimEnd().length > 0) {
467
- const length = escapeSequence.length +
738
+ // the escape sequence: up to 6 hexadecimal digits, followed by an optional space
739
+ const escapeLength = this.escapeSequenceLength(parseInfo);
740
+ if (escapeLength > 0) {
741
+ const length = escapeLength +
468
742
  1 +
469
- (isWhiteSpace(parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset))
470
- ? 1
471
- : 0);
743
+ (isWhiteSpace(parseInfo.codes[parseInfo.currentPosition - parseInfo.offset]) ? 1 : 0);
472
744
  decodeSegments = true;
473
745
  this.advance(parseInfo, length);
474
746
  continue;
@@ -476,7 +748,7 @@ class Tokenizer {
476
748
  this.advance(parseInfo, 2);
477
749
  continue;
478
750
  }
479
- if (charCode == quote) {
751
+ if (charCode === quote) {
480
752
  this.advanceCode(parseInfo);
481
753
  return this.makeToken(parseInfo,
482
754
  /* hasNewLine ? EnumToken.BadStringTokenType : */ EnumToken.StringTokenType, null, decodeSegments);
@@ -500,36 +772,18 @@ class Tokenizer {
500
772
  const quote = this.advanceCode(parseInfo);
501
773
  let charCode;
502
774
  let decodeSegments = false;
503
- while ((charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) == charCode) {
504
- if (charCode == 92 /* TokenMap.REVERSE_SOLIDUS */) {
505
- if (charCode == parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1)) {
775
+ while ((charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset]) !== undefined) {
776
+ if (charCode === 92 /* TokenMap.REVERSE_SOLIDUS */) {
777
+ if (charCode === parseInfo.codes[parseInfo.currentPosition - parseInfo.offset + 1]) {
506
778
  this.advance(parseInfo, 2);
507
779
  continue;
508
780
  }
509
- const sequence = this.peek(parseInfo, 7);
510
- let escapeSequence = "";
511
- let codepoint;
512
- let i;
513
- for (i = 1; i < sequence.length; i++) {
514
- codepoint = sequence.charCodeAt(i);
515
- if (codepoint == 0x20 ||
516
- (codepoint >= 0x61 && codepoint <= 0x66) ||
517
- (codepoint >= 0x41 && codepoint <= 0x46) ||
518
- (codepoint >= 0x30 && codepoint <= 0x39)) {
519
- escapeSequence += sequence[i];
520
- if (codepoint == 0x20) {
521
- break;
522
- }
523
- continue;
524
- }
525
- break;
526
- }
527
- if (escapeSequence.trimEnd().length > 0) {
528
- const length = escapeSequence.length +
781
+ // the escape sequence: up to 6 hexadecimal digits, followed by an optional space
782
+ const escapeLength = this.escapeSequenceLength(parseInfo);
783
+ if (escapeLength > 0) {
784
+ const length = escapeLength +
529
785
  1 +
530
- (isWhiteSpace(parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset))
531
- ? 1
532
- : 0);
786
+ (isWhiteSpace(parseInfo.codes[parseInfo.currentPosition - parseInfo.offset]) ? 1 : 0);
533
787
  decodeSegments = true;
534
788
  this.advance(parseInfo, length);
535
789
  continue;
@@ -537,15 +791,15 @@ class Tokenizer {
537
791
  this.advance(parseInfo, 2);
538
792
  continue;
539
793
  }
540
- if (charCode == quote) {
794
+ if (charCode === quote) {
541
795
  this.advanceCode(parseInfo);
542
796
  let k = 1;
543
- let end = parseInfo.stream.length - parseInfo.offset;
797
+ let end = parseInfo.length - parseInfo.offset;
544
798
  let position = parseInfo.currentPosition - parseInfo.offset;
545
799
  while (position + k < end) {
546
- charCode = parseInfo.stream.charCodeAt(position);
547
- // NaN != NaN
548
- if (charCode != charCode) {
800
+ charCode = parseInfo.codes[position];
801
+ // the end of the stream
802
+ if (charCode === undefined) {
549
803
  this.advance(parseInfo, k);
550
804
  return this.makeToken(parseInfo, EnumToken.BadUrlTokenType);
551
805
  }
@@ -554,7 +808,7 @@ class Tokenizer {
554
808
  k++;
555
809
  continue;
556
810
  }
557
- if (charCode != 41 /* TokenMap.RIGHT_PARENTHESIS */) {
811
+ if (charCode !== 41 /* TokenMap.RIGHT_PARENTHESIS */) {
558
812
  this.advance(parseInfo, k);
559
813
  return this.makeToken(parseInfo, EnumToken.BadUrlTokenType);
560
814
  }
@@ -568,12 +822,12 @@ class Tokenizer {
568
822
  if (isNewLine(charCode)) {
569
823
  // bad string
570
824
  this.advanceCode(parseInfo);
571
- while ((charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) == charCode) {
572
- if (charCode == 92 /* TokenMap.REVERSE_SOLIDUS */) {
825
+ while ((charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset]) !== undefined) {
826
+ if (charCode === 92 /* TokenMap.REVERSE_SOLIDUS */) {
573
827
  this.advance(parseInfo, 2);
574
828
  continue;
575
829
  }
576
- if (charCode == 41 /* TokenMap.RIGHT_PARENTHESIS */) {
830
+ if (charCode === 41 /* TokenMap.RIGHT_PARENTHESIS */) {
577
831
  return this.makeToken(parseInfo, EnumToken.BadUrlTokenType);
578
832
  }
579
833
  this.advanceCode(parseInfo);
@@ -587,260 +841,142 @@ class Tokenizer {
587
841
  // return result;
588
842
  }
589
843
  /**
590
- * consume number, dimension, or percentage
844
+ * consume a unicode range: `U+` followed by one to six hexadecimal digits or `?` (the `?` are the last
845
+ * characters), optionally followed by `-` and one to six hexadecimal digits: `U+0025-00FF`, `U+4??`.
846
+ * the range is followed by the end of the stream, a whitespace, `,`, `;`, `}`, `)` or `!`: `u+a{` is a selector
591
847
  * @param parseInfo
592
- * @returns
848
+ * @returns the length of the token, 0 if it is not a unicode range
593
849
  */
594
- consumeNumericToken(parseInfo) {
595
- let position = parseInfo.currentPosition - parseInfo.offset;
596
- let offset = position;
597
- let hasDigits = false;
598
- let hasLetter = false;
599
- let hasPercent = false;
600
- let codepoint = parseInfo.stream.charCodeAt(position);
601
- this.slice = null;
602
- this.hint = null;
603
- // '+' '-'
604
- if (codepoint == 0x2b || codepoint == 0x2d) {
605
- position++;
850
+ consumeUnicodeRangeToken(parseInfo) {
851
+ const codes = parseInfo.codes;
852
+ const start = parseInfo.currentPosition - parseInfo.offset;
853
+ let position = start + 2;
854
+ let count = 0;
855
+ let wildcards = 0;
856
+ let codepoint;
857
+ if (codes[start + 1] !== 43 /* TokenMap.PLUS */) {
858
+ return 0;
606
859
  }
607
- // consume digits
608
- while (position < parseInfo.stream.length) {
609
- codepoint = parseInfo.stream.charCodeAt(position);
610
- if (isDigit(codepoint)) {
611
- hasDigits = true;
860
+ while (count < 6) {
861
+ codepoint = codes[position];
862
+ if (wildcards === 0 && isHexDigit(codepoint)) {
863
+ count++;
612
864
  position++;
613
- continue;
614
865
  }
615
- // '.' 'E' 'e'
616
- if (codepoint == 0x2e || codepoint == 0x45 || codepoint == 0x65) {
866
+ else if (codepoint === 63 /* TokenMap.QUESTION */) {
867
+ wildcards++;
868
+ count++;
617
869
  position++;
618
- break;
619
- }
620
- if (isWhiteSpace(codepoint) ||
621
- codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
622
- codepoint == 59 /* TokenMap.SEMICOLON */ ||
623
- codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
624
- codepoint == 47 /* TokenMap.SLASH */ ||
625
- codepoint == 42 /* TokenMap.STAR */ ||
626
- codepoint == 44 /* TokenMap.COMMA */) {
627
- return !hasDigits ? 0 : position - offset;
628
- }
629
- if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
630
- hasPercent = true;
631
- break;
632
- }
633
- if (isLetter(codepoint)) {
634
- hasLetter = true;
635
- break;
636
870
  }
637
- // dashed ident as unit
638
- if (codepoint == 0x2d && parseInfo.stream.charCodeAt(position + 1) == 0x2d) {
639
- hasLetter = true;
871
+ else {
640
872
  break;
641
873
  }
874
+ }
875
+ if (count === 0) {
642
876
  return 0;
643
877
  }
644
- if (!hasLetter && !hasPercent) {
645
- // '.'
646
- if (codepoint == 0x2e) {
647
- codepoint = parseInfo.stream.charCodeAt(position);
648
- if (codepoint != codepoint) {
649
- return !hasDigits ? 0 : position - offset;
650
- }
651
- if (isWhiteSpace(codepoint) ||
652
- codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
653
- codepoint == 59 /* TokenMap.SEMICOLON */ ||
654
- codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
655
- codepoint == 47 /* TokenMap.SLASH */ ||
656
- codepoint == 42 /* TokenMap.STAR */ ||
657
- codepoint == 44 /* TokenMap.COMMA */) {
658
- return !hasDigits ? 0 : position - offset;
659
- }
660
- if (!isDigit(codepoint)) {
661
- if (!hasDigits) {
662
- return 0;
663
- }
664
- if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
665
- hasPercent = true;
666
- }
667
- else if (isLetter(codepoint)) {
668
- hasLetter = true;
669
- }
670
- // custom ident as unit
671
- else if (codepoint == 0x2d && parseInfo.stream.charCodeAt(position + 1) == 0x2d) {
672
- hasLetter = true;
673
- }
674
- else {
675
- return 0;
676
- }
677
- }
678
- else {
679
- position++;
680
- hasDigits = true;
681
- }
878
+ // the end of the range
879
+ if (wildcards === 0 && codes[position] === 45 /* TokenMap.MINUS */ && isHexDigit(codes[position + 1])) {
880
+ position++;
881
+ count = 0;
882
+ while (count < 6 && isHexDigit(codes[position])) {
883
+ count++;
884
+ position++;
682
885
  }
683
- if (!hasLetter && !hasPercent) {
684
- while (position + 1 <= parseInfo.stream.length) {
685
- codepoint = parseInfo.stream.charCodeAt(position);
686
- if (isDigit(codepoint)) {
687
- position++;
688
- continue;
689
- }
690
- if (!hasDigits) {
691
- return 0;
692
- }
693
- // 'E' 'e'
694
- if (codepoint == 0x45 || codepoint == 0x65) {
695
- position++;
696
- break;
697
- }
698
- if (isWhiteSpace(codepoint) ||
699
- codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
700
- codepoint == 59 /* TokenMap.SEMICOLON */ ||
701
- codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
702
- codepoint == 47 /* TokenMap.SLASH */ ||
703
- codepoint == 42 /* TokenMap.STAR */ ||
704
- codepoint == 44 /* TokenMap.COMMA */) {
705
- return position - offset;
706
- }
707
- if (isLetter(codepoint)) {
708
- hasLetter = true;
709
- break;
710
- }
711
- else if (codepoint == 0x2d && parseInfo.stream.charCodeAt(position + 1) == 0x2d) {
712
- hasLetter = true;
713
- break;
714
- }
715
- if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
716
- hasPercent = true;
717
- break;
718
- }
719
- return 0;
720
- }
721
- // 'E' 'e' - 'em'
722
- if ((codepoint == 0x45 || codepoint == 0x65) && hasDigits && !hasLetter && !hasPercent) {
723
- if (isLetter(parseInfo.stream.charCodeAt(position))) {
724
- hasLetter = true;
725
- }
726
- }
727
- if (!hasLetter && !hasPercent) {
728
- // 'E' 'e'
729
- if (codepoint == 0x45 || codepoint == 0x65) {
730
- codepoint = parseInfo.stream.charCodeAt(position + 1);
731
- // '+' '-'
732
- if (codepoint == 0x2b || codepoint == 0x2d) {
733
- position++;
734
- }
735
- codepoint = position = parseInfo.stream.charCodeAt(position + 1);
736
- if (!isDigit(codepoint)) {
737
- if (!hasDigits) {
738
- return 0;
739
- }
740
- if (isLetter(codepoint)) {
741
- hasLetter = true;
742
- }
743
- else if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
744
- hasPercent = true;
745
- }
746
- else {
747
- return 0;
748
- }
749
- }
750
- }
751
- if (!hasLetter && !hasPercent) {
752
- while (++position < parseInfo.stream.length) {
753
- codepoint = parseInfo.stream.charCodeAt(position);
754
- // eof
755
- if (codepoint != codepoint) {
756
- break;
757
- }
758
- if (isDigit(codepoint)) {
759
- position++;
760
- continue;
761
- }
762
- if (!hasDigits) {
763
- return 0;
764
- }
765
- if (isWhiteSpace(codepoint) ||
766
- codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
767
- codepoint == 59 /* TokenMap.SEMICOLON */ ||
768
- codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
769
- codepoint == 47 /* TokenMap.SLASH */ ||
770
- codepoint == 42 /* TokenMap.STAR */ ||
771
- codepoint == 44 /* TokenMap.COMMA */) {
772
- return position - offset;
773
- }
774
- else if (isLetter(codepoint)) {
775
- hasLetter = true;
776
- break;
777
- }
778
- // custom ident as unit
779
- else if (codepoint == 0x2d && parseInfo.stream.charCodeAt(position + 1) == 0x2d) {
780
- hasLetter = true;
781
- break;
782
- }
783
- else if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
784
- hasPercent = true;
785
- break;
786
- }
787
- else {
788
- return 0;
789
- }
790
- }
791
- if (!hasLetter && !hasPercent) {
792
- return position - offset;
793
- }
794
- }
795
- }
886
+ }
887
+ codepoint = codes[position];
888
+ if (
889
+ // eof
890
+ codepoint === undefined ||
891
+ isWhiteSpace(codepoint) ||
892
+ codepoint === 44 /* TokenMap.COMMA */ ||
893
+ codepoint === 59 /* TokenMap.SEMICOLON */ ||
894
+ codepoint === 125 /* TokenMap.RIGHT_BRACE */ ||
895
+ codepoint === 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
896
+ codepoint === 33 /* TokenMap.EXCLAMATION */) {
897
+ return position - start;
898
+ }
899
+ return 0;
900
+ }
901
+ /**
902
+ * consume number, dimension, or percentage. the number is `[+-]? digits? [. digits]? [(e|E) [+-]? digits]?`:
903
+ * `1.5e2`, `1e-3`, `.5e1`. the exponent requires a digit: `2em` is the number `2` and the unit `em`.
904
+ * the token is followed by the end of the stream, a whitespace, `)`, `;`, `!`, `}`, `/`, `*`, `,` (and `+` after
905
+ * a dimension: `2n+1`): otherwise it is not a numeric token
906
+ * @param parseInfo
907
+ * @returns the length of the token, 0 if it is not a numeric token
908
+ */
909
+ consumeNumericToken(parseInfo) {
910
+ const codes = parseInfo.codes;
911
+ const offset = parseInfo.currentPosition - parseInfo.offset;
912
+ let position = offset;
913
+ let hasDigits = false;
914
+ let codepoint = codes[position];
915
+ this.slice = null;
916
+ this.hint = null;
917
+ // '+' '-'
918
+ if (codepoint === 43 /* TokenMap.PLUS */ || codepoint === 45 /* TokenMap.MINUS */) {
919
+ position++;
920
+ }
921
+ // integer part
922
+ while (isDigit(codes[position])) {
923
+ position++;
924
+ hasDigits = true;
925
+ }
926
+ // decimal part
927
+ if (codes[position] === 46 /* TokenMap.DOT */ && isDigit(codes[position + 1])) {
928
+ position++;
929
+ while (isDigit(codes[position])) {
930
+ position++;
796
931
  }
932
+ hasDigits = true;
797
933
  }
798
934
  if (!hasDigits) {
799
935
  return 0;
800
936
  }
801
- if (hasPercent) {
937
+ // exponent: 'E' 'e', followed by a digit or a sign and a digit
938
+ codepoint = codes[position];
939
+ if (codepoint === 0x45 || codepoint === 0x65) {
940
+ let k = position + 1;
941
+ if (codes[k] === 43 /* TokenMap.PLUS */ || codes[k] === 45 /* TokenMap.MINUS */) {
942
+ k++;
943
+ }
944
+ if (isDigit(codes[k])) {
945
+ position = k;
946
+ while (isDigit(codes[position])) {
947
+ position++;
948
+ }
949
+ }
950
+ }
951
+ codepoint = codes[position];
952
+ // percentage
953
+ if (codepoint === 37 /* TokenMap.PERCENTAGE */) {
802
954
  const slice = position;
803
- codepoint = parseInfo.stream.charCodeAt(++position);
804
- if (codepoint != codepoint ||
805
- isWhiteSpace(codepoint) ||
806
- codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
807
- codepoint == 59 /* TokenMap.SEMICOLON */ ||
808
- codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
809
- codepoint == 47 /* TokenMap.SLASH */ ||
810
- codepoint == 42 /* TokenMap.STAR */ ||
811
- codepoint == 44 /* TokenMap.COMMA */) {
812
- this.slice = slice;
813
- this.hint = EnumToken.PercentageTokenType;
814
- return position - offset;
955
+ if (!isNumericTokenEnd(codes[++position], false)) {
956
+ return 0;
815
957
  }
816
- return 0;
958
+ this.slice = slice;
959
+ this.hint = EnumToken.PercentageTokenType;
960
+ return position - offset;
817
961
  }
818
- if (hasLetter) {
819
- codepoint = parseInfo.stream.charCodeAt(position - 1);
820
- // 'E' 'e'
821
- const slice = codepoint == 0x45 || codepoint == 0x65 ? position - 1 : position;
822
- while (position + 1 <= parseInfo.stream.length) {
823
- codepoint = parseInfo.stream.charCodeAt(++position);
824
- if (!isLetter(codepoint) && codepoint != 0x2d) {
962
+ // dimension: a unit, or a dashed ident (user defined unit)
963
+ if (isLetter(codepoint) || (codepoint === 45 /* TokenMap.MINUS */ && codes[position + 1] === 45 /* TokenMap.MINUS */)) {
964
+ const slice = position;
965
+ while (position < parseInfo.length) {
966
+ codepoint = codes[++position];
967
+ if (!isLetter(codepoint) && codepoint !== 45 /* TokenMap.MINUS */) {
825
968
  break;
826
969
  }
827
970
  }
828
- if (codepoint != codepoint ||
829
- isWhiteSpace(codepoint) ||
830
- codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
831
- codepoint == 59 /* TokenMap.SEMICOLON */ ||
832
- codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
833
- codepoint == 43 /* TokenMap.PLUS */ ||
834
- codepoint == 47 /* TokenMap.SLASH */ ||
835
- codepoint == 42 /* TokenMap.STAR */ ||
836
- codepoint == 44 /* TokenMap.COMMA */) {
837
- this.slice = slice;
838
- this.hint = getSymbolHint(parseInfo, slice, position) ?? EnumToken.DimensionTokenType;
839
- return position - offset;
971
+ if (!isNumericTokenEnd(codes[position], true)) {
972
+ return 0;
840
973
  }
841
- return 0;
974
+ this.slice = slice;
975
+ this.hint = getSymbolHint(parseInfo, slice, position) ?? EnumToken.DimensionTokenType;
976
+ return position - offset;
842
977
  }
843
- return 0;
978
+ // number
979
+ return isNumericTokenEnd(codepoint, false) ? position - offset : 0;
844
980
  }
845
981
  /**
846
982
  *
@@ -851,36 +987,36 @@ class Tokenizer {
851
987
  let position = parseInfo.currentPosition - parseInfo.offset;
852
988
  let offset = position;
853
989
  this.identNonAscii = false;
854
- let codepoint = parseInfo.stream.charCodeAt(position);
855
- if (!isIdentStart(codepoint) && codepoint != 45 /* TokenMap.MINUS */) {
990
+ let codepoint = parseInfo.codes[position];
991
+ if (!isIdentStart(codepoint) && codepoint !== 45 /* TokenMap.MINUS */) {
856
992
  return 0;
857
993
  }
858
- if (codepoint == 45 /* TokenMap.MINUS */) {
994
+ if (codepoint === 45 /* TokenMap.MINUS */) {
859
995
  position++;
860
- codepoint = parseInfo.stream.charCodeAt(position);
861
- if (!isIdentStart(codepoint) && codepoint != 45 /* TokenMap.MINUS */) {
996
+ codepoint = parseInfo.codes[position];
997
+ if (!isIdentStart(codepoint) && codepoint !== 45 /* TokenMap.MINUS */) {
862
998
  return 0;
863
999
  }
864
1000
  }
865
- while ((codepoint = parseInfo.stream.charCodeAt(position)) == codepoint) {
866
- if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1001
+ while ((codepoint = parseInfo.codes[position]) !== undefined) {
1002
+ if (codepoint === 92 /* TokenMap.REVERSE_SOLIDUS */) {
867
1003
  // eof
868
- if ((codepoint = parseInfo.stream.charCodeAt(position + 1)) != codepoint) {
1004
+ if ((codepoint = parseInfo.codes[position + 1]) === undefined) {
869
1005
  return 0;
870
1006
  }
871
1007
  // \n \r \f \v
872
- if (codepoint == 0xa ||
873
- codepoint == 0xb ||
874
- codepoint == 0xc ||
875
- codepoint == 0xd ||
876
- codepoint == 0x2028 ||
877
- codepoint == 0x2029) {
1008
+ if (codepoint === 0xa ||
1009
+ codepoint === 0xb ||
1010
+ codepoint === 0xc ||
1011
+ codepoint === 0xd ||
1012
+ codepoint === 0x2028 ||
1013
+ codepoint === 0x2029) {
878
1014
  return 0;
879
1015
  }
880
1016
  position += 2;
881
1017
  continue;
882
1018
  }
883
- if (codepoint == 0x2d || isIdentCodepoint(codepoint)) {
1019
+ if (codepoint === 0x2d || isIdentCodepoint(codepoint)) {
884
1020
  if (codepoint > 0x7f) {
885
1021
  this.identNonAscii = true;
886
1022
  }
@@ -911,7 +1047,7 @@ class Tokenizer {
911
1047
  case 43 /* TokenMap.PLUS */:
912
1048
  return position - offset;
913
1049
  }
914
- if (codepoint != codepoint || isWhiteSpace(codepoint)) {
1050
+ if (codepoint === undefined || isWhiteSpace(codepoint)) {
915
1051
  return position - offset;
916
1052
  }
917
1053
  return 0;
@@ -927,14 +1063,14 @@ class Tokenizer {
927
1063
  consumeColor(parseInfo) {
928
1064
  let position = parseInfo.currentPosition - parseInfo.offset;
929
1065
  let offset = position;
930
- let codepoint = parseInfo.stream.charCodeAt(position);
931
- if (codepoint != 35 /* TokenMap.HASH */) {
1066
+ let codepoint = parseInfo.codes[position];
1067
+ if (codepoint !== 35 /* TokenMap.HASH */) {
932
1068
  return 0;
933
1069
  }
934
1070
  position++;
935
1071
  let count = 0;
936
1072
  while (true) {
937
- codepoint = parseInfo.stream.charCodeAt(position);
1073
+ codepoint = parseInfo.codes[position];
938
1074
  // 'a-f0-9' 'A-F0-9'
939
1075
  if ((codepoint >= 0x30 && codepoint <= 0x39) ||
940
1076
  (codepoint >= 0x61 && codepoint <= 0x66) ||
@@ -945,16 +1081,16 @@ class Tokenizer {
945
1081
  }
946
1082
  break;
947
1083
  }
948
- if (count != 3 && count != 4 && count != 6 && count != 8) {
1084
+ if (count !== 3 && count !== 4 && count !== 6 && count !== 8) {
949
1085
  return 0;
950
1086
  }
951
- codepoint = parseInfo.stream.charCodeAt(position);
952
- if (codepoint != codepoint ||
1087
+ codepoint = parseInfo.codes[position];
1088
+ if (codepoint === undefined ||
953
1089
  isWhiteSpace(codepoint) ||
954
- codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
955
- codepoint == 59 /* TokenMap.SEMICOLON */ ||
956
- codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
957
- codepoint == 44 /* TokenMap.COMMA */) {
1090
+ codepoint === 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
1091
+ codepoint === 59 /* TokenMap.SEMICOLON */ ||
1092
+ codepoint === 125 /* TokenMap.RIGHT_BRACE */ ||
1093
+ codepoint === 44 /* TokenMap.COMMA */) {
958
1094
  return position - offset;
959
1095
  }
960
1096
  return 0;
@@ -963,18 +1099,18 @@ class Tokenizer {
963
1099
  let charCode;
964
1100
  // consume an <url>
965
1101
  // inlined: this.peekCharCode(parseInfo)
966
- while (isWhiteSpace(parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset))) {
1102
+ while (isWhiteSpace(parseInfo.codes[parseInfo.currentPosition - parseInfo.offset])) {
967
1103
  this.advanceCode(parseInfo);
968
1104
  }
969
1105
  // inlined: this.peekCharCode(parseInfo)
970
- charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
971
- if (charCode == 34 /* TokenMap.DOUBLE_QUOTE */ || charCode == 39 /* TokenMap.SINGLE_QUOTE */) {
1106
+ charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset];
1107
+ if (charCode === 34 /* TokenMap.DOUBLE_QUOTE */ || charCode === 39 /* TokenMap.SINGLE_QUOTE */) {
972
1108
  return this.consumeStringAsURLToken(parseInfo);
973
1109
  }
974
1110
  do {
975
1111
  this.advanceCode(parseInfo);
976
1112
  // inlined: this.peekCharCode(parseInfo)
977
- charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
1113
+ charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset];
978
1114
  } while (
979
1115
  // !(value === "/" && this.match(parseInfo, "/*") &&
980
1116
  charCode !== 41 /* TokenMap.RIGHT_PARENTHESIS */ &&
@@ -983,14 +1119,14 @@ class Tokenizer {
983
1119
  if (charCode !== 41 /* TokenMap.RIGHT_PARENTHESIS */) {
984
1120
  let k = 1;
985
1121
  while (k < endPosition) {
986
- charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + k);
1122
+ charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset + k];
987
1123
  if (isWhiteSpace(charCode)) {
988
1124
  k++;
989
1125
  continue;
990
1126
  }
991
1127
  break;
992
1128
  }
993
- if (charCode != charCode || (charCode != 41 /* TokenMap.RIGHT_PARENTHESIS */ && !isIdentStart(charCode))) {
1129
+ if (charCode === undefined || (charCode !== 41 /* TokenMap.RIGHT_PARENTHESIS */ && !isIdentStart(charCode))) {
994
1130
  this.advance(parseInfo, k);
995
1131
  return this.makeToken(parseInfo, EnumToken.BadUrlTokenType);
996
1132
  }
@@ -998,97 +1134,60 @@ class Tokenizer {
998
1134
  return this.makeToken(parseInfo,
999
1135
  // parseInfo.position < parseInfo.currentPosition
1000
1136
  // inlined: this.peekCharCode(parseInfo)
1001
- (charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) != charCode || !this.isURLToken(parseInfo)
1137
+ (charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset]) === undefined ||
1138
+ !this.isURLToken(parseInfo)
1002
1139
  ? EnumToken.BadUrlTokenType
1003
1140
  : EnumToken.UrlTokenTokenType);
1004
1141
  }
1005
1142
  /**
1006
- *
1143
+ * set the current token: its type, its numeric value and the range of its string value (see {@link field} and
1144
+ * {@link valueKind}). no string is created
1007
1145
  * @param parseInfo
1008
- * @param hint
1009
- * @param options
1146
+ * @param hint token type
1147
+ * @param slice start of the unit of a dimension, or of the value
1148
+ * @param decodeSegments the value contains escape sequences
1010
1149
  * @returns
1011
1150
  */
1012
1151
  makeToken(parseInfo, hint, slice, decodeSegments) {
1013
- let val = null;
1152
+ const start = parseInfo.position - parseInfo.offset;
1153
+ const end = parseInfo.currentPosition - parseInfo.offset;
1154
+ // the spelling of a known unit or function: a range of the canonical text, -1 otherwise
1155
+ let canonical = -1;
1156
+ let valueStart = start;
1157
+ let hasValue = false;
1014
1158
  this.typ = null;
1015
- this.nam = null;
1016
- this.val = null;
1017
- this.unit = null;
1018
1159
  this.kin = null;
1019
- this.decodeString = null;
1160
+ this.num = 0;
1161
+ this.field = 0 /* TokenValueField.None */;
1020
1162
  this.slice = null;
1021
1163
  this.hint = null;
1022
1164
  if (slice) {
1023
1165
  this.slice = slice;
1024
1166
  }
1025
- if (decodeSegments) {
1026
- this.decodeString = true;
1027
- }
1028
1167
  if (hint != null) {
1029
1168
  // the spelling of units and function names is looked up in the tables
1030
1169
  let isList = false;
1031
1170
  let hasUnit = false;
1032
1171
  switch (hint) {
1033
1172
  case EnumToken.TransformFunctionTokenDefType:
1034
- isList = true;
1035
- break;
1036
1173
  case EnumToken.ColorFunctionTokenDefType:
1037
- isList = true;
1038
- break;
1039
1174
  case EnumToken.ContainerFunctionTokenDefType:
1040
- isList = true;
1041
- break;
1042
1175
  case EnumToken.UrlFunctionTokenDefType:
1043
- isList = true;
1044
- break;
1045
1176
  case EnumToken.GridTemplateFuncTokenDefType:
1046
- isList = true;
1047
- break;
1048
1177
  case EnumToken.ImageFunctionTokenDefType:
1049
- isList = true;
1050
- break;
1051
1178
  case EnumToken.TimelineFunctionTokenDefType:
1052
- isList = true;
1053
- break;
1054
- // case EnumToken.GeneralEnclosedFunctionTokenDefType:
1055
- // searchArray = generalEnclosedFunc;
1056
- // break;
1057
1179
  case EnumToken.SupportsFunctionTokenDefType:
1058
- isList = true;
1059
- break;
1060
1180
  case EnumToken.TimingFunctionTokenDefType:
1061
- isList = true;
1062
- break;
1063
1181
  case EnumToken.MathFunctionTokenDefType:
1064
- isList = true;
1065
- break;
1066
1182
  case EnumToken.WhenElseFunctionTokenDefType:
1067
- isList = true;
1068
- break;
1069
1183
  case EnumToken.WildCardFunctionTokenDefType:
1070
1184
  isList = true;
1071
1185
  break;
1072
1186
  case EnumToken.FrequencyTokenType:
1073
- isList = true;
1074
- hasUnit = true;
1075
- break;
1076
1187
  case EnumToken.ResolutionTokenType:
1077
- isList = true;
1078
- hasUnit = true;
1079
- break;
1080
1188
  case EnumToken.LengthTokenType:
1081
- isList = true;
1082
- hasUnit = true;
1083
- break;
1084
1189
  case EnumToken.FlexTokenType:
1085
- isList = true;
1086
- hasUnit = true;
1087
- break;
1088
1190
  case EnumToken.AngleTokenType:
1089
- isList = true;
1090
- hasUnit = true;
1091
- break;
1092
1191
  case EnumToken.TimeTokenType:
1093
1192
  isList = true;
1094
1193
  hasUnit = true;
@@ -1099,56 +1198,73 @@ class Tokenizer {
1099
1198
  }
1100
1199
  const isHint = hintsEnum.has(hint);
1101
1200
  if (isList) {
1102
- val = findItem(hint, parseInfo, hasUnit ? slice : parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
1201
+ valueStart = hasUnit ? slice : start;
1202
+ canonical = findItem(hint, parseInfo, valueStart, end);
1203
+ hasValue = canonical !== -1;
1103
1204
  }
1104
- else if (hint == EnumToken.NumberTokenType || hint == EnumToken.WhitespaceTokenType) ;
1205
+ else if (hint === EnumToken.NumberTokenType || hint === EnumToken.WhitespaceTokenType) ;
1105
1206
  else if (!isHint) {
1106
- val = parseInfo.stream.slice(slice ?? parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
1207
+ valueStart = slice ?? start;
1208
+ // the canonical spelling of a known unit: `6DB` is rendered `6dB`
1209
+ canonical =
1210
+ hint === EnumToken.DimensionTokenType
1211
+ ? findItem(EnumToken.DimensionTokenType, parseInfo, valueStart, end)
1212
+ : -1;
1213
+ hasValue = true;
1107
1214
  }
1108
- if (this.decodeString) {
1109
- val = decodeEscapeSequences(val);
1110
- }
1111
- if (isHint) {
1112
- this.typ = hint;
1113
- }
1114
- else {
1115
- this.typ = hint;
1116
- if (hasUnit || hint == EnumToken.PercentageTokenType || hint == EnumToken.DimensionTokenType) {
1117
- this.val = parseNumber(parseInfo.stream, parseInfo.position - parseInfo.offset, slice);
1118
- if (hint != EnumToken.PercentageTokenType) {
1119
- this.unit = val;
1120
- if (val.startsWith("--")) {
1215
+ this.typ = hint;
1216
+ if (!isHint) {
1217
+ if (hasUnit || hint === EnumToken.PercentageTokenType || hint === EnumToken.DimensionTokenType) {
1218
+ this.num = parseNumber(parseInfo.codes, start, slice);
1219
+ if (hint !== EnumToken.PercentageTokenType) {
1220
+ this.field = hasValue ? 4 /* TokenValueField.Unit */ : 1 /* TokenValueField.Number */;
1221
+ // a dashed ident: a user defined unit. the canonical units are not dashed idents
1222
+ if (canonical === -1 &&
1223
+ parseInfo.codes[valueStart] === 0x2d &&
1224
+ parseInfo.codes[valueStart + 1] === 0x2d) {
1121
1225
  this.typ = EnumToken.UserDefinedDimensionTokenType;
1122
1226
  }
1123
1227
  }
1228
+ else {
1229
+ this.field = 1 /* TokenValueField.Number */;
1230
+ }
1124
1231
  }
1125
- else if (hint == EnumToken.NumberTokenType) {
1126
- this.val = parseNumber(parseInfo.stream, slice ?? parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
1232
+ else if (hint === EnumToken.NumberTokenType) {
1233
+ this.num = parseNumber(parseInfo.codes, slice ?? start, end);
1234
+ this.field = 1 /* TokenValueField.Number */;
1127
1235
  }
1128
- else if (hint == EnumToken.AtRuleTokenType) {
1129
- this.nam = val;
1236
+ else if (hint === EnumToken.AtRuleTokenType) {
1237
+ this.field = hasValue ? 2 /* TokenValueField.Name */ : 0 /* TokenValueField.None */;
1130
1238
  }
1131
1239
  else {
1132
- this.val = val;
1133
- if (hint == EnumToken.ColorTokenType) {
1240
+ this.field = hasValue ? 3 /* TokenValueField.Value */ : 0 /* TokenValueField.None */;
1241
+ if (hint === EnumToken.ColorTokenType) {
1134
1242
  this.kin = ColorType.HEX;
1135
1243
  }
1136
1244
  }
1137
1245
  }
1138
1246
  }
1139
- else {
1140
- if (this.equalsIgnoreCase(parseInfo, "!important")) {
1141
- this.typ = EnumToken.ImportantTokenType;
1142
- }
1247
+ else if (this.equalsIgnoreCase(parseInfo, "!important")) {
1248
+ this.typ = EnumToken.ImportantTokenType;
1143
1249
  }
1144
1250
  if (this.typ == null) {
1145
- val = parseInfo.stream.slice(parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
1146
- if (decodeSegments) {
1147
- val = decodeEscapeSequences(val);
1148
- this.decodeString = true;
1149
- }
1251
+ valueStart = start;
1252
+ hasValue = true;
1150
1253
  this.typ = EnumToken.LiteralTokenType;
1151
- this.val = val;
1254
+ this.field = 3 /* TokenValueField.Value */;
1255
+ }
1256
+ if (hasValue) {
1257
+ if (canonical !== -1) {
1258
+ this.valueKind = 1 /* TokenValueKind.Canonical */;
1259
+ this.valueStart = canonicalStart(canonical);
1260
+ this.valueEnd = canonicalEnd(canonical);
1261
+ }
1262
+ else {
1263
+ // the escape sequences are decoded when the value is interned
1264
+ this.valueKind = decodeSegments ? 2 /* TokenValueKind.Escaped */ : 0 /* TokenValueKind.Source */;
1265
+ this.valueStart = valueStart;
1266
+ this.valueEnd = end;
1267
+ }
1152
1268
  }
1153
1269
  this.srcId = parseInfo.source.id;
1154
1270
  this.sta = parseInfo.position;
@@ -1158,24 +1274,65 @@ class Tokenizer {
1158
1274
  return this;
1159
1275
  }
1160
1276
  /**
1277
+ * id of the string value of the current token in the arena (see {@link AstArena.internRange}). no string is
1278
+ * created:
1279
+ *
1280
+ * - a range of the source: the arena stores the source index and the offsets. without a source index (the tokens
1281
+ * have no location, see `parseString()`), the code units are copied to the string pool of the arena
1282
+ * - a range of the canonical text: the arena stores the offsets
1283
+ * - a value with escape sequences: the decoded code units are copied to the string pool of the arena
1161
1284
  *
1285
+ * @param arena
1286
+ * @param sourceIndex index of the source in the arena, -1 when the tokens have no location
1287
+ */
1288
+ internValue(arena, sourceIndex) {
1289
+ switch (this.valueKind) {
1290
+ case 1 /* TokenValueKind.Canonical */:
1291
+ return arena.internRange(canonicalCodes(), this.valueStart, this.valueEnd, -2 /* StringSource.Canonical */);
1292
+ case 2 /* TokenValueKind.Escaped */: {
1293
+ // the buffer may be reallocated by the decoder: it is read after the decoding
1294
+ const length = decodeEscapes(this.parseInfo.codes, this.valueStart, this.valueEnd);
1295
+ return arena.internRange(decoded, 0, length, -1 /* StringSource.Pool */);
1296
+ }
1297
+ default:
1298
+ return arena.internRange(this.parseInfo.codes, this.valueStart, this.valueEnd, sourceIndex < 0 ? -1 /* StringSource.Pool */ : sourceIndex);
1299
+ }
1300
+ }
1301
+ /**
1302
+ * content of the source being tokenized: the offsets of the tokens are offsets of the content
1303
+ */
1304
+ get content() {
1305
+ return this.parseInfo.source.content;
1306
+ }
1307
+ /**
1308
+ * code units of the source being tokenized
1309
+ */
1310
+ get codes() {
1311
+ return this.parseInfo.codes;
1312
+ }
1313
+ /**
1314
+ * true if the text of the token being read equals the input, ignoring the case. the token text starts at
1315
+ * parseInfo.position: `0!important` is the number `0` followed by `!important`
1162
1316
  * @param parseInfo
1163
1317
  * @param input
1164
1318
  * @returns
1165
1319
  */
1166
1320
  equalsIgnoreCase(parseInfo, input) {
1167
- let position = parseInfo.currentPosition - parseInfo.offset;
1321
+ let position = parseInfo.position - parseInfo.offset;
1322
+ if (parseInfo.currentPosition - parseInfo.position !== input.length) {
1323
+ return false;
1324
+ }
1168
1325
  let ca;
1169
1326
  let cb;
1170
1327
  for (let i = 0; i < input.length; i++) {
1171
- ca = parseInfo.stream.charCodeAt(position + i);
1328
+ ca = parseInfo.codes[position + i];
1172
1329
  cb = input.charCodeAt(i);
1173
1330
  // Normalize A-Z to a-z
1174
1331
  if (ca >= 65 && ca <= 90)
1175
1332
  ca += 32;
1176
1333
  if (cb >= 65 && cb <= 90)
1177
1334
  cb += 32;
1178
- if (ca != cb) {
1335
+ if (ca !== cb) {
1179
1336
  return false;
1180
1337
  }
1181
1338
  }
@@ -1191,58 +1348,62 @@ class Tokenizer {
1191
1348
  let position = parseInfo.currentPosition - parseInfo.offset;
1192
1349
  // compare the character codes: no single character strings
1193
1350
  for (let i = 0; i < input.length; i++) {
1194
- if (parseInfo.stream.charCodeAt(position + i) != input.charCodeAt(i)) {
1351
+ if (parseInfo.codes[position + i] !== input.charCodeAt(i)) {
1195
1352
  return false;
1196
1353
  }
1197
1354
  }
1198
1355
  return true;
1199
1356
  }
1200
1357
  /**
1201
- * Get the current character code without creating a string
1202
- * @param parseInfo
1203
- * @returns charCode at current position
1204
- */
1205
- peekCharCode(parseInfo) {
1206
- return parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
1207
- }
1208
- /**
1209
- *
1358
+ * length of the escape sequence that follows the backslash at the current position: the hexadecimal digits (6
1359
+ * characters at most, the space included) followed by an optional space. 0 when there is no hexadecimal digit.
1360
+ * the code units are read: no string is created
1210
1361
  * @param parseInfo
1211
- * @param count
1212
- * @returns
1213
1362
  */
1214
- peek(parseInfo, count = 1) {
1215
- if (count == 1) {
1216
- return parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset);
1217
- }
1363
+ escapeSequenceLength(parseInfo) {
1364
+ const codes = parseInfo.codes;
1218
1365
  const position = parseInfo.currentPosition - parseInfo.offset;
1219
- return parseInfo.stream.slice(position, position + count);
1366
+ const limit = parseInfo.length - position < 7 ? parseInfo.length - position : 7;
1367
+ let digits = 0;
1368
+ let length = 0;
1369
+ for (let i = 1; i < limit; i++) {
1370
+ const codepoint = codes[position + i];
1371
+ if (codepoint === 0x20) {
1372
+ length++;
1373
+ break;
1374
+ }
1375
+ if (!isHexDigit(codepoint)) {
1376
+ break;
1377
+ }
1378
+ digits++;
1379
+ length++;
1380
+ }
1381
+ return digits > 0 ? length : 0;
1220
1382
  }
1221
1383
  /**
1222
- * move forward by count characters (at most to the end of the stream) and record the new lines. the characters
1223
- * are read from the stream: no string is created
1384
+ * move forward by count characters (at most to the end of the stream) and record the new lines. the code units are
1385
+ * read: no string is created
1224
1386
  * @param parseInfo
1225
1387
  * @param count
1226
1388
  */
1227
1389
  advance(parseInfo, count = 1) {
1228
- const stream = parseInfo.stream;
1390
+ const codes = parseInfo.codes;
1229
1391
  const position = parseInfo.currentPosition - parseInfo.offset;
1230
- const remaining = stream.length - position;
1392
+ const remaining = parseInfo.length - position;
1231
1393
  const length = count < remaining ? (count > 0 ? count : 0) : remaining > 0 ? remaining : 0;
1232
1394
  const lineStarts = parseInfo.source.lineStarts.lineStarts;
1233
1395
  let codepoint;
1234
1396
  for (let i = 0; i < length; i++) {
1235
- codepoint = stream.charCodeAt(position + i);
1236
- if (codepoint == 0xa || // \n
1237
- codepoint == 0xb || // \v
1238
- codepoint == 0xc || // \f
1239
- codepoint == 0xd || // \r
1240
- codepoint == 0x2028 || // \u2028
1241
- codepoint == 0x2029 // \u2029
1397
+ codepoint = codes[position + i];
1398
+ if (codepoint === 0xa || // \n
1399
+ codepoint === 0xb || // \v
1400
+ codepoint === 0xc || // \f
1401
+ codepoint === 0xd || // \r
1402
+ codepoint === 0x2028 || // \u2028
1403
+ codepoint === 0x2029 // \u2029
1242
1404
  ) {
1243
- // \r\n: a single line start
1244
- if (codepoint == 0xa && i > 0 && stream.charCodeAt(position + i - 1) == 0xd) ;
1245
- else {
1405
+ // \r\n: a single line start, at the \n
1406
+ if (codepoint !== 0xd || codes[position + i + 1] !== 0xa) {
1246
1407
  lineStarts.push(position + parseInfo.offset + i);
1247
1408
  }
1248
1409
  }
@@ -1256,7 +1417,7 @@ class Tokenizer {
1256
1417
  * @param count
1257
1418
  */
1258
1419
  skip(parseInfo, count) {
1259
- const remaining = parseInfo.stream.length - (parseInfo.currentPosition - parseInfo.offset);
1420
+ const remaining = parseInfo.length - (parseInfo.currentPosition - parseInfo.offset);
1260
1421
  parseInfo.currentPosition += count < remaining ? count : remaining > 0 ? remaining : 0;
1261
1422
  }
1262
1423
  /**
@@ -1280,16 +1441,18 @@ class Tokenizer {
1280
1441
  */
1281
1442
  advanceCode(parseInfo) {
1282
1443
  const position = parseInfo.currentPosition - parseInfo.offset;
1283
- const codepoint = parseInfo.stream.charCodeAt(position);
1284
- if (codepoint != codepoint) {
1285
- return codepoint;
1286
- }
1287
- if (codepoint == 0xa || // \n
1288
- codepoint == 0xb || // \v
1289
- codepoint == 0xc || // \f
1290
- codepoint == 0xd || // \r
1291
- codepoint == 0x2028 ||
1292
- codepoint == 0x2029) {
1444
+ const codepoint = parseInfo.codes[position];
1445
+ // the end of the stream: NaN
1446
+ if (codepoint === undefined) {
1447
+ return NaN;
1448
+ }
1449
+ if (codepoint === 0xa || // \n
1450
+ codepoint === 0xb || // \v
1451
+ codepoint === 0xc || // \f
1452
+ // \r\n: a single line start, at the \n
1453
+ (codepoint === 0xd && parseInfo.codes[position + 1] !== 0xa) ||
1454
+ codepoint === 0x2028 ||
1455
+ codepoint === 0x2029) {
1293
1456
  parseInfo.source.lineStarts.lineStarts.push(position + parseInfo.offset);
1294
1457
  }
1295
1458
  parseInfo.currentPosition++;
@@ -1319,15 +1482,15 @@ class Tokenizer {
1319
1482
  // }
1320
1483
  // }
1321
1484
  j--;
1322
- let codepoint = parseInfo.stream.charCodeAt(i);
1485
+ let codepoint = parseInfo.codes[i];
1323
1486
  // -
1324
- if (codepoint == 0x2d) {
1487
+ if (codepoint === 0x2d) {
1325
1488
  let nextCodepoint;
1326
- // NaN != NaN
1327
- if ((nextCodepoint = parseInfo.stream.charCodeAt(i + 1)) != nextCodepoint) {
1489
+ // the end of the stream
1490
+ if ((nextCodepoint = parseInfo.codes[i + 1]) === undefined) {
1328
1491
  return false;
1329
1492
  }
1330
- if (!isIdentStart(nextCodepoint) && nextCodepoint != 0x2d) {
1493
+ if (!isIdentStart(nextCodepoint) && nextCodepoint !== 0x2d) {
1331
1494
  return false;
1332
1495
  }
1333
1496
  codepoint = nextCodepoint;
@@ -1336,17 +1499,18 @@ class Tokenizer {
1336
1499
  if (codepoint !== 0x2d && !isIdentStart(codepoint)) {
1337
1500
  return false;
1338
1501
  }
1339
- if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1340
- codepoint = parseInfo.stream.charCodeAt(i + 1);
1341
- i += String.fromCodePoint(codepoint).length;
1502
+ if (codepoint === 92 /* TokenMap.REVERSE_SOLIDUS */) {
1503
+ codepoint = parseInfo.codes[i + 1];
1504
+ // a code unit
1505
+ i++;
1342
1506
  }
1343
1507
  while (i < j) {
1344
- i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1345
- codepoint = parseInfo.stream.charCodeAt(i);
1346
- if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1347
- i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1348
- codepoint = parseInfo.stream.charCodeAt(i);
1349
- i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1508
+ i++;
1509
+ codepoint = parseInfo.codes[i];
1510
+ if (codepoint === 92 /* TokenMap.REVERSE_SOLIDUS */) {
1511
+ i++;
1512
+ codepoint = parseInfo.codes[i];
1513
+ i++;
1350
1514
  continue;
1351
1515
  }
1352
1516
  if (codepoint !== 0x2d && !isIdentCodepoint(codepoint)) {
@@ -1366,7 +1530,7 @@ class Tokenizer {
1366
1530
  let j = input.length;
1367
1531
  // compare the character codes: no single character strings
1368
1532
  while (i < j) {
1369
- if (parseInfo.stream.charCodeAt(parseInfo.position - parseInfo.offset + i) != input.charCodeAt(i)) {
1533
+ if (parseInfo.codes[parseInfo.position - parseInfo.offset + i] !== input.charCodeAt(i)) {
1370
1534
  return false;
1371
1535
  }
1372
1536
  i++;
@@ -1382,30 +1546,30 @@ class Tokenizer {
1382
1546
  let i = parseInfo.position - parseInfo.offset;
1383
1547
  let c;
1384
1548
  while (++i < parseInfo.currentPosition) {
1385
- c = parseInfo.stream.charCodeAt(i);
1549
+ c = parseInfo.codes[i];
1386
1550
  // single quote or double quote or start parenthesis or close parenthesis
1387
- if (isNonPrintable(c) || c == 0x27 || c == 0x22 || c == 0x28 || c == 0x29) {
1551
+ if (isNonPrintable(c) || c === 0x27 || c === 0x22 || c === 0x28 || c === 0x29) {
1388
1552
  return false;
1389
1553
  }
1390
1554
  // valid escape
1391
- if (c == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1555
+ if (c === 92 /* TokenMap.REVERSE_SOLIDUS */) {
1392
1556
  i++;
1393
1557
  if (i >= parseInfo.currentPosition) {
1394
1558
  return false;
1395
1559
  }
1396
- c = parseInfo.stream.charCodeAt(i);
1560
+ c = parseInfo.codes[i];
1397
1561
  // c is not '\n' or '\r' or '\f'
1398
- if (c == 0x6e || c == 0x72 || c == 0x66) {
1562
+ if (c === 0x6e || c === 0x72 || c === 0x66) {
1399
1563
  return false;
1400
1564
  }
1401
1565
  continue;
1402
1566
  }
1403
1567
  // is white space
1404
- if (c == 0x20 || c == 0x09) {
1568
+ if (c === 0x20 || c === 0x09) {
1405
1569
  break;
1406
1570
  }
1407
1571
  }
1408
- return i == parseInfo.currentPosition;
1572
+ return i === parseInfo.currentPosition;
1409
1573
  }
1410
1574
  done() {
1411
1575
  return this.typ === EnumToken.EOF;
@@ -1420,19 +1584,26 @@ class Tokenizer {
1420
1584
  let nextCharCode;
1421
1585
  // const result: TokenizeResult[] = [];
1422
1586
  // allow 10 characters buffer for the streaming parser to avoid incomplete tokens
1423
- const endPosition = parseInfo.stream.length - 1; // yieldEOFToken ? parseInfo.stream.length - 1 : parseInfo.stream.length - 10;
1587
+ const endPosition = parseInfo.length - 1;
1424
1588
  let tokensCount;
1425
- // NaN is not equal to NaN
1426
- // inlined: this.peekCharCode(parseInfo)
1427
- while ((charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) == charCode) {
1589
+ // the code units are undefined after the end of the stream
1590
+ while ((charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset]) !== undefined) {
1428
1591
  if (this.state === EnumToken.UrlFunctionTokenDefType) {
1429
1592
  this.state = null;
1430
1593
  return this.consumeURLToken(parseInfo, endPosition);
1431
1594
  }
1432
- if (parseInfo.position == parseInfo.currentPosition) {
1433
- if (charCode == 45 /* TokenMap.MINUS */ ||
1434
- charCode == 43 /* TokenMap.PLUS */ ||
1435
- charCode == 46 /* TokenMap.DOT */ ||
1595
+ if (parseInfo.position === parseInfo.currentPosition) {
1596
+ // `U+0025-00FF`
1597
+ if (charCode === 0x55 || charCode === 0x75) {
1598
+ tokensCount = this.consumeUnicodeRangeToken(parseInfo);
1599
+ if (tokensCount > 0) {
1600
+ this.skip(parseInfo, tokensCount);
1601
+ return this.makeToken(parseInfo, EnumToken.UniCodeRange);
1602
+ }
1603
+ }
1604
+ if (charCode === 45 /* TokenMap.MINUS */ ||
1605
+ charCode === 43 /* TokenMap.PLUS */ ||
1606
+ charCode === 46 /* TokenMap.DOT */ ||
1436
1607
  isDigit(charCode)) {
1437
1608
  tokensCount = this.consumeNumericToken(parseInfo);
1438
1609
  if (tokensCount > 0) {
@@ -1441,27 +1612,28 @@ class Tokenizer {
1441
1612
  return this.makeToken(parseInfo, this.hint ?? EnumToken.NumberTokenType, this.slice);
1442
1613
  }
1443
1614
  }
1444
- if (isIdentStart(charCode) || charCode == 45 /* TokenMap.MINUS */) {
1615
+ if (isIdentStart(charCode) || charCode === 45 /* TokenMap.MINUS */) {
1445
1616
  tokensCount = this.consumeIdentToken(parseInfo);
1446
1617
  if (tokensCount > 0) {
1447
1618
  this.skipIdent(parseInfo, tokensCount);
1448
1619
  // inlined: this.peekCharCode(parseInfo)
1449
- charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
1620
+ charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset];
1450
1621
  // do not match function
1451
- if (40 /* TokenMap.LEFT_PARENTHESIS */ != charCode) {
1622
+ if (40 /* TokenMap.LEFT_PARENTHESIS */ !== charCode) {
1452
1623
  return this.makeToken(parseInfo, this.startsWith(parseInfo, "--")
1453
1624
  ? EnumToken.DashedIdenTokenType
1454
1625
  : EnumToken.IdenTokenType);
1455
1626
  }
1456
1627
  }
1457
1628
  }
1458
- if (charCode == 64 /* TokenMap.AT */) {
1629
+ if (charCode === 64 /* TokenMap.AT */) {
1459
1630
  this.skip(parseInfo, 1);
1460
1631
  // inlined: this.peekCharCode(parseInfo)
1461
- charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
1632
+ charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset];
1462
1633
  // match at-rule
1463
1634
  // inlined: this.peekCharCode(parseInfo)
1464
- if (charCode == 45 /* TokenMap.MINUS */ || isIdentStart(parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset))) {
1635
+ if (charCode === 45 /* TokenMap.MINUS */ ||
1636
+ isIdentStart(parseInfo.codes[parseInfo.currentPosition - parseInfo.offset])) {
1465
1637
  // consume '@'
1466
1638
  parseInfo.position = parseInfo.currentPosition;
1467
1639
  tokensCount = this.consumeIdentToken(parseInfo);
@@ -1471,7 +1643,7 @@ class Tokenizer {
1471
1643
  }
1472
1644
  }
1473
1645
  }
1474
- if (charCode == 35 /* TokenMap.HASH */) {
1646
+ if (charCode === 35 /* TokenMap.HASH */) {
1475
1647
  tokensCount = this.consumeColor(parseInfo);
1476
1648
  if (tokensCount > 0) {
1477
1649
  this.skip(parseInfo, tokensCount);
@@ -1499,7 +1671,7 @@ class Tokenizer {
1499
1671
  // return this.makeToken(parseInfo);
1500
1672
  // }
1501
1673
  this.skip(parseInfo, 1);
1502
- charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
1674
+ charCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset];
1503
1675
  // if (isDigit(charCode)) {
1504
1676
  // tokensCount = this.consumeNumericToken(parseInfo);
1505
1677
  // if (tokensCount > 0) {
@@ -1512,8 +1684,8 @@ class Tokenizer {
1512
1684
  // }
1513
1685
  return this.makeToken(parseInfo, EnumToken.Plus);
1514
1686
  case 45 /* TokenMap.MINUS */:
1515
- if (parseInfo.position == parseInfo.currentPosition) {
1516
- nextCharCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1);
1687
+ if (parseInfo.position === parseInfo.currentPosition) {
1688
+ nextCharCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset + 1];
1517
1689
  // not a number
1518
1690
  if (isWhiteSpace(nextCharCode)) {
1519
1691
  this.skip(parseInfo, 1);
@@ -1550,13 +1722,6 @@ class Tokenizer {
1550
1722
  // '('
1551
1723
  case 40 /* TokenMap.LEFT_PARENTHESIS */:
1552
1724
  if (parseInfo.position < parseInfo.currentPosition) {
1553
- // if (
1554
- // parseInfo.stream[parseInfo.position - parseInfo.offset] === ":" &&
1555
- // this.isPseudo(parseInfo)
1556
- //) {
1557
- // this.skip(parseInfo, 1);
1558
- // return this.makeToken(parseInfo, EnumToken.PseudoClassFunctionTokenDefType);
1559
- // } else
1560
1725
  if (this.isIdentToken(parseInfo)) {
1561
1726
  const hint = this.startsWith(parseInfo, "--")
1562
1727
  ? EnumToken.CustomFunctionTokenDefType
@@ -1606,7 +1771,7 @@ class Tokenizer {
1606
1771
  // }
1607
1772
  this.skip(parseInfo, 1);
1608
1773
  // inlined: this.peekCharCode(parseInfo)
1609
- if (parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset) == 58 /* TokenMap.COLON */) {
1774
+ if (parseInfo.codes[parseInfo.currentPosition - parseInfo.offset] === 58 /* TokenMap.COLON */) {
1610
1775
  this.skip(parseInfo, 1);
1611
1776
  return this.makeToken(parseInfo, EnumToken.DoubleColonTokenType);
1612
1777
  }
@@ -1625,14 +1790,14 @@ class Tokenizer {
1625
1790
  }
1626
1791
  this.advanceCode(parseInfo);
1627
1792
  // inlined: this.peekCharCode(parseInfo)
1628
- nextCharCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
1629
- while (nextCharCode == 0x20 ||
1793
+ nextCharCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset];
1794
+ while (nextCharCode === 0x20 ||
1630
1795
  (nextCharCode >= 0x9 && nextCharCode <= 0xd) ||
1631
- nextCharCode == 0x2028 ||
1632
- nextCharCode == 0x2029) {
1796
+ nextCharCode === 0x2028 ||
1797
+ nextCharCode === 0x2029) {
1633
1798
  this.advanceCode(parseInfo);
1634
1799
  // inlined: this.peekCharCode(parseInfo)
1635
- nextCharCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
1800
+ nextCharCode = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset];
1636
1801
  }
1637
1802
  return this.makeToken(parseInfo, EnumToken.WhitespaceTokenType);
1638
1803
  case 44 /* TokenMap.COMMA */:
@@ -1722,8 +1887,8 @@ class Tokenizer {
1722
1887
  return this.makeToken(parseInfo, getSymbolHint(parseInfo, parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset));
1723
1888
  }
1724
1889
  this.skip(parseInfo, 2);
1725
- while ((charCode = this.advanceCode(parseInfo)) == charCode) {
1726
- if (charCode == 42 /* TokenMap.STAR */) {
1890
+ while ((charCode = this.advanceCode(parseInfo)) === charCode) {
1891
+ if (charCode === 42 /* TokenMap.STAR */) {
1727
1892
  if (this.match(parseInfo, "/")) {
1728
1893
  this.skip(parseInfo, 1);
1729
1894
  return this.makeToken(parseInfo, EnumToken.CommentTokenType);
@@ -1755,8 +1920,8 @@ class Tokenizer {
1755
1920
  this.skip(parseInfo, 1);
1756
1921
  if (this.match(parseInfo, "!--")) {
1757
1922
  this.skip(parseInfo, 3);
1758
- while ((charCode = this.advanceCode(parseInfo)) == charCode) {
1759
- if (charCode == 45 /* TokenMap.MINUS */ && this.match(parseInfo, "->")) {
1923
+ while ((charCode = this.advanceCode(parseInfo)) === charCode) {
1924
+ if (charCode === 45 /* TokenMap.MINUS */ && this.match(parseInfo, "->")) {
1760
1925
  break;
1761
1926
  }
1762
1927
  }
@@ -1771,13 +1936,13 @@ class Tokenizer {
1771
1936
  {
1772
1937
  const startPosition = parseInfo.position - parseInfo.offset;
1773
1938
  let offset = 1;
1774
- let charCode = parseInfo.stream.charCodeAt(startPosition + offset);
1939
+ let charCode = parseInfo.codes[startPosition + offset];
1775
1940
  if (isLetter(charCode)) {
1776
- charCode = parseInfo.stream.charCodeAt(++offset + startPosition);
1777
- while (offset < endPosition && (charCode == 45 /* TokenMap.MINUS */ || isLetter(charCode))) {
1778
- charCode = parseInfo.stream.charCodeAt(++offset + startPosition);
1941
+ charCode = parseInfo.codes[++offset + startPosition];
1942
+ while (offset < endPosition && (charCode === 45 /* TokenMap.MINUS */ || isLetter(charCode))) {
1943
+ charCode = parseInfo.codes[++offset + startPosition];
1779
1944
  }
1780
- if (offset > 1 && charCode == 62 /* TokenMap.GREATERTHAN */) {
1945
+ if (offset > 1 && charCode === 62 /* TokenMap.GREATERTHAN */) {
1781
1946
  this.skip(parseInfo, offset);
1782
1947
  return this.makeToken(parseInfo, EnumToken.CssTypeTokenType);
1783
1948
  }
@@ -1795,7 +1960,8 @@ class Tokenizer {
1795
1960
  this.advance(parseInfo);
1796
1961
  // EOF
1797
1962
  // end of stream ignore \\
1798
- if (!this.peek(parseInfo) && parseInfo.position < parseInfo.currentPosition) {
1963
+ if (parseInfo.codes[parseInfo.currentPosition - parseInfo.offset] === undefined &&
1964
+ parseInfo.position < parseInfo.currentPosition) {
1799
1965
  return this.makeToken(parseInfo);
1800
1966
  }
1801
1967
  this.advance(parseInfo);
@@ -1807,8 +1973,8 @@ class Tokenizer {
1807
1973
  }
1808
1974
  return this.consumeString(parseInfo);
1809
1975
  case 46 /* TokenMap.DOT */:
1810
- const codepoint = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1);
1811
- if (isIdentStart(codepoint) || codepoint == 45 /* TokenMap.MINUS */) {
1976
+ const codepoint = parseInfo.codes[parseInfo.currentPosition - parseInfo.offset + 1];
1977
+ if (isIdentStart(codepoint) || codepoint === 45 /* TokenMap.MINUS */) {
1812
1978
  // if (parseInfo.position < parseInfo.currentPosition) {
1813
1979
  // return this.makeToken(parseInfo);
1814
1980
  // }
@@ -1844,7 +2010,6 @@ class Tokenizer {
1844
2010
  const decoder = new TextDecoder("utf-8");
1845
2011
  const reader = this.input.getReader();
1846
2012
  let parseInfo = this.parseInfo;
1847
- parseInfo.stream = "";
1848
2013
  while (true) {
1849
2014
  const { done, value } = await reader.read();
1850
2015
  const stream = ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
@@ -1856,10 +2021,9 @@ class Tokenizer {
1856
2021
  break;
1857
2022
  }
1858
2023
  }
1859
- // inlined: parseInfo.source.getContent()
1860
- parseInfo.stream = parseInfo.source.content;
2024
+ Tokenizer.load(parseInfo, parseInfo.source.content);
1861
2025
  return this; // .next();
1862
2026
  }
1863
2027
  }
1864
2028
 
1865
- export { TokenMap, Tokenizer, hintsEnum };
2029
+ export { TokenMap, TokenValueField, TokenValueKind, Tokenizer, createParseInfo, hintsEnum };