@tbela99/css-parser 2.1.0 → 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 (125) hide show
  1. package/CHANGELOG.md +42 -1
  2. package/README.md +14 -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 +5 -2
  6. package/dist/index-umd-web.js +10738 -4359
  7. package/dist/index.d.ts +734 -100
  8. package/dist/{src/lib → lib}/ast/arena-node.js +10 -10
  9. package/dist/{src/lib → lib}/ast/arena.js +304 -139
  10. package/dist/{src/lib → lib}/ast/cache.js +2 -2
  11. package/dist/{src/lib → lib}/ast/features/calc.js +11 -9
  12. package/dist/{src/lib → lib}/ast/features/if.js +6 -6
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +12 -10
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +39 -39
  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 +24 -24
  21. package/dist/{src/lib → lib}/ast/math/math.js +2 -2
  22. package/dist/{src/lib → lib}/ast/minify.js +745 -201
  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 +2 -2
  37. package/dist/{src/lib → lib}/ast/walk.js +2 -2
  38. package/dist/{src/lib → lib}/fs/resolve.js +2 -2
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +25 -8
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +15 -15
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +5 -5
  42. package/dist/{src/lib → lib}/parser/input.js +18 -3
  43. package/dist/{src/lib → lib}/parser/linesmap.js +2 -2
  44. package/dist/{src/lib → lib}/parser/parse.js +425 -258
  45. package/dist/{src/lib → lib}/parser/source.js +2 -2
  46. package/dist/{src/lib → lib}/parser/tokenize.js +480 -312
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +12 -12
  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 +7 -7
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +10 -10
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +14 -14
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +9 -14
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +6 -6
  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 +31 -26
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +2 -2
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +2 -2
  64. package/dist/{src/lib → lib}/parser/utils/selector.js +305 -63
  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 +9 -7
  68. package/dist/{src/lib → lib}/printer/render.js +420 -152
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +2 -2
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +2 -2
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +2 -2
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +10 -10
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +2 -2
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +18 -18
  77. package/dist/{src/lib → lib}/syntax/color/color.js +18 -18
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +3 -3
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +2 -2
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +2 -2
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +2 -2
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +2 -2
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +2 -2
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +3 -3
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +2 -2
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +2 -2
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +14 -14
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +4 -4
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +5 -5
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +4 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +10 -10
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +5 -5
  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 +3 -3
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +2 -2
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +2 -2
  99. package/dist/{src/lib → lib}/syntax/constants.js +26 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +37 -29
  101. package/dist/{src/lib → lib}/validation/config.js +23 -10
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +292 -150
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +2 -2
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +2 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +52 -34
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +10 -5
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +47 -34
  113. package/package.json +12 -12
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -95
  116. package/dist/src/lib/ast/features/transform.js +0 -90
  117. package/dist/src/lib/ast/transform/compute.js +0 -362
  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/utils/cache.js +0 -54
@@ -1,8 +1,8 @@
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.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken, ColorType } from '../ast/types.js';
@@ -10,138 +10,181 @@ import { validationUnits, pseudoElements, containerFunc, urlFunc, gridTemplateFu
10
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
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';
13
15
 
14
- const SymbolsMapTokens = Object.create(null);
15
- const SymbolsMapTokensLower = Object.create(null);
16
- // Regex for escape sequence decoding - compile once, reuse many times
17
- const ESCAPE_SEQUENCE_REGEX = /\\((\n|\r|\f|\v|\u2028|\u2029|([0-9a-fA-F]{1,6})) ?)/gms;
18
- function decodeEscapeSequences(value) {
19
- return value.replace(ESCAPE_SEQUENCE_REGEX, (_, sequence) => {
20
- // \n \r \f \v
21
- switch (sequence.charCodeAt(0)) {
22
- case 0xa:
23
- case 0xb:
24
- case 0xc:
25
- case 0xd:
26
- case 0x2028:
27
- case 0x2029:
28
- return "";
29
- }
30
- const codepoint = parseInt(sequence, 16);
31
- if (codepoint === 0 ||
32
- // leading surrogate
33
- (0xd800 <= codepoint && codepoint <= 0xdbff) ||
34
- // trailing surrogate
35
- (0xdc00 <= codepoint && codepoint <= 0xdfff) ||
36
- codepoint > 0x10ffff) {
37
- return "\uFFFD";
38
- }
39
- return String.fromCodePoint(codepoint);
40
- });
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;
41
86
  }
42
87
  /**
43
- * the code units of a string
88
+ * the lowercase code units of an ascii string
44
89
  * @param str
45
90
  */
46
- function codesOf(str) {
91
+ function lowerCodesOf(str) {
47
92
  const codes = new Uint16Array(str.length);
48
93
  for (let i = 0; i < str.length; i++) {
49
- codes[i] = str.charCodeAt(i);
94
+ codes[i] = lowerAscii(str.charCodeAt(i));
50
95
  }
51
96
  return codes;
52
97
  }
53
98
  /**
54
- * hash table of ascii strings, looked up by a range of the code units of the css source without allocating a string.
55
- * the keys are lowercase, the lookups ignore the case of ascii letters
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
56
102
  */
57
- class RangeTable {
103
+ class HashTable {
104
+ /**
105
+ * lowercase code units of the keys, by entry
106
+ */
58
107
  keys = [];
108
+ /**
109
+ * values, by entry
110
+ */
59
111
  values = [];
60
- slots = new Int32Array(1024).fill(-1);
61
112
  /**
62
- * hash of a range of code units, ascii letters are lowercased. -1 when the range contains a non ascii character
63
- * @param codes
64
- * @param start
65
- * @param end
113
+ * hash of the keys, by entry
66
114
  */
67
- static hash(codes, start, end) {
68
- let hash = 0x811c9dc5;
69
- for (let i = start; i < end; i++) {
70
- let code = codes[i];
71
- if (code > 0x7f) {
72
- return -1;
73
- }
74
- if (code >= 65 && code <= 90) {
75
- code += 32;
76
- }
77
- hash = Math.imul(hash ^ code, 0x01000193);
78
- }
79
- return hash >>> 0;
80
- }
115
+ hashes = [];
116
+ /**
117
+ * entry + 1 by slot, 0 when the slot is empty
118
+ */
119
+ slots = new Int32Array(2048);
81
120
  /**
82
- * the value of a range of code units
121
+ * the value of a range of code units, ignoring the case
83
122
  * @param codes
84
123
  * @param start
85
124
  * @param end
86
125
  */
87
126
  get(codes, start, end) {
88
- const hash = RangeTable.hash(codes, start, end);
127
+ const hash = hashRangeLower(codes, start, end);
89
128
  if (hash === -1) {
90
129
  return undefined;
91
130
  }
92
- const index = this.slots[this.find(codes, start, end, hash)];
93
- return index === -1 ? undefined : this.values[index];
131
+ const entry = this.slots[this.find(codes, start, end, hash)];
132
+ return entry === 0 ? undefined : this.values[entry - 1];
94
133
  }
95
134
  /**
96
- * the value of a key
135
+ * the value of a key, ignoring the case
97
136
  * @param key
98
137
  */
99
138
  getKey(key) {
100
- return this.get(codesOf(key), 0, key.length);
139
+ return this.get(lowerCodesOf(key), 0, key.length);
101
140
  }
141
+ /**
142
+ * set the value of a key. the key is lowercased
143
+ * @param key
144
+ * @param value
145
+ */
102
146
  set(key, value) {
103
- const codes = codesOf(key);
104
- const slot = this.find(codes, 0, codes.length, RangeTable.hash(codes, 0, codes.length));
105
- if (this.slots[slot] === -1) {
106
- this.slots[slot] = this.keys.length;
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) {
107
151
  this.keys.push(codes);
108
152
  this.values.push(value);
153
+ this.hashes.push(hash);
154
+ this.slots[slot] = this.keys.length;
109
155
  }
110
156
  else {
111
- this.values[this.slots[slot]] = value;
157
+ this.values[this.slots[slot] - 1] = value;
112
158
  }
113
159
  }
114
160
  /**
115
- * slot of a key. the table is never full: it is sized for the static tables of the tokenizer
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
116
163
  * @param codes
117
164
  * @param start
118
165
  * @param end
119
- * @param hash
166
+ * @param hash hash of the key
120
167
  */
121
168
  find(codes, start, end, hash) {
122
169
  const mask = this.slots.length - 1;
123
170
  const length = end - start;
124
171
  let slot = hash & mask;
125
172
  for (;;) {
126
- const index = this.slots[slot];
127
- if (index === -1) {
173
+ const entry = this.slots[slot];
174
+ if (entry === 0) {
128
175
  return slot;
129
176
  }
130
- const key = this.keys[index];
131
- if (key.length === length) {
132
- let k = 0;
133
- while (k < length) {
134
- let code = codes[start + k];
135
- if (code >= 65 && code <= 90) {
136
- 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++;
137
184
  }
138
- if (code !== key[k]) {
139
- break;
185
+ if (k === length) {
186
+ return slot;
140
187
  }
141
- k++;
142
- }
143
- if (k === length) {
144
- return slot;
145
188
  }
146
189
  }
147
190
  slot = (slot + 1) & mask;
@@ -149,14 +192,14 @@ class RangeTable {
149
192
  }
150
193
  }
151
194
  /**
152
- * value of a number: the digits are read from the code units of the source without creating a string when the result
153
- * is exact (at most 15 significant digits, no exponent), parseFloat() otherwise
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})
154
198
  * @param codes the code units of the source
155
- * @param text the text of the source
156
199
  * @param start
157
200
  * @param end
158
201
  */
159
- function parseNumber(codes, text, start, end) {
202
+ function parseNumber(codes, start, end) {
160
203
  let i = start;
161
204
  let code = codes[i];
162
205
  let negative = false;
@@ -164,6 +207,7 @@ function parseNumber(codes, text, start, end) {
164
207
  negative = code === 0x2d;
165
208
  code = codes[++i];
166
209
  }
210
+ const digitsStart = i;
167
211
  let mantissa = 0;
168
212
  let count = 0;
169
213
  let digits = 0;
@@ -186,14 +230,112 @@ function parseNumber(codes, text, start, end) {
186
230
  break;
187
231
  }
188
232
  }
189
- // exponent, too many digits or not a number: the (correctly rounded) result of parseFloat()
190
- if (i !== end || count === 0 || digits > 15 || decimals > 22 || decimals === 0) {
191
- return parseFloat(text.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;
192
260
  }
193
- // mantissa and 10^decimals are exact: the division is correctly rounded like parseFloat()
194
- const value = decimals > 0 ? mantissa / powersOf10[decimals] : mantissa;
261
+ if (digits > 15 || exponent > 22 || exponent < -22) {
262
+ const value = exactNumber(codes, digitsStart, digitsEnd, exponent);
263
+ return negative ? -value : value;
264
+ }
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;
195
268
  return negative ? -value : value;
196
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
+ }
197
339
  const powersOf10 = [
198
340
  1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20,
199
341
  1e21, 1e22,
@@ -201,57 +343,60 @@ const powersOf10 = [
201
343
  /**
202
344
  * token type of a symbol, unit or function name (the name followed by `(`), ignoring the case
203
345
  */
204
- const symbolTable = new RangeTable();
346
+ const symbolTable = new HashTable();
205
347
  /**
206
348
  * units and function names of each token type, by lowercase name: the spelling of the first matching item
207
349
  */
208
- 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();
209
355
  function assignTokenMap(entries, tokenType, suffix = "", lowercase = false) {
210
356
  for (const entry of entries) {
211
357
  const key = (lowercase ? entry.toLowerCase() : entry) + suffix;
212
- SymbolsMapTokens[key] = tokenType;
213
- SymbolsMapTokensLower[key.toLowerCase()] = tokenType;
358
+ symbolDefinitions.set(key, tokenType);
214
359
  let items = itemTable.getKey(entry);
215
360
  if (items == null) {
216
361
  items = new Map();
217
362
  itemTable.set(entry.toLowerCase(), items);
218
363
  }
219
- // 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
220
365
  if (!items.has(tokenType)) {
221
- items.set(tokenType, entry);
366
+ items.set(tokenType, registerCanonical(entry));
222
367
  }
223
368
  }
224
369
  }
225
- SymbolsMapTokens["+"] = EnumToken.Plus;
226
- SymbolsMapTokens["="] = EnumToken.DelimTokenType;
227
- SymbolsMapTokens["|"] = EnumToken.PipeTokenType;
228
- SymbolsMapTokens["||"] = EnumToken.ColumnCombinatorTokenType;
229
- SymbolsMapTokens["|="] = EnumToken.DashMatchTokenType;
230
- SymbolsMapTokens["&"] = EnumToken.NestingSelectorTokenType;
231
- SymbolsMapTokens["*"] = EnumToken.Star;
232
- SymbolsMapTokens["*="] = EnumToken.ContainMatchTokenType;
233
- SymbolsMapTokens["~"] = EnumToken.Tilda;
234
- SymbolsMapTokens["~="] = EnumToken.IncludeMatchTokenType;
235
- SymbolsMapTokens["^="] = EnumToken.StartMatchTokenType;
236
- SymbolsMapTokens["$="] = EnumToken.EndMatchTokenType;
237
- SymbolsMapTokens[","] = EnumToken.Comma;
238
- SymbolsMapTokens[":"] = EnumToken.ColonTokenType;
239
- SymbolsMapTokens["::"] = EnumToken.DoubleColonTokenType;
240
- SymbolsMapTokens[";"] = EnumToken.SemiColonTokenType;
241
- SymbolsMapTokens["("] = EnumToken.StartParensTokenType;
242
- SymbolsMapTokens[")"] = EnumToken.EndParensTokenType;
243
- SymbolsMapTokens["["] = EnumToken.AttrStartTokenType;
244
- SymbolsMapTokens["]"] = EnumToken.AttrEndTokenType;
245
- SymbolsMapTokens["{"] = EnumToken.BlockStartTokenType;
246
- SymbolsMapTokens["}"] = EnumToken.BlockEndTokenType;
247
- SymbolsMapTokens["<="] = EnumToken.LteTokenType;
248
- SymbolsMapTokens[">"] = EnumToken.GtTokenType;
249
- SymbolsMapTokens[">="] = EnumToken.GteTokenType;
250
- SymbolsMapTokens[" "] = EnumToken.Whitespace;
251
- SymbolsMapTokens["\t"] = EnumToken.Whitespace;
252
- SymbolsMapTokens["\r"] = EnumToken.Whitespace;
253
- SymbolsMapTokens["\n"] = EnumToken.Whitespace;
254
- 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);
255
400
  assignTokenMap(flexUnits, EnumToken.FlexTokenType);
256
401
  assignTokenMap(dimensionUnits, EnumToken.LengthTokenType);
257
402
  assignTokenMap(resolutionUnits, EnumToken.ResolutionTokenType);
@@ -266,7 +411,7 @@ for (const unit of validationUnits) {
266
411
  itemTable.set(unit.toLowerCase(), items);
267
412
  }
268
413
  if (!items.has(EnumToken.DimensionTokenType)) {
269
- items.set(EnumToken.DimensionTokenType, unit);
414
+ items.set(EnumToken.DimensionTokenType, registerCanonical(unit));
270
415
  }
271
416
  }
272
417
  assignTokenMap(pseudoElements, EnumToken.PseudoElementTokenType);
@@ -282,9 +427,11 @@ assignTokenMap(mathFuncs, EnumToken.MathFunctionTokenDefType, "(");
282
427
  assignTokenMap(transformFunctions, EnumToken.TransformFunctionTokenDefType, "(", true);
283
428
  assignTokenMap(whenElseFunc, EnumToken.WhenElseFunctionTokenDefType, "(");
284
429
  assignTokenMap(wildCardFuncs, EnumToken.WildCardFunctionTokenDefType, "(");
285
- for (const key of Object.keys(SymbolsMapTokens)) {
286
- 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);
287
433
  }
434
+ symbolDefinitions.clear();
288
435
  // do not capture the value
289
436
  const hintsEnum = new Set([
290
437
  EnumToken.CommaTokenType,
@@ -297,6 +444,50 @@ const hintsEnum = new Set([
297
444
  EnumToken.ColonTokenType,
298
445
  EnumToken.EOFTokenType,
299
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 = {}));
300
491
  var TokenMap;
301
492
  (function (TokenMap) {
302
493
  TokenMap[TokenMap["EXCLAMATION"] = 33] = "EXCLAMATION";
@@ -338,22 +529,12 @@ function getSymbolHint(parseInfo, start, end) {
338
529
  if (end - start <= 0) {
339
530
  return null;
340
531
  }
341
- // ascii names are looked up without allocating a string
342
- const type = symbolTable.get(codes, start, end);
343
- if (type !== undefined) {
344
- return type;
345
- }
346
- for (let i = start; i < end; i++) {
347
- // the unicode case mapping of toLowerCase() may produce ascii letters
348
- if (codes[i] > 0x7f) {
349
- const value = parseInfo.stream.slice(start, end);
350
- return SymbolsMapTokens[value] ?? SymbolsMapTokensLower[value.toLowerCase()] ?? null;
351
- }
352
- }
353
- return null;
532
+ // the range is looked up by its hash: no string is created
533
+ return symbolTable.get(codes, start, end) ?? null;
354
534
  }
355
535
  /**
356
- * 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
357
538
  * @param type
358
539
  * @param parseInfo
359
540
  * @param start
@@ -361,9 +542,9 @@ function getSymbolHint(parseInfo, start, end) {
361
542
  */
362
543
  function findItem(type, parseInfo, start, end) {
363
544
  if (end - start <= 0) {
364
- return null;
545
+ return -1;
365
546
  }
366
- return itemTable.get(parseInfo.codes, start, end)?.get(type) ?? null;
547
+ return itemTable.get(parseInfo.codes, start, end)?.get(type) ?? -1;
367
548
  }
368
549
  /**
369
550
  * true if the character is a hexadecimal digit
@@ -403,7 +584,6 @@ function isNumericTokenEnd(codepoint, isDimension) {
403
584
  */
404
585
  function createParseInfo(input, source) {
405
586
  const parseInfo = {
406
- stream: "",
407
587
  codes: new Uint8Array(0),
408
588
  length: 0,
409
589
  source,
@@ -431,17 +611,27 @@ class Tokenizer {
431
611
  */
432
612
  kin = null;
433
613
  /**
434
- * token name
614
+ * numeric value of the number, dimension and percentage tokens
435
615
  */
436
- nam = null;
616
+ num = 0;
437
617
  /**
438
- * token value
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)
439
620
  */
440
- val = null;
621
+ field = 0 /* TokenValueField.None */;
441
622
  /**
442
- * token unit
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
443
625
  */
444
- unit = null;
626
+ valueKind = 0 /* TokenValueKind.Source */;
627
+ /**
628
+ * start of the string value: in the code units of the source, or in the canonical text
629
+ */
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;
445
635
  /**
446
636
  * source id
447
637
  */
@@ -458,10 +648,6 @@ class Tokenizer {
458
648
  * bytes in
459
649
  */
460
650
  bytesIn = null;
461
- /**
462
- * decode string
463
- */
464
- decodeString = null;
465
651
  /**
466
652
  * token slice
467
653
  */
@@ -493,14 +679,14 @@ class Tokenizer {
493
679
  else {
494
680
  // the parse info of a stream is loaded by tokenizeStream()
495
681
  if (parseInfo.codes == null && input == null) {
496
- Tokenizer.load(parseInfo, parseInfo.stream);
682
+ Tokenizer.load(parseInfo, parseInfo.source.content);
497
683
  }
498
684
  this.parseInfo = parseInfo;
499
685
  }
500
686
  }
501
687
  /**
502
- * set the css read by the tokenizer: the code units of the css (an ascii ArrayBuffer is read without copy) and its
503
- * text, the content of the source file
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
504
690
  * @param parseInfo
505
691
  * @param input
506
692
  */
@@ -508,10 +694,7 @@ class Tokenizer {
508
694
  const { codes, text } = toCodeUnits(input);
509
695
  parseInfo.codes = codes;
510
696
  parseInfo.length = codes.length;
511
- parseInfo.stream = text;
512
- if (parseInfo.source.content === "") {
513
- parseInfo.source.content = text;
514
- }
697
+ parseInfo.source.content = text;
515
698
  }
516
699
  /**
517
700
  * id of the source file being tokenized
@@ -552,26 +735,10 @@ class Tokenizer {
552
735
  decodeSegments = true;
553
736
  continue;
554
737
  }
555
- const sequence = this.peek(parseInfo, 7);
556
- let escapeSequence = "";
557
- let codepoint;
558
- let i;
559
- for (i = 1; i < sequence.length; i++) {
560
- codepoint = sequence.charCodeAt(i);
561
- if (codepoint === 0x20 ||
562
- (codepoint >= 0x61 && codepoint <= 0x66) ||
563
- (codepoint >= 0x41 && codepoint <= 0x46) ||
564
- (codepoint >= 0x30 && codepoint <= 0x39)) {
565
- escapeSequence += sequence[i];
566
- if (codepoint === 0x20) {
567
- break;
568
- }
569
- continue;
570
- }
571
- break;
572
- }
573
- if (escapeSequence.trimEnd().length > 0) {
574
- 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 +
575
742
  1 +
576
743
  (isWhiteSpace(parseInfo.codes[parseInfo.currentPosition - parseInfo.offset]) ? 1 : 0);
577
744
  decodeSegments = true;
@@ -611,26 +778,10 @@ class Tokenizer {
611
778
  this.advance(parseInfo, 2);
612
779
  continue;
613
780
  }
614
- const sequence = this.peek(parseInfo, 7);
615
- let escapeSequence = "";
616
- let codepoint;
617
- let i;
618
- for (i = 1; i < sequence.length; i++) {
619
- codepoint = sequence.charCodeAt(i);
620
- if (codepoint === 0x20 ||
621
- (codepoint >= 0x61 && codepoint <= 0x66) ||
622
- (codepoint >= 0x41 && codepoint <= 0x46) ||
623
- (codepoint >= 0x30 && codepoint <= 0x39)) {
624
- escapeSequence += sequence[i];
625
- if (codepoint === 0x20) {
626
- break;
627
- }
628
- continue;
629
- }
630
- break;
631
- }
632
- if (escapeSequence.trimEnd().length > 0) {
633
- 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 +
634
785
  1 +
635
786
  (isWhiteSpace(parseInfo.codes[parseInfo.currentPosition - parseInfo.offset]) ? 1 : 0);
636
787
  decodeSegments = true;
@@ -989,93 +1140,54 @@ class Tokenizer {
989
1140
  : EnumToken.UrlTokenTokenType);
990
1141
  }
991
1142
  /**
992
- *
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
993
1145
  * @param parseInfo
994
- * @param hint
995
- * @param slice
996
- * @param decodeSegments
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
997
1149
  * @returns
998
1150
  */
999
1151
  makeToken(parseInfo, hint, slice, decodeSegments) {
1000
- 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;
1001
1158
  this.typ = null;
1002
- this.nam = null;
1003
- this.val = null;
1004
- this.unit = null;
1005
1159
  this.kin = null;
1006
- this.decodeString = null;
1160
+ this.num = 0;
1161
+ this.field = 0 /* TokenValueField.None */;
1007
1162
  this.slice = null;
1008
1163
  this.hint = null;
1009
1164
  if (slice) {
1010
1165
  this.slice = slice;
1011
1166
  }
1012
- if (decodeSegments) {
1013
- this.decodeString = true;
1014
- }
1015
1167
  if (hint != null) {
1016
1168
  // the spelling of units and function names is looked up in the tables
1017
1169
  let isList = false;
1018
1170
  let hasUnit = false;
1019
1171
  switch (hint) {
1020
1172
  case EnumToken.TransformFunctionTokenDefType:
1021
- isList = true;
1022
- break;
1023
1173
  case EnumToken.ColorFunctionTokenDefType:
1024
- isList = true;
1025
- break;
1026
1174
  case EnumToken.ContainerFunctionTokenDefType:
1027
- isList = true;
1028
- break;
1029
1175
  case EnumToken.UrlFunctionTokenDefType:
1030
- isList = true;
1031
- break;
1032
1176
  case EnumToken.GridTemplateFuncTokenDefType:
1033
- isList = true;
1034
- break;
1035
1177
  case EnumToken.ImageFunctionTokenDefType:
1036
- isList = true;
1037
- break;
1038
1178
  case EnumToken.TimelineFunctionTokenDefType:
1039
- isList = true;
1040
- break;
1041
- // case EnumToken.GeneralEnclosedFunctionTokenDefType:
1042
- // searchArray = generalEnclosedFunc;
1043
- // break;
1044
1179
  case EnumToken.SupportsFunctionTokenDefType:
1045
- isList = true;
1046
- break;
1047
1180
  case EnumToken.TimingFunctionTokenDefType:
1048
- isList = true;
1049
- break;
1050
1181
  case EnumToken.MathFunctionTokenDefType:
1051
- isList = true;
1052
- break;
1053
1182
  case EnumToken.WhenElseFunctionTokenDefType:
1054
- isList = true;
1055
- break;
1056
1183
  case EnumToken.WildCardFunctionTokenDefType:
1057
1184
  isList = true;
1058
1185
  break;
1059
1186
  case EnumToken.FrequencyTokenType:
1060
- isList = true;
1061
- hasUnit = true;
1062
- break;
1063
1187
  case EnumToken.ResolutionTokenType:
1064
- isList = true;
1065
- hasUnit = true;
1066
- break;
1067
1188
  case EnumToken.LengthTokenType:
1068
- isList = true;
1069
- hasUnit = true;
1070
- break;
1071
1189
  case EnumToken.FlexTokenType:
1072
- isList = true;
1073
- hasUnit = true;
1074
- break;
1075
1190
  case EnumToken.AngleTokenType:
1076
- isList = true;
1077
- hasUnit = true;
1078
- break;
1079
1191
  case EnumToken.TimeTokenType:
1080
1192
  isList = true;
1081
1193
  hasUnit = true;
@@ -1086,61 +1198,73 @@ class Tokenizer {
1086
1198
  }
1087
1199
  const isHint = hintsEnum.has(hint);
1088
1200
  if (isList) {
1089
- 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;
1090
1204
  }
1091
1205
  else if (hint === EnumToken.NumberTokenType || hint === EnumToken.WhitespaceTokenType) ;
1092
1206
  else if (!isHint) {
1207
+ valueStart = slice ?? start;
1093
1208
  // the canonical spelling of a known unit: `6DB` is rendered `6dB`
1094
- val =
1095
- (hint === EnumToken.DimensionTokenType
1096
- ? findItem(EnumToken.DimensionTokenType, parseInfo, slice ?? parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset)
1097
- : null) ??
1098
- parseInfo.stream.slice(slice ?? parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
1209
+ canonical =
1210
+ hint === EnumToken.DimensionTokenType
1211
+ ? findItem(EnumToken.DimensionTokenType, parseInfo, valueStart, end)
1212
+ : -1;
1213
+ hasValue = true;
1099
1214
  }
1100
- if (this.decodeString) {
1101
- val = decodeEscapeSequences(val);
1102
- }
1103
- if (isHint) {
1104
- this.typ = hint;
1105
- }
1106
- else {
1107
- this.typ = hint;
1215
+ this.typ = hint;
1216
+ if (!isHint) {
1108
1217
  if (hasUnit || hint === EnumToken.PercentageTokenType || hint === EnumToken.DimensionTokenType) {
1109
- this.val = parseNumber(parseInfo.codes, parseInfo.stream, parseInfo.position - parseInfo.offset, slice);
1218
+ this.num = parseNumber(parseInfo.codes, start, slice);
1110
1219
  if (hint !== EnumToken.PercentageTokenType) {
1111
- this.unit = val;
1112
- if (val.startsWith("--")) {
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) {
1113
1225
  this.typ = EnumToken.UserDefinedDimensionTokenType;
1114
1226
  }
1115
1227
  }
1228
+ else {
1229
+ this.field = 1 /* TokenValueField.Number */;
1230
+ }
1116
1231
  }
1117
1232
  else if (hint === EnumToken.NumberTokenType) {
1118
- this.val = parseNumber(parseInfo.codes, parseInfo.stream, slice ?? parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
1233
+ this.num = parseNumber(parseInfo.codes, slice ?? start, end);
1234
+ this.field = 1 /* TokenValueField.Number */;
1119
1235
  }
1120
1236
  else if (hint === EnumToken.AtRuleTokenType) {
1121
- this.nam = val;
1237
+ this.field = hasValue ? 2 /* TokenValueField.Name */ : 0 /* TokenValueField.None */;
1122
1238
  }
1123
1239
  else {
1124
- this.val = val;
1240
+ this.field = hasValue ? 3 /* TokenValueField.Value */ : 0 /* TokenValueField.None */;
1125
1241
  if (hint === EnumToken.ColorTokenType) {
1126
1242
  this.kin = ColorType.HEX;
1127
1243
  }
1128
1244
  }
1129
1245
  }
1130
1246
  }
1131
- else {
1132
- if (this.equalsIgnoreCase(parseInfo, "!important")) {
1133
- this.typ = EnumToken.ImportantTokenType;
1134
- }
1247
+ else if (this.equalsIgnoreCase(parseInfo, "!important")) {
1248
+ this.typ = EnumToken.ImportantTokenType;
1135
1249
  }
1136
1250
  if (this.typ == null) {
1137
- val = parseInfo.stream.slice(parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
1138
- if (decodeSegments) {
1139
- val = decodeEscapeSequences(val);
1140
- this.decodeString = true;
1141
- }
1251
+ valueStart = start;
1252
+ hasValue = true;
1142
1253
  this.typ = EnumToken.LiteralTokenType;
1143
- 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
+ }
1144
1268
  }
1145
1269
  this.srcId = parseInfo.source.id;
1146
1270
  this.sta = parseInfo.position;
@@ -1149,6 +1273,43 @@ class Tokenizer {
1149
1273
  parseInfo.position = parseInfo.currentPosition;
1150
1274
  return this;
1151
1275
  }
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
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
+ }
1152
1313
  /**
1153
1314
  * true if the text of the token being read equals the input, ignoring the case. the token text starts at
1154
1315
  * parseInfo.position: `0!important` is the number `0` followed by `!important`
@@ -1194,17 +1355,30 @@ class Tokenizer {
1194
1355
  return true;
1195
1356
  }
1196
1357
  /**
1197
- *
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
1198
1361
  * @param parseInfo
1199
- * @param count
1200
- * @returns
1201
1362
  */
1202
- peek(parseInfo, count = 1) {
1203
- if (count === 1) {
1204
- return parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset);
1205
- }
1363
+ escapeSequenceLength(parseInfo) {
1364
+ const codes = parseInfo.codes;
1206
1365
  const position = parseInfo.currentPosition - parseInfo.offset;
1207
- 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;
1208
1382
  }
1209
1383
  /**
1210
1384
  * move forward by count characters (at most to the end of the stream) and record the new lines. the code units are
@@ -1228,9 +1402,8 @@ class Tokenizer {
1228
1402
  codepoint === 0x2028 || // \u2028
1229
1403
  codepoint === 0x2029 // \u2029
1230
1404
  ) {
1231
- // \r\n: a single line start
1232
- if (codepoint === 0xa && i > 0 && codes[position + i - 1] === 0xd) ;
1233
- else {
1405
+ // \r\n: a single line start, at the \n
1406
+ if (codepoint !== 0xd || codes[position + i + 1] !== 0xa) {
1234
1407
  lineStarts.push(position + parseInfo.offset + i);
1235
1408
  }
1236
1409
  }
@@ -1276,7 +1449,8 @@ class Tokenizer {
1276
1449
  if (codepoint === 0xa || // \n
1277
1450
  codepoint === 0xb || // \v
1278
1451
  codepoint === 0xc || // \f
1279
- codepoint === 0xd || // \r
1452
+ // \r\n: a single line start, at the \n
1453
+ (codepoint === 0xd && parseInfo.codes[position + 1] !== 0xa) ||
1280
1454
  codepoint === 0x2028 ||
1281
1455
  codepoint === 0x2029) {
1282
1456
  parseInfo.source.lineStarts.lineStarts.push(position + parseInfo.offset);
@@ -1327,15 +1501,16 @@ class Tokenizer {
1327
1501
  }
1328
1502
  if (codepoint === 92 /* TokenMap.REVERSE_SOLIDUS */) {
1329
1503
  codepoint = parseInfo.codes[i + 1];
1330
- i += String.fromCodePoint(codepoint).length;
1504
+ // a code unit
1505
+ i++;
1331
1506
  }
1332
1507
  while (i < j) {
1333
- i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1508
+ i++;
1334
1509
  codepoint = parseInfo.codes[i];
1335
1510
  if (codepoint === 92 /* TokenMap.REVERSE_SOLIDUS */) {
1336
- i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1511
+ i++;
1337
1512
  codepoint = parseInfo.codes[i];
1338
- i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1513
+ i++;
1339
1514
  continue;
1340
1515
  }
1341
1516
  if (codepoint !== 0x2d && !isIdentCodepoint(codepoint)) {
@@ -1547,13 +1722,6 @@ class Tokenizer {
1547
1722
  // '('
1548
1723
  case 40 /* TokenMap.LEFT_PARENTHESIS */:
1549
1724
  if (parseInfo.position < parseInfo.currentPosition) {
1550
- // if (
1551
- // parseInfo.stream[parseInfo.position - parseInfo.offset] === ":" &&
1552
- // this.isPseudo(parseInfo)
1553
- //) {
1554
- // this.skip(parseInfo, 1);
1555
- // return this.makeToken(parseInfo, EnumToken.PseudoClassFunctionTokenDefType);
1556
- // } else
1557
1725
  if (this.isIdentToken(parseInfo)) {
1558
1726
  const hint = this.startsWith(parseInfo, "--")
1559
1727
  ? EnumToken.CustomFunctionTokenDefType
@@ -1792,7 +1960,8 @@ class Tokenizer {
1792
1960
  this.advance(parseInfo);
1793
1961
  // EOF
1794
1962
  // end of stream ignore \\
1795
- if (!this.peek(parseInfo) && parseInfo.position < parseInfo.currentPosition) {
1963
+ if (parseInfo.codes[parseInfo.currentPosition - parseInfo.offset] === undefined &&
1964
+ parseInfo.position < parseInfo.currentPosition) {
1796
1965
  return this.makeToken(parseInfo);
1797
1966
  }
1798
1967
  this.advance(parseInfo);
@@ -1841,7 +2010,6 @@ class Tokenizer {
1841
2010
  const decoder = new TextDecoder("utf-8");
1842
2011
  const reader = this.input.getReader();
1843
2012
  let parseInfo = this.parseInfo;
1844
- parseInfo.stream = "";
1845
2013
  while (true) {
1846
2014
  const { done, value } = await reader.read();
1847
2015
  const stream = ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
@@ -1858,4 +2026,4 @@ class Tokenizer {
1858
2026
  }
1859
2027
  }
1860
2028
 
1861
- export { TokenMap, Tokenizer, createParseInfo, hintsEnum };
2029
+ export { TokenMap, TokenValueField, TokenValueKind, Tokenizer, createParseInfo, hintsEnum };