@tbela99/css-parser 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/CHANGELOG.md +42 -1
  2. package/README.md +14 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +5 -2
  6. package/dist/index-umd-web.js +10738 -4359
  7. package/dist/index.d.ts +734 -100
  8. package/dist/{src/lib → lib}/ast/arena-node.js +10 -10
  9. package/dist/{src/lib → lib}/ast/arena.js +304 -139
  10. package/dist/{src/lib → lib}/ast/cache.js +2 -2
  11. package/dist/{src/lib → lib}/ast/features/calc.js +11 -9
  12. package/dist/{src/lib → lib}/ast/features/if.js +6 -6
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +12 -10
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +39 -39
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +24 -24
  21. package/dist/{src/lib → lib}/ast/math/math.js +2 -2
  22. package/dist/{src/lib → lib}/ast/minify.js +745 -201
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +2 -2
  37. package/dist/{src/lib → lib}/ast/walk.js +2 -2
  38. package/dist/{src/lib → lib}/fs/resolve.js +2 -2
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +25 -8
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +15 -15
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +5 -5
  42. package/dist/{src/lib → lib}/parser/input.js +18 -3
  43. package/dist/{src/lib → lib}/parser/linesmap.js +2 -2
  44. package/dist/{src/lib → lib}/parser/parse.js +425 -258
  45. package/dist/{src/lib → lib}/parser/source.js +2 -2
  46. package/dist/{src/lib → lib}/parser/tokenize.js +480 -312
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +12 -12
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +7 -7
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +10 -10
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +14 -14
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +9 -14
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +6 -6
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +31 -26
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +2 -2
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +2 -2
  64. package/dist/{src/lib → lib}/parser/utils/selector.js +305 -63
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +9 -7
  68. package/dist/{src/lib → lib}/printer/render.js +420 -152
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +2 -2
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +2 -2
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +2 -2
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +10 -10
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +2 -2
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +18 -18
  77. package/dist/{src/lib → lib}/syntax/color/color.js +18 -18
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +3 -3
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +2 -2
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +2 -2
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +2 -2
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +2 -2
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +2 -2
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +3 -3
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +2 -2
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +2 -2
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +14 -14
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +4 -4
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +5 -5
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +4 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +10 -10
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +5 -5
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +3 -3
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +2 -2
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +2 -2
  99. package/dist/{src/lib → lib}/syntax/constants.js +26 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +37 -29
  101. package/dist/{src/lib → lib}/validation/config.js +23 -10
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +292 -150
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +2 -2
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +2 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +52 -34
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +10 -5
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +47 -34
  113. package/package.json +12 -12
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -95
  116. package/dist/src/lib/ast/features/transform.js +0 -90
  117. package/dist/src/lib/ast/transform/compute.js +0 -362
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/utils/cache.js +0 -54
@@ -1,15 +1,15 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { getAngle, convertColor } from '../syntax/color/color.js';
9
9
  import { reduceHexValue } from '../syntax/color/hex.js';
10
10
  import { EnumToken, ColorType, EnumAstNodeStatus } from '../ast/types.js';
11
11
  import { SourceMap } from './sourcemap/sourcemap.js';
12
- import { pseudoElements, anglePrecision, urlTokenMatcher, tokensfuncSet } from '../syntax/constants.js';
12
+ import { pseudoElements, anglePrecision, urlTokenMatcher, isFunctionType } from '../syntax/constants.js';
13
13
  import { minifyNumber, toPrecisionAngle, toPrecisionValue, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor, isWhiteSpace } from '../syntax/syntax.js';
14
14
  import { equalsIgnoreCase } from '../parser/utils/text.js';
15
15
  import { toDegrees } from '../parser/utils/angle.js';
@@ -18,6 +18,8 @@ import { dirname } from '../fs/resolve.js';
18
18
  import { SourceFile } from '../parser/source.js';
19
19
  import { TokenField } from '../ast/arena.js';
20
20
  import { StructuralMemo } from '../ast/cache.js';
21
+ import { Writer, DEFAULT_CAPACITY } from './writer.js';
22
+ import { RenderResult } from './result.js';
21
23
 
22
24
  /**
23
25
  * Rendered strings shared by the renderer.
@@ -57,6 +59,17 @@ class RenderMemo {
57
59
  * rendered constructs: colors and tokens with sub tokens (functions, parentheses, binary expressions, ...)
58
60
  */
59
61
  constructs = new StructuralMemo();
62
+ /**
63
+ * location of the declarations in the render buffer, by declaration slot: writer id, start offset, end offset and
64
+ * number of bytes minus the number of UTF-16 code units (4 values per slot). an identical declaration written to the
65
+ * same buffer is copied from there (see {@link Writer.copy})
66
+ */
67
+ locations = new Int32Array(1024);
68
+ /**
69
+ * rendered values of the declarations (without the name), by declaration slot: a declaration written to another
70
+ * buffer is not rendered again
71
+ */
72
+ values = [];
60
73
  /**
61
74
  * a cleared memo
62
75
  */
@@ -72,9 +85,35 @@ class RenderMemo {
72
85
  if (RenderMemo.free.length < 4 && memo.declarations.footprint + memo.constructs.footprint <= 1 << 20) {
73
86
  memo.declarations.clear();
74
87
  memo.constructs.clear();
88
+ memo.values.length = 0;
89
+ // the writer ids are never reused: the locations are not cleared
75
90
  RenderMemo.free.push(memo);
76
91
  }
77
92
  }
93
+ /**
94
+ * record the location of a declaration in a render buffer
95
+ * @param slot declaration slot
96
+ * @param writer render buffer
97
+ * @param start start offset
98
+ * @param extra value of {@link Writer.extra} at the start offset
99
+ */
100
+ locate(slot, writer, start, extra) {
101
+ let index = slot << 2;
102
+ if (index + 4 > this.locations.length) {
103
+ let size = this.locations.length * 2;
104
+ while (size < index + 4) {
105
+ size *= 2;
106
+ }
107
+ const locations = new Int32Array(size);
108
+ locations.set(this.locations);
109
+ this.locations = locations;
110
+ }
111
+ const locations = this.locations;
112
+ locations[index++] = writer.id;
113
+ locations[index++] = start;
114
+ locations[index++] = writer.length;
115
+ locations[index] = writer.extra - extra;
116
+ }
78
117
  }
79
118
  /**
80
119
  * Render state shared by the nodes and the tokens of a render
@@ -184,7 +223,8 @@ function resolveRenderOptions(options) {
184
223
  }
185
224
  /**
186
225
  * Render nodes with the same options: the options are resolved once and identical values are rendered once.
187
- * Same output as {@link doRender}, used by the minifier to compare the size of the nodes
226
+ * Same output as {@link doRender}, used by the minifier to compare the size of the nodes. The output is not kept: only
227
+ * its size is returned
188
228
  *
189
229
  * @internal
190
230
  */
@@ -230,6 +270,10 @@ class NodeRenderer {
230
270
  * the nodes are rendered without doRender(): no nesting expansion, no parents, no source map
231
271
  */
232
272
  direct;
273
+ /**
274
+ * output buffer, reused by the renders: only the size of the output is read
275
+ */
276
+ writer = new Writer();
233
277
  /**
234
278
  * @param options render options
235
279
  */
@@ -244,11 +288,11 @@ class NodeRenderer {
244
288
  this.resolved.output == null;
245
289
  }
246
290
  /**
247
- * render a node
291
+ * rendered size of a node: the length of the rendered css (UTF-16 code units)
248
292
  * @param arena
249
293
  * @param node
250
294
  */
251
- render(arena, node) {
295
+ size(arena, node) {
252
296
  if (this.arena !== arena) {
253
297
  if (this.arena != null) {
254
298
  this.cache = new RenderCache(new RenderMemo(), this.resolved);
@@ -257,11 +301,40 @@ class NodeRenderer {
257
301
  this.reducer = createReducer(arena, this.resolved, this.cache);
258
302
  }
259
303
  if (!this.direct) {
260
- return doRender(arena, node, this.options, null, this.cache.memo).code;
304
+ return RenderResult.codeLength(doRender(arena, node, this.options, null, this.cache.memo));
261
305
  }
262
- return renderAstNode(arena, node, this.resolved, null, this.location, null, this.errors, this.reducer, this.cache, 0, this.indents);
306
+ // the output is not kept: the buffer is reused
307
+ const writer = this.writer;
308
+ writer.reset();
309
+ renderAstNode(arena, node, this.resolved, null, this.location, null, this.errors, this.reducer, this.cache, writer, 0, this.indents);
310
+ return writer.codeLength;
263
311
  }
264
312
  }
313
+ /**
314
+ * extra capacity of the render buffer: the output may be larger than the input (beautified minified css, css modules
315
+ * exports, inline source map, ...)
316
+ */
317
+ const OUTPUT_PADDING = 1 << 12;
318
+ /**
319
+ * capacity of the buffer a node is rendered to. the minified or beautified output is usually not larger than the
320
+ * input: the length of the input the node was parsed from, padded
321
+ * - stylesheet: the length of the css parsed into the stylesheet, imported files included
322
+ * - other nodes: the length of their location in the source
323
+ * @param arena
324
+ * @param node
325
+ *
326
+ * @internal
327
+ */
328
+ function outputCapacity(arena, node) {
329
+ let length = 0;
330
+ if (arena.dataTypes[node] === EnumToken.StyleSheetNodeType) {
331
+ length = arena.inputLengths[node] ?? 0;
332
+ }
333
+ else if (arena.flags[node] & EnumAstNodeStatus.HasLocation) {
334
+ length = arena.getSpanEnd(node) - arena.getSpanStart(node);
335
+ }
336
+ return length > 0 ? length + OUTPUT_PADDING : DEFAULT_CAPACITY;
337
+ }
265
338
  /**
266
339
  * render ast
267
340
  * @param arena
@@ -273,6 +346,8 @@ class NodeRenderer {
273
346
  */
274
347
  function doRender(arena, data, options = {}, mapping, memo) {
275
348
  options = resolveRenderOptions(options);
349
+ // estimated from the node to render: the nodes rendered below may be copies
350
+ const capacity = outputCapacity(arena, data);
276
351
  const expandNesting = options.expandNestingRules &&
277
352
  [EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(arena.dataTypes[data]) &&
278
353
  (arena.flags[data] & EnumAstNodeStatus.HasChildren) !== 0;
@@ -313,8 +388,10 @@ function doRender(arena, data, options = {}, mapping, memo) {
313
388
  end: 0,
314
389
  };
315
390
  const linesMap = options.sourcemap ? new LineMap() : null;
316
- let code = "";
391
+ // the generated css is written to the buffer. its size is estimated from the input: it should not grow
392
+ const writer = new Writer(capacity);
317
393
  if (mapping != null) {
394
+ let code = "";
318
395
  if (mapping.importMapping != null) {
319
396
  const absolutePath = options.resolve(options.output != null ? dirname(options.output) : dirname(options.src), options.cwd).absolute;
320
397
  for (let [key, value] of Object.entries(mapping.importMapping)) {
@@ -326,6 +403,7 @@ function doRender(arena, data, options = {}, mapping, memo) {
326
403
  }
327
404
  }
328
405
  code += `:export${options.indent}{${options.newLine}${Object.entries(mapping.mapping).reduce((acc, [k, v]) => acc + (acc.length > 0 ? options.newLine : "") + `${options.indent}${k}:${options.indent}${v};`, "")}${options.newLine}}${options.newLine}`;
406
+ writer.write(code);
329
407
  if (sourcemap != null) {
330
408
  move(sourceLocation, linesMap, code);
331
409
  }
@@ -334,13 +412,9 @@ function doRender(arena, data, options = {}, mapping, memo) {
334
412
  // @ts-ignore
335
413
  options.output = options.resolve(options.output, options.cwd).absolute;
336
414
  }
337
- const result = {
338
- code: code +
339
- renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMap, errors, createReducer(arena, options, cache), cache),
340
- errors,
341
- stats: {
342
- total: `${(performance.now() - startTime).toFixed(2)}ms`,
343
- },
415
+ renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMap, errors, createReducer(arena, options, cache), cache, writer);
416
+ const stats = {
417
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
344
418
  };
345
419
  if (ownMemo) {
346
420
  RenderMemo.release(cache.memo);
@@ -358,12 +432,11 @@ function doRender(arena, data, options = {}, mapping, memo) {
358
432
  sourcemap.addSourceContent(source.id, source.file, source.content);
359
433
  }
360
434
  sourcemap.add(sourcemaps.maps);
361
- result.map = sourcemap;
362
435
  if (options.sourcemap === "inline") {
363
- result.code += `\n/*# sourceMappingURL=${result.map.toUrl()} */`;
436
+ writer.write(`\n/*# sourceMappingURL=${sourcemap.toUrl()} */`);
364
437
  }
365
438
  }
366
- return result;
439
+ return new RenderResult(writer.bytes(), errors, stats, sourcemap ?? undefined, writer.codeLength);
367
440
  }
368
441
  /**
369
442
  * Update source map
@@ -497,7 +570,28 @@ function move(sourceLocation, linesMap, str, start, end) {
497
570
  }
498
571
  }
499
572
  /**
500
- * render ast node
573
+ * write an interned string: an ascii string is copied from the string table, the string is not created
574
+ * @param arena
575
+ * @param id
576
+ * @param writer
577
+ */
578
+ function writeString(arena, id, writer) {
579
+ const length = arena.stringLength(id);
580
+ if (length === 0) {
581
+ return;
582
+ }
583
+ writer.reserve(length);
584
+ const written = arena.copyStringAscii(id, writer.buffer, writer.length);
585
+ if (written >= 0) {
586
+ writer.length += written;
587
+ }
588
+ else {
589
+ writer.write(arena.resolveString(id));
590
+ }
591
+ }
592
+ /**
593
+ * render ast node: the rendered css is written to the writer. nothing is written when the node is rendered as an empty
594
+ * string (removed comments, empty rules, ...)
501
595
  * @param arena
502
596
  * @param data node id
503
597
  * @param options
@@ -507,117 +601,210 @@ function move(sourceLocation, linesMap, str, start, end) {
507
601
  * @param errors
508
602
  * @param reducer
509
603
  * @param cache
604
+ * @param writer output
510
605
  * @param level
511
606
  * @param indents
512
607
  *
513
608
  * @internal
514
609
  */
515
- function renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level = 0, indents = []) {
610
+ function renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, writer, level = 0, indents = []) {
516
611
  if (indents.length < level + 1) {
517
612
  indents.push(options.indent.repeat(level));
518
613
  }
519
614
  if (indents.length < level + 2) {
520
615
  indents.push(options.indent.repeat(level + 1));
521
616
  }
522
- // @ts-ignore
523
- let children = "";
524
- let str = "";
525
617
  const indent = indents[level];
526
- const indentSub = indents[level + 1];
527
618
  const typ = arena.dataTypes[data];
528
619
  switch (typ) {
529
620
  case EnumToken.DeclarationNodeType:
530
- return `${arena.strings[arena.name[data]]}:${options.indent}${renderDeclarationValue(arena, data, options.minifyValues, reducer)}`;
621
+ writeString(arena, arena.name[data], writer);
622
+ writer.writeByte(0x3a);
623
+ writer.write(options.indent);
624
+ writer.write(renderDeclarationValue(arena, data, options.minifyValues, reducer));
625
+ return;
531
626
  case EnumToken.CommentNodeType:
532
627
  case EnumToken.CDOCOMMNodeType: {
533
- const val = arena.strings[arena.stringValue[data]];
534
- if (val.startsWith("/*# sourceMappingURL=")) {
628
+ const id = arena.stringValue[data];
629
+ if (arena.stringStartsWith(id, "/*# sourceMappingURL=")) {
535
630
  // ignore sourcemap comment
536
- return "";
631
+ return;
537
632
  }
538
- return !options.removeComments || (options.preserveLicense && val.startsWith("/*!")) ? val : "";
633
+ // the removed comments are not created
634
+ if (!options.removeComments || (options.preserveLicense && arena.stringStartsWith(id, "/*!"))) {
635
+ writeString(arena, id, writer);
636
+ }
637
+ return;
539
638
  }
540
- case EnumToken.StyleSheetNodeType:
639
+ case EnumToken.StyleSheetNodeType: {
640
+ const newLine = options.newLine;
641
+ let empty = true;
642
+ let mark;
643
+ let markExtra;
644
+ let start;
541
645
  for (let node = arena.firstChild[data]; node !== 0; node = arena.nextSibling[node]) {
542
- str = renderAstNode(arena, node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level, indents);
543
- if (str === "") {
544
- continue;
646
+ mark = writer.length;
647
+ markExtra = writer.extra;
648
+ // the separator is dropped when the node is empty
649
+ if (!empty && newLine !== "") {
650
+ writer.write(newLine);
545
651
  }
546
- if (children.length > 0) {
547
- str = options.newLine + str;
652
+ start = writer.length;
653
+ renderAstNode(arena, node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, writer, level, indents);
654
+ if (writer.length === start) {
655
+ writer.truncate(mark, markExtra);
656
+ continue;
548
657
  }
549
- children += str;
550
- if (sourcemaps != null && str !== "" && options.newLine) {
551
- move(sourceLocation, linesMap, options.newLine);
658
+ empty = false;
659
+ if (sourcemaps != null && newLine) {
660
+ move(sourceLocation, linesMap, newLine);
552
661
  }
553
662
  }
554
- return children;
663
+ return;
664
+ }
555
665
  case EnumToken.AtRuleNodeType:
556
666
  case EnumToken.RuleNodeType:
557
667
  case EnumToken.KeyframesRuleNodeType:
558
668
  case EnumToken.KeyframesAtRuleNodeType: {
559
669
  const isAtRule = typ === EnumToken.AtRuleNodeType || typ === EnumToken.KeyframesAtRuleNodeType;
560
- const nam = arena.strings[arena.name[data]];
561
- const val = arena.strings[arena.stringValue[data]];
562
- if (isAtRule && !(arena.flags[data] & EnumAstNodeStatus.HasChildren)) {
563
- return `${(indent.length > 0 ? options.newLine : "") + indent}@${nam}${val === "" ? "" : options.indent || " "}${val};`;
670
+ // the name and the value are copied from the string table: the strings are not created
671
+ const nameId = arena.name[data];
672
+ const valueId = arena.stringValue[data];
673
+ const newLine = options.newLine;
674
+ // start of the node: the node is removed when it has no children
675
+ const start = writer.length;
676
+ const startExtra = writer.extra;
677
+ if (indent.length > 0 && newLine !== "") {
678
+ writer.write(newLine);
679
+ }
680
+ if (indent.length > 0) {
681
+ writer.write(indent);
682
+ }
683
+ if (isAtRule) {
684
+ writer.writeByte(0x40);
685
+ writeString(arena, nameId, writer);
686
+ if (arena.stringLength(valueId) > 0) {
687
+ writer.write(options.indent || " ");
688
+ writeString(arena, valueId, writer);
689
+ }
690
+ if (!(arena.flags[data] & EnumAstNodeStatus.HasChildren)) {
691
+ // @rule prelude;
692
+ writer.writeByte(0x3b);
693
+ return;
694
+ }
564
695
  }
696
+ else {
697
+ writeString(arena, nameId, writer);
698
+ }
699
+ if (options.indent !== "") {
700
+ writer.write(options.indent);
701
+ }
702
+ writer.writeByte(0x7b);
565
703
  // inlined: linesMap.getLineStarts()
566
704
  const lineMapLength = linesMap ? linesMap.lineStarts.length : 0;
567
- const prelude = (indent.length > 0 ? options.newLine : "") +
568
- indent +
569
- (isAtRule
570
- ? `@${nam}${val === "" ? "" : options.indent || " "}${val}${options.indent}{`
571
- : nam + `${options.indent}{`);
705
+ // the prelude is only needed by the source map
706
+ let prelude = "";
572
707
  if (sourcemaps != null) {
708
+ const nam = arena.resolveString(nameId);
709
+ const val = arena.resolveString(valueId);
710
+ prelude =
711
+ (indent.length > 0 ? newLine : "") +
712
+ indent +
713
+ (isAtRule
714
+ ? `@${nam}${val === "" ? "" : options.indent || " "}${val}${options.indent}{`
715
+ : nam + `${options.indent}{`);
573
716
  updateSourceMap(arena, data, options, cache, sourcemaps, sourceLocation, linesMap, prelude);
574
717
  }
718
+ const indentSub = indents[level + 1];
719
+ // the children are written after the prelude
720
+ const body = writer.length;
721
+ let str;
575
722
  let node;
576
723
  let nodeType;
724
+ let mark;
725
+ let childStart;
577
726
  let recordDeclarationSourceMap = typ === EnumToken.AtRuleNodeType;
578
- // rendering of the last child, without its trailing semicolon, appended to the children when the next child
579
- // is rendered. the trailing semicolon of the last child is dropped: it is known from the type of the child,
580
- // the rendered strings are not read (reading a concatenated string flattens it)
581
- let last = "";
727
+ // the last child ends with a semicolon that is not written yet: it is written before the next child.
728
+ // the semicolon that ends the last child is dropped
582
729
  let semicolon = false;
583
730
  for (node = arena.firstChild[data]; node !== 0; node = arena.nextSibling[node]) {
584
731
  nodeType = arena.dataTypes[node];
732
+ if (nodeType === EnumToken.DeclarationNodeType && sourcemaps == null) {
733
+ // fast path: the declaration is written to the buffer
734
+ if (semicolon) {
735
+ writer.writeByte(0x3b);
736
+ }
737
+ if (newLine !== "") {
738
+ writer.write(newLine);
739
+ }
740
+ if (indentSub !== "") {
741
+ writer.write(indentSub);
742
+ }
743
+ writeDeclaration(arena, node, options, cache, reducer, writer);
744
+ semicolon = true;
745
+ continue;
746
+ }
585
747
  if (nodeType === EnumToken.CommentNodeType) {
586
- const comment = arena.strings[arena.stringValue[node]];
587
- str =
588
- options.removeComments && (!options.preserveLicense || !comment.startsWith("/*!"))
589
- ? ""
590
- : comment;
748
+ const commentId = arena.stringValue[node];
749
+ // the removed comments are not created
750
+ if (options.removeComments &&
751
+ (!options.preserveLicense || !arena.stringStartsWith(commentId, "/*!"))) {
752
+ continue;
753
+ }
754
+ str = arena.resolveString(commentId);
591
755
  }
592
756
  else if (nodeType === EnumToken.DeclarationNodeType) {
593
757
  // the semicolon is not part of the rendered declaration
594
758
  str = renderDeclaration(arena, node, options, cache, reducer);
595
759
  }
596
760
  else {
597
- str = renderAstNode(arena, node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level + 1, indents);
598
- if (str === "") {
761
+ mark = writer.length;
762
+ if (semicolon) {
763
+ writer.writeByte(0x3b);
764
+ }
765
+ childStart = writer.length;
766
+ renderAstNode(arena, node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, writer, level + 1, indents);
767
+ if (writer.length === childStart) {
768
+ // empty node: the pending semicolon is not written
769
+ writer.length = mark;
599
770
  continue;
600
771
  }
601
- children += semicolon ? last + ";" : last;
602
772
  // at-rules without a block ('@layer name;') end with a semicolon
603
- semicolon = endsWithSemicolon(arena, node, str);
604
- last = semicolon ? str.slice(0, -1) : str;
605
- str = "";
773
+ semicolon = endsWithSemicolon(arena, node, writer);
774
+ if (semicolon) {
775
+ writer.length--;
776
+ }
606
777
  continue;
607
778
  }
608
779
  if (str === "") {
609
780
  continue;
610
781
  }
611
- str = options.newLine + indentSub + str;
612
- children += semicolon ? last + ";" : last;
782
+ if (semicolon) {
783
+ writer.writeByte(0x3b);
784
+ }
785
+ if (sourcemaps == null) {
786
+ // comments
787
+ if (newLine !== "") {
788
+ writer.write(newLine);
789
+ }
790
+ if (indentSub !== "") {
791
+ writer.write(indentSub);
792
+ }
793
+ writer.write(str);
794
+ }
795
+ else {
796
+ str = newLine + indentSub + str;
797
+ writer.write(str);
798
+ }
613
799
  if (nodeType === EnumToken.DeclarationNodeType) {
614
- last = str;
615
800
  semicolon = true;
616
801
  }
617
802
  else {
618
- // comments are flat strings
619
- semicolon = str.charCodeAt(str.length - 1) === 0x3b;
620
- last = semicolon ? str.slice(0, -1) : str;
803
+ // comments
804
+ semicolon = writer.lastByte() === 0x3b;
805
+ if (semicolon) {
806
+ writer.length--;
807
+ }
621
808
  }
622
809
  if (sourcemaps != null) {
623
810
  if (nodeType === EnumToken.DeclarationNodeType && recordDeclarationSourceMap) {
@@ -642,32 +829,45 @@ function renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMa
642
829
  if (semicolon) {
643
830
  sourceLocation.end--;
644
831
  }
645
- children += last;
646
- if (options.removeEmpty && children === "") {
832
+ // an empty contents block is kept: `@apply --mixin {}` does not use the default contents of the mixin.
833
+ // an empty mixin or custom function is a definition
834
+ if (options.removeEmpty &&
835
+ writer.length === body &&
836
+ !(typ === EnumToken.AtRuleNodeType &&
837
+ (arena.stringEquals(nameId, "apply") ||
838
+ arena.stringEquals(nameId, "mixin") ||
839
+ arena.stringEquals(nameId, "function")))) {
647
840
  if (sourcemaps != null) {
648
841
  sourceLocation.end -= prelude.length;
649
842
  // inlined: linesMap!.getLineStarts()
650
843
  linesMap.lineStarts.length = lineMapLength;
651
844
  }
652
- return "";
845
+ writer.truncate(start, startExtra);
846
+ return;
847
+ }
848
+ if (newLine !== "") {
849
+ writer.write(newLine);
653
850
  }
654
- const end = options.newLine + indent + `}`;
851
+ if (indent !== "") {
852
+ writer.write(indent);
853
+ }
854
+ writer.writeByte(0x7d);
655
855
  if (sourcemaps != null) {
656
- move(sourceLocation, linesMap, end);
856
+ move(sourceLocation, linesMap, newLine + indent + "}");
657
857
  }
658
- return prelude + children + end;
858
+ return;
659
859
  }
660
860
  default:
661
- return "";
861
+ return;
662
862
  }
663
863
  }
664
864
  /**
665
865
  * true if a rendered child node ends with a semicolon
666
866
  * @param arena
667
867
  * @param node child node
668
- * @param rendered rendered child node, not empty
868
+ * @param writer output, the child node is the last node written, it is not empty
669
869
  */
670
- function endsWithSemicolon(arena, node, rendered) {
870
+ function endsWithSemicolon(arena, node, writer) {
671
871
  switch (arena.dataTypes[node]) {
672
872
  case EnumToken.AtRuleNodeType:
673
873
  case EnumToken.KeyframesAtRuleNodeType:
@@ -679,7 +879,7 @@ function endsWithSemicolon(arena, node, rendered) {
679
879
  return false;
680
880
  default:
681
881
  // comments: flat strings
682
- return rendered.charCodeAt(rendered.length - 1) === 0x3b;
882
+ return writer.lastByte() === 0x3b;
683
883
  }
684
884
  }
685
885
  function renderTokenList(tokens, reducer) {
@@ -689,6 +889,34 @@ function renderTokenList(tokens, reducer) {
689
889
  }
690
890
  return result;
691
891
  }
892
+ /**
893
+ * render a declaration without memoization: `name:value`, without the semicolon
894
+ * @param arena
895
+ * @param node declaration
896
+ * @param options
897
+ * @param cache
898
+ * @param reducer
899
+ */
900
+ function renderDeclarationString(arena, node, options, cache, reducer) {
901
+ if (options.minifyValues && isUnminifiedDeclaration(arena, node)) {
902
+ // override options to avoid minifying values for custom properties
903
+ options = { ...options, minifyValues: false };
904
+ // override the reducer
905
+ reducer = createReducer(arena, options, cache);
906
+ }
907
+ return `${arena.resolveString(arena.name[node])}:${options.indent}${renderDeclarationValue(arena, node, options.minifyValues, reducer).trimEnd()}`;
908
+ }
909
+ /**
910
+ * true if the value of the declaration is not minified:
911
+ * - the custom properties
912
+ * - the `initial-value` descriptor of `@property`: the value is parsed with the syntax of the registered custom
913
+ * property, a zero dimension keeps its unit: `0deg` is a valid `<angle>`, `0` is not
914
+ * @param arena
915
+ * @param node declaration
916
+ */
917
+ function isUnminifiedDeclaration(arena, node) {
918
+ return arena.startsWith(node, "nam", "--") || arena.stringEquals(arena.name[node], "initial-value");
919
+ }
692
920
  /**
693
921
  * render a declaration of a block: `name:value`, without the semicolon. identical declarations (same name, same value
694
922
  * tokens) are rendered once per render
@@ -700,30 +928,70 @@ function renderTokenList(tokens, reducer) {
700
928
  */
701
929
  function renderDeclaration(arena, node, options, cache, reducer) {
702
930
  const memo = getRenderMemo(cache, options);
703
- let minifyValues = options.minifyValues;
704
- if (minifyValues) {
705
- minifyValues = !arena.startsWith(node, "nam", "--");
706
- if (!minifyValues) {
707
- // override options to avoid minifying values for custom properties
708
- options = { ...options, minifyValues: false };
709
- // override the reducer
710
- reducer = createReducer(arena, options, cache);
711
- }
712
- }
713
931
  if (memo == null) {
714
- return `${arena.strings[arena.name[node]]}:${options.indent}${renderDeclarationValue(arena, node, options.minifyValues, reducer).trimEnd()}`;
932
+ return renderDeclarationString(arena, node, options, cache, reducer);
715
933
  }
716
934
  // the name is part of the key
717
935
  const slot = memo.declarations.list(arena, arena.firstToken[node], arena.name[node]);
718
936
  // inlined: memo.declarations.get(slot)
719
937
  let rendered = memo.declarations.values[slot];
720
938
  if (rendered === undefined) {
721
- rendered = `${arena.strings[arena.name[node]]}:${options.indent}${renderDeclarationValue(arena, node, options.minifyValues, reducer).trimEnd()}`;
939
+ rendered = renderDeclarationString(arena, node, options, cache, reducer);
722
940
  // inlined: memo.declarations.set(slot, rendered)
723
941
  memo.declarations.values[slot] = rendered;
724
942
  }
725
943
  return rendered;
726
944
  }
945
+ /**
946
+ * write a declaration of a block: `name:value`, without the semicolon. identical declarations (same name, same value
947
+ * tokens) are rendered and encoded once per render: the bytes written first are copied
948
+ * @param arena
949
+ * @param node declaration
950
+ * @param options
951
+ * @param cache
952
+ * @param reducer
953
+ * @param writer
954
+ */
955
+ function writeDeclaration(arena, node, options, cache, reducer, writer) {
956
+ const memo = getRenderMemo(cache, options);
957
+ let slot = -1;
958
+ if (memo !== null) {
959
+ // the name is part of the key
960
+ slot = memo.declarations.list(arena, arena.firstToken[node], arena.name[node]);
961
+ const index = slot << 2;
962
+ // an identical declaration is already written to the buffer: its bytes are copied
963
+ if (index < memo.locations.length && memo.locations[index] === writer.id) {
964
+ writer.copy(memo.locations[index + 1], memo.locations[index + 2], memo.locations[index + 3]);
965
+ return;
966
+ }
967
+ }
968
+ const start = writer.length;
969
+ const extra = writer.extra;
970
+ // the rendered value of an identical declaration, rendered to another buffer (see NodeRenderer)
971
+ let value = memo === null ? undefined : memo.values[slot];
972
+ if (value === undefined) {
973
+ if (options.minifyValues && isUnminifiedDeclaration(arena, node)) {
974
+ // override options to avoid minifying values for custom properties
975
+ options = { ...options, minifyValues: false };
976
+ // override the reducer
977
+ reducer = createReducer(arena, options, cache);
978
+ }
979
+ value = renderDeclarationValue(arena, node, options.minifyValues, reducer).trimEnd();
980
+ if (memo !== null) {
981
+ memo.values[slot] = value;
982
+ }
983
+ }
984
+ // the parts are written directly: `name`, `:`, `indent`, `value`. the name is copied from the string table
985
+ writeString(arena, arena.name[node], writer);
986
+ writer.writeByte(0x3a);
987
+ if (options.indent !== "") {
988
+ writer.write(options.indent);
989
+ }
990
+ writer.write(value);
991
+ if (memo !== null) {
992
+ memo.locate(slot, writer, start, extra);
993
+ }
994
+ }
727
995
  /**
728
996
  * render the value tokens of a declaration with the reducer. with minifyValues, the whitespace that is not needed is
729
997
  * skipped like {@link filterValues} does, without copying the tokens into an array: the whitespace before
@@ -764,7 +1032,7 @@ function renderDeclarationValue(arena, node, minifyValues, reducer) {
764
1032
  pending = 0;
765
1033
  check = false;
766
1034
  }
767
- else if (tokensfuncSet.has(type) &&
1035
+ else if (isFunctionType(type) &&
768
1036
  // color keywords and hex colors are not functions
769
1037
  (type !== EnumToken.ColorTokenType || arena.firstToken[token] !== 0) &&
770
1038
  type !== EnumToken.WildCardFunctionTokenType &&
@@ -795,10 +1063,10 @@ const defaultCache = Object.create(null);
795
1063
  function createReducer(arena, options, cache, errors) {
796
1064
  const reducer = (acc, curr) => {
797
1065
  if (arena.dataTypes[curr] === EnumToken.CommentTokenType && options.removeComments) {
798
- if (!options.preserveLicense || !arena.strings[arena.stringValue[curr]].startsWith("/*!")) {
1066
+ if (!options.preserveLicense || !arena.resolveString(arena.stringValue[curr]).startsWith("/*!")) {
799
1067
  return acc;
800
1068
  }
801
- return acc + arena.strings[arena.stringValue[curr]];
1069
+ return acc + arena.resolveString(arena.stringValue[curr]);
802
1070
  }
803
1071
  return acc + renderValue(arena, curr, options, cache, reducer);
804
1072
  };
@@ -821,8 +1089,8 @@ function renderReduced(arena, first, options, cache, reducer, errors) {
821
1089
  result = reducer(result, token);
822
1090
  }
823
1091
  else if (arena.dataTypes[token] === EnumToken.CommentTokenType && options.removeComments) {
824
- if (options.preserveLicense && arena.strings[arena.stringValue[token]].startsWith("/*!")) {
825
- result += arena.strings[arena.stringValue[token]];
1092
+ if (options.preserveLicense && arena.resolveString(arena.stringValue[token]).startsWith("/*!")) {
1093
+ result += arena.resolveString(arena.stringValue[token]);
826
1094
  }
827
1095
  }
828
1096
  else {
@@ -881,25 +1149,25 @@ function renderColor(arena, token, options, cache, reducer, errors) {
881
1149
  token = value;
882
1150
  }
883
1151
  if (arena.colorType[token] === ColorType.HEX || arena.colorType[token] === ColorType.LIT) {
884
- return reduceHexValue(arena.strings[arena.stringValue[token]]);
1152
+ return reduceHexValue(arena.resolveString(arena.stringValue[token]));
885
1153
  }
886
1154
  }
887
1155
  if (arena.colorType[token] === ColorType.SYS ||
888
1156
  arena.colorType[token] === ColorType.DPSYS ||
889
1157
  arena.colorType[token] === ColorType.NON_STD) {
890
- return arena.strings[arena.stringValue[token]];
1158
+ return arena.resolveString(arena.stringValue[token]);
891
1159
  }
892
1160
  if (arena.colorType[token] === ColorType.HEX || arena.colorType[token] === ColorType.LIT) {
893
- return arena.strings[arena.stringValue[token]];
1161
+ return arena.resolveString(arena.stringValue[token]);
894
1162
  }
895
1163
  if (arena.firstToken[token] === 0) {
896
1164
  // rendered like a function (no argument)
897
- return ((arena.strings[arena.stringValue[token]] ?? "") +
1165
+ return ((arena.resolveString(arena.stringValue[token]) ?? "") +
898
1166
  "(" +
899
1167
  renderReduced(arena, arena.firstToken[token], options, cache, reducer) +
900
1168
  ")");
901
1169
  }
902
- const fnName = arena.strings[arena.stringValue[token]];
1170
+ const fnName = arena.resolveString(arena.stringValue[token]);
903
1171
  const count = arena.tokenCount(token);
904
1172
  const isLegacy = legacyColorFunctions.has(fnName);
905
1173
  const hasAlpha = alphaColorFunctions.has(fnName);
@@ -922,7 +1190,7 @@ function renderColor(arena, token, options, cache, reducer, errors) {
922
1190
  result = result.trimEnd() + " ";
923
1191
  }
924
1192
  else if (arena.dataTypes[curr] === EnumToken.CommaTokenType ||
925
- (arena.dataTypes[curr] === EnumToken.LiteralTokenType && arena.strings[arena.stringValue[curr]] === "/")) {
1193
+ (arena.dataTypes[curr] === EnumToken.LiteralTokenType && arena.resolveString(arena.stringValue[curr]) === "/")) {
926
1194
  result = result.trimEnd() + (arena.dataTypes[curr] === EnumToken.CommaTokenType ? "," : "/");
927
1195
  }
928
1196
  else {
@@ -975,7 +1243,7 @@ function renderValue(arena, token, options = defaultRenderOptions, cache = defau
975
1243
  */
976
1244
  function renderToken(arena, token, options, cache, reducer, errors) {
977
1245
  if (arena.dataTypes[token] === EnumToken.WhenElseFunctionTokenType &&
978
- equalsIgnoreCase(arena.strings[arena.stringValue[token]], "supports")) {
1246
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), "supports")) {
979
1247
  options = { ...options, minifyValues: false, minify: false, convertColor: false };
980
1248
  reducer = null;
981
1249
  }
@@ -999,7 +1267,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
999
1267
  case EnumToken.GeneralEnclosedFunctionTokenDefType:
1000
1268
  case EnumToken.CustomFunctionTokenDefType:
1001
1269
  case EnumToken.WildCardFunctionTokenDefType:
1002
- return arena.strings[arena.stringValue[token]] + "(";
1270
+ return arena.resolveString(arena.stringValue[token]) + "(";
1003
1271
  case EnumToken.ListToken:
1004
1272
  return renderList(arena, arena.firstToken[token], options, cache);
1005
1273
  case EnumToken.BinaryExpressionTokenType: {
@@ -1068,7 +1336,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1068
1336
  }
1069
1337
  result += renderValue(arena, curr, options, cache);
1070
1338
  }
1071
- return arena.strings[arena.stringValue[token]] + "(" + result + ")";
1339
+ return arena.resolveString(arena.stringValue[token]) + "(" + result + ")";
1072
1340
  }
1073
1341
  return renderColor(arena, token, options, cache, reducer);
1074
1342
  }
@@ -1080,7 +1348,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1080
1348
  }
1081
1349
  for (let child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
1082
1350
  if (arena.dataTypes[child] === EnumToken.StringTokenType) {
1083
- const val = arena.strings[arena.stringValue[child]];
1351
+ const val = arena.resolveString(arena.stringValue[child]);
1084
1352
  if (val.slice(1, 5) !== "data:" && urlTokenMatcher.test(val)) {
1085
1353
  arena.dataTypes[child] = EnumToken.UrlTokenTokenType;
1086
1354
  // inlined: arena.setStr(child, val.slice(1, -1))
@@ -1096,7 +1364,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1096
1364
  case EnumToken.ImageFunctionTokenType:
1097
1365
  if (options.minifyValues && arena.dataTypes[token] === EnumToken.ImageFunctionTokenType) {
1098
1366
  const slice = arena.tokens(token);
1099
- switch (arena.strings[arena.stringValue[token]]) {
1367
+ switch (arena.resolveString(arena.stringValue[token])) {
1100
1368
  case "linear-gradient":
1101
1369
  case "repeating-linear-gradient":
1102
1370
  {
@@ -1110,7 +1378,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1110
1378
  break;
1111
1379
  }
1112
1380
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1113
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "to")) {
1381
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "to")) {
1114
1382
  k = i + 1;
1115
1383
  while (k < slice.length &&
1116
1384
  (arena.dataTypes[slice[k]] === EnumToken.WhitespaceTokenType ||
@@ -1119,7 +1387,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1119
1387
  }
1120
1388
  if (k < slice.length && arena.dataTypes[slice[k]] === EnumToken.IdenTokenType) {
1121
1389
  // convert keyword to angle
1122
- key = arena.strings[arena.stringValue[slice[k]]].toLowerCase();
1390
+ key = arena.resolveString(arena.stringValue[slice[k]]).toLowerCase();
1123
1391
  switch (key) {
1124
1392
  case "right":
1125
1393
  slice.splice(i, k - i + 1, arena.createToken(EnumToken.AngleTokenType, 90, "deg"));
@@ -1200,16 +1468,16 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1200
1468
  const size = [];
1201
1469
  const colorSpaceDef = [];
1202
1470
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType) {
1203
- if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "farthest-corner") ||
1204
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "ellipse")) {
1471
+ if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "farthest-corner") ||
1472
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "ellipse")) {
1205
1473
  i++;
1206
1474
  }
1207
- else if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "closest-side") ||
1208
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "closest-corner") ||
1209
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "farthest-side")) {
1475
+ else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "closest-side") ||
1476
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "closest-corner") ||
1477
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "farthest-side")) {
1210
1478
  size.push(slice[i++]);
1211
1479
  }
1212
- else if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "circle")) {
1480
+ else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "circle")) {
1213
1481
  form.push(slice[i++]);
1214
1482
  }
1215
1483
  while (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
@@ -1218,16 +1486,16 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1218
1486
  }
1219
1487
  }
1220
1488
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType) {
1221
- if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "farthest-corner") ||
1222
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "ellipse")) {
1489
+ if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "farthest-corner") ||
1490
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "ellipse")) {
1223
1491
  i++;
1224
1492
  }
1225
- else if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "closest-side") ||
1226
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "closest-corner") ||
1227
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "farthest-side")) {
1493
+ else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "closest-side") ||
1494
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "closest-corner") ||
1495
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "farthest-side")) {
1228
1496
  size.push(slice[i++]);
1229
1497
  }
1230
- else if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "circle")) {
1498
+ else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "circle")) {
1231
1499
  form.push(slice[i++]);
1232
1500
  }
1233
1501
  while (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
@@ -1236,7 +1504,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1236
1504
  }
1237
1505
  }
1238
1506
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1239
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "at")) {
1507
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "at")) {
1240
1508
  i++;
1241
1509
  }
1242
1510
  while (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
@@ -1247,14 +1515,14 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1247
1515
  (arena.dataTypes[slice[i]] === EnumToken.NumberTokenType &&
1248
1516
  0 === arena.numericValue[slice[i]]) ||
1249
1517
  (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1250
- (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "center") ||
1251
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "top") ||
1252
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "bottom") ||
1253
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "left") ||
1254
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "right")))) {
1518
+ (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "center") ||
1519
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "top") ||
1520
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "bottom") ||
1521
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "left") ||
1522
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "right")))) {
1255
1523
  do {
1256
1524
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1257
- "in" === arena.strings[arena.stringValue[slice[i]]]) {
1525
+ "in" === arena.resolveString(arena.stringValue[slice[i]])) {
1258
1526
  break;
1259
1527
  }
1260
1528
  positions.push(slice[i]);
@@ -1274,10 +1542,10 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1274
1542
  for (k = 0; k < positions.length; k++) {
1275
1543
  if (arena.dataTypes[positions[k]] === EnumToken.IdenTokenType) {
1276
1544
  if (position1.length === 0) {
1277
- position1 = arena.strings[arena.stringValue[positions[k]]];
1545
+ position1 = arena.resolveString(arena.stringValue[positions[k]]);
1278
1546
  }
1279
1547
  else {
1280
- position2 = arena.strings[arena.stringValue[positions[k]]];
1548
+ position2 = arena.resolveString(arena.stringValue[positions[k]]);
1281
1549
  }
1282
1550
  }
1283
1551
  else if (arena.dataTypes[positions[k]] === EnumToken.PercentageTokenType ||
@@ -1404,7 +1672,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1404
1672
  // i++;
1405
1673
  // }
1406
1674
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1407
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "from")) {
1675
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "from")) {
1408
1676
  angles.push(slice[i++]);
1409
1677
  while (i < slice.length &&
1410
1678
  (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
@@ -1428,7 +1696,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1428
1696
  }
1429
1697
  }
1430
1698
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1431
- equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "at")) {
1699
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "at")) {
1432
1700
  i++;
1433
1701
  while (i < slice.length &&
1434
1702
  (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
@@ -1438,8 +1706,8 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1438
1706
  let position1 = "";
1439
1707
  let position2 = "";
1440
1708
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1441
- !equalsIgnoreCase("in", arena.strings[arena.stringValue[slice[i]]])) {
1442
- position1 = arena.strings[arena.stringValue[slice[i]]];
1709
+ !equalsIgnoreCase("in", arena.resolveString(arena.stringValue[slice[i]]))) {
1710
+ position1 = arena.resolveString(arena.stringValue[slice[i]]);
1443
1711
  positions.push(slice[i++]);
1444
1712
  }
1445
1713
  else if (arena.dataTypes[slice[i]] === EnumToken.PercentageTokenType ||
@@ -1453,8 +1721,8 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1453
1721
  positions.push(slice[i++]);
1454
1722
  }
1455
1723
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1456
- !equalsIgnoreCase("in", arena.strings[arena.stringValue[slice[i]]])) {
1457
- position2 = arena.strings[arena.stringValue[slice[i]]];
1724
+ !equalsIgnoreCase("in", arena.resolveString(arena.stringValue[slice[i]]))) {
1725
+ position2 = arena.resolveString(arena.stringValue[slice[i]]);
1458
1726
  positions.push(slice[i++]);
1459
1727
  }
1460
1728
  else if (arena.dataTypes[slice[i]] === EnumToken.PercentageTokenType ||
@@ -1477,7 +1745,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1477
1745
  i++;
1478
1746
  }
1479
1747
  if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1480
- equalsIgnoreCase("in", arena.strings[arena.stringValue[slice[i]]])) {
1748
+ equalsIgnoreCase("in", arena.resolveString(arena.stringValue[slice[i]]))) {
1481
1749
  while (i < slice.length && arena.dataTypes[slice[i]] !== EnumToken.CommaTokenType) {
1482
1750
  colorSpaceDef.push(slice[i++]);
1483
1751
  }
@@ -1524,7 +1792,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1524
1792
  }
1525
1793
  break;
1526
1794
  }
1527
- return (arena.strings[arena.stringValue[token]] +
1795
+ return (arena.resolveString(arena.stringValue[token]) +
1528
1796
  "(" +
1529
1797
  renderTokenList(slice, reducer ?? createReducer(arena, options, cache)) +
1530
1798
  ")");
@@ -1548,7 +1816,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1548
1816
  firstType !== EnumToken.FractionTokenType &&
1549
1817
  firstType !== EnumToken.IdenTokenType &&
1550
1818
  !arena.hasFraction(first)) {
1551
- return (arena.strings[arena.stringValue[token]] +
1819
+ return (arena.resolveString(arena.stringValue[token]) +
1552
1820
  "(" +
1553
1821
  renderValue(arena, first, options, cache,
1554
1822
  // the reducer renders with the options of this token
@@ -1556,7 +1824,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1556
1824
  ")");
1557
1825
  }
1558
1826
  }
1559
- return ((arena.strings[arena.stringValue[token]] ?? "") +
1827
+ return ((arena.resolveString(arena.stringValue[token]) ?? "") +
1560
1828
  "(" +
1561
1829
  renderReduced(arena, arena.firstToken[token], options, cache, reducer) +
1562
1830
  ")");
@@ -1630,13 +1898,13 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1630
1898
  let val = arena.hasFraction(token)
1631
1899
  ? renderValue(arena, arena.getFraction(token), options, cache)
1632
1900
  : minifyNumber(arena.numericValue[token]);
1633
- let unit = arena.strings[arena.unit[token]];
1901
+ let unit = arena.resolveString(arena.unit[token]);
1634
1902
  if (arena.dataTypes[token] === EnumToken.AngleTokenType && !val.includes("/")) {
1635
1903
  const angle = getAngle(arena, token);
1636
1904
  let v;
1637
1905
  let value = val + unit;
1638
1906
  for (const u of angleUnits) {
1639
- if (arena.strings[arena.unit[token]] === u) {
1907
+ if (arena.resolveString(arena.unit[token]) === u) {
1640
1908
  continue;
1641
1909
  }
1642
1910
  switch (u) {
@@ -1725,19 +1993,19 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1725
1993
  case EnumToken.NaNTokenType:
1726
1994
  return "NaN";
1727
1995
  case EnumToken.AtRuleTokenType:
1728
- return "@" + arena.strings[arena.name[token]];
1996
+ return "@" + arena.resolveString(arena.name[token]);
1729
1997
  case EnumToken.CommentTokenType:
1730
1998
  case EnumToken.CDOCOMMNodeType:
1731
1999
  if (options.removeComments &&
1732
- (!options.preserveLicense || !arena.strings[arena.stringValue[token]].startsWith("/*!"))) {
2000
+ (!options.preserveLicense || !arena.resolveString(arena.stringValue[token]).startsWith("/*!"))) {
1733
2001
  return "";
1734
2002
  }
1735
2003
  case EnumToken.PseudoClassTokenType:
1736
2004
  case EnumToken.PseudoElementTokenType:
1737
2005
  // https://www.w3.org/TR/selectors-4/#single-colon-pseudos
1738
2006
  if (arena.dataTypes[token] === EnumToken.PseudoElementTokenType &&
1739
- pseudoElements.includes(arena.strings[arena.stringValue[token]].slice(1))) {
1740
- return arena.strings[arena.stringValue[token]].slice(1);
2007
+ pseudoElements.includes(arena.resolveString(arena.stringValue[token]).slice(1))) {
2008
+ return arena.resolveString(arena.stringValue[token]).slice(1);
1741
2009
  }
1742
2010
  case EnumToken.CssTypeTokenType:
1743
2011
  case EnumToken.UrlTokenTokenType:
@@ -1749,7 +2017,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1749
2017
  case EnumToken.DashedIdenTokenType:
1750
2018
  case EnumToken.PseudoPageTokenType:
1751
2019
  case EnumToken.ClassSelectorTokenType:
1752
- return arena.strings[arena.stringValue[token]];
2020
+ return arena.resolveString(arena.stringValue[token]);
1753
2021
  case EnumToken.NestingSelectorTokenType:
1754
2022
  return "&";
1755
2023
  case EnumToken.SupportsQueryUnaryConditionTokenType:
@@ -1777,7 +2045,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1777
2045
  (options.indent ?? "") +
1778
2046
  renderValue(arena, arena.field(token, TokenField.R), options, cache, reducer));
1779
2047
  case EnumToken.DeclarationNodeType:
1780
- return (arena.strings[arena.name[token]] +
2048
+ return (arena.resolveString(arena.name[token]) +
1781
2049
  ":" +
1782
2050
  renderTokenList(options.minifyValues ? filterValues(arena, arena.tokens(token)) : arena.tokens(token), (acc, curr) => acc + renderValue(arena, curr, options, cache, reducer)));
1783
2051
  case EnumToken.MediaQueryUnaryFeatureTokenType:
@@ -1808,7 +2076,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
1808
2076
  renderValue(arena, arena.field(token, TokenField.Op2), options, cache, reducer) +
1809
2077
  renderField(arena, token, TokenField.R, options, cache, reducer));
1810
2078
  case EnumToken.MediaFeatureTokenType:
1811
- return arena.strings[arena.stringValue[token]];
2079
+ return arena.resolveString(arena.stringValue[token]);
1812
2080
  case EnumToken.NotTokenType:
1813
2081
  return "not";
1814
2082
  case EnumToken.OnlyTokenType:
@@ -1845,7 +2113,7 @@ function filterValues(arena, values) {
1845
2113
  arena.dataTypes[values[i - 1]] === EnumToken.WhitespaceTokenType) {
1846
2114
  values.splice(i - 1, 1);
1847
2115
  }
1848
- else if (tokensfuncSet.has(arena.dataTypes[values[i]]) &&
2116
+ else if (isFunctionType(arena.dataTypes[values[i]]) &&
1849
2117
  // color keywords and hex colors are not functions
1850
2118
  (arena.dataTypes[values[i]] !== EnumToken.ColorTokenType || arena.firstToken[values[i]] !== 0) &&
1851
2119
  arena.dataTypes[values[i]] !== EnumToken.WildCardFunctionTokenType &&
@@ -1856,4 +2124,4 @@ function filterValues(arena, values) {
1856
2124
  return values;
1857
2125
  }
1858
2126
 
1859
- export { NodeRenderer, RenderMemo, doRender, filterValues, move, renderValue };
2127
+ export { NodeRenderer, RenderMemo, doRender, filterValues, move, outputCapacity, renderValue };