@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,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.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken, EnumAstNodeStatus, ColorType } from './types.js';
@@ -12,7 +12,7 @@ import { splitRule } from './minify.js';
12
12
  import { combinators } from '../syntax/constants.js';
13
13
  import { parseString } from '../parser/parse.js';
14
14
  import { renderValue } from '../printer/render.js';
15
- import { equalsIgnoreCase } from '../parser/utils/text.js';
15
+ import { StringTable } from './strings.js';
16
16
 
17
17
  /**
18
18
  * position of the structured token fields in the children list
@@ -92,23 +92,23 @@ function structuredFields(typ) {
92
92
  fields = noFields;
93
93
  }
94
94
  else {
95
- const single = typ == EnumToken.BinaryExpressionTokenType ||
96
- typ == EnumToken.IfElseConditionTokenType ||
97
- typ == EnumToken.MatchExpressionTokenType ||
98
- typ == EnumToken.NameSpaceAttributeTokenType;
95
+ const single = typ === EnumToken.BinaryExpressionTokenType ||
96
+ typ === EnumToken.IfElseConditionTokenType ||
97
+ typ === EnumToken.MatchExpressionTokenType ||
98
+ typ === EnumToken.NameSpaceAttributeTokenType;
99
99
  const singleLeft = single ||
100
- typ == EnumToken.MediaQueryConditionTokenType ||
101
- typ == EnumToken.MediaQueryUnaryFeatureTokenType ||
102
- typ == EnumToken.SupportsQueryUnaryConditionTokenType ||
103
- typ == EnumToken.WhenElseUnaryConditionTokenType;
104
- const singleRight = single || typ == EnumToken.ComposesSelectorNodeType;
100
+ typ === EnumToken.MediaQueryConditionTokenType ||
101
+ typ === EnumToken.MediaQueryUnaryFeatureTokenType ||
102
+ typ === EnumToken.SupportsQueryUnaryConditionTokenType ||
103
+ typ === EnumToken.WhenElseUnaryConditionTokenType;
104
+ const singleRight = single || typ === EnumToken.ComposesSelectorNodeType;
105
105
  fields = [
106
106
  ["l", TokenField.L, singleLeft],
107
107
  ["r", TokenField.R, singleRight],
108
108
  [
109
- typ == EnumToken.MediaRangeQueryTokenType ? "op1" : "op",
109
+ typ === EnumToken.MediaRangeQueryTokenType ? "op1" : "op",
110
110
  TokenField.Op,
111
- typ != EnumToken.ContainerStyleRangeTokenType,
111
+ typ !== EnumToken.ContainerStyleRangeTokenType,
112
112
  ],
113
113
  ["op2", TokenField.Op2, true],
114
114
  ["val", TokenField.Val, false],
@@ -120,7 +120,7 @@ function structuredFields(typ) {
120
120
  /**
121
121
  * little endian platform
122
122
  */
123
- const littleEndian = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
123
+ const littleEndian = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
124
124
  /**
125
125
  * arena columns stored as typed arrays, in the order of the transferable buffers
126
126
  */
@@ -181,13 +181,9 @@ let visitTop = 0;
181
181
  */
182
182
  class Arena {
183
183
  /**
184
- * string interner identifiers
184
+ * interned strings: ranges of the sources, of the canonical text, of the pool, or strings. see {@link StringTable}
185
185
  */
186
- stringIds = new Map();
187
- /**
188
- * string interner values
189
- */
190
- strings = [];
186
+ stringTable = new StringTable();
191
187
  /**
192
188
  * root node
193
189
  */
@@ -197,10 +193,6 @@ class Arena {
197
193
  * 0 is never allocated: it means "no node"
198
194
  */
199
195
  current = 1;
200
- /**
201
- * head of the list of recycled node ids. the list is linked through the `nextSibling` column, see {@link recycle}
202
- */
203
- freeList = 0;
204
196
  /**
205
197
  * node EnumToken type array
206
198
  */
@@ -261,17 +253,27 @@ class Arena {
261
253
  * token spans. 8 bits for source type. 28 bits for start position, 28 bits for end position. This allows us to store the start and end positions of tokens in a single 64-bit integer, which is more efficient than storing them separately.
262
254
  */
263
255
  spans;
256
+ /**
257
+ * error cache
258
+ */
259
+ errors = new Map();
260
+ /**
261
+ * head of the list of recycled node ids. the list is linked through the `nextSibling` column, see {@link recycle}
262
+ */
263
+ freeList = 0;
264
+ /**
265
+ * number of nodes the columns can hold: the length of the columns. the length of a typed array is a getter,
266
+ * allocate() reads the capacity of the arena for each node
267
+ */
268
+ capacity = 0;
264
269
  /**
265
270
  * 32 bits view of the spans: the low word holds the end position and the 4 low bits of the start position,
266
271
  * the high word holds the 24 high bits of the start position and the source
267
272
  */
268
273
  spans32;
269
- /**
270
- * error cache
271
- */
272
- errors = new Map();
273
274
  constructor(size = 4096) {
274
275
  this.dataTypes = new Uint8Array(size);
276
+ this.capacity = size;
275
277
  this.parents = new Uint32Array(size);
276
278
  this.nextSibling = new Uint32Array(size);
277
279
  this.previousSibling = new Uint32Array(size);
@@ -289,6 +291,12 @@ class Arena {
289
291
  this.spans32 = new Uint32Array(this.spans.buffer);
290
292
  this.dataTypes[this.root] = EnumToken.StyleSheetNodeType;
291
293
  }
294
+ /**
295
+ * true when the column buffers have been transferred to another thread: the arena can no longer be used
296
+ */
297
+ get transferred() {
298
+ return this.dataTypes.length === 0 && this.current > 0;
299
+ }
292
300
  /**
293
301
  * Export the arena as a structured-cloneable object and the list of its column buffers, to send an arena
294
302
  * to a worker (or back to the main thread) without copying the node data.
@@ -327,66 +335,17 @@ class Arena {
327
335
  arena.importProperties(properties, transferables);
328
336
  return arena;
329
337
  }
330
- /**
331
- * true when the column buffers have been transferred to another thread: the arena can no longer be used
332
- */
333
- get transferred() {
334
- return this.dataTypes.length == 0 && this.current > 0;
335
- }
336
- /**
337
- * structured-cloneable properties of the arena
338
- */
339
- exportProperties() {
340
- return {
341
- kind: "Arena",
342
- current: this.current,
343
- strings: this.strings,
344
- errors: [...this.errors.entries()],
345
- };
346
- }
347
- /**
348
- * column buffers, in the order of {@link arenaColumns}. the `spans` buffer is shared with the `spans32` view
349
- */
350
- exportTransferables() {
351
- const buffers = [];
352
- for (const column of arenaColumns) {
353
- const array = this[column];
354
- // the columns always own their buffer: no offset or partial view
355
- buffers.push(array.buffer);
356
- }
357
- return buffers;
358
- }
359
- /**
360
- * initialize the arena from exported properties and column buffers
361
- * @param properties
362
- * @param transferables
363
- */
364
- importProperties(properties, transferables) {
365
- if (transferables.length != arenaColumns.length) {
366
- throw new TypeError(`expected ${arenaColumns.length} column buffers, got ${transferables.length}`);
367
- }
368
- for (let i = 0; i < arenaColumns.length; i++) {
369
- const column = arenaColumns[i];
370
- // @ts-ignore
371
- this[column] = new arenaColumnTypes[column](transferables[i]);
372
- }
373
- this.spans32 = new Uint32Array(this.spans.buffer);
374
- this.current = properties.current;
375
- this.strings = properties.strings.slice();
376
- this.stringIds = new Map(this.strings.map((str, index) => [str, index]));
377
- this.errors = new Map(properties.errors);
378
- }
379
338
  allocate() {
380
339
  let id = this.freeList;
381
- if (id != 0) {
340
+ if (id !== 0) {
382
341
  // reuse a recycled node: its record is already zeroed, except the free list link
383
342
  this.freeList = this.nextSibling[id];
384
343
  this.nextSibling[id] = 0;
385
344
  return id;
386
345
  }
387
346
  id = ++this.current;
388
- if (id >= this.dataTypes.length) {
389
- this.resize(this.dataTypes.length * 2);
347
+ if (id >= this.capacity) {
348
+ this.resize(this.capacity * 2);
390
349
  }
391
350
  return id;
392
351
  }
@@ -412,74 +371,267 @@ class Arena {
412
371
  }
413
372
  /**
414
373
  * Interns a string and returns its unique ID. If the string has already been interned, it returns the existing ID.
374
+ * The strings are looked up by the hash of their code units, see {@link internRange}
415
375
  * @param str
416
376
  * @returns
417
377
  */
418
378
  internString(str) {
419
- let id = this.stringIds.get(str);
420
- if (id != null) {
421
- return id;
422
- }
423
- id = this.strings.length;
424
- this.stringIds.set(str, id);
425
- this.strings.push(str);
426
- return id;
379
+ return this.stringTable.intern(str);
380
+ }
381
+ /**
382
+ * Interns the string made of a range of code units and returns its unique ID. Same ID as {@link internString} for
383
+ * the same content. The range is looked up by its hash and compared with the code units of the interned strings:
384
+ * no string is created.
385
+ *
386
+ * - a range of a source (the code units read by the tokenizer): the source index and the offsets are stored, the
387
+ * text of the source is set with {@link setSourceText}
388
+ * - `StringSource.Canonical`: a range of the canonical text (the spelling of the known units and function names)
389
+ * - `StringSource.Pool`: the code units are copied (decoded escape sequences, ...)
390
+ *
391
+ * @param codes code units
392
+ * @param start start offset of the range
393
+ * @param end end offset of the range
394
+ * @param source source index, `StringSource.Canonical` or `StringSource.Pool`
395
+ */
396
+ internRange(codes, start, end, source) {
397
+ return this.stringTable.internRange(codes, start, end, source);
398
+ }
399
+ /**
400
+ * set the text of a source: the strings interned from the source are ranges of the text, see {@link internRange}
401
+ * @param source source index
402
+ * @param text content of the source
403
+ * @param codes code units of the text while the source is tokenized (faster comparisons)
404
+ */
405
+ setSourceText(source, text, codes = null) {
406
+ this.stringTable.setText(source, text, codes);
407
+ }
408
+ /**
409
+ * drop the code units of a source set by {@link setSourceText}: the source is no longer tokenized
410
+ * @param source source index
411
+ */
412
+ releaseSourceCodes(source) {
413
+ this.stringTable.releaseCodes(source);
414
+ }
415
+ /**
416
+ * id of an interned string, -1 when the string is not interned
417
+ * @param str
418
+ */
419
+ stringId(str) {
420
+ return this.stringTable.lookup(str);
427
421
  }
428
422
  /**
429
- * set node data for a given id. The first argument is the id, the second is the type, the third is the string value, the fourth is the numeric value, and the fifth is the unit.
423
+ * length of an interned string. the string is not created
430
424
  * @param id
431
- * @param args
432
425
  */
433
- set(id, ...args) {
434
- this.dataTypes[id] = args[0];
435
- if (args.length > 1) {
436
- // 0 is null string, so we can use it to indicate that there is no string value for this node.
437
- this.stringValue[id] = args[1];
438
- }
439
- if (args.length > 2) {
440
- this.numericValue[id] = args[2];
441
- }
442
- if (args.length > 3) {
443
- this.unit[id] = args[3];
444
- }
426
+ stringLength(id) {
427
+ return this.stringTable.length(id);
428
+ }
429
+ /**
430
+ * code unit of an interned string. the string is not created
431
+ * @param id
432
+ * @param index
433
+ */
434
+ stringCodeAt(id, index) {
435
+ return this.stringTable.codeAt(id, index);
436
+ }
437
+ /**
438
+ * index of the first occurrence of a code unit in an interned string, -1 when it is not found. the string is not
439
+ * created
440
+ * @param id
441
+ * @param code
442
+ */
443
+ stringIndexOf(id, code) {
444
+ return this.stringTable.indexOfCode(id, code);
445
+ }
446
+ /**
447
+ * true if an interned string equals a string. the interned string is not created
448
+ * @param id
449
+ * @param str
450
+ */
451
+ stringEquals(id, str) {
452
+ return this.stringTable.equals(id, str);
453
+ }
454
+ /**
455
+ * true if an interned string equals a string, ignoring the case of the ascii letters. the interned string is not
456
+ * created
457
+ * @param id
458
+ * @param str
459
+ */
460
+ stringEqualsIgnoreCase(id, str) {
461
+ return this.stringTable.equalsIgnoreCase(id, str);
462
+ }
463
+ /**
464
+ * true if an interned string starts with a string. the interned string is not created
465
+ * @param id
466
+ * @param str
467
+ */
468
+ stringStartsWith(id, str) {
469
+ return this.stringTable.startsWith(id, str);
470
+ }
471
+ /**
472
+ * id of the lowercase version of an interned string (like `toLowerCase()`). no string is created when the string
473
+ * is ascii. the result is cached
474
+ * @param id
475
+ */
476
+ lowerStringId(id) {
477
+ return this.stringTable.lower(id);
478
+ }
479
+ /**
480
+ * copy an interned string to a byte buffer when it is ascii (UTF-8 encoding). the string is not created
481
+ * @param id
482
+ * @param target
483
+ * @param offset offset in the target. the target has room for the string ({@link stringLength})
484
+ * @returns the number of bytes, -1 when the string is not ascii
485
+ */
486
+ copyStringAscii(id, target, offset) {
487
+ return this.stringTable.copyAscii(id, target, offset);
488
+ }
489
+ /**
490
+ * a boolean property of an interned string, computed once per string id: the string is read once
491
+ * @param id
492
+ * @param flag
493
+ * @param compute computes the property from the string
494
+ */
495
+ stringFlag(id, flag, compute) {
496
+ return this.stringTable.flag(id, flag, compute);
445
497
  }
498
+ /**
499
+ * a boolean property of an interned string, computed once per string id from the id (see {@link stringCodeAt})
500
+ * @param id
501
+ * @param flag
502
+ * @param compute computes the property from the string id
503
+ */
504
+ stringFlagId(id, flag, compute) {
505
+ return this.stringTable.flagId(id, flag, compute);
506
+ }
507
+ /**
508
+ * a value derived from an interned string, computed once per string id
509
+ * @param id
510
+ * @param slot
511
+ * @param compute computes the value from the string id
512
+ */
513
+ stringDerived(id, slot, compute) {
514
+ return this.stringTable.derived(id, slot, compute);
515
+ }
516
+ /**
517
+ * copy the code units of an interned string to a buffer. the string is not created
518
+ * @param id
519
+ * @param target the target has room for the string ({@link stringLength})
520
+ * @param offset
521
+ * @returns the number of code units
522
+ */
523
+ copyString(id, target, offset) {
524
+ return this.stringTable.copy(id, target, offset);
525
+ }
526
+ /**
527
+ * append an interned string to a builder. the string is not created
528
+ * @param builder
529
+ * @param id
530
+ */
531
+ appendString(builder, id) {
532
+ return builder.appendId(this.stringTable, id);
533
+ }
534
+ /**
535
+ * intern the string built by a builder: no string is created
536
+ * @param builder
537
+ */
538
+ internBuilder(builder) {
539
+ return builder.intern(this.stringTable);
540
+ }
541
+ /**
542
+ * identity of the string ids: a new object when the strings are renumbered ({@link compact},
543
+ * {@link compactStrings}). the caches keyed by string ids use it as a key
544
+ */
545
+ get stringEpoch() {
546
+ return this.stringTable.epoch;
547
+ }
548
+ /**
549
+ *
550
+ * @param id
551
+ */
446
552
  getType(id) {
447
553
  return this.dataTypes[id];
448
554
  }
449
555
  /**
450
- * type of a node or a token. null when the id is null
556
+ * type of node or a token. null when the id is null
451
557
  * @param id
452
558
  */
453
559
  typeOf(id) {
454
560
  return id == null ? null : this.dataTypes[id];
455
561
  }
562
+ /**
563
+ *
564
+ * @param id
565
+ * @param type
566
+ */
456
567
  setType(id, type) {
457
568
  this.dataTypes[id] = type;
458
569
  }
570
+ /**
571
+ *
572
+ * @param id
573
+ * @param nameId
574
+ */
459
575
  setName(id, nameId) {
460
576
  this.name[id] = nameId;
461
577
  }
578
+ /**
579
+ *
580
+ * @param id
581
+ */
462
582
  getName(id) {
463
- return this.strings[this.name[id]] ?? null;
583
+ return this.resolveString(this.name[id]) ?? null;
464
584
  }
585
+ /**
586
+ *
587
+ * @param id
588
+ * @param valueId
589
+ */
465
590
  setValue(id, valueId) {
466
591
  this.stringValue[id] = valueId;
467
592
  }
593
+ /**
594
+ *
595
+ * @param id
596
+ */
468
597
  getValue(id) {
469
- return this.strings[this.stringValue[id]] ?? null;
598
+ return this.resolveString(this.stringValue[id]) ?? null;
470
599
  }
600
+ /**
601
+ *
602
+ * @param id
603
+ * @param numericValue
604
+ */
471
605
  setNumericValue(id, numericValue) {
472
606
  this.numericValue[id] = numericValue;
473
607
  }
608
+ /**
609
+ *
610
+ * @param id
611
+ * @param unit
612
+ */
474
613
  setUnit(id, unit) {
475
614
  this.unit[id] = unit;
476
615
  }
616
+ /**
617
+ *
618
+ * @param id
619
+ */
477
620
  getUnit(id) {
478
- return this.strings[this.unit[id]] ?? null;
621
+ return this.resolveString(this.unit[id]) ?? null;
479
622
  }
623
+ /**
624
+ *
625
+ * @param id
626
+ */
480
627
  getNumericValue(id) {
481
628
  return this.numericValue[id];
482
629
  }
630
+ /**
631
+ *
632
+ * @param id
633
+ * @param parentId
634
+ */
483
635
  setParent(id, parentId) {
484
636
  this.parents[id] = parentId;
485
637
  }
@@ -535,7 +687,7 @@ class Arena {
535
687
  * number of interned strings
536
688
  */
537
689
  getStringCount() {
538
- return this.strings.length;
690
+ return this.stringTable.count;
539
691
  }
540
692
  /**
541
693
  * replace the first child
@@ -544,11 +696,11 @@ class Arena {
544
696
  */
545
697
  setFirstChild(id, firstChildId) {
546
698
  const firstChild = this.firstChild[id];
547
- if (firstChild == firstChildId) {
699
+ if (firstChild === firstChildId) {
548
700
  return;
549
701
  }
550
702
  this.detach(firstChildId);
551
- if (firstChild != 0) {
703
+ if (firstChild !== 0) {
552
704
  this.#insertBefore(id, firstChild, firstChildId);
553
705
  this.detach(firstChild);
554
706
  }
@@ -557,6 +709,10 @@ class Arena {
557
709
  this.appendChildList(id, [firstChildId]);
558
710
  }
559
711
  }
712
+ /**
713
+ *
714
+ * @param id
715
+ */
560
716
  getLastChild(id) {
561
717
  return this.lastChild[id];
562
718
  }
@@ -567,11 +723,11 @@ class Arena {
567
723
  */
568
724
  setLastChild(id, lastChildId) {
569
725
  const lastChild = this.lastChild[id];
570
- if (lastChild == lastChildId) {
726
+ if (lastChild === lastChildId) {
571
727
  return;
572
728
  }
573
729
  this.detach(lastChildId);
574
- if (lastChild != 0) {
730
+ if (lastChild !== 0) {
575
731
  this.detach(lastChild);
576
732
  }
577
733
  // inlined: this.appendChildren(id, lastChildId)
@@ -583,7 +739,7 @@ class Arena {
583
739
  * @param nextSiblingId
584
740
  */
585
741
  setNextSibling(id, nextSiblingId) {
586
- if (id == nextSiblingId) {
742
+ if (id === nextSiblingId) {
587
743
  return;
588
744
  }
589
745
  this.detach(nextSiblingId);
@@ -593,13 +749,13 @@ class Arena {
593
749
  this.previousSibling[nextSiblingId] = id;
594
750
  this.nextSibling[id] = nextSiblingId;
595
751
  this.parents[nextSiblingId] = parent;
596
- if (next != 0) {
752
+ if (next !== 0) {
597
753
  this.previousSibling[next] = nextSiblingId;
598
754
  }
599
- else if (this.lastChild[parent] == id) {
755
+ else if (this.lastChild[parent] === id) {
600
756
  this.lastChild[parent] = nextSiblingId;
601
757
  }
602
- else if (this.lastToken[parent] == id) {
758
+ else if (this.lastToken[parent] === id) {
603
759
  this.lastToken[parent] = nextSiblingId;
604
760
  }
605
761
  }
@@ -609,7 +765,7 @@ class Arena {
609
765
  * @param previousSiblingId
610
766
  */
611
767
  setPreviousSibling(id, previousSiblingId) {
612
- if (id == previousSiblingId) {
768
+ if (id === previousSiblingId) {
613
769
  return;
614
770
  }
615
771
  this.detach(previousSiblingId);
@@ -622,11 +778,11 @@ class Arena {
622
778
  */
623
779
  setFirstToken(id, firstTokenId) {
624
780
  const firstToken = this.firstToken[id];
625
- if (firstToken == firstTokenId) {
781
+ if (firstToken === firstTokenId) {
626
782
  return;
627
783
  }
628
784
  this.detach(firstTokenId);
629
- if (firstToken != 0) {
785
+ if (firstToken !== 0) {
630
786
  this.#insertBefore(id, firstToken, firstTokenId);
631
787
  this.detach(firstToken);
632
788
  }
@@ -635,28 +791,56 @@ class Arena {
635
791
  this.appendTokenList(id, [firstTokenId]);
636
792
  }
637
793
  }
794
+ /**
795
+ *
796
+ * @param id
797
+ * @param colorType
798
+ */
638
799
  setColorType(id, colorType) {
639
800
  this.colorType[id] = colorType;
640
801
  }
802
+ /**
803
+ *
804
+ * @param id
805
+ */
641
806
  getColorType(id) {
642
807
  return this.colorType[id];
643
808
  }
809
+ /**
810
+ *
811
+ * @param id
812
+ * @param flags
813
+ */
644
814
  setFlags(id, flags) {
645
815
  this.flags[id] = flags;
646
816
  }
817
+ /**
818
+ *
819
+ * @param id
820
+ */
647
821
  getFlags(id) {
648
822
  return this.flags[id];
649
823
  }
824
+ /**
825
+ *
826
+ * @param id
827
+ * @param flag
828
+ */
650
829
  hasFlags(id, flag) {
651
- return (this.flags[id] & flag) == flag;
830
+ return (this.flags[id] & flag) === flag;
652
831
  }
832
+ /**
833
+ *
834
+ * @param id
835
+ * @param index
836
+ */
653
837
  getChildAt(id, index) {
654
838
  let child = this.firstChild[id];
655
- while (index > 0 && child != 0) {
839
+ while (index > 0 && child !== 0) {
656
840
  child = this.nextSibling[child];
657
841
  index--;
658
842
  }
659
- return child == 0 ? null : child;
843
+ return child === 0 ? null : child;
660
844
  }
661
845
  /**
662
846
  * token at the given position of the token list
@@ -665,11 +849,11 @@ class Arena {
665
849
  */
666
850
  getTokenAt(id, index) {
667
851
  let token = this.firstToken[id];
668
- while (index > 0 && token != 0) {
852
+ while (index > 0 && token !== 0) {
669
853
  token = this.nextSibling[token];
670
854
  index--;
671
855
  }
672
- return token == 0 ? null : token;
856
+ return token === 0 ? null : token;
673
857
  }
674
858
  /**
675
859
  * replace the child at the given index. the node is appended when the index is out of range
@@ -679,7 +863,7 @@ class Arena {
679
863
  */
680
864
  setChildAt(id, index, child) {
681
865
  const current = this.getChildAt(id, index);
682
- if (current == child) {
866
+ if (current === child) {
683
867
  return;
684
868
  }
685
869
  this.detach(child);
@@ -691,29 +875,6 @@ class Arena {
691
875
  this.#insertBefore(id, current, child);
692
876
  this.detach(current);
693
877
  }
694
- /**
695
- * link a node before a reference node of the given parent
696
- * @param parent
697
- * @param reference
698
- * @param node
699
- */
700
- #insertBefore(parent, reference, node) {
701
- const previous = this.previousSibling[reference];
702
- this.previousSibling[node] = previous;
703
- this.nextSibling[node] = reference;
704
- this.previousSibling[reference] = node;
705
- this.parents[node] = parent;
706
- if (previous != 0) {
707
- this.nextSibling[previous] = node;
708
- }
709
- else if (this.firstChild[parent] == reference) {
710
- this.firstChild[parent] = node;
711
- }
712
- else if (this.firstToken[parent] == reference) {
713
- this.firstToken[parent] = node;
714
- }
715
- }
716
- // ---- structured token fields ---------------------------------------------------------
717
878
  /**
718
879
  * list node holding the tokens of a structured token field. created when missing
719
880
  * @param id
@@ -723,13 +884,13 @@ class Arena {
723
884
  let child = this.firstChild[id];
724
885
  let index = 0;
725
886
  while (index < field) {
726
- if (child == 0) {
887
+ if (child === 0) {
727
888
  break;
728
889
  }
729
890
  child = this.nextSibling[child];
730
891
  index++;
731
892
  }
732
- if (child != 0) {
893
+ if (child !== 0) {
733
894
  return child;
734
895
  }
735
896
  // create the missing slots
@@ -750,7 +911,7 @@ class Arena {
750
911
  */
751
912
  hasField(id, field) {
752
913
  const list = this.getChildAt(id, field);
753
- return list != null && this.firstToken[list] != 0;
914
+ return list != null && this.firstToken[list] !== 0;
754
915
  }
755
916
  /**
756
917
  * first token of a structured token field (single token fields: `l` and `r` of binary expressions, `op`, ...)
@@ -770,6 +931,7 @@ class Arena {
770
931
  const list = this.getChildAt(id, field);
771
932
  return list == null ? [] : this.tokens(list);
772
933
  }
934
+ // ---- structured token fields ---------------------------------------------------------
773
935
  /**
774
936
  * set the tokens of a structured token field
775
937
  * @param id
@@ -788,35 +950,77 @@ class Arena {
788
950
  appendField(id, field, ...tokens) {
789
951
  this.appendTokenList(this.fieldList(id, field), tokens);
790
952
  }
953
+ /**
954
+ *
955
+ * @param id
956
+ * @param children
957
+ */
791
958
  setLeft(id, ...children) {
792
959
  // inlined: this.setField(id, TokenField.L, ...children)
793
960
  this.setTokenList(this.fieldList(id, TokenField.L), children);
794
961
  }
962
+ /**
963
+ *
964
+ * @param id
965
+ */
795
966
  getLeft(id) {
796
967
  return this.getChildAt(id, TokenField.L) == null ? null : this.fieldTokens(id, TokenField.L);
797
968
  }
798
- setRight(id, ...rightId) {
799
- // inlined: this.setField(id, TokenField.R, ...(rightId == null || rightId[0] == null ? [] : (rightId as number[])))
800
- this.setTokenList(this.fieldList(id, TokenField.R), (rightId == null || rightId[0] == null ? [] : rightId));
969
+ /**
970
+ *
971
+ * @param id
972
+ * @param rightId
973
+ */
974
+ setRight(id, ...rightId) {
975
+ // inlined: this.setField(id, TokenField.R, ...(rightId == null || rightId[0] == null ? [] : (rightId as number[])))
976
+ this.setTokenList(this.fieldList(id, TokenField.R), rightId == null || rightId[0] == null ? [] : rightId);
801
977
  }
978
+ /**
979
+ *
980
+ * @param id
981
+ */
802
982
  getRight(id) {
803
983
  return this.getChildAt(id, TokenField.R) == null ? null : this.fieldTokens(id, TokenField.R);
804
984
  }
985
+ /**
986
+ *
987
+ * @param id
988
+ * @param opId
989
+ */
805
990
  setOp(id, opId) {
806
991
  // inlined: this.setField(id, TokenField.Op, opId)
807
992
  this.setTokenList(this.fieldList(id, TokenField.Op), [opId]);
808
993
  }
994
+ /**
995
+ *
996
+ * @param id
997
+ */
809
998
  getOp(id) {
810
999
  return this.field(id, TokenField.Op);
811
1000
  }
1001
+ /**
1002
+ *
1003
+ * @param id
1004
+ * @param op2Id
1005
+ */
812
1006
  setOp2(id, op2Id) {
813
1007
  // inlined: this.setField(id, TokenField.Op2, op2Id)
814
1008
  this.setTokenList(this.fieldList(id, TokenField.Op2), [op2Id]);
815
1009
  }
1010
+ /**
1011
+ *
1012
+ * @param id
1013
+ */
816
1014
  getOp2(id) {
817
1015
  return this.field(id, TokenField.Op2);
818
1016
  }
819
- // ---- spans ---------------------------------------------------------------------
1017
+ /**
1018
+ *
1019
+ * @param id
1020
+ * @param sourceId
1021
+ * @param start
1022
+ * @param end
1023
+ */
820
1024
  setSpan(id, sourceId, start, end) {
821
1025
  if (littleEndian) {
822
1026
  const index = id << 1;
@@ -838,12 +1042,20 @@ class Arena {
838
1042
  this.spans32[t] = this.spans32[s];
839
1043
  this.spans32[t + 1] = this.spans32[s + 1];
840
1044
  }
1045
+ /**
1046
+ *
1047
+ * @param id
1048
+ */
841
1049
  getSourceId(id) {
842
1050
  if (littleEndian) {
843
1051
  return this.spans32[(id << 1) + 1] >>> 24;
844
1052
  }
845
1053
  return Number((this.spans[id] >> 56n) & 0xffn);
846
1054
  }
1055
+ /**
1056
+ *
1057
+ * @param id
1058
+ */
847
1059
  getSpanStart(id) {
848
1060
  if (littleEndian) {
849
1061
  const index = id << 1;
@@ -851,51 +1063,76 @@ class Arena {
851
1063
  }
852
1064
  return Number((this.spans[id] >> 28n) & 0xfffffffn);
853
1065
  }
1066
+ // ---- spans ---------------------------------------------------------------------
1067
+ /**
1068
+ *
1069
+ * @param id
1070
+ * @param start
1071
+ */
854
1072
  setSpandStart(id, start) {
855
1073
  this.setSpan(id, this.getSourceId(id), start, this.getSpanEnd(id));
856
1074
  }
1075
+ /**
1076
+ *
1077
+ * @param id
1078
+ */
857
1079
  getSpanEnd(id) {
858
1080
  if (littleEndian) {
859
1081
  return this.spans32[id << 1] & 0xfffffff;
860
1082
  }
861
1083
  return Number(this.spans[id] & 0xfffffffn);
862
1084
  }
1085
+ /**
1086
+ *
1087
+ * @param id
1088
+ * @param end
1089
+ */
863
1090
  setSpanEnd(id, end) {
864
1091
  this.setSpan(id, this.getSourceId(id), this.getSpanStart(id), end);
865
1092
  }
1093
+ /**
1094
+ *
1095
+ * @param id
1096
+ */
866
1097
  getParent(id) {
867
1098
  return this.parents[id];
868
1099
  }
1100
+ /**
1101
+ *
1102
+ * @param id
1103
+ */
869
1104
  resolveString(id) {
870
- return this.strings[id];
1105
+ const str = this.stringTable.cache[id];
1106
+ // the string is created the first time it is read
1107
+ return str !== undefined ? str : this.stringTable.get(id);
871
1108
  }
872
1109
  /**
873
1110
  * detach a node from its parent
874
1111
  * @param id
875
1112
  */
876
1113
  detach(id) {
877
- if (id == 0 || this.parents[id] == 0) {
1114
+ if (id === 0 || this.parents[id] === 0) {
878
1115
  return;
879
1116
  }
880
1117
  const parent = this.parents[id];
881
1118
  const next = this.nextSibling[id];
882
1119
  const previous = this.previousSibling[id];
883
- if (previous != 0) {
1120
+ if (previous !== 0) {
884
1121
  this.nextSibling[previous] = next;
885
1122
  }
886
- else if (this.firstChild[parent] == id) {
1123
+ else if (this.firstChild[parent] === id) {
887
1124
  this.firstChild[parent] = next;
888
1125
  }
889
- else if (this.firstToken[parent] == id) {
1126
+ else if (this.firstToken[parent] === id) {
890
1127
  this.firstToken[parent] = next;
891
1128
  }
892
- if (next != 0) {
1129
+ if (next !== 0) {
893
1130
  this.previousSibling[next] = previous;
894
1131
  }
895
- else if (this.lastChild[parent] == id) {
1132
+ else if (this.lastChild[parent] === id) {
896
1133
  this.lastChild[parent] = previous;
897
1134
  }
898
- else if (this.lastToken[parent] == id) {
1135
+ else if (this.lastToken[parent] === id) {
899
1136
  this.lastToken[parent] = previous;
900
1137
  }
901
1138
  this.nextSibling[id] = 0;
@@ -995,16 +1232,16 @@ class Arena {
995
1232
  // the children, in reverse order
996
1233
  const first = top;
997
1234
  let token = this.firstToken[value];
998
- while (token != 0) {
1235
+ while (token !== 0) {
999
1236
  stack[top++] = token;
1000
1237
  token = this.nextSibling[token];
1001
1238
  }
1002
- if ((this.flags[value] & EnumAstNodeStatus.HasChildren) == 0) {
1239
+ if ((this.flags[value] & EnumAstNodeStatus.HasChildren) === 0) {
1003
1240
  let child = this.firstChild[value];
1004
- while (child != 0) {
1005
- if (this.dataTypes[child] == EnumToken.ListTokenType) {
1241
+ while (child !== 0) {
1242
+ if (this.dataTypes[child] === EnumToken.ListTokenType) {
1006
1243
  token = this.firstToken[child];
1007
- while (token != 0) {
1244
+ while (token !== 0) {
1008
1245
  stack[top++] = token;
1009
1246
  token = this.nextSibling[token];
1010
1247
  }
@@ -1033,7 +1270,7 @@ class Arena {
1033
1270
  */
1034
1271
  getValueParent(id) {
1035
1272
  const parent = this.parents[id];
1036
- return parent != 0 && this.dataTypes[parent] == EnumToken.ListTokenType ? this.parents[parent] : parent;
1273
+ return parent !== 0 && this.dataTypes[parent] === EnumToken.ListTokenType ? this.parents[parent] : parent;
1037
1274
  }
1038
1275
  /**
1039
1276
  * node that owns a value token: the declaration, the rule or the at-rule. 0 when the token is detached
@@ -1041,7 +1278,8 @@ class Arena {
1041
1278
  */
1042
1279
  getValueOwner(id) {
1043
1280
  let parent = this.parents[id];
1044
- while (parent != 0 && (this.flags[parent] & (EnumAstNodeStatus.HasChildren | EnumAstNodeStatus.HasValues)) == 0) {
1281
+ while (parent !== 0 &&
1282
+ (this.flags[parent] & (EnumAstNodeStatus.HasChildren | EnumAstNodeStatus.HasValues)) === 0) {
1045
1283
  parent = this.parents[parent];
1046
1284
  }
1047
1285
  return parent;
@@ -1128,7 +1366,7 @@ class Arena {
1128
1366
  const walker = new NodeWalker(this, node);
1129
1367
  for (let r = walker.next(); !r.done; r = walker.next()) {
1130
1368
  const current = r.value;
1131
- if (this.firstToken[current] == 0) {
1369
+ if (this.firstToken[current] === 0) {
1132
1370
  continue;
1133
1371
  }
1134
1372
  const values = new ValueWalker(this, this.tokens(current));
@@ -1150,10 +1388,10 @@ class Arena {
1150
1388
  * @returns the token id or null
1151
1389
  */
1152
1390
  findValue(node, matcher) {
1153
- if (node == null || node == 0) {
1391
+ if (node == null || node === 0) {
1154
1392
  return null;
1155
1393
  }
1156
- if ((this.flags[node] & (EnumAstNodeStatus.HasChildren | EnumAstNodeStatus.HasValues)) == 0) {
1394
+ if ((this.flags[node] & (EnumAstNodeStatus.HasChildren | EnumAstNodeStatus.HasValues)) === 0) {
1157
1395
  // token: the token itself is tested first
1158
1396
  if (matcher(this, node)) {
1159
1397
  return node;
@@ -1177,7 +1415,7 @@ class Arena {
1177
1415
  subTokens(id) {
1178
1416
  const result = [];
1179
1417
  let token = this.firstToken[id];
1180
- while (token != 0) {
1418
+ while (token !== 0) {
1181
1419
  result.push(token);
1182
1420
  token = this.nextSibling[token];
1183
1421
  }
@@ -1186,10 +1424,10 @@ class Arena {
1186
1424
  return result;
1187
1425
  }
1188
1426
  let child = this.firstChild[id];
1189
- while (child != 0) {
1190
- if (this.dataTypes[child] == EnumToken.ListTokenType) {
1427
+ while (child !== 0) {
1428
+ if (this.dataTypes[child] === EnumToken.ListTokenType) {
1191
1429
  token = this.firstToken[child];
1192
- while (token != 0) {
1430
+ while (token !== 0) {
1193
1431
  result.push(token);
1194
1432
  token = this.nextSibling[token];
1195
1433
  }
@@ -1201,9 +1439,14 @@ class Arena {
1201
1439
  }
1202
1440
  return result;
1203
1441
  }
1442
+ /**
1443
+ *
1444
+ * @param id
1445
+ * @param predicate
1446
+ */
1204
1447
  findChild(id, predicate) {
1205
1448
  let child = this.firstChild[id];
1206
- while (child != 0) {
1449
+ while (child !== 0) {
1207
1450
  if (predicate(child)) {
1208
1451
  return child;
1209
1452
  }
@@ -1218,7 +1461,7 @@ class Arena {
1218
1461
  */
1219
1462
  findToken(id, predicate) {
1220
1463
  let token = this.firstToken[id];
1221
- while (token != 0) {
1464
+ while (token !== 0) {
1222
1465
  if (predicate(this, token)) {
1223
1466
  return token;
1224
1467
  }
@@ -1234,7 +1477,7 @@ class Arena {
1234
1477
  siblings(id, filter) {
1235
1478
  let siblings = [];
1236
1479
  let child = this.firstChild[this.parents[id]];
1237
- while (child != 0) {
1480
+ while (child !== 0) {
1238
1481
  if (filter == null || filter(this, child)) {
1239
1482
  siblings.push(child);
1240
1483
  }
@@ -1250,7 +1493,7 @@ class Arena {
1250
1493
  children(id, filter) {
1251
1494
  let children = [];
1252
1495
  let child = this.firstChild[id];
1253
- while (child != 0) {
1496
+ while (child !== 0) {
1254
1497
  if (filter != null && !filter(this, child)) {
1255
1498
  child = this.nextSibling[child];
1256
1499
  continue;
@@ -1268,7 +1511,7 @@ class Arena {
1268
1511
  tokens(id, filter) {
1269
1512
  let tokens = [];
1270
1513
  let token = this.firstToken[id];
1271
- while (token != 0) {
1514
+ while (token !== 0) {
1272
1515
  if (filter != null && !filter(this, token)) {
1273
1516
  token = this.nextSibling[token];
1274
1517
  continue;
@@ -1285,12 +1528,17 @@ class Arena {
1285
1528
  tokenCount(id) {
1286
1529
  let count = 0;
1287
1530
  let token = this.firstToken[id];
1288
- while (token != 0) {
1531
+ while (token !== 0) {
1289
1532
  count++;
1290
1533
  token = this.nextSibling[token];
1291
1534
  }
1292
1535
  return count;
1293
1536
  }
1537
+ /**
1538
+ *
1539
+ * @param id
1540
+ * @param children
1541
+ */
1294
1542
  setChildren(id, ...children) {
1295
1543
  this.setChildList(id, children);
1296
1544
  }
@@ -1301,7 +1549,7 @@ class Arena {
1301
1549
  */
1302
1550
  setChildList(id, children) {
1303
1551
  let child = this.firstChild[id];
1304
- while (child != 0) {
1552
+ while (child !== 0) {
1305
1553
  const next = this.nextSibling[child];
1306
1554
  this.nextSibling[child] = 0;
1307
1555
  this.previousSibling[child] = 0;
@@ -1312,6 +1560,11 @@ class Arena {
1312
1560
  this.lastChild[id] = 0;
1313
1561
  this.appendChildList(id, children);
1314
1562
  }
1563
+ /**
1564
+ *
1565
+ * @param id
1566
+ * @param children
1567
+ */
1315
1568
  setTokens(id, ...children) {
1316
1569
  this.setTokenList(id, children);
1317
1570
  }
@@ -1322,7 +1575,7 @@ class Arena {
1322
1575
  */
1323
1576
  setTokenList(id, tokens) {
1324
1577
  let token = this.firstToken[id];
1325
- while (token != 0) {
1578
+ while (token !== 0) {
1326
1579
  const next = this.nextSibling[token];
1327
1580
  this.nextSibling[token] = 0;
1328
1581
  this.previousSibling[token] = 0;
@@ -1340,14 +1593,14 @@ class Arena {
1340
1593
  * @param before the children are inserted before this child of `to`. they are appended when 0
1341
1594
  */
1342
1595
  moveChildren(from, to, before = 0) {
1343
- if (from == to) {
1596
+ if (from === to) {
1344
1597
  return;
1345
1598
  }
1346
1599
  let child = this.firstChild[from];
1347
- while (child != 0) {
1600
+ while (child !== 0) {
1348
1601
  const next = this.nextSibling[child];
1349
1602
  this.detach(child);
1350
- if (before == 0) {
1603
+ if (before === 0) {
1351
1604
  this.appendChild(to, child);
1352
1605
  }
1353
1606
  else {
@@ -1356,6 +1609,11 @@ class Arena {
1356
1609
  child = next;
1357
1610
  }
1358
1611
  }
1612
+ /**
1613
+ *
1614
+ * @param id
1615
+ * @param children
1616
+ */
1359
1617
  appendChildren(id, ...children) {
1360
1618
  this.appendChildList(id, children);
1361
1619
  }
@@ -1365,19 +1623,19 @@ class Arena {
1365
1623
  * @param children
1366
1624
  */
1367
1625
  appendChildList(id, children) {
1368
- if (children.length == 0) {
1626
+ if (children.length === 0) {
1369
1627
  return;
1370
1628
  }
1371
1629
  let lastChild;
1372
1630
  for (let i = 0; i < children.length; i++) {
1373
1631
  const child = children[i];
1374
- if (child == id || child == 0) {
1632
+ if (child === id || child === 0) {
1375
1633
  continue;
1376
1634
  }
1377
1635
  // the child may already be the last child: detach before reading the last child
1378
1636
  this.detach(child);
1379
1637
  lastChild = this.lastChild[id];
1380
- if (lastChild == 0) {
1638
+ if (lastChild === 0) {
1381
1639
  this.firstChild[id] = child;
1382
1640
  }
1383
1641
  else {
@@ -1389,6 +1647,11 @@ class Arena {
1389
1647
  this.lastChild[id] = child;
1390
1648
  }
1391
1649
  }
1650
+ /**
1651
+ *
1652
+ * @param id
1653
+ * @param children
1654
+ */
1392
1655
  appendTokens(id, ...children) {
1393
1656
  this.appendTokenList(id, children);
1394
1657
  }
@@ -1398,19 +1661,19 @@ class Arena {
1398
1661
  * @param children
1399
1662
  */
1400
1663
  appendTokenList(id, children) {
1401
- if (children.length == 0) {
1664
+ if (children.length === 0) {
1402
1665
  return;
1403
1666
  }
1404
1667
  let lastToken;
1405
1668
  for (let i = 0; i < children.length; i++) {
1406
1669
  const token = children[i];
1407
- if (token == id || token == 0) {
1670
+ if (token === id || token === 0) {
1408
1671
  continue;
1409
1672
  }
1410
1673
  // the token may already be the last token: detach before reading the last token
1411
1674
  this.detach(token);
1412
1675
  lastToken = this.lastToken[id];
1413
- if (lastToken == 0) {
1676
+ if (lastToken === 0) {
1414
1677
  this.firstToken[id] = token;
1415
1678
  }
1416
1679
  else {
@@ -1428,14 +1691,14 @@ class Arena {
1428
1691
  * @param child
1429
1692
  */
1430
1693
  appendChild(id, child) {
1431
- if (child == id || child == 0) {
1694
+ if (child === id || child === 0) {
1432
1695
  return;
1433
1696
  }
1434
- if (this.parents[child] != 0) {
1697
+ if (this.parents[child] !== 0) {
1435
1698
  this.detach(child);
1436
1699
  }
1437
1700
  const lastChild = this.lastChild[id];
1438
- if (lastChild == 0) {
1701
+ if (lastChild === 0) {
1439
1702
  this.firstChild[id] = child;
1440
1703
  }
1441
1704
  else {
@@ -1452,14 +1715,14 @@ class Arena {
1452
1715
  * @param token
1453
1716
  */
1454
1717
  appendToken(id, token) {
1455
- if (token == id || token == 0) {
1718
+ if (token === id || token === 0) {
1456
1719
  return;
1457
1720
  }
1458
- if (this.parents[token] != 0) {
1721
+ if (this.parents[token] !== 0) {
1459
1722
  this.detach(token);
1460
1723
  }
1461
1724
  const lastToken = this.lastToken[id];
1462
- if (lastToken == 0) {
1725
+ if (lastToken === 0) {
1463
1726
  this.firstToken[id] = token;
1464
1727
  }
1465
1728
  else {
@@ -1477,41 +1740,41 @@ class Arena {
1477
1740
  */
1478
1741
  prependTokens(id, ...tokens) {
1479
1742
  const first = this.firstToken[id];
1480
- if (first == 0) {
1743
+ if (first === 0) {
1481
1744
  this.appendTokenList(id, tokens);
1482
1745
  return;
1483
1746
  }
1484
1747
  for (const token of tokens) {
1485
- if (token == id || token == 0) {
1748
+ if (token === id || token === 0) {
1486
1749
  continue;
1487
1750
  }
1488
1751
  this.setPreviousSibling(first, token);
1489
1752
  }
1490
1753
  }
1754
+ /**
1755
+ *
1756
+ * @param node
1757
+ * @param key
1758
+ * @param string
1759
+ */
1491
1760
  equalsIgnoreCase(node, key, string) {
1492
- const id = key == "nam" ? this.name[node] : this.stringValue[node];
1493
- const stringId = this.stringIds.get(string);
1494
- if (stringId != null && id == stringId) {
1495
- return true;
1496
- }
1497
- const value = this.strings[id];
1498
- if (value == null || string.length != value.length) {
1499
- return false;
1500
- }
1501
- return equalsIgnoreCase(value, string);
1761
+ // the string of the node is not created
1762
+ return this.stringTable.equalsIgnoreCase(key === "nam" ? this.name[node] : this.stringValue[node], string);
1502
1763
  }
1764
+ /**
1765
+ *
1766
+ * @param node
1767
+ * @param key
1768
+ * @param string
1769
+ */
1503
1770
  startsWith(node, key, string) {
1504
- const id = key == "nam" ? this.name[node] : this.stringValue[node];
1505
- const stringId = this.stringIds.get(string);
1506
- if (stringId != null && id == stringId) {
1507
- return true;
1508
- }
1509
- const value = this.strings[id];
1510
- if (value == null || string.length > value.length) {
1511
- return false;
1512
- }
1513
- return value.startsWith(string);
1771
+ // the string of the node is not created
1772
+ return this.stringTable.startsWith(key === "nam" ? this.name[node] : this.stringValue[node], string);
1514
1773
  }
1774
+ /**
1775
+ *
1776
+ * @param newSize
1777
+ */
1515
1778
  resize(newSize) {
1516
1779
  if (newSize <= this.dataTypes.length) {
1517
1780
  return;
@@ -1519,6 +1782,7 @@ class Arena {
1519
1782
  const newDataTypes = new Uint8Array(newSize);
1520
1783
  newDataTypes.set(this.dataTypes);
1521
1784
  this.dataTypes = newDataTypes;
1785
+ this.capacity = newSize;
1522
1786
  const newParents = new Uint32Array(newSize);
1523
1787
  newParents.set(this.parents);
1524
1788
  this.parents = newParents;
@@ -1567,7 +1831,7 @@ class Arena {
1567
1831
  * drop the interned strings that are no longer referenced by any node
1568
1832
  */
1569
1833
  compactStrings() {
1570
- const used = new Uint8Array(this.strings.length);
1834
+ const used = new Uint8Array(this.stringTable.count);
1571
1835
  const size = this.current + 1;
1572
1836
  let count = 0;
1573
1837
  // the empty string is always kept
@@ -1580,29 +1844,16 @@ class Arena {
1580
1844
  for (let i = 0; i < used.length; i++) {
1581
1845
  count += used[i];
1582
1846
  }
1583
- if (count == this.strings.length) {
1847
+ if (count === this.stringTable.count) {
1584
1848
  return;
1585
1849
  }
1586
1850
  // remap the string ids
1587
- const mapping = new Uint32Array(this.strings.length);
1588
- const strings = new Array(count);
1589
- const stringIds = new Map();
1590
- let next = 0;
1591
- for (let i = 0; i < used.length; i++) {
1592
- if (used[i]) {
1593
- mapping[i] = next;
1594
- strings[next] = this.strings[i];
1595
- stringIds.set(this.strings[i], next);
1596
- next++;
1597
- }
1598
- }
1851
+ const mapping = this.stringTable.compact(used);
1599
1852
  for (let id = 1; id < size; id++) {
1600
1853
  this.name[id] = mapping[this.name[id]];
1601
1854
  this.stringValue[id] = mapping[this.stringValue[id]];
1602
1855
  this.unit[id] = mapping[this.unit[id]];
1603
1856
  }
1604
- this.strings = strings;
1605
- this.stringIds = stringIds;
1606
1857
  }
1607
1858
  /**
1608
1859
  * drop the nodes that are not reachable from the root and renumber the remaining nodes in document order.
@@ -1622,10 +1873,10 @@ class Arena {
1622
1873
  const id = stack.pop();
1623
1874
  order[next] = id;
1624
1875
  mapping[id] = next++;
1625
- for (let child = this.lastChild[id]; child != 0; child = this.previousSibling[child]) {
1876
+ for (let child = this.lastChild[id]; child !== 0; child = this.previousSibling[child]) {
1626
1877
  stack.push(child);
1627
1878
  }
1628
- for (let token = this.lastToken[id]; token != 0; token = this.previousSibling[token]) {
1879
+ for (let token = this.lastToken[id]; token !== 0; token = this.previousSibling[token]) {
1629
1880
  stack.push(token);
1630
1881
  }
1631
1882
  }
@@ -1647,7 +1898,7 @@ class Arena {
1647
1898
  const spans = new BigUint64Array(capacity);
1648
1899
  const spans32 = new Uint32Array(spans.buffer);
1649
1900
  const nodeErrors = new Map();
1650
- const used = new Uint8Array(this.strings.length);
1901
+ const used = new Uint8Array(this.stringTable.count);
1651
1902
  // the empty string is always kept
1652
1903
  used[0] = 1;
1653
1904
  for (let target = 1; target < next; target++) {
@@ -1677,40 +1928,28 @@ class Arena {
1677
1928
  for (let i = 0; i < used.length; i++) {
1678
1929
  count += used[i];
1679
1930
  }
1680
- if (count < this.strings.length) {
1681
- const stringMapping = new Uint32Array(this.strings.length);
1682
- const strings = new Array(count);
1683
- const stringIds = new Map();
1684
- let index = 0;
1685
- for (let i = 0; i < used.length; i++) {
1686
- if (used[i]) {
1687
- stringMapping[i] = index;
1688
- strings[index] = this.strings[i];
1689
- stringIds.set(this.strings[i], index);
1690
- index++;
1691
- }
1692
- }
1931
+ if (count < this.stringTable.count) {
1932
+ const stringMapping = this.stringTable.compact(used);
1693
1933
  for (let id = 1; id < next; id++) {
1694
1934
  name[id] = stringMapping[name[id]];
1695
1935
  stringValue[id] = stringMapping[stringValue[id]];
1696
1936
  unit[id] = stringMapping[unit[id]];
1697
1937
  }
1698
- this.strings = strings;
1699
- this.stringIds = stringIds;
1700
1938
  }
1701
1939
  for (const [id, list] of this.errors) {
1702
- if (mapping[id] != 0) {
1940
+ if (mapping[id] !== 0) {
1703
1941
  nodeErrors.set(mapping[id], list);
1704
1942
  }
1705
1943
  }
1706
1944
  if (errors != null) {
1707
1945
  for (const error of errors) {
1708
- if (typeof error.node == "number") {
1946
+ if (typeof error.node === "number") {
1709
1947
  error.node = mapping[error.node] || null;
1710
1948
  }
1711
1949
  }
1712
1950
  }
1713
1951
  this.dataTypes = dataTypes;
1952
+ this.capacity = dataTypes.length;
1714
1953
  this.parents = parents;
1715
1954
  this.nextSibling = nextSibling;
1716
1955
  this.previousSibling = previousSibling;
@@ -1738,7 +1977,7 @@ class Arena {
1738
1977
  */
1739
1978
  mark(...ids) {
1740
1979
  for (const id of ids) {
1741
- if (id != 0 && id != this.root) {
1980
+ if (id !== 0 && id !== this.root) {
1742
1981
  this.flags[id] |= EnumAstNodeStatus.Marked;
1743
1982
  }
1744
1983
  }
@@ -1748,7 +1987,7 @@ class Arena {
1748
1987
  * @param id
1749
1988
  */
1750
1989
  isMarked(id) {
1751
- return (this.flags[id] & EnumAstNodeStatus.Marked) != 0;
1990
+ return (this.flags[id] & EnumAstNodeStatus.Marked) !== 0;
1752
1991
  }
1753
1992
  /**
1754
1993
  * release nodes: detach them and drop their data (interned strings, errors). node ids are never reused:
@@ -1766,18 +2005,18 @@ class Arena {
1766
2005
  }
1767
2006
  return;
1768
2007
  }
1769
- if (id == 0 || id == this.root) {
2008
+ if (id === 0 || id === this.root) {
1770
2009
  return;
1771
2010
  }
1772
2011
  if (deep) {
1773
2012
  let child = this.firstChild[id];
1774
- while (child != 0) {
2013
+ while (child !== 0) {
1775
2014
  const next = this.nextSibling[child];
1776
2015
  this.release(child, true);
1777
2016
  child = next;
1778
2017
  }
1779
2018
  let token = this.firstToken[id];
1780
- while (token != 0) {
2019
+ while (token !== 0) {
1781
2020
  const next = this.nextSibling[token];
1782
2021
  this.release(token, true);
1783
2022
  token = next;
@@ -1799,15 +2038,24 @@ class Arena {
1799
2038
  this.release();
1800
2039
  return this.compact(errors);
1801
2040
  }
2041
+ /**
2042
+ *
2043
+ * @param id
2044
+ * @param errors
2045
+ */
1802
2046
  setErrors(id, errors) {
1803
2047
  // empty error lists are not stored
1804
- if (errors == null || errors.length == 0) {
2048
+ if (errors == null || errors.length === 0) {
1805
2049
  this.errors.delete(id);
1806
2050
  }
1807
2051
  else {
1808
2052
  this.errors.set(id, errors);
1809
2053
  }
1810
2054
  }
2055
+ /**
2056
+ *
2057
+ * @param id
2058
+ */
1811
2059
  getErrors(id) {
1812
2060
  return this.errors.get(id) ?? null;
1813
2061
  }
@@ -1818,12 +2066,77 @@ class Arena {
1818
2066
  childCount(id) {
1819
2067
  let count = 0;
1820
2068
  let child = this.firstChild[id];
1821
- while (child != 0) {
2069
+ while (child !== 0) {
1822
2070
  count++;
1823
2071
  child = this.nextSibling[child];
1824
2072
  }
1825
2073
  return count;
1826
2074
  }
2075
+ /**
2076
+ * structured-cloneable properties of the arena
2077
+ */
2078
+ exportProperties() {
2079
+ return {
2080
+ kind: "Arena",
2081
+ current: this.current,
2082
+ strings: this.stringTable.export(),
2083
+ errors: [...this.errors.entries()],
2084
+ };
2085
+ }
2086
+ /**
2087
+ * column buffers, in the order of {@link arenaColumns}. the `spans` buffer is shared with the `spans32` view
2088
+ */
2089
+ exportTransferables() {
2090
+ const buffers = [];
2091
+ for (const column of arenaColumns) {
2092
+ const array = this[column];
2093
+ // the columns always own their buffer: no offset or partial view
2094
+ buffers.push(array.buffer);
2095
+ }
2096
+ return buffers;
2097
+ }
2098
+ /**
2099
+ * initialize the arena from exported properties and column buffers
2100
+ * @param properties
2101
+ * @param transferables
2102
+ */
2103
+ importProperties(properties, transferables) {
2104
+ if (transferables.length !== arenaColumns.length) {
2105
+ throw new TypeError(`expected ${arenaColumns.length} column buffers, got ${transferables.length}`);
2106
+ }
2107
+ for (let i = 0; i < arenaColumns.length; i++) {
2108
+ const column = arenaColumns[i];
2109
+ // @ts-ignore
2110
+ this[column] = new arenaColumnTypes[column](transferables[i]);
2111
+ }
2112
+ this.spans32 = new Uint32Array(this.spans.buffer);
2113
+ this.capacity = this.dataTypes.length;
2114
+ this.current = properties.current;
2115
+ this.stringTable.import(properties.strings);
2116
+ this.errors = new Map(properties.errors);
2117
+ }
2118
+ /**
2119
+ * link a node before a reference node of the given parent
2120
+ * @param parent
2121
+ * @param reference
2122
+ * @param node
2123
+ */
2124
+ #insertBefore(parent, reference, node) {
2125
+ const previous = this.previousSibling[reference];
2126
+ this.previousSibling[node] = previous;
2127
+ this.nextSibling[node] = reference;
2128
+ this.previousSibling[reference] = node;
2129
+ this.parents[node] = parent;
2130
+ if (previous !== 0) {
2131
+ this.nextSibling[previous] = node;
2132
+ }
2133
+ else if (this.firstChild[parent] === reference) {
2134
+ this.firstChild[parent] = node;
2135
+ }
2136
+ else if (this.firstToken[parent] === reference) {
2137
+ this.firstToken[parent] = node;
2138
+ }
2139
+ }
1827
2140
  }
1828
2141
  /**
1829
2142
  * Css ast arena: ast nodes and value tokens.
@@ -1833,44 +2146,32 @@ class Arena {
1833
2146
  */
1834
2147
  class AstArena extends Arena {
1835
2148
  /**
1836
- * raw selector (minification cache). sparse: only rules have an entry
2149
+ * raw selector (minification cache), by rule id
1837
2150
  */
1838
- raw = [];
2151
+ raw = new Map();
1839
2152
  /**
1840
- * optimized selector (minification cache). sparse: only rules have an entry
2153
+ * optimized selector (minification cache), by rule id
1841
2154
  */
1842
- optimized = [];
2155
+ optimized = new Map();
1843
2156
  /**
1844
2157
  * source ids. `spans` store the index in this table (8 bits)
1845
2158
  */
1846
2159
  sources = [];
2160
+ /**
2161
+ * length of the css parsed into a stylesheet, imported files included. sparse: only the stylesheets created by
2162
+ * the parser have an entry. the render buffer is allocated from this length
2163
+ */
2164
+ inputLengths = [];
2165
+ /**
2166
+ *
2167
+ * @param size
2168
+ */
1847
2169
  constructor(size = 4096) {
1848
2170
  super(size);
1849
2171
  // string id 0 is the null / empty string
1850
2172
  this.internString("");
1851
2173
  this.flags[this.root] = EnumAstNodeStatus.HasChildren;
1852
2174
  }
1853
- /**
1854
- * @inheritDoc
1855
- */
1856
- exportProperties() {
1857
- return {
1858
- ...super.exportProperties(),
1859
- kind: "AstArena",
1860
- sources: this.sources,
1861
- raw: this.raw,
1862
- optimized: this.optimized,
1863
- };
1864
- }
1865
- /**
1866
- * @inheritDoc
1867
- */
1868
- importProperties(properties, transferables) {
1869
- super.importProperties(properties, transferables);
1870
- this.sources = properties.sources?.slice() ?? [];
1871
- this.raw = properties.raw?.slice() ?? [];
1872
- this.optimized = properties.optimized?.slice() ?? [];
1873
- }
1874
2175
  /**
1875
2176
  * @inheritDoc
1876
2177
  */
@@ -1878,11 +2179,14 @@ class AstArena extends Arena {
1878
2179
  super.release(id, deep);
1879
2180
  if (id != null) {
1880
2181
  // drop the minification caches
1881
- if (this.raw[id] != null) {
1882
- this.raw[id] = undefined;
2182
+ if (this.raw.get(id) != null) {
2183
+ this.raw.delete(id);
2184
+ }
2185
+ if (this.optimized.get(id) != null) {
2186
+ this.optimized.delete(id);
1883
2187
  }
1884
- if (this.optimized[id] != null) {
1885
- this.optimized[id] = undefined;
2188
+ if (this.inputLengths[id] != null) {
2189
+ this.inputLengths[id] = undefined;
1886
2190
  }
1887
2191
  }
1888
2192
  }
@@ -1892,11 +2196,18 @@ class AstArena extends Arena {
1892
2196
  compact(errors) {
1893
2197
  const mapping = super.compact(errors);
1894
2198
  // the minification caches are dropped
1895
- this.raw = [];
1896
- this.optimized = [];
2199
+ this.raw = new Map();
2200
+ this.optimized = new Map();
2201
+ // the input lengths of the stylesheets that are kept
2202
+ const inputLengths = [];
2203
+ for (let id = 0; id < this.inputLengths.length; id++) {
2204
+ if (this.inputLengths[id] != null && id < mapping.length && mapping[id] !== 0) {
2205
+ inputLengths[mapping[id]] = this.inputLengths[id];
2206
+ }
2207
+ }
2208
+ this.inputLengths = inputLengths;
1897
2209
  return mapping;
1898
2210
  }
1899
- // ---- state ---------------------------------------------------------------------
1900
2211
  /**
1901
2212
  * validation state bits
1902
2213
  * @param id
@@ -1912,21 +2223,21 @@ class AstArena extends Arena {
1912
2223
  setState(id, state) {
1913
2224
  this.flags[id] = (this.flags[id] & ~EnumAstNodeStatus.StateMask) | ((state ?? 0) & EnumAstNodeStatus.StateMask);
1914
2225
  }
2226
+ // ---- state ---------------------------------------------------------------------
1915
2227
  /**
1916
2228
  * true if the node is invalid
1917
2229
  * @param id
1918
2230
  */
1919
2231
  isInvalid(id) {
1920
- return (this.flags[id] & EnumAstNodeStatus.Invalid) != 0;
2232
+ return (this.flags[id] & EnumAstNodeStatus.Invalid) !== 0;
1921
2233
  }
1922
- // ---- location ------------------------------------------------------------------
1923
2234
  /**
1924
2235
  * index of a source id in the sources table
1925
2236
  * @param srcId
1926
2237
  */
1927
2238
  sourceIndex(srcId) {
1928
2239
  let index = this.sources.indexOf(srcId);
1929
- if (index == -1) {
2240
+ if (index === -1) {
1930
2241
  index = this.sources.length;
1931
2242
  this.sources.push(srcId);
1932
2243
  }
@@ -1949,6 +2260,7 @@ class AstArena extends Arena {
1949
2260
  this.flags[id] |= EnumAstNodeStatus.HasLocation;
1950
2261
  this.setSpan(id, this.sourceIndex(srcId ?? 0), sta ?? 0, end ?? 0);
1951
2262
  }
2263
+ // ---- location ------------------------------------------------------------------
1952
2264
  /**
1953
2265
  * set the location of a token from the tokenizer values. the source index is the index of the source id in the sources table
1954
2266
  * @param id
@@ -1977,7 +2289,7 @@ class AstArena extends Arena {
1977
2289
  * @param end
1978
2290
  */
1979
2291
  spanLocation(id, start, end) {
1980
- if ((this.flags[start] & EnumAstNodeStatus.HasLocation) == 0) {
2292
+ if ((this.flags[start] & EnumAstNodeStatus.HasLocation) === 0) {
1981
2293
  this.copyLocation(id, end);
1982
2294
  return;
1983
2295
  }
@@ -1989,14 +2301,14 @@ class AstArena extends Arena {
1989
2301
  * @param id
1990
2302
  */
1991
2303
  hasLocation(id) {
1992
- return (this.flags[id] & EnumAstNodeStatus.HasLocation) != 0;
2304
+ return (this.flags[id] & EnumAstNodeStatus.HasLocation) !== 0;
1993
2305
  }
1994
2306
  /**
1995
2307
  * source location
1996
2308
  * @param id
1997
2309
  */
1998
2310
  getLocation(id) {
1999
- if ((this.flags[id] & EnumAstNodeStatus.HasLocation) == 0) {
2311
+ if ((this.flags[id] & EnumAstNodeStatus.HasLocation) === 0) {
2000
2312
  return null;
2001
2313
  }
2002
2314
  return {
@@ -2012,7 +2324,6 @@ class AstArena extends Arena {
2012
2324
  getSource(id) {
2013
2325
  return this.sources[this.getSourceId(id)] ?? 0;
2014
2326
  }
2015
- // ---- ast nodes -------------------------------------------------------------------
2016
2327
  /**
2017
2328
  * allocate a stylesheet node
2018
2329
  */
@@ -2033,7 +2344,8 @@ class AstArena extends Arena {
2033
2344
  createRule(typ, sel, tokens, state, errors) {
2034
2345
  const id = this.allocate();
2035
2346
  this.dataTypes[id] = typ;
2036
- this.name[id] = this.internString(sel);
2347
+ // a string or an interned string id
2348
+ this.name[id] = typeof sel === "number" ? sel : this.internString(sel);
2037
2349
  this.flags[id] = EnumAstNodeStatus.HasChildren | ((state ?? 0) & EnumAstNodeStatus.StateMask);
2038
2350
  if (tokens != null) {
2039
2351
  this.appendTokenList(id, tokens);
@@ -2043,6 +2355,7 @@ class AstArena extends Arena {
2043
2355
  }
2044
2356
  return id;
2045
2357
  }
2358
+ // ---- ast nodes -------------------------------------------------------------------
2046
2359
  /**
2047
2360
  * allocate an at-rule node
2048
2361
  * @param typ
@@ -2056,8 +2369,9 @@ class AstArena extends Arena {
2056
2369
  createAtRule(typ, nam, val, block, tokens, state, errors) {
2057
2370
  const id = this.allocate();
2058
2371
  this.dataTypes[id] = typ;
2059
- this.name[id] = this.internString(nam);
2060
- this.stringValue[id] = this.internString(val);
2372
+ // strings or interned string ids
2373
+ this.name[id] = typeof nam === "number" ? nam : this.internString(nam);
2374
+ this.stringValue[id] = typeof val === "number" ? val : this.internString(val);
2061
2375
  this.flags[id] = (block ? EnumAstNodeStatus.HasChildren : 0) | ((state ?? 0) & EnumAstNodeStatus.StateMask);
2062
2376
  if (tokens != null) {
2063
2377
  this.appendTokenList(id, tokens);
@@ -2078,7 +2392,8 @@ class AstArena extends Arena {
2078
2392
  createDeclaration(typ, nam, val, state, errors) {
2079
2393
  const id = this.allocate();
2080
2394
  this.dataTypes[id] = typ;
2081
- this.name[id] = this.internString(nam);
2395
+ // a string or an interned string id
2396
+ this.name[id] = typeof nam === "number" ? nam : this.internString(nam);
2082
2397
  this.flags[id] = EnumAstNodeStatus.HasValues | ((state ?? 0) & EnumAstNodeStatus.StateMask);
2083
2398
  if (val != null) {
2084
2399
  this.appendTokenList(id, val);
@@ -2096,11 +2411,11 @@ class AstArena extends Arena {
2096
2411
  createComment(typ, val) {
2097
2412
  const id = this.allocate();
2098
2413
  this.dataTypes[id] = typ;
2099
- this.stringValue[id] = this.internString(val);
2414
+ // a string or an interned string id
2415
+ this.stringValue[id] = typeof val === "number" ? val : this.internString(val);
2100
2416
  this.flags[id] = 0;
2101
2417
  return id;
2102
2418
  }
2103
- // ---- tokens ----------------------------------------------------------------------
2104
2419
  /**
2105
2420
  * allocate a token. string values are interned, numeric values are stored in the `numericValue` column
2106
2421
  * @param typ
@@ -2110,10 +2425,10 @@ class AstArena extends Arena {
2110
2425
  createToken(typ, val, unit) {
2111
2426
  const id = this.allocate();
2112
2427
  this.dataTypes[id] = typ;
2113
- if (typeof val == "string") {
2428
+ if (typeof val === "string") {
2114
2429
  this.stringValue[id] = this.internString(val);
2115
2430
  }
2116
- else if (typeof val == "number") {
2431
+ else if (typeof val === "number") {
2117
2432
  this.numericValue[id] = val;
2118
2433
  }
2119
2434
  if (unit != null) {
@@ -2138,6 +2453,7 @@ class AstArena extends Arena {
2138
2453
  }
2139
2454
  return id;
2140
2455
  }
2456
+ // ---- tokens ----------------------------------------------------------------------
2141
2457
  /**
2142
2458
  * allocate a function token
2143
2459
  * @param typ function token type
@@ -2200,7 +2516,7 @@ class AstArena extends Arena {
2200
2516
  */
2201
2517
  hasFraction(id) {
2202
2518
  const child = this.firstChild[id];
2203
- return child != 0 && this.dataTypes[child] == EnumToken.FractionTokenType;
2519
+ return child !== 0 && this.dataTypes[child] === EnumToken.FractionTokenType;
2204
2520
  }
2205
2521
  /**
2206
2522
  * fraction token of a numeric token
@@ -2208,7 +2524,7 @@ class AstArena extends Arena {
2208
2524
  */
2209
2525
  getFraction(id) {
2210
2526
  const child = this.firstChild[id];
2211
- return child != 0 && this.dataTypes[child] == EnumToken.FractionTokenType ? child : 0;
2527
+ return child !== 0 && this.dataTypes[child] === EnumToken.FractionTokenType ? child : 0;
2212
2528
  }
2213
2529
  /**
2214
2530
  * set the fraction value of a numeric token. the numeric value is set to the fraction value
@@ -2217,10 +2533,10 @@ class AstArena extends Arena {
2217
2533
  */
2218
2534
  setFraction(id, fraction) {
2219
2535
  const child = this.getFraction(id);
2220
- if (child != 0) {
2536
+ if (child !== 0) {
2221
2537
  this.detach(child);
2222
2538
  }
2223
- if (fraction != null && fraction != 0) {
2539
+ if (fraction != null && fraction !== 0) {
2224
2540
  // inlined: this.appendChildren(id, fraction)
2225
2541
  this.appendChildList(id, [fraction]);
2226
2542
  this.numericValue[id] =
@@ -2246,7 +2562,7 @@ class AstArena extends Arena {
2246
2562
  * @param id
2247
2563
  */
2248
2564
  str(id) {
2249
- return this.strings[this.stringValue[id]];
2565
+ return this.resolveString(this.stringValue[id]);
2250
2566
  }
2251
2567
  /**
2252
2568
  * set the string value of a token (`val`)
@@ -2277,11 +2593,11 @@ class AstArena extends Arena {
2277
2593
  * @param tokens
2278
2594
  */
2279
2595
  replaceToken(id, ...tokens) {
2280
- if (this.parents[id] == 0) {
2596
+ if (this.parents[id] === 0) {
2281
2597
  return;
2282
2598
  }
2283
2599
  const index = tokens.indexOf(id);
2284
- if (index == -1) {
2600
+ if (index === -1) {
2285
2601
  this.insertBefore(id, ...tokens);
2286
2602
  this.detach(id);
2287
2603
  return;
@@ -2295,7 +2611,7 @@ class AstArena extends Arena {
2295
2611
  * @param token
2296
2612
  */
2297
2613
  take(token) {
2298
- return this.parents[token] == 0 ? token : this.clone(token, true);
2614
+ return this.parents[token] === 0 ? token : this.clone(token, true);
2299
2615
  }
2300
2616
  /**
2301
2617
  * copy the data columns of a token (type, values, unit, color type, location)
@@ -2318,212 +2634,48 @@ class AstArena extends Arena {
2318
2634
  * @param typ
2319
2635
  * @param val
2320
2636
  * @param unit
2321
- */
2322
- resetToken(id, typ, val, unit) {
2323
- this.dataTypes[id] = typ;
2324
- this.stringValue[id] = 0;
2325
- this.numericValue[id] = 0;
2326
- this.unit[id] = 0;
2327
- this.colorType[id] = 0;
2328
- this.name[id] = 0;
2329
- if (typeof val == "string") {
2330
- this.stringValue[id] = this.internString(val);
2331
- }
2332
- else if (typeof val == "number") {
2333
- this.numericValue[id] = val;
2334
- }
2335
- if (unit != null) {
2336
- this.unit[id] = this.internString(unit);
2337
- }
2338
- // inlined: this.setTokens(id)
2339
- this.setTokenList(id, []);
2340
- // inlined: this.setChildren(id)
2341
- this.setChildList(id, []);
2342
- }
2343
- // ---- plain objects -----------------------------------------------------------------
2344
- /**
2345
- * Import an object: an {@link AstArenaNode} or a plain object ({@link PlainAstNode}, {@link PlainToken}).
2346
- *
2347
- * - importing a view of this arena is a noop: its node id is returned, the node is not copied,
2348
- * - a view of another arena is copied with its descendants (see {@link import}),
2349
- * - a plain object is allocated in the arena. `chi`, `val`, `l`, `r`, ... are created recursively, they can be
2350
- * node ids, views or plain objects.
2351
- *
2352
- * @param node
2353
- * @returns the node id
2354
- */
2355
- fromObject(node) {
2356
- const id = node[ARENAID];
2357
- if (id !== undefined) {
2358
- // no copy for the nodes of this arena
2359
- return this.import(node[ARENA], id);
2360
- }
2361
- return isNodeObjectType(node.typ)
2362
- ? this.nodeFromObject(node)
2363
- : this.tokenFromObject(node);
2364
- }
2365
- /**
2366
- * create an ast node from a plain object
2367
- * @param node
2368
- */
2369
- nodeFromObject(node) {
2370
- const id = this.allocate();
2371
- const typ = node.typ;
2372
- this.dataTypes[id] = typ;
2373
- switch (typ) {
2374
- case EnumToken.StyleSheetNodeType:
2375
- this.flags[id] = EnumAstNodeStatus.HasChildren;
2376
- break;
2377
- case EnumToken.RuleNodeType:
2378
- case EnumToken.KeyframesRuleNodeType:
2379
- this.name[id] = this.internString((node.sel ?? node.nam ?? ""));
2380
- this.flags[id] = EnumAstNodeStatus.HasChildren;
2381
- if (node.raw != null) {
2382
- this.raw[id] = node.raw;
2383
- }
2384
- if (node.optimized != null) {
2385
- this.optimized[id] = node.optimized;
2386
- }
2387
- if (typeof node.val == "string") {
2388
- this.stringValue[id] = this.internString(node.val);
2389
- }
2390
- break;
2391
- case EnumToken.AtRuleNodeType:
2392
- case EnumToken.KeyframesAtRuleNodeType:
2393
- this.name[id] = this.internString(node.nam ?? "");
2394
- this.stringValue[id] = this.internString(node.val ?? "");
2395
- this.flags[id] = node.chi != null ? EnumAstNodeStatus.HasChildren : 0;
2396
- break;
2397
- case EnumToken.CommentNodeType:
2398
- case EnumToken.CDOCOMMNodeType:
2399
- this.stringValue[id] = this.internString(node.val ?? "");
2400
- this.flags[id] = 0;
2401
- break;
2402
- default:
2403
- // declaration like nodes
2404
- this.name[id] = this.internString(node.nam ?? "");
2405
- this.flags[id] = EnumAstNodeStatus.HasValues;
2406
- if (Array.isArray(node.val)) {
2407
- for (const token of node.val) {
2408
- this.appendToken(id, typeof token == "number" ? token : this.fromObject(token));
2409
- }
2410
- }
2411
- break;
2412
- }
2413
- if (Array.isArray(node.tokens)) {
2414
- for (const token of node.tokens) {
2415
- this.appendToken(id, typeof token == "number" ? token : this.fromObject(token));
2416
- }
2417
- }
2418
- // inlined: this.setState(id, node.state)
2419
- this.flags[id] = (this.flags[id] & ~EnumAstNodeStatus.StateMask) | ((node.state ?? 0) & EnumAstNodeStatus.StateMask);
2420
- const loc = node.loc;
2421
- if (loc != null) {
2422
- this.setLocation(id, loc.srcId, loc.sta, loc.end);
2423
- }
2424
- const errors = node.errors;
2425
- if (errors != null && errors.length > 0) {
2426
- this.errors.set(id, errors);
2427
- }
2428
- if (Array.isArray(node.chi) && this.flags[id] & EnumAstNodeStatus.HasChildren) {
2429
- for (const child of node.chi) {
2430
- // inlined: this.appendChildren(id, typeof child == "number" ? child : this.fromObject(child))
2431
- this.appendChildList(id, [(typeof child == "number" ? child : this.fromObject(child))]);
2432
- }
2433
- }
2434
- return id;
2435
- }
2436
- /**
2437
- * create a token from a plain object
2438
- * @param token
2439
- */
2440
- tokenFromObject(token) {
2441
- const id = this.allocate();
2442
- const typ = token.typ;
2443
- const plain = token;
2444
- this.dataTypes[id] = typ;
2445
- if (typeof plain.nam == "string") {
2446
- this.name[id] = this.internString(plain.nam);
2447
- }
2448
- if (typeof plain.unit == "string") {
2449
- this.unit[id] = this.internString(plain.unit);
2450
- }
2451
- if (typeof plain.kin == "number") {
2452
- // inlined: this.setColorType(id, plain.kin)
2453
- this.colorType[id] = plain.kin;
2454
- }
2455
- if (typeof plain.cal == "string") {
2456
- this.unit[id] = this.internString(plain.cal);
2457
- }
2458
- if (typeof plain.attr == "string") {
2459
- this.unit[id] = this.internString(plain.attr);
2460
- }
2461
- if (typ == EnumToken.BinaryExpressionTokenType) {
2462
- this.numericValue[id] = plain.op;
2463
- }
2464
- else if (typ == EnumToken.UnaryExpressionTokenType) {
2465
- this.numericValue[id] = plain.sign;
2466
- // inlined: this.appendChildren(id, typeof plain.val == "number" ? plain.val : this.fromObject(plain.val))
2467
- this.appendChildList(id, [(typeof plain.val == "number" ? plain.val : this.fromObject(plain.val))]);
2468
- }
2469
- else if (typ == EnumToken.FractionTokenType) {
2470
- // inlined: this.appendChildren(id, typeof plain.l == "number" ? plain.l : this.fromObject(plain.l), typeof plain.r == "number" ? plain.r : this.fromObject(plain.r))
2471
- this.appendChildList(id, [(typeof plain.l == "number" ? plain.l : this.fromObject(plain.l)), (typeof plain.r == "number" ? plain.r : this.fromObject(plain.r))]);
2472
- }
2473
- else if (typeof plain.val == "string") {
2474
- if (numericTokenTypes.has(typ)) {
2475
- // numeric value as a string
2476
- this.numericValue[id] = +plain.val;
2477
- }
2478
- else {
2479
- this.stringValue[id] = this.internString(plain.val);
2480
- }
2481
- }
2482
- else if (typeof plain.val == "number") {
2483
- this.numericValue[id] = plain.val;
2484
- }
2485
- else if (plain.val != null && typeof plain.val == "object" && !Array.isArray(plain.val)) {
2486
- // fraction value
2487
- this.setFraction(id, this.fromObject(plain.val));
2488
- }
2489
- else if (Array.isArray(plain.val) && typ != EnumToken.MediaRangeQueryTokenType) {
2490
- // declaration like tokens
2491
- for (const child of plain.val) {
2492
- this.appendToken(id, typeof child == "number" ? child : this.fromObject(child));
2493
- }
2494
- }
2495
- if (Array.isArray(plain.chi)) {
2496
- for (const child of plain.chi) {
2497
- this.appendToken(id, typeof child == "number" ? child : this.fromObject(child));
2498
- }
2499
- }
2500
- if (structuredTokenTypes.has(typ)) {
2501
- const fields = [
2502
- ["l", TokenField.L],
2503
- ["r", TokenField.R],
2504
- ["op", TokenField.Op],
2505
- ["op1", TokenField.Op],
2506
- ["op2", TokenField.Op2],
2507
- ["val", TokenField.Val],
2508
- ];
2509
- for (const [key, field] of fields) {
2510
- if (plain[key] == null) {
2511
- continue;
2512
- }
2513
- const values = Array.isArray(plain[key]) ? plain[key] : [plain[key]];
2514
- const fieldTokens = [];
2515
- for (const value of values) {
2516
- fieldTokens.push(typeof value == "number" ? value : this.fromObject(value));
2517
- }
2518
- // inlined: this.setField(id, field, ...fieldTokens)
2519
- this.setTokenList(this.fieldList(id, field), fieldTokens);
2520
- }
2637
+ */
2638
+ resetToken(id, typ, val, unit) {
2639
+ this.dataTypes[id] = typ;
2640
+ this.stringValue[id] = 0;
2641
+ this.numericValue[id] = 0;
2642
+ this.unit[id] = 0;
2643
+ this.colorType[id] = 0;
2644
+ this.name[id] = 0;
2645
+ if (typeof val === "string") {
2646
+ this.stringValue[id] = this.internString(val);
2521
2647
  }
2522
- const loc = plain.loc;
2523
- if (loc != null) {
2524
- this.setLocation(id, loc.srcId, loc.sta, loc.end);
2648
+ else if (typeof val === "number") {
2649
+ this.numericValue[id] = val;
2525
2650
  }
2526
- return id;
2651
+ if (unit != null) {
2652
+ this.unit[id] = this.internString(unit);
2653
+ }
2654
+ // inlined: this.setTokens(id)
2655
+ this.setTokenList(id, []);
2656
+ // inlined: this.setChildren(id)
2657
+ this.setChildList(id, []);
2658
+ }
2659
+ /**
2660
+ * Import an object: an {@link AstArenaNode} or a plain object ({@link PlainAstNode}, {@link PlainToken}).
2661
+ *
2662
+ * - importing a view of this arena is a noop: its node id is returned, the node is not copied,
2663
+ * - a view of another arena is copied with its descendants (see {@link import}),
2664
+ * - a plain object is allocated in the arena. `chi`, `val`, `l`, `r`, ... are created recursively, they can be
2665
+ * node ids, views or plain objects.
2666
+ *
2667
+ * @param node
2668
+ * @returns the node id
2669
+ */
2670
+ fromObject(node) {
2671
+ const id = node[ARENAID];
2672
+ if (id !== undefined) {
2673
+ // no copy for the nodes of this arena
2674
+ return this.import(node[ARENA], id);
2675
+ }
2676
+ return isNodeObjectType(node.typ)
2677
+ ? this.nodeFromObject(node)
2678
+ : this.tokenFromObject(node);
2527
2679
  }
2528
2680
  /**
2529
2681
  * Object view of a node or a token: an {@link AstArenaNode}. The view does not copy the node: its properties read
@@ -2544,6 +2696,7 @@ class AstArena extends Arena {
2544
2696
  toObject(id) {
2545
2697
  return new AstArenaNode(this, id);
2546
2698
  }
2699
+ // ---- plain objects -----------------------------------------------------------------
2547
2700
  /**
2548
2701
  * export a node or a token as a plain object (deep copy). `JSON.stringify()` of an {@link AstArenaNode} returns
2549
2702
  * the plain object
@@ -2561,19 +2714,19 @@ class AstArena extends Arena {
2561
2714
  break;
2562
2715
  case EnumToken.RuleNodeType:
2563
2716
  case EnumToken.KeyframesRuleNodeType:
2564
- node.sel = this.strings[this.name[id]];
2717
+ node.sel = this.resolveString(this.name[id]);
2565
2718
  break;
2566
2719
  case EnumToken.AtRuleNodeType:
2567
2720
  case EnumToken.KeyframesAtRuleNodeType:
2568
- node.nam = this.strings[this.name[id]];
2569
- node.val = this.strings[this.stringValue[id]];
2721
+ node.nam = this.resolveString(this.name[id]);
2722
+ node.val = this.resolveString(this.stringValue[id]);
2570
2723
  break;
2571
2724
  case EnumToken.CommentNodeType:
2572
2725
  case EnumToken.CDOCOMMNodeType:
2573
- node.val = this.strings[this.stringValue[id]];
2726
+ node.val = this.resolveString(this.stringValue[id]);
2574
2727
  break;
2575
2728
  default:
2576
- node.nam = this.strings[this.name[id]];
2729
+ node.nam = this.resolveString(this.name[id]);
2577
2730
  node.val = [];
2578
2731
  for (const token of this.tokens(id)) {
2579
2732
  node.val.push(this.tokenToObject(token));
@@ -2592,7 +2745,7 @@ class AstArena extends Arena {
2592
2745
  if (flags & EnumAstNodeStatus.StateMask) {
2593
2746
  node.state = flags & EnumAstNodeStatus.StateMask;
2594
2747
  }
2595
- if ((flags & EnumAstNodeStatus.HasValues) == 0 && this.firstToken[id] != 0) {
2748
+ if ((flags & EnumAstNodeStatus.HasValues) === 0 && this.firstToken[id] !== 0) {
2596
2749
  node.tokens = [];
2597
2750
  for (const token of this.tokens(id)) {
2598
2751
  node.tokens.push(this.tokenToObject(token));
@@ -2601,11 +2754,11 @@ class AstArena extends Arena {
2601
2754
  if (this.errors.has(id)) {
2602
2755
  node.errors = this.errors.get(id);
2603
2756
  }
2604
- if (this.raw[id] != null) {
2605
- node.raw = this.raw[id];
2757
+ if (this.raw.get(id) != null) {
2758
+ node.raw = this.raw.get(id);
2606
2759
  }
2607
- if (this.optimized[id] != null) {
2608
- node.optimized = this.optimized[id];
2760
+ if (this.optimized.get(id) != null) {
2761
+ node.optimized = this.optimized.get(id);
2609
2762
  }
2610
2763
  return node;
2611
2764
  }
@@ -2616,49 +2769,49 @@ class AstArena extends Arena {
2616
2769
  tokenToObject(id) {
2617
2770
  const typ = this.dataTypes[id];
2618
2771
  const token = { typ };
2619
- if (this.name[id] != 0) {
2620
- token.nam = this.strings[this.name[id]];
2772
+ if (this.name[id] !== 0) {
2773
+ token.nam = this.resolveString(this.name[id]);
2621
2774
  }
2622
- if (typ == EnumToken.BinaryExpressionTokenType) {
2775
+ if (typ === EnumToken.BinaryExpressionTokenType) {
2623
2776
  token.op = this.numericValue[id];
2624
2777
  }
2625
- else if (typ == EnumToken.UnaryExpressionTokenType) {
2778
+ else if (typ === EnumToken.UnaryExpressionTokenType) {
2626
2779
  token.sign = this.numericValue[id];
2627
- token.val = this.firstChild[id] == 0 ? null : this.tokenToObject(this.firstChild[id]);
2780
+ token.val = this.firstChild[id] === 0 ? null : this.tokenToObject(this.firstChild[id]);
2628
2781
  }
2629
- else if (typ == EnumToken.FractionTokenType) {
2782
+ else if (typ === EnumToken.FractionTokenType) {
2630
2783
  token.l = this.tokenToObject(this.firstChild[id]);
2631
2784
  token.r = this.tokenToObject(this.lastChild[id]);
2632
2785
  }
2633
2786
  else if (numericTokenTypes.has(typ)) {
2634
2787
  const fraction = this.getFraction(id);
2635
- token.val = fraction == 0 ? this.numericValue[id] : this.tokenToObject(fraction);
2788
+ token.val = fraction === 0 ? this.numericValue[id] : this.tokenToObject(fraction);
2636
2789
  }
2637
- else if (this.stringValue[id] != 0 || typ == EnumToken.IdenTokenType || typ == EnumToken.StringTokenType) {
2638
- token.val = this.strings[this.stringValue[id]];
2790
+ else if (this.stringValue[id] !== 0 || typ === EnumToken.IdenTokenType || typ === EnumToken.StringTokenType) {
2791
+ token.val = this.resolveString(this.stringValue[id]);
2639
2792
  }
2640
- if (this.unit[id] != 0) {
2641
- if (typ == EnumToken.ColorTokenType) {
2642
- token.cal = this.strings[this.unit[id]];
2793
+ if (this.unit[id] !== 0) {
2794
+ if (typ === EnumToken.ColorTokenType) {
2795
+ token.cal = this.resolveString(this.unit[id]);
2643
2796
  }
2644
- else if (typ == EnumToken.MatchExpressionTokenType) {
2645
- token.attr = this.strings[this.unit[id]];
2797
+ else if (typ === EnumToken.MatchExpressionTokenType) {
2798
+ token.attr = this.resolveString(this.unit[id]);
2646
2799
  }
2647
2800
  else {
2648
- token.unit = this.strings[this.unit[id]];
2801
+ token.unit = this.resolveString(this.unit[id]);
2649
2802
  }
2650
2803
  }
2651
- if (typ == EnumToken.ColorTokenType && this.colorType[id] != ColorType.NONE) {
2804
+ if (typ === EnumToken.ColorTokenType && this.colorType[id] !== ColorType.NONE) {
2652
2805
  token.kin = this.colorType[id];
2653
2806
  }
2654
- if (this.firstToken[id] != 0) {
2807
+ if (this.firstToken[id] !== 0) {
2655
2808
  const tokens = [];
2656
2809
  for (const child of this.tokens(id)) {
2657
2810
  tokens.push(this.tokenToObject(child));
2658
2811
  }
2659
- if (typ == EnumToken.DeclarationNodeType ||
2660
- typ == EnumToken.CssVariableTokenType ||
2661
- typ == EnumToken.CssVariableImportTokenType) {
2812
+ if (typ === EnumToken.DeclarationNodeType ||
2813
+ typ === EnumToken.CssVariableTokenType ||
2814
+ typ === EnumToken.CssVariableImportTokenType) {
2662
2815
  token.val = tokens;
2663
2816
  }
2664
2817
  else {
@@ -2683,12 +2836,32 @@ class AstArena extends Arena {
2683
2836
  }
2684
2837
  return token;
2685
2838
  }
2686
- // ---- clone / import ----------------------------------------------------------------
2687
2839
  /**
2688
- * clone a node or a token. tokens are always cloned deeply
2840
+ * copy the data of a node (type, strings, values, flags, location, errors): its tokens, its children and the
2841
+ * minifier caches (`raw`, `optimized`) are not copied
2842
+ * @param id
2843
+ */
2844
+ cloneNode(id) {
2845
+ const target = this.allocate();
2846
+ this.dataTypes[target] = this.dataTypes[id];
2847
+ this.name[target] = this.name[id];
2848
+ this.stringValue[target] = this.stringValue[id];
2849
+ this.numericValue[target] = this.numericValue[id];
2850
+ this.unit[target] = this.unit[id];
2851
+ this.colorType[target] = this.colorType[id];
2852
+ this.flags[target] = this.flags[id];
2853
+ this.copySpan(target, id);
2854
+ if (this.errors.size > 0 && this.errors.has(id)) {
2855
+ this.errors.set(target, this.errors.get(id));
2856
+ }
2857
+ return target;
2858
+ }
2859
+ /**
2860
+ * copy a node or a token. the tokens of a node are copied, its children are copied with `deep`. the values of a
2861
+ * declaration are copied with `deep`
2689
2862
  * @param id
2690
- * @param deep also clone the children (rules) or the value tokens (declarations)
2691
- * @param cloneMap map of the source ids to their clones
2863
+ * @param deep copy the children and the values of the declarations
2864
+ * @param cloneMap records the copies: id of the copy by original id
2692
2865
  */
2693
2866
  clone(id, deep = false, cloneMap = null) {
2694
2867
  const target = this.allocate();
@@ -2702,27 +2875,27 @@ class AstArena extends Arena {
2702
2875
  this.colorType[target] = this.colorType[id];
2703
2876
  this.flags[target] = flags;
2704
2877
  this.copySpan(target, id);
2705
- if (this.raw[id] != null) {
2706
- this.raw[target] = this.raw[id];
2878
+ if (this.raw.get(id) != null) {
2879
+ this.raw.set(target, this.raw.get(id));
2707
2880
  }
2708
- if (this.optimized[id] != null) {
2709
- this.optimized[target] = this.optimized[id];
2881
+ if (this.optimized.get(id) != null) {
2882
+ this.optimized.set(target, this.optimized.get(id));
2710
2883
  }
2711
2884
  if (this.errors.size > 0 && this.errors.has(id)) {
2712
2885
  this.errors.set(target, this.errors.get(id));
2713
2886
  }
2714
- const isNode = (flags & (EnumAstNodeStatus.HasChildren | EnumAstNodeStatus.HasValues)) != 0;
2887
+ const isNode = (flags & (EnumAstNodeStatus.HasChildren | EnumAstNodeStatus.HasValues)) !== 0;
2715
2888
  // declaration values are cloned only when requested
2716
- if (!isNode || deep || (flags & EnumAstNodeStatus.HasValues) == 0) {
2889
+ if (!isNode || deep || (flags & EnumAstNodeStatus.HasValues) === 0) {
2717
2890
  let token = this.firstToken[id];
2718
- while (token != 0) {
2891
+ while (token !== 0) {
2719
2892
  this.appendToken(target, this.clone(token, true, cloneMap));
2720
2893
  token = this.nextSibling[token];
2721
2894
  }
2722
2895
  }
2723
2896
  if (!isNode || deep) {
2724
2897
  let child = this.firstChild[id];
2725
- while (child != 0) {
2898
+ while (child !== 0) {
2726
2899
  this.appendChild(target, this.clone(child, true, cloneMap));
2727
2900
  child = this.nextSibling[child];
2728
2901
  }
@@ -2742,17 +2915,17 @@ class AstArena extends Arena {
2742
2915
  const target = this.allocate();
2743
2916
  const flags = source.flags[id];
2744
2917
  this.dataTypes[target] = source.dataTypes[id];
2745
- this.name[target] = this.internString(source.strings[source.name[id]] ?? "");
2746
- this.stringValue[target] = this.internString(source.strings[source.stringValue[id]] ?? "");
2918
+ this.name[target] = this.internString(source.resolveString(source.name[id]) ?? "");
2919
+ this.stringValue[target] = this.internString(source.resolveString(source.stringValue[id]) ?? "");
2747
2920
  this.numericValue[target] = source.numericValue[id];
2748
- this.unit[target] = this.internString(source.strings[source.unit[id]] ?? "");
2921
+ this.unit[target] = this.internString(source.resolveString(source.unit[id]) ?? "");
2749
2922
  this.colorType[target] = source.colorType[id];
2750
2923
  this.flags[target] = flags & ~EnumAstNodeStatus.HasLocation;
2751
- if (source.raw[id] != null) {
2752
- this.raw[target] = source.raw[id];
2924
+ if (source.raw.get(id) != null) {
2925
+ this.raw.set(target, source.raw.get(id));
2753
2926
  }
2754
- if (source.optimized[id] != null) {
2755
- this.optimized[target] = source.optimized[id];
2927
+ if (source.optimized.get(id) != null) {
2928
+ this.optimized.set(target, source.optimized.get(id));
2756
2929
  }
2757
2930
  if (flags & EnumAstNodeStatus.HasLocation) {
2758
2931
  // inlined: source.getSource(id)
@@ -2762,20 +2935,19 @@ class AstArena extends Arena {
2762
2935
  this.errors.set(target, source.errors.get(id));
2763
2936
  }
2764
2937
  let token = source.firstToken[id];
2765
- while (token != 0) {
2938
+ while (token !== 0) {
2766
2939
  this.appendToken(target, this.import(source, token));
2767
2940
  token = source.nextSibling[token];
2768
2941
  }
2769
- if (deep || (flags & EnumAstNodeStatus.HasChildren) == 0) {
2942
+ if (deep || (flags & EnumAstNodeStatus.HasChildren) === 0) {
2770
2943
  let child = source.firstChild[id];
2771
- while (child != 0) {
2944
+ while (child !== 0) {
2772
2945
  this.appendChild(target, this.import(source, child));
2773
2946
  child = source.nextSibling[child];
2774
2947
  }
2775
2948
  }
2776
2949
  return target;
2777
2950
  }
2778
- // ---- tree helpers --------------------------------------------------------------
2779
2951
  /**
2780
2952
  * index of a child in its parent's children (or tokens) list
2781
2953
  * @param id
@@ -2783,10 +2955,10 @@ class AstArena extends Arena {
2783
2955
  indexOf(id) {
2784
2956
  let index = 0;
2785
2957
  let child = this.previousSibling[id];
2786
- if (this.parents[id] == 0) {
2958
+ if (this.parents[id] === 0) {
2787
2959
  return -1;
2788
2960
  }
2789
- while (child != 0) {
2961
+ while (child !== 0) {
2790
2962
  index++;
2791
2963
  child = this.previousSibling[child];
2792
2964
  }
@@ -2799,12 +2971,13 @@ class AstArena extends Arena {
2799
2971
  */
2800
2972
  insertBefore(reference, ...nodes) {
2801
2973
  for (const node of nodes) {
2802
- if (node == reference || node == 0) {
2974
+ if (node === reference || node === 0) {
2803
2975
  continue;
2804
2976
  }
2805
2977
  this.setPreviousSibling(reference, node);
2806
2978
  }
2807
2979
  }
2980
+ // ---- clone / import ----------------------------------------------------------------
2808
2981
  /**
2809
2982
  * insert nodes after a child
2810
2983
  * @param reference
@@ -2812,7 +2985,7 @@ class AstArena extends Arena {
2812
2985
  */
2813
2986
  insertAfter(reference, ...nodes) {
2814
2987
  for (const node of nodes) {
2815
- if (node == reference || node == 0) {
2988
+ if (node === reference || node === 0) {
2816
2989
  continue;
2817
2990
  }
2818
2991
  this.setNextSibling(reference, node);
@@ -2826,24 +2999,25 @@ class AstArena extends Arena {
2826
2999
  */
2827
3000
  prependChildren(id, ...nodes) {
2828
3001
  const first = this.firstChild[id];
2829
- if (first == 0) {
3002
+ if (first === 0) {
2830
3003
  this.appendChildList(id, nodes);
2831
3004
  }
2832
3005
  else {
2833
3006
  this.insertBefore(first, ...nodes);
2834
3007
  }
2835
3008
  }
3009
+ // ---- tree helpers --------------------------------------------------------------
2836
3010
  /**
2837
3011
  * replace a child with other nodes. the node itself may be part of the replacement
2838
3012
  * @param id
2839
3013
  * @param nodes
2840
3014
  */
2841
3015
  replaceChild(id, ...nodes) {
2842
- if (this.parents[id] == 0) {
3016
+ if (this.parents[id] === 0) {
2843
3017
  return;
2844
3018
  }
2845
3019
  const index = nodes.indexOf(id);
2846
- if (index == -1) {
3020
+ if (index === -1) {
2847
3021
  this.insertBefore(id, ...nodes);
2848
3022
  this.detach(id);
2849
3023
  return;
@@ -2856,14 +3030,14 @@ class AstArena extends Arena {
2856
3030
  * @param id
2857
3031
  */
2858
3032
  hasChildren(id) {
2859
- return (this.flags[id] & EnumAstNodeStatus.HasChildren) != 0;
3033
+ return (this.flags[id] & EnumAstNodeStatus.HasChildren) !== 0;
2860
3034
  }
2861
3035
  /**
2862
3036
  * rule selector
2863
3037
  * @param id
2864
3038
  */
2865
3039
  getSelector(id) {
2866
- return this.strings[this.name[id]];
3040
+ return this.resolveString(this.name[id]);
2867
3041
  }
2868
3042
  /**
2869
3043
  * set the rule selector
@@ -2889,7 +3063,6 @@ class AstArena extends Arena {
2889
3063
  setValueString(id, val) {
2890
3064
  this.stringValue[id] = this.internString(val);
2891
3065
  }
2892
- // ---- nesting rules expansion ---------------------------------------------------------------
2893
3066
  /**
2894
3067
  * Expand the css nesting rules of a stylesheet, a rule or an at-rule. The tree is expanded in place: the nested rules
2895
3068
  * are moved into the parent of their rule and their selectors are resolved
@@ -2909,17 +3082,21 @@ class AstArena extends Arena {
2909
3082
  return ast;
2910
3083
  }
2911
3084
  let node = this.firstChild[ast];
2912
- while (node != 0) {
3085
+ while (node !== 0) {
2913
3086
  const next = this.nextSibling[node];
2914
3087
  const typ = this.dataTypes[node];
2915
3088
  if (typ === EnumToken.RuleNodeType) {
2916
3089
  // the expanded rules are inserted after the rule
2917
3090
  this.expandRule(node, ast, next);
2918
3091
  }
2919
- else if (typ == EnumToken.AtRuleNodeType && this.flags[node] & EnumAstNodeStatus.HasChildren) {
3092
+ else if (typ === EnumToken.AtRuleNodeType &&
3093
+ this.flags[node] & EnumAstNodeStatus.HasChildren &&
3094
+ // the mixin body is nested in the rule the mixin is applied to
3095
+ !this.stringEquals(this.name[node], "mixin")) {
2920
3096
  let j = this.lastChild[node];
2921
- while (j != 0) {
2922
- if (this.dataTypes[j] == EnumToken.RuleNodeType || this.dataTypes[j] == EnumToken.AtRuleNodeType) {
3097
+ while (j !== 0) {
3098
+ if (this.dataTypes[j] === EnumToken.RuleNodeType ||
3099
+ this.dataTypes[j] === EnumToken.AtRuleNodeType) {
2923
3100
  this.expand(node);
2924
3101
  break;
2925
3102
  }
@@ -2930,6 +3107,218 @@ class AstArena extends Arena {
2930
3107
  }
2931
3108
  return ast;
2932
3109
  }
3110
+ /**
3111
+ * replace the nesting selector '&' of a selector
3112
+ * @internal
3113
+ * @param input
3114
+ * @param replace
3115
+ */
3116
+ replaceCompound(input, replace) {
3117
+ const tokens = parseString(this, input);
3118
+ // inlined: this.walkValues(tokens)
3119
+ for (const t of new ValueWalker(this, tokens)) {
3120
+ if (this.dataTypes[t] === EnumToken.NestingSelectorTokenType) {
3121
+ if (tokens.length === 2) {
3122
+ this.resetToken(t, EnumToken.LiteralTokenType, this.replaceCompoundLiteral(this.resolveString(this.stringValue[t]), replace));
3123
+ continue;
3124
+ }
3125
+ const rule = splitRule(replace);
3126
+ this.resetToken(t, EnumToken.LiteralTokenType, rule.length > 1 ? ":is(" + replace + ")" : replace);
3127
+ }
3128
+ }
3129
+ return tokens.reduce((acc, curr) => acc + renderValue(this, curr), "");
3130
+ }
3131
+ /**
3132
+ * @inheritDoc
3133
+ */
3134
+ exportProperties() {
3135
+ return {
3136
+ ...super.exportProperties(),
3137
+ kind: "AstArena",
3138
+ sources: this.sources,
3139
+ raw: [...this.raw],
3140
+ optimized: [...this.optimized],
3141
+ inputLengths: this.inputLengths,
3142
+ };
3143
+ }
3144
+ /**
3145
+ * @inheritDoc
3146
+ */
3147
+ importProperties(properties, transferables) {
3148
+ super.importProperties(properties, transferables);
3149
+ this.sources = properties.sources?.slice() ?? [];
3150
+ this.raw = new Map(properties.raw ?? []);
3151
+ this.optimized = new Map(properties.optimized ?? []);
3152
+ this.inputLengths = properties.inputLengths?.slice() ?? [];
3153
+ }
3154
+ // ---- nesting rules expansion ---------------------------------------------------------------
3155
+ /**
3156
+ * create an ast node from a plain object
3157
+ * @param node
3158
+ */
3159
+ nodeFromObject(node) {
3160
+ const id = this.allocate();
3161
+ const typ = node.typ;
3162
+ this.dataTypes[id] = typ;
3163
+ switch (typ) {
3164
+ case EnumToken.StyleSheetNodeType:
3165
+ this.flags[id] = EnumAstNodeStatus.HasChildren;
3166
+ break;
3167
+ case EnumToken.RuleNodeType:
3168
+ case EnumToken.KeyframesRuleNodeType:
3169
+ this.name[id] = this.internString((node.sel ?? node.nam ?? ""));
3170
+ this.flags[id] = EnumAstNodeStatus.HasChildren;
3171
+ if (node.raw != null) {
3172
+ this.raw.set(id, node.raw);
3173
+ }
3174
+ if (node.optimized != null) {
3175
+ this.optimized.set(id, node.optimized);
3176
+ }
3177
+ if (typeof node.val === "string") {
3178
+ this.stringValue[id] = this.internString(node.val);
3179
+ }
3180
+ break;
3181
+ case EnumToken.AtRuleNodeType:
3182
+ case EnumToken.KeyframesAtRuleNodeType:
3183
+ this.name[id] = this.internString(node.nam ?? "");
3184
+ this.stringValue[id] = this.internString(node.val ?? "");
3185
+ this.flags[id] = node.chi != null ? EnumAstNodeStatus.HasChildren : 0;
3186
+ break;
3187
+ case EnumToken.CommentNodeType:
3188
+ case EnumToken.CDOCOMMNodeType:
3189
+ this.stringValue[id] = this.internString(node.val ?? "");
3190
+ this.flags[id] = 0;
3191
+ break;
3192
+ default:
3193
+ // declaration like nodes
3194
+ this.name[id] = this.internString(node.nam ?? "");
3195
+ this.flags[id] = EnumAstNodeStatus.HasValues;
3196
+ if (Array.isArray(node.val)) {
3197
+ for (const token of node.val) {
3198
+ this.appendToken(id, typeof token === "number" ? token : this.fromObject(token));
3199
+ }
3200
+ }
3201
+ break;
3202
+ }
3203
+ if (Array.isArray(node.tokens)) {
3204
+ for (const token of node.tokens) {
3205
+ this.appendToken(id, typeof token === "number" ? token : this.fromObject(token));
3206
+ }
3207
+ }
3208
+ // inlined: this.setState(id, node.state)
3209
+ this.flags[id] =
3210
+ (this.flags[id] & ~EnumAstNodeStatus.StateMask) | ((node.state ?? 0) & EnumAstNodeStatus.StateMask);
3211
+ const loc = node.loc;
3212
+ if (loc != null) {
3213
+ this.setLocation(id, loc.srcId, loc.sta, loc.end);
3214
+ }
3215
+ const errors = node.errors;
3216
+ if (errors != null && errors.length > 0) {
3217
+ this.errors.set(id, errors);
3218
+ }
3219
+ if (Array.isArray(node.chi) && this.flags[id] & EnumAstNodeStatus.HasChildren) {
3220
+ for (const child of node.chi) {
3221
+ // inlined: this.appendChildren(id, typeof child == "number" ? child : this.fromObject(child))
3222
+ this.appendChildList(id, [typeof child === "number" ? child : this.fromObject(child)]);
3223
+ }
3224
+ }
3225
+ return id;
3226
+ }
3227
+ /**
3228
+ * create a token from a plain object
3229
+ * @param token
3230
+ */
3231
+ tokenFromObject(token) {
3232
+ const id = this.allocate();
3233
+ const typ = token.typ;
3234
+ const plain = token;
3235
+ this.dataTypes[id] = typ;
3236
+ if (typeof plain.nam === "string") {
3237
+ this.name[id] = this.internString(plain.nam);
3238
+ }
3239
+ if (typeof plain.unit === "string") {
3240
+ this.unit[id] = this.internString(plain.unit);
3241
+ }
3242
+ if (typeof plain.kin === "number") {
3243
+ // inlined: this.setColorType(id, plain.kin)
3244
+ this.colorType[id] = plain.kin;
3245
+ }
3246
+ if (typeof plain.cal === "string") {
3247
+ this.unit[id] = this.internString(plain.cal);
3248
+ }
3249
+ if (typeof plain.attr === "string") {
3250
+ this.unit[id] = this.internString(plain.attr);
3251
+ }
3252
+ if (typ === EnumToken.BinaryExpressionTokenType) {
3253
+ this.numericValue[id] = plain.op;
3254
+ }
3255
+ else if (typ === EnumToken.UnaryExpressionTokenType) {
3256
+ this.numericValue[id] = plain.sign;
3257
+ // inlined: this.appendChildren(id, typeof plain.val == "number" ? plain.val : this.fromObject(plain.val))
3258
+ this.appendChildList(id, [typeof plain.val === "number" ? plain.val : this.fromObject(plain.val)]);
3259
+ }
3260
+ else if (typ === EnumToken.FractionTokenType) {
3261
+ // inlined: this.appendChildren(id, typeof plain.l == "number" ? plain.l : this.fromObject(plain.l), typeof plain.r == "number" ? plain.r : this.fromObject(plain.r))
3262
+ this.appendChildList(id, [
3263
+ typeof plain.l === "number" ? plain.l : this.fromObject(plain.l),
3264
+ typeof plain.r === "number" ? plain.r : this.fromObject(plain.r),
3265
+ ]);
3266
+ }
3267
+ else if (typeof plain.val === "string") {
3268
+ if (numericTokenTypes.has(typ)) {
3269
+ // numeric value as a string
3270
+ this.numericValue[id] = +plain.val;
3271
+ }
3272
+ else {
3273
+ this.stringValue[id] = this.internString(plain.val);
3274
+ }
3275
+ }
3276
+ else if (typeof plain.val === "number") {
3277
+ this.numericValue[id] = plain.val;
3278
+ }
3279
+ else if (plain.val != null && typeof plain.val === "object" && !Array.isArray(plain.val)) {
3280
+ // fraction value
3281
+ this.setFraction(id, this.fromObject(plain.val));
3282
+ }
3283
+ else if (Array.isArray(plain.val) && typ !== EnumToken.MediaRangeQueryTokenType) {
3284
+ // declaration like tokens
3285
+ for (const child of plain.val) {
3286
+ this.appendToken(id, typeof child === "number" ? child : this.fromObject(child));
3287
+ }
3288
+ }
3289
+ if (Array.isArray(plain.chi)) {
3290
+ for (const child of plain.chi) {
3291
+ this.appendToken(id, typeof child === "number" ? child : this.fromObject(child));
3292
+ }
3293
+ }
3294
+ if (structuredTokenTypes.has(typ)) {
3295
+ const fields = [
3296
+ ["l", TokenField.L],
3297
+ ["r", TokenField.R],
3298
+ ["op", TokenField.Op],
3299
+ ["op1", TokenField.Op],
3300
+ ["op2", TokenField.Op2],
3301
+ ["val", TokenField.Val],
3302
+ ];
3303
+ for (const [key, field] of fields) {
3304
+ if (plain[key] == null) {
3305
+ continue;
3306
+ }
3307
+ const values = Array.isArray(plain[key]) ? plain[key] : [plain[key]];
3308
+ const fieldTokens = [];
3309
+ for (const value of values) {
3310
+ fieldTokens.push(typeof value === "number" ? value : this.fromObject(value));
3311
+ }
3312
+ // inlined: this.setField(id, field, ...fieldTokens)
3313
+ this.setTokenList(this.fieldList(id, field), fieldTokens);
3314
+ }
3315
+ }
3316
+ const loc = plain.loc;
3317
+ if (loc != null) {
3318
+ this.setLocation(id, loc.srcId, loc.sta, loc.end);
3319
+ }
3320
+ return id;
3321
+ }
2933
3322
  /**
2934
3323
  * expand a rule: the nested rules and at-rules are moved out of the rule, into `parent` before `before`.
2935
3324
  * the rule is detached when it has no children left
@@ -2942,30 +3331,30 @@ class AstArena extends Arena {
2942
3331
  return;
2943
3332
  }
2944
3333
  const ast = node;
2945
- const sel = this.strings[this.name[ast]];
3334
+ const sel = this.resolveString(this.name[ast]);
2946
3335
  const place = (child) => {
2947
- if (before == 0) {
3336
+ if (before === 0) {
2948
3337
  this.appendChild(parent, child);
2949
3338
  }
2950
3339
  else {
2951
3340
  this.insertBefore(before, child);
2952
3341
  }
2953
3342
  };
2954
- if (this.dataTypes[ast] == EnumToken.RuleNodeType) {
3343
+ if (this.dataTypes[ast] === EnumToken.RuleNodeType) {
2955
3344
  let child = this.firstChild[ast];
2956
- while (child != 0) {
3345
+ while (child !== 0) {
2957
3346
  const next = this.nextSibling[child];
2958
3347
  const typ = this.dataTypes[child];
2959
- if (typ == EnumToken.RuleNodeType) {
3348
+ if (typ === EnumToken.RuleNodeType) {
2960
3349
  const rule = child;
2961
- const ruleSel = this.strings[this.name[rule]];
3350
+ const ruleSel = this.resolveString(this.name[rule]);
2962
3351
  if (!ruleSel.includes("&")) {
2963
3352
  const selRule = splitRule(ruleSel);
2964
3353
  const arSelf = splitRule(sel)
2965
- .filter((r) => r.every((t) => t != ":before" && t != ":after" && !t.startsWith("::")))
3354
+ .filter((r) => r.every((t) => t !== ":before" && t !== ":after" && !t.startsWith("::")))
2966
3355
  .reduce((acc, curr) => acc.concat([curr.join("")]), [])
2967
3356
  .join(",");
2968
- if (arSelf.length == 0) {
3357
+ if (arSelf.length === 0) {
2969
3358
  this.detach(rule);
2970
3359
  child = next;
2971
3360
  continue;
@@ -2988,23 +3377,23 @@ class AstArena extends Arena {
2988
3377
  let withoutCompound = [];
2989
3378
  // pseudo elements cannot be used with '&'
2990
3379
  // https://www.w3.org/TR/css-nesting-1/#example-7145ff1e
2991
- const rules = splitRule(sel).filter((r) => r.every((t) => t != ":before" && t != ":after" && !t.startsWith("::")));
3380
+ const rules = splitRule(sel).filter((r) => r.every((t) => t !== ":before" && t !== ":after" && !t.startsWith("::")));
2992
3381
  const parentSelector = !sel.includes("&");
2993
- if (rules.length == 0) {
3382
+ if (rules.length === 0) {
2994
3383
  this.detach(rule);
2995
3384
  child = next;
2996
3385
  continue;
2997
3386
  }
2998
- for (const sel of this.raw[rule] ?? splitRule(ruleSel)) {
3387
+ for (const sel of this.raw.get(rule) ?? splitRule(ruleSel)) {
2999
3388
  const s = sel.join("");
3000
3389
  if (s.includes("&") || parentSelector) {
3001
- if (s.indexOf("&", 1) == -1) {
3002
- if (s.at(0) == "&") {
3003
- if (s.at(1) == " ") {
3390
+ if (s.indexOf("&", 1) === -1) {
3391
+ if (s.at(0) === "&") {
3392
+ if (s.at(1) === " ") {
3004
3393
  childSelectorCompound.push(s.slice(2));
3005
3394
  }
3006
3395
  else {
3007
- if (s == "&" || parentSelector) {
3396
+ if (s === "&" || parentSelector) {
3008
3397
  withCompound.push(s);
3009
3398
  }
3010
3399
  }
@@ -3019,9 +3408,11 @@ class AstArena extends Arena {
3019
3408
  }
3020
3409
  }
3021
3410
  const selectors = [];
3022
- const selector = rules.length > 1 ? ":is(" + rules.map((a) => a.join("")).join(",") + ")" : rules[0].join("");
3411
+ const selector = rules.length > 1
3412
+ ? ":is(" + rules.map((a) => a.join("")).join(",") + ")"
3413
+ : rules[0].join("");
3023
3414
  if (childSelectorCompound.length > 0) {
3024
- if (childSelectorCompound.length == 1) {
3415
+ if (childSelectorCompound.length === 1) {
3025
3416
  selectors.push(this.replaceCompound("& " + childSelectorCompound[0].trim(), selector));
3026
3417
  }
3027
3418
  else {
@@ -3031,20 +3422,20 @@ class AstArena extends Arena {
3031
3422
  }
3032
3423
  }
3033
3424
  if (withCompound.length > 0) {
3034
- if (withCompound.every((t) => t[0] == "&" && t.indexOf("&", 1) == -1)) {
3425
+ if (withCompound.every((t) => t[0] === "&" && t.indexOf("&", 1) === -1)) {
3035
3426
  withoutCompound.push(...withCompound.map((t) => t.slice(1)));
3036
3427
  withCompound.length = 0;
3037
3428
  }
3038
3429
  }
3039
3430
  if (withoutCompound.length > 0) {
3040
- if (withoutCompound.length == 1) {
3041
- const useIs = rules.length == 1 &&
3431
+ if (withoutCompound.length === 1) {
3432
+ const useIs = rules.length === 1 &&
3042
3433
  selector.match(/^[a-zA-Z.:]/) != null &&
3043
3434
  selector.includes(" ") &&
3044
- withoutCompound.length == 1 &&
3435
+ withoutCompound.length === 1 &&
3045
3436
  withoutCompound[0].match(/^[a-zA-Z]+$/) != null;
3046
3437
  const compound = useIs ? ":is(&)" : "&";
3047
- selectors.push(this.replaceCompound(rules.length == 1
3438
+ selectors.push(this.replaceCompound(rules.length === 1
3048
3439
  ? useIs
3049
3440
  ? withoutCompound[0] + ":is(&)"
3050
3441
  : selector.match(/^[.:]/) && withoutCompound[0].match(/^[a-zA-Z]+$/)
@@ -3064,29 +3455,32 @@ class AstArena extends Arena {
3064
3455
  }
3065
3456
  }
3066
3457
  if (withCompound.length > 0) {
3067
- if (withCompound.length == 1) {
3458
+ if (withCompound.length === 1) {
3068
3459
  selectors.push(this.replaceCompound(withCompound[0], selector));
3069
3460
  }
3070
3461
  }
3071
3462
  // inlined: this.setSelector(rule, selectors.reduce((acc, curr) => (curr.length == 0 ? acc : acc + (acc.length > 0 ? "," : "") + curr), ""))
3072
- this.name[rule] = this.internString(selectors.reduce((acc, curr) => (curr.length == 0 ? acc : acc + (acc.length > 0 ? "," : "") + curr), ""));
3463
+ this.name[rule] = this.internString(selectors.reduce((acc, curr) => (curr.length === 0 ? acc : acc + (acc.length > 0 ? "," : "") + curr), ""));
3073
3464
  }
3074
3465
  place(rule);
3075
3466
  this.expandRule(rule, parent, before);
3076
3467
  }
3077
- else if (typ == EnumToken.AtRuleNodeType) {
3468
+ else if (typ === EnumToken.AtRuleNodeType &&
3469
+ (!(this.flags[child] & EnumAstNodeStatus.HasChildren) ||
3470
+ this.stringEquals(this.name[child], "apply"))) ;
3471
+ else if (typ === EnumToken.AtRuleNodeType) {
3078
3472
  const astAtRule = child;
3079
- const atRuleName = this.strings[this.name[astAtRule]];
3473
+ const atRuleName = this.resolveString(this.name[astAtRule]);
3080
3474
  if (atRuleName === "scope") {
3081
- const atRuleValue = this.strings[this.stringValue[astAtRule]];
3475
+ const atRuleValue = this.resolveString(this.stringValue[astAtRule]);
3082
3476
  if (atRuleValue.includes("&")) {
3083
3477
  // inlined: this.setValueString(astAtRule, this.replaceCompound(atRuleValue, sel))
3084
3478
  this.stringValue[astAtRule] = this.internString(this.replaceCompound(atRuleValue, sel));
3085
3479
  }
3086
3480
  let hasNesting = false;
3087
- for (let t = this.firstChild[astAtRule]; t != 0; t = this.nextSibling[t]) {
3088
- if (this.dataTypes[t] == EnumToken.RuleNodeType &&
3089
- this.strings[this.name[t]].includes("&")) {
3481
+ for (let t = this.firstChild[astAtRule]; t !== 0; t = this.nextSibling[t]) {
3482
+ if (this.dataTypes[t] === EnumToken.RuleNodeType &&
3483
+ this.resolveString(this.name[t]).includes("&")) {
3090
3484
  hasNesting = true;
3091
3485
  break;
3092
3486
  }
@@ -3106,9 +3500,9 @@ class AstArena extends Arena {
3106
3500
  // the rules are expanded inside the at-rule: [clone, ...expanded nodes]
3107
3501
  this.moveChildren(astAtRule, clone);
3108
3502
  this.expandRule(clone, astAtRule, 0);
3109
- if (this.firstChild[clone] != 0) {
3503
+ if (this.firstChild[clone] !== 0) {
3110
3504
  // the rule declarations are wrapped in the clone, before the expanded nodes
3111
- if (this.firstChild[astAtRule] == 0) {
3505
+ if (this.firstChild[astAtRule] === 0) {
3112
3506
  this.appendChild(astAtRule, clone);
3113
3507
  }
3114
3508
  else {
@@ -3118,21 +3512,21 @@ class AstArena extends Arena {
3118
3512
  place(astAtRule);
3119
3513
  // the expanded at-rules are moved after the at-rule
3120
3514
  let r = this.firstChild[astAtRule];
3121
- while (r != 0) {
3515
+ while (r !== 0) {
3122
3516
  const nextRule = this.nextSibling[r];
3123
- if (this.dataTypes[r] == EnumToken.AtRuleNodeType) {
3517
+ if (this.dataTypes[r] === EnumToken.AtRuleNodeType) {
3124
3518
  if (this.flags[r] & EnumAstNodeStatus.HasChildren) {
3125
- const val = this.strings[this.stringValue[astAtRule]];
3126
- const rVal = this.strings[this.stringValue[r]];
3519
+ const val = this.resolveString(this.stringValue[astAtRule]);
3520
+ const rVal = this.resolveString(this.stringValue[r]);
3127
3521
  if (val !== "" && rVal !== "") {
3128
- const rName = this.strings[this.name[r]];
3522
+ const rName = this.resolveString(this.name[r]);
3129
3523
  if (atRuleName === "media" && rName === "media") {
3130
3524
  // inlined: this.setValueString(r, val + " and " + rVal)
3131
- this.stringValue[r] = this.internString((val + " and " + rVal));
3525
+ this.stringValue[r] = this.internString(val + " and " + rVal);
3132
3526
  }
3133
- else if (atRuleName == "layer" && rName == "layer") {
3527
+ else if (atRuleName === "layer" && rName === "layer") {
3134
3528
  // inlined: this.setValueString(r, val + "." + rVal)
3135
- this.stringValue[r] = this.internString((val + "." + rVal));
3529
+ this.stringValue[r] = this.internString(val + "." + rVal);
3136
3530
  }
3137
3531
  }
3138
3532
  place(r);
@@ -3144,54 +3538,39 @@ class AstArena extends Arena {
3144
3538
  r = nextRule;
3145
3539
  }
3146
3540
  }
3147
- if (this.firstChild[astAtRule] == 0) {
3541
+ if (this.firstChild[astAtRule] === 0) {
3148
3542
  this.detach(astAtRule);
3149
3543
  }
3150
3544
  }
3151
3545
  child = next;
3152
3546
  }
3153
3547
  }
3154
- if (this.firstChild[ast] == 0) {
3548
+ if (this.firstChild[ast] === 0) {
3155
3549
  this.detach(ast);
3156
3550
  }
3157
3551
  }
3158
3552
  /**
3159
- * replace the nesting selector '&' of a selector
3160
- * @internal
3161
- * @param input
3553
+ *
3554
+ * @param selector
3162
3555
  * @param replace
3556
+ * @protected
3163
3557
  */
3164
- replaceCompound(input, replace) {
3165
- const tokens = parseString(this, input);
3166
- // inlined: this.walkValues(tokens)
3167
- for (const t of new ValueWalker(this, tokens)) {
3168
- if (this.dataTypes[t] == EnumToken.NestingSelectorTokenType) {
3169
- if (tokens.length == 2) {
3170
- this.resetToken(t, EnumToken.LiteralTokenType, this.replaceCompoundLiteral(this.strings[this.stringValue[t]], replace));
3171
- continue;
3172
- }
3173
- const rule = splitRule(replace);
3174
- this.resetToken(t, EnumToken.LiteralTokenType, rule.length > 1 ? ":is(" + replace + ")" : replace);
3175
- }
3176
- }
3177
- return tokens.reduce((acc, curr) => acc + renderValue(this, curr), "");
3178
- }
3179
3558
  replaceCompoundLiteral(selector, replace) {
3180
3559
  const tokens = [""];
3181
3560
  let i = 0;
3182
3561
  for (; i < selector.length; i++) {
3183
- if (selector.charAt(i) == "&") {
3562
+ if (selector.charAt(i) === "&") {
3184
3563
  tokens.push("&", "");
3185
3564
  }
3186
3565
  }
3187
3566
  return tokens
3188
3567
  .sort((a, b) => {
3189
- if (a == "&") {
3568
+ if (a === "&") {
3190
3569
  return 1;
3191
3570
  }
3192
- return b == "&" ? -1 : 0;
3571
+ return b === "&" ? -1 : 0;
3193
3572
  })
3194
- .reduce((acc, curr) => acc + (curr == "&" ? replace : curr), "");
3573
+ .reduce((acc, curr) => acc + (curr === "&" ? replace : curr), "");
3195
3574
  }
3196
3575
  }
3197
3576