@tbela99/css-parser 2.1.0 → 2.3.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 (130) hide show
  1. package/CHANGELOG.md +66 -1
  2. package/README.md +14 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/lib/ast → ast}/arena-node.js +10 -10
  5. package/dist/{src/lib/ast → ast}/arena.js +304 -139
  6. package/dist/{src/lib/ast → ast}/cache.js +40 -24
  7. package/dist/{src/lib/ast → ast}/features/calc.js +11 -9
  8. package/dist/{src/lib/ast → ast}/features/if.js +6 -6
  9. package/dist/ast/features/index.d.ts +7 -0
  10. package/dist/{src/lib/ast → ast}/features/index.js +3 -2
  11. package/dist/{src/lib/ast → ast}/features/inlinecssvariables.js +12 -10
  12. package/dist/ast/features/mixin.js +953 -0
  13. package/dist/{src/lib/ast → ast}/features/prefix.js +39 -39
  14. package/dist/ast/features/shorthand.js +129 -0
  15. package/dist/ast/features/transform.js +188 -0
  16. package/dist/{src/lib/ast → ast}/features/type.js +2 -2
  17. package/dist/{src/lib/ast → ast}/math/expression.js +24 -24
  18. package/dist/{src/lib/ast → ast}/math/math.js +2 -2
  19. package/dist/{src/lib/ast → ast}/minify.js +745 -201
  20. package/dist/ast/mixins/custom-function.js +393 -0
  21. package/dist/ast/mixins/utils.js +53 -0
  22. package/dist/ast/mixins/variables.js +678 -0
  23. package/dist/ast/strings.js +910 -0
  24. package/dist/ast/transform/compute.js +560 -0
  25. package/dist/ast/transform/matrix.js +67 -0
  26. package/dist/ast/transform/minify.js +325 -0
  27. package/dist/ast/transform/perspective.js +24 -0
  28. package/dist/{src/lib/ast → ast}/transform/rotate.js +16 -7
  29. package/dist/ast/transform/scale.js +43 -0
  30. package/dist/ast/transform/skew.js +38 -0
  31. package/dist/ast/transform/translate.js +56 -0
  32. package/dist/ast/transform/utils.js +390 -0
  33. package/dist/{src/lib/ast → ast}/types.js +2 -2
  34. package/dist/{src/lib/ast → ast}/walk.js +2 -2
  35. package/dist/{src/data → data}/properties.json.js +86 -2
  36. package/dist/{src/data → data}/validation.json.js +30 -21
  37. package/dist/{src/lib/fs → fs}/resolve.js +2 -2
  38. package/dist/index-umd-web.js +19727 -10719
  39. package/dist/index.d.ts +738 -100
  40. package/dist/{src/node.js → node.js} +70 -52
  41. package/dist/{src/lib/parser → parser}/declaration/list.js +25 -8
  42. package/dist/{src/lib/parser → parser}/declaration/map.js +15 -15
  43. package/dist/{src/lib/parser → parser}/declaration/set.js +5 -5
  44. package/dist/{src/lib/parser → parser}/input.js +18 -3
  45. package/dist/{src/lib/parser → parser}/linesmap.js +2 -2
  46. package/dist/{src/lib/parser → parser}/parse.js +444 -278
  47. package/dist/{src/lib/parser → parser}/source.js +3 -3
  48. package/dist/{src/lib/parser → parser}/tokenize.js +480 -312
  49. package/dist/{src/lib/parser → parser}/utils/angle.js +3 -3
  50. package/dist/{src/lib/parser → parser}/utils/at-rule-container.js +12 -12
  51. package/dist/{src/lib/parser → parser}/utils/at-rule-font-feature-values.js +3 -3
  52. package/dist/{src/lib/parser → parser}/utils/at-rule-generic.js +7 -7
  53. package/dist/{src/lib/parser → parser}/utils/at-rule-import.js +10 -10
  54. package/dist/{src/lib/parser → parser}/utils/at-rule-media.js +14 -14
  55. package/dist/{src/lib/parser → parser}/utils/at-rule-support.js +9 -14
  56. package/dist/{src/lib/parser → parser}/utils/at-rule-when-else.js +6 -6
  57. package/dist/{src/lib/parser → parser}/utils/at-rule.js +3 -3
  58. package/dist/{src/lib/parser → parser}/utils/base64.js +2 -2
  59. package/dist/parser/utils/cache.js +53 -0
  60. package/dist/parser/utils/canonical.js +78 -0
  61. package/dist/parser/utils/codes.js +93 -0
  62. package/dist/{src/lib/parser → parser}/utils/config.js +3 -3
  63. package/dist/{src/lib/parser → parser}/utils/declaration.js +31 -26
  64. package/dist/{src/lib/parser → parser}/utils/eq.js +2 -2
  65. package/dist/{src/lib/parser → parser}/utils/hash.js +2 -2
  66. package/dist/{src/lib/parser → parser}/utils/selector.js +339 -87
  67. package/dist/{src/lib/parser → parser}/utils/text.js +2 -2
  68. package/dist/{src/lib/parser → parser}/utils/token.js +2 -2
  69. package/dist/{src/lib/parser → parser}/utils/type.js +9 -7
  70. package/dist/{src/lib/printer/sourcemap → printer}/lib/codec.js +2 -2
  71. package/dist/{src/lib/printer → printer}/render.js +582 -153
  72. package/dist/printer/result.js +148 -0
  73. package/dist/{src/lib/printer/sourcemap → printer}/sourcemap.js +4 -3
  74. package/dist/printer/writer.js +252 -0
  75. package/dist/{src/lib/syntax → syntax}/color/a98rgb.js +2 -2
  76. package/dist/{src/lib/syntax → syntax}/color/alpha.js +10 -10
  77. package/dist/{src/lib/syntax → syntax}/color/cmyk.js +2 -2
  78. package/dist/{src/lib/syntax → syntax}/color/color-mix.js +18 -18
  79. package/dist/{src/lib/syntax → syntax}/color/color.js +18 -18
  80. package/dist/{src/lib/syntax → syntax}/color/hex.js +3 -3
  81. package/dist/{src/lib/syntax → syntax}/color/hsl.js +2 -2
  82. package/dist/{src/lib/syntax → syntax}/color/hwb.js +2 -2
  83. package/dist/{src/lib/syntax → syntax}/color/lab.js +2 -2
  84. package/dist/{src/lib/syntax → syntax}/color/lch.js +2 -2
  85. package/dist/{src/lib/syntax → syntax}/color/oklab.js +2 -2
  86. package/dist/{src/lib/syntax → syntax}/color/oklch.js +3 -3
  87. package/dist/{src/lib/syntax → syntax}/color/p3.js +2 -2
  88. package/dist/{src/lib/syntax → syntax}/color/prophotorgb.js +2 -2
  89. package/dist/{src/lib/syntax → syntax}/color/rec2020.js +2 -2
  90. package/dist/{src/lib/syntax → syntax}/color/relative-color.js +14 -14
  91. package/dist/{src/lib/syntax → syntax}/color/rgb.js +4 -4
  92. package/dist/{src/lib/syntax → syntax}/color/srgb.js +5 -5
  93. package/dist/{src/lib/syntax → syntax}/color/utils/color-type.js +4 -4
  94. package/dist/{src/lib/syntax → syntax}/color/utils/colorspace.js +10 -10
  95. package/dist/{src/lib/syntax → syntax}/color/utils/components.js +5 -5
  96. package/dist/{src/lib/syntax → syntax}/color/utils/distance.js +2 -2
  97. package/dist/{src/lib/syntax → syntax}/color/utils/make-color.js +3 -3
  98. package/dist/{src/lib/syntax → syntax}/color/utils/matrix.js +2 -2
  99. package/dist/{src/lib/syntax → syntax}/color/xyz.js +2 -2
  100. package/dist/{src/lib/syntax → syntax}/color/xyzd50.js +2 -2
  101. package/dist/{src/lib/syntax → syntax}/constants.js +27 -4
  102. package/dist/{src/lib/syntax → syntax}/syntax.js +37 -29
  103. package/dist/{src/types.js → types.js} +2 -2
  104. package/dist/{src/utils → utils}/sync.js +12 -7
  105. package/dist/validation/compiled/builtins.js +32 -0
  106. package/dist/validation/compiled/runtime.js +705 -0
  107. package/dist/validation/compiled/syntaxes.js +1620 -0
  108. package/dist/{src/lib/validation → validation}/config.js +63 -16
  109. package/dist/validation/first.js +574 -0
  110. package/dist/{src/lib/validation → validation}/match.js +376 -161
  111. package/dist/{src/lib/validation → validation}/parser/parse.js +2 -2
  112. package/dist/{src/lib/validation → validation}/parser/typedef.js +2 -2
  113. package/dist/{src/lib/validation → validation}/utils/list.js +3 -19
  114. package/dist/version.js +13 -0
  115. package/dist/{src/web.js → web.js} +68 -55
  116. package/package.json +15 -13
  117. package/dist/package.json.js +0 -10
  118. package/dist/src/lib/ast/features/shorthand.js +0 -95
  119. package/dist/src/lib/ast/features/transform.js +0 -90
  120. package/dist/src/lib/ast/transform/compute.js +0 -362
  121. package/dist/src/lib/ast/transform/matrix.js +0 -88
  122. package/dist/src/lib/ast/transform/minify.js +0 -267
  123. package/dist/src/lib/ast/transform/perspective.js +0 -16
  124. package/dist/src/lib/ast/transform/scale.js +0 -39
  125. package/dist/src/lib/ast/transform/skew.js +0 -30
  126. package/dist/src/lib/ast/transform/translate.js +0 -39
  127. package/dist/src/lib/ast/transform/utils.js +0 -287
  128. package/dist/src/lib/parser/utils/cache.js +0 -54
  129. package/dist/types.d.ts +0 -21
  130. package/dist/utils/sync.d.ts +0 -16
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.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
@@ -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
  */
@@ -375,18 +371,179 @@ class Arena {
375
371
  }
376
372
  /**
377
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}
378
375
  * @param str
379
376
  * @returns
380
377
  */
381
378
  internString(str) {
382
- let id = this.stringIds.get(str);
383
- if (id != null) {
384
- return id;
385
- }
386
- id = this.strings.length;
387
- this.stringIds.set(str, id);
388
- this.strings.push(str);
389
- 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);
421
+ }
422
+ /**
423
+ * length of an interned string. the string is not created
424
+ * @param id
425
+ */
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);
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;
390
547
  }
391
548
  /**
392
549
  *
@@ -423,7 +580,7 @@ class Arena {
423
580
  * @param id
424
581
  */
425
582
  getName(id) {
426
- return this.strings[this.name[id]] ?? null;
583
+ return this.resolveString(this.name[id]) ?? null;
427
584
  }
428
585
  /**
429
586
  *
@@ -438,7 +595,7 @@ class Arena {
438
595
  * @param id
439
596
  */
440
597
  getValue(id) {
441
- return this.strings[this.stringValue[id]] ?? null;
598
+ return this.resolveString(this.stringValue[id]) ?? null;
442
599
  }
443
600
  /**
444
601
  *
@@ -461,7 +618,7 @@ class Arena {
461
618
  * @param id
462
619
  */
463
620
  getUnit(id) {
464
- return this.strings[this.unit[id]] ?? null;
621
+ return this.resolveString(this.unit[id]) ?? null;
465
622
  }
466
623
  /**
467
624
  *
@@ -530,7 +687,7 @@ class Arena {
530
687
  * number of interned strings
531
688
  */
532
689
  getStringCount() {
533
- return this.strings.length;
690
+ return this.stringTable.count;
534
691
  }
535
692
  /**
536
693
  * replace the first child
@@ -945,7 +1102,9 @@ class Arena {
945
1102
  * @param id
946
1103
  */
947
1104
  resolveString(id) {
948
- 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);
949
1108
  }
950
1109
  /**
951
1110
  * detach a node from its parent
@@ -1599,16 +1758,8 @@ class Arena {
1599
1758
  * @param string
1600
1759
  */
1601
1760
  equalsIgnoreCase(node, key, string) {
1602
- const id = key === "nam" ? this.name[node] : this.stringValue[node];
1603
- const stringId = this.stringIds.get(string);
1604
- if (stringId != null && id === stringId) {
1605
- return true;
1606
- }
1607
- const value = this.strings[id];
1608
- if (value == null || string.length !== value.length) {
1609
- return false;
1610
- }
1611
- 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);
1612
1763
  }
1613
1764
  /**
1614
1765
  *
@@ -1617,16 +1768,8 @@ class Arena {
1617
1768
  * @param string
1618
1769
  */
1619
1770
  startsWith(node, key, string) {
1620
- const id = key === "nam" ? this.name[node] : this.stringValue[node];
1621
- const stringId = this.stringIds.get(string);
1622
- if (stringId != null && id === stringId) {
1623
- return true;
1624
- }
1625
- const value = this.strings[id];
1626
- if (value == null || string.length > value.length) {
1627
- return false;
1628
- }
1629
- 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);
1630
1773
  }
1631
1774
  /**
1632
1775
  *
@@ -1688,7 +1831,7 @@ class Arena {
1688
1831
  * drop the interned strings that are no longer referenced by any node
1689
1832
  */
1690
1833
  compactStrings() {
1691
- const used = new Uint8Array(this.strings.length);
1834
+ const used = new Uint8Array(this.stringTable.count);
1692
1835
  const size = this.current + 1;
1693
1836
  let count = 0;
1694
1837
  // the empty string is always kept
@@ -1701,29 +1844,16 @@ class Arena {
1701
1844
  for (let i = 0; i < used.length; i++) {
1702
1845
  count += used[i];
1703
1846
  }
1704
- if (count === this.strings.length) {
1847
+ if (count === this.stringTable.count) {
1705
1848
  return;
1706
1849
  }
1707
1850
  // remap the string ids
1708
- const mapping = new Uint32Array(this.strings.length);
1709
- const strings = new Array(count);
1710
- const stringIds = new Map();
1711
- let next = 0;
1712
- for (let i = 0; i < used.length; i++) {
1713
- if (used[i]) {
1714
- mapping[i] = next;
1715
- strings[next] = this.strings[i];
1716
- stringIds.set(this.strings[i], next);
1717
- next++;
1718
- }
1719
- }
1851
+ const mapping = this.stringTable.compact(used);
1720
1852
  for (let id = 1; id < size; id++) {
1721
1853
  this.name[id] = mapping[this.name[id]];
1722
1854
  this.stringValue[id] = mapping[this.stringValue[id]];
1723
1855
  this.unit[id] = mapping[this.unit[id]];
1724
1856
  }
1725
- this.strings = strings;
1726
- this.stringIds = stringIds;
1727
1857
  }
1728
1858
  /**
1729
1859
  * drop the nodes that are not reachable from the root and renumber the remaining nodes in document order.
@@ -1768,7 +1898,7 @@ class Arena {
1768
1898
  const spans = new BigUint64Array(capacity);
1769
1899
  const spans32 = new Uint32Array(spans.buffer);
1770
1900
  const nodeErrors = new Map();
1771
- const used = new Uint8Array(this.strings.length);
1901
+ const used = new Uint8Array(this.stringTable.count);
1772
1902
  // the empty string is always kept
1773
1903
  used[0] = 1;
1774
1904
  for (let target = 1; target < next; target++) {
@@ -1798,26 +1928,13 @@ class Arena {
1798
1928
  for (let i = 0; i < used.length; i++) {
1799
1929
  count += used[i];
1800
1930
  }
1801
- if (count < this.strings.length) {
1802
- const stringMapping = new Uint32Array(this.strings.length);
1803
- const strings = new Array(count);
1804
- const stringIds = new Map();
1805
- let index = 0;
1806
- for (let i = 0; i < used.length; i++) {
1807
- if (used[i]) {
1808
- stringMapping[i] = index;
1809
- strings[index] = this.strings[i];
1810
- stringIds.set(this.strings[i], index);
1811
- index++;
1812
- }
1813
- }
1931
+ if (count < this.stringTable.count) {
1932
+ const stringMapping = this.stringTable.compact(used);
1814
1933
  for (let id = 1; id < next; id++) {
1815
1934
  name[id] = stringMapping[name[id]];
1816
1935
  stringValue[id] = stringMapping[stringValue[id]];
1817
1936
  unit[id] = stringMapping[unit[id]];
1818
1937
  }
1819
- this.strings = strings;
1820
- this.stringIds = stringIds;
1821
1938
  }
1822
1939
  for (const [id, list] of this.errors) {
1823
1940
  if (mapping[id] !== 0) {
@@ -1962,7 +2079,7 @@ class Arena {
1962
2079
  return {
1963
2080
  kind: "Arena",
1964
2081
  current: this.current,
1965
- strings: this.strings,
2082
+ strings: this.stringTable.export(),
1966
2083
  errors: [...this.errors.entries()],
1967
2084
  };
1968
2085
  }
@@ -1995,8 +2112,7 @@ class Arena {
1995
2112
  this.spans32 = new Uint32Array(this.spans.buffer);
1996
2113
  this.capacity = this.dataTypes.length;
1997
2114
  this.current = properties.current;
1998
- this.strings = properties.strings.slice();
1999
- this.stringIds = new Map(this.strings.map((str, index) => [str, index]));
2115
+ this.stringTable.import(properties.strings);
2000
2116
  this.errors = new Map(properties.errors);
2001
2117
  }
2002
2118
  /**
@@ -2030,17 +2146,22 @@ class Arena {
2030
2146
  */
2031
2147
  class AstArena extends Arena {
2032
2148
  /**
2033
- * raw selector (minification cache). sparse: only rules have an entry
2149
+ * raw selector (minification cache), by rule id
2034
2150
  */
2035
- raw = [];
2151
+ raw = new Map();
2036
2152
  /**
2037
- * optimized selector (minification cache). sparse: only rules have an entry
2153
+ * optimized selector (minification cache), by rule id
2038
2154
  */
2039
- optimized = [];
2155
+ optimized = new Map();
2040
2156
  /**
2041
2157
  * source ids. `spans` store the index in this table (8 bits)
2042
2158
  */
2043
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 = [];
2044
2165
  /**
2045
2166
  *
2046
2167
  * @param size
@@ -2058,11 +2179,14 @@ class AstArena extends Arena {
2058
2179
  super.release(id, deep);
2059
2180
  if (id != null) {
2060
2181
  // drop the minification caches
2061
- if (this.raw[id] != null) {
2062
- this.raw[id] = undefined;
2182
+ if (this.raw.get(id) != null) {
2183
+ this.raw.delete(id);
2063
2184
  }
2064
- if (this.optimized[id] != null) {
2065
- this.optimized[id] = undefined;
2185
+ if (this.optimized.get(id) != null) {
2186
+ this.optimized.delete(id);
2187
+ }
2188
+ if (this.inputLengths[id] != null) {
2189
+ this.inputLengths[id] = undefined;
2066
2190
  }
2067
2191
  }
2068
2192
  }
@@ -2072,8 +2196,16 @@ class AstArena extends Arena {
2072
2196
  compact(errors) {
2073
2197
  const mapping = super.compact(errors);
2074
2198
  // the minification caches are dropped
2075
- this.raw = [];
2076
- 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;
2077
2209
  return mapping;
2078
2210
  }
2079
2211
  /**
@@ -2212,7 +2344,8 @@ class AstArena extends Arena {
2212
2344
  createRule(typ, sel, tokens, state, errors) {
2213
2345
  const id = this.allocate();
2214
2346
  this.dataTypes[id] = typ;
2215
- 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);
2216
2349
  this.flags[id] = EnumAstNodeStatus.HasChildren | ((state ?? 0) & EnumAstNodeStatus.StateMask);
2217
2350
  if (tokens != null) {
2218
2351
  this.appendTokenList(id, tokens);
@@ -2236,8 +2369,9 @@ class AstArena extends Arena {
2236
2369
  createAtRule(typ, nam, val, block, tokens, state, errors) {
2237
2370
  const id = this.allocate();
2238
2371
  this.dataTypes[id] = typ;
2239
- this.name[id] = this.internString(nam);
2240
- 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);
2241
2375
  this.flags[id] = (block ? EnumAstNodeStatus.HasChildren : 0) | ((state ?? 0) & EnumAstNodeStatus.StateMask);
2242
2376
  if (tokens != null) {
2243
2377
  this.appendTokenList(id, tokens);
@@ -2258,7 +2392,8 @@ class AstArena extends Arena {
2258
2392
  createDeclaration(typ, nam, val, state, errors) {
2259
2393
  const id = this.allocate();
2260
2394
  this.dataTypes[id] = typ;
2261
- 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);
2262
2397
  this.flags[id] = EnumAstNodeStatus.HasValues | ((state ?? 0) & EnumAstNodeStatus.StateMask);
2263
2398
  if (val != null) {
2264
2399
  this.appendTokenList(id, val);
@@ -2276,7 +2411,8 @@ class AstArena extends Arena {
2276
2411
  createComment(typ, val) {
2277
2412
  const id = this.allocate();
2278
2413
  this.dataTypes[id] = typ;
2279
- 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);
2280
2416
  this.flags[id] = 0;
2281
2417
  return id;
2282
2418
  }
@@ -2426,7 +2562,7 @@ class AstArena extends Arena {
2426
2562
  * @param id
2427
2563
  */
2428
2564
  str(id) {
2429
- return this.strings[this.stringValue[id]];
2565
+ return this.resolveString(this.stringValue[id]);
2430
2566
  }
2431
2567
  /**
2432
2568
  * set the string value of a token (`val`)
@@ -2578,19 +2714,19 @@ class AstArena extends Arena {
2578
2714
  break;
2579
2715
  case EnumToken.RuleNodeType:
2580
2716
  case EnumToken.KeyframesRuleNodeType:
2581
- node.sel = this.strings[this.name[id]];
2717
+ node.sel = this.resolveString(this.name[id]);
2582
2718
  break;
2583
2719
  case EnumToken.AtRuleNodeType:
2584
2720
  case EnumToken.KeyframesAtRuleNodeType:
2585
- node.nam = this.strings[this.name[id]];
2586
- node.val = this.strings[this.stringValue[id]];
2721
+ node.nam = this.resolveString(this.name[id]);
2722
+ node.val = this.resolveString(this.stringValue[id]);
2587
2723
  break;
2588
2724
  case EnumToken.CommentNodeType:
2589
2725
  case EnumToken.CDOCOMMNodeType:
2590
- node.val = this.strings[this.stringValue[id]];
2726
+ node.val = this.resolveString(this.stringValue[id]);
2591
2727
  break;
2592
2728
  default:
2593
- node.nam = this.strings[this.name[id]];
2729
+ node.nam = this.resolveString(this.name[id]);
2594
2730
  node.val = [];
2595
2731
  for (const token of this.tokens(id)) {
2596
2732
  node.val.push(this.tokenToObject(token));
@@ -2618,11 +2754,11 @@ class AstArena extends Arena {
2618
2754
  if (this.errors.has(id)) {
2619
2755
  node.errors = this.errors.get(id);
2620
2756
  }
2621
- if (this.raw[id] != null) {
2622
- node.raw = this.raw[id];
2757
+ if (this.raw.get(id) != null) {
2758
+ node.raw = this.raw.get(id);
2623
2759
  }
2624
- if (this.optimized[id] != null) {
2625
- node.optimized = this.optimized[id];
2760
+ if (this.optimized.get(id) != null) {
2761
+ node.optimized = this.optimized.get(id);
2626
2762
  }
2627
2763
  return node;
2628
2764
  }
@@ -2634,7 +2770,7 @@ class AstArena extends Arena {
2634
2770
  const typ = this.dataTypes[id];
2635
2771
  const token = { typ };
2636
2772
  if (this.name[id] !== 0) {
2637
- token.nam = this.strings[this.name[id]];
2773
+ token.nam = this.resolveString(this.name[id]);
2638
2774
  }
2639
2775
  if (typ === EnumToken.BinaryExpressionTokenType) {
2640
2776
  token.op = this.numericValue[id];
@@ -2652,17 +2788,17 @@ class AstArena extends Arena {
2652
2788
  token.val = fraction === 0 ? this.numericValue[id] : this.tokenToObject(fraction);
2653
2789
  }
2654
2790
  else if (this.stringValue[id] !== 0 || typ === EnumToken.IdenTokenType || typ === EnumToken.StringTokenType) {
2655
- token.val = this.strings[this.stringValue[id]];
2791
+ token.val = this.resolveString(this.stringValue[id]);
2656
2792
  }
2657
2793
  if (this.unit[id] !== 0) {
2658
2794
  if (typ === EnumToken.ColorTokenType) {
2659
- token.cal = this.strings[this.unit[id]];
2795
+ token.cal = this.resolveString(this.unit[id]);
2660
2796
  }
2661
2797
  else if (typ === EnumToken.MatchExpressionTokenType) {
2662
- token.attr = this.strings[this.unit[id]];
2798
+ token.attr = this.resolveString(this.unit[id]);
2663
2799
  }
2664
2800
  else {
2665
- token.unit = this.strings[this.unit[id]];
2801
+ token.unit = this.resolveString(this.unit[id]);
2666
2802
  }
2667
2803
  }
2668
2804
  if (typ === EnumToken.ColorTokenType && this.colorType[id] !== ColorType.NONE) {
@@ -2701,10 +2837,31 @@ class AstArena extends Arena {
2701
2837
  return token;
2702
2838
  }
2703
2839
  /**
2704
- * 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`
2705
2862
  * @param id
2706
- * @param deep also clone the children (rules) or the value tokens (declarations)
2707
- * @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
2708
2865
  */
2709
2866
  clone(id, deep = false, cloneMap = null) {
2710
2867
  const target = this.allocate();
@@ -2718,11 +2875,11 @@ class AstArena extends Arena {
2718
2875
  this.colorType[target] = this.colorType[id];
2719
2876
  this.flags[target] = flags;
2720
2877
  this.copySpan(target, id);
2721
- if (this.raw[id] != null) {
2722
- this.raw[target] = this.raw[id];
2878
+ if (this.raw.get(id) != null) {
2879
+ this.raw.set(target, this.raw.get(id));
2723
2880
  }
2724
- if (this.optimized[id] != null) {
2725
- this.optimized[target] = this.optimized[id];
2881
+ if (this.optimized.get(id) != null) {
2882
+ this.optimized.set(target, this.optimized.get(id));
2726
2883
  }
2727
2884
  if (this.errors.size > 0 && this.errors.has(id)) {
2728
2885
  this.errors.set(target, this.errors.get(id));
@@ -2758,17 +2915,17 @@ class AstArena extends Arena {
2758
2915
  const target = this.allocate();
2759
2916
  const flags = source.flags[id];
2760
2917
  this.dataTypes[target] = source.dataTypes[id];
2761
- this.name[target] = this.internString(source.strings[source.name[id]] ?? "");
2762
- 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]) ?? "");
2763
2920
  this.numericValue[target] = source.numericValue[id];
2764
- this.unit[target] = this.internString(source.strings[source.unit[id]] ?? "");
2921
+ this.unit[target] = this.internString(source.resolveString(source.unit[id]) ?? "");
2765
2922
  this.colorType[target] = source.colorType[id];
2766
2923
  this.flags[target] = flags & ~EnumAstNodeStatus.HasLocation;
2767
- if (source.raw[id] != null) {
2768
- this.raw[target] = source.raw[id];
2924
+ if (source.raw.get(id) != null) {
2925
+ this.raw.set(target, source.raw.get(id));
2769
2926
  }
2770
- if (source.optimized[id] != null) {
2771
- this.optimized[target] = source.optimized[id];
2927
+ if (source.optimized.get(id) != null) {
2928
+ this.optimized.set(target, source.optimized.get(id));
2772
2929
  }
2773
2930
  if (flags & EnumAstNodeStatus.HasLocation) {
2774
2931
  // inlined: source.getSource(id)
@@ -2880,7 +3037,7 @@ class AstArena extends Arena {
2880
3037
  * @param id
2881
3038
  */
2882
3039
  getSelector(id) {
2883
- return this.strings[this.name[id]];
3040
+ return this.resolveString(this.name[id]);
2884
3041
  }
2885
3042
  /**
2886
3043
  * set the rule selector
@@ -2932,7 +3089,10 @@ class AstArena extends Arena {
2932
3089
  // the expanded rules are inserted after the rule
2933
3090
  this.expandRule(node, ast, next);
2934
3091
  }
2935
- 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")) {
2936
3096
  let j = this.lastChild[node];
2937
3097
  while (j !== 0) {
2938
3098
  if (this.dataTypes[j] === EnumToken.RuleNodeType ||
@@ -2959,7 +3119,7 @@ class AstArena extends Arena {
2959
3119
  for (const t of new ValueWalker(this, tokens)) {
2960
3120
  if (this.dataTypes[t] === EnumToken.NestingSelectorTokenType) {
2961
3121
  if (tokens.length === 2) {
2962
- this.resetToken(t, EnumToken.LiteralTokenType, this.replaceCompoundLiteral(this.strings[this.stringValue[t]], replace));
3122
+ this.resetToken(t, EnumToken.LiteralTokenType, this.replaceCompoundLiteral(this.resolveString(this.stringValue[t]), replace));
2963
3123
  continue;
2964
3124
  }
2965
3125
  const rule = splitRule(replace);
@@ -2976,8 +3136,9 @@ class AstArena extends Arena {
2976
3136
  ...super.exportProperties(),
2977
3137
  kind: "AstArena",
2978
3138
  sources: this.sources,
2979
- raw: this.raw,
2980
- optimized: this.optimized,
3139
+ raw: [...this.raw],
3140
+ optimized: [...this.optimized],
3141
+ inputLengths: this.inputLengths,
2981
3142
  };
2982
3143
  }
2983
3144
  /**
@@ -2986,8 +3147,9 @@ class AstArena extends Arena {
2986
3147
  importProperties(properties, transferables) {
2987
3148
  super.importProperties(properties, transferables);
2988
3149
  this.sources = properties.sources?.slice() ?? [];
2989
- this.raw = properties.raw?.slice() ?? [];
2990
- this.optimized = properties.optimized?.slice() ?? [];
3150
+ this.raw = new Map(properties.raw ?? []);
3151
+ this.optimized = new Map(properties.optimized ?? []);
3152
+ this.inputLengths = properties.inputLengths?.slice() ?? [];
2991
3153
  }
2992
3154
  // ---- nesting rules expansion ---------------------------------------------------------------
2993
3155
  /**
@@ -3007,10 +3169,10 @@ class AstArena extends Arena {
3007
3169
  this.name[id] = this.internString((node.sel ?? node.nam ?? ""));
3008
3170
  this.flags[id] = EnumAstNodeStatus.HasChildren;
3009
3171
  if (node.raw != null) {
3010
- this.raw[id] = node.raw;
3172
+ this.raw.set(id, node.raw);
3011
3173
  }
3012
3174
  if (node.optimized != null) {
3013
- this.optimized[id] = node.optimized;
3175
+ this.optimized.set(id, node.optimized);
3014
3176
  }
3015
3177
  if (typeof node.val === "string") {
3016
3178
  this.stringValue[id] = this.internString(node.val);
@@ -3169,7 +3331,7 @@ class AstArena extends Arena {
3169
3331
  return;
3170
3332
  }
3171
3333
  const ast = node;
3172
- const sel = this.strings[this.name[ast]];
3334
+ const sel = this.resolveString(this.name[ast]);
3173
3335
  const place = (child) => {
3174
3336
  if (before === 0) {
3175
3337
  this.appendChild(parent, child);
@@ -3185,7 +3347,7 @@ class AstArena extends Arena {
3185
3347
  const typ = this.dataTypes[child];
3186
3348
  if (typ === EnumToken.RuleNodeType) {
3187
3349
  const rule = child;
3188
- const ruleSel = this.strings[this.name[rule]];
3350
+ const ruleSel = this.resolveString(this.name[rule]);
3189
3351
  if (!ruleSel.includes("&")) {
3190
3352
  const selRule = splitRule(ruleSel);
3191
3353
  const arSelf = splitRule(sel)
@@ -3222,7 +3384,7 @@ class AstArena extends Arena {
3222
3384
  child = next;
3223
3385
  continue;
3224
3386
  }
3225
- for (const sel of this.raw[rule] ?? splitRule(ruleSel)) {
3387
+ for (const sel of this.raw.get(rule) ?? splitRule(ruleSel)) {
3226
3388
  const s = sel.join("");
3227
3389
  if (s.includes("&") || parentSelector) {
3228
3390
  if (s.indexOf("&", 1) === -1) {
@@ -3303,11 +3465,14 @@ class AstArena extends Arena {
3303
3465
  place(rule);
3304
3466
  this.expandRule(rule, parent, before);
3305
3467
  }
3468
+ else if (typ === EnumToken.AtRuleNodeType &&
3469
+ (!(this.flags[child] & EnumAstNodeStatus.HasChildren) ||
3470
+ this.stringEquals(this.name[child], "apply"))) ;
3306
3471
  else if (typ === EnumToken.AtRuleNodeType) {
3307
3472
  const astAtRule = child;
3308
- const atRuleName = this.strings[this.name[astAtRule]];
3473
+ const atRuleName = this.resolveString(this.name[astAtRule]);
3309
3474
  if (atRuleName === "scope") {
3310
- const atRuleValue = this.strings[this.stringValue[astAtRule]];
3475
+ const atRuleValue = this.resolveString(this.stringValue[astAtRule]);
3311
3476
  if (atRuleValue.includes("&")) {
3312
3477
  // inlined: this.setValueString(astAtRule, this.replaceCompound(atRuleValue, sel))
3313
3478
  this.stringValue[astAtRule] = this.internString(this.replaceCompound(atRuleValue, sel));
@@ -3315,7 +3480,7 @@ class AstArena extends Arena {
3315
3480
  let hasNesting = false;
3316
3481
  for (let t = this.firstChild[astAtRule]; t !== 0; t = this.nextSibling[t]) {
3317
3482
  if (this.dataTypes[t] === EnumToken.RuleNodeType &&
3318
- this.strings[this.name[t]].includes("&")) {
3483
+ this.resolveString(this.name[t]).includes("&")) {
3319
3484
  hasNesting = true;
3320
3485
  break;
3321
3486
  }
@@ -3351,10 +3516,10 @@ class AstArena extends Arena {
3351
3516
  const nextRule = this.nextSibling[r];
3352
3517
  if (this.dataTypes[r] === EnumToken.AtRuleNodeType) {
3353
3518
  if (this.flags[r] & EnumAstNodeStatus.HasChildren) {
3354
- const val = this.strings[this.stringValue[astAtRule]];
3355
- const rVal = this.strings[this.stringValue[r]];
3519
+ const val = this.resolveString(this.stringValue[astAtRule]);
3520
+ const rVal = this.resolveString(this.stringValue[r]);
3356
3521
  if (val !== "" && rVal !== "") {
3357
- const rName = this.strings[this.name[r]];
3522
+ const rName = this.resolveString(this.name[r]);
3358
3523
  if (atRuleName === "media" && rName === "media") {
3359
3524
  // inlined: this.setValueString(r, val + " and " + rVal)
3360
3525
  this.stringValue[r] = this.internString(val + " and " + rVal);