@tbela99/css-parser 1.8.2 → 2.0.1

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 (105) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27383 -24083
  4. package/dist/index.d.ts +2737 -2200
  5. package/dist/package.json.js +2 -2
  6. package/dist/src/data/properties.json.js +1 -1
  7. package/dist/src/data/validation.json.js +1 -1
  8. package/dist/src/lib/ast/arena-node.js +548 -0
  9. package/dist/src/lib/ast/arena.js +3198 -0
  10. package/dist/src/lib/ast/cache.js +333 -0
  11. package/dist/src/lib/ast/features/calc.js +85 -76
  12. package/dist/src/lib/ast/features/if.js +155 -115
  13. package/dist/src/lib/ast/features/index.js +1 -1
  14. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  15. package/dist/src/lib/ast/features/prefix.js +184 -183
  16. package/dist/src/lib/ast/features/shorthand.js +40 -46
  17. package/dist/src/lib/ast/features/transform.js +24 -24
  18. package/dist/src/lib/ast/features/type.js +1 -1
  19. package/dist/src/lib/ast/math/expression.js +458 -543
  20. package/dist/src/lib/ast/math/math.js +19 -41
  21. package/dist/src/lib/ast/minify.js +590 -647
  22. package/dist/src/lib/ast/transform/compute.js +82 -85
  23. package/dist/src/lib/ast/transform/matrix.js +21 -38
  24. package/dist/src/lib/ast/transform/minify.js +82 -193
  25. package/dist/src/lib/ast/transform/perspective.js +2 -3
  26. package/dist/src/lib/ast/transform/rotate.js +1 -1
  27. package/dist/src/lib/ast/transform/scale.js +1 -1
  28. package/dist/src/lib/ast/transform/skew.js +1 -1
  29. package/dist/src/lib/ast/transform/translate.js +1 -1
  30. package/dist/src/lib/ast/transform/utils.js +2 -2
  31. package/dist/src/lib/ast/types.js +263 -258
  32. package/dist/src/lib/ast/walk.js +246 -610
  33. package/dist/src/lib/fs/resolve.js +1 -1
  34. package/dist/src/lib/parser/declaration/list.js +223 -139
  35. package/dist/src/lib/parser/declaration/map.js +251 -238
  36. package/dist/src/lib/parser/declaration/set.js +74 -66
  37. package/dist/src/lib/parser/linesmap.js +1 -1
  38. package/dist/src/lib/parser/parse.js +1379 -1658
  39. package/dist/src/lib/parser/source.js +7 -4
  40. package/dist/src/lib/parser/tokenize.js +406 -199
  41. package/dist/src/lib/parser/utils/angle.js +11 -13
  42. package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
  43. package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
  44. package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
  45. package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
  46. package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
  47. package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
  48. package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
  49. package/dist/src/lib/parser/utils/at-rule.js +5 -5
  50. package/dist/src/lib/parser/utils/base64.js +2 -2
  51. package/dist/src/lib/parser/utils/cache.js +1 -1
  52. package/dist/src/lib/parser/utils/config.js +1 -1
  53. package/dist/src/lib/parser/utils/declaration.js +233 -234
  54. package/dist/src/lib/parser/utils/eq.js +50 -3
  55. package/dist/src/lib/parser/utils/hash.js +2 -35
  56. package/dist/src/lib/parser/utils/selector.js +309 -387
  57. package/dist/src/lib/parser/utils/text.js +18 -4
  58. package/dist/src/lib/parser/utils/token.js +16 -69
  59. package/dist/src/lib/parser/utils/type.js +37 -24
  60. package/dist/src/lib/printer/render.js +965 -511
  61. package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
  62. package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
  63. package/dist/src/lib/syntax/color/a98rgb.js +1 -1
  64. package/dist/src/lib/syntax/color/alpha.js +36 -37
  65. package/dist/src/lib/syntax/color/cmyk.js +35 -52
  66. package/dist/src/lib/syntax/color/color-mix.js +96 -132
  67. package/dist/src/lib/syntax/color/color.js +222 -231
  68. package/dist/src/lib/syntax/color/hex.js +45 -55
  69. package/dist/src/lib/syntax/color/hsl.js +54 -62
  70. package/dist/src/lib/syntax/color/hwb.js +60 -68
  71. package/dist/src/lib/syntax/color/lab.js +59 -67
  72. package/dist/src/lib/syntax/color/lch.js +59 -67
  73. package/dist/src/lib/syntax/color/oklab.js +60 -68
  74. package/dist/src/lib/syntax/color/oklch.js +60 -67
  75. package/dist/src/lib/syntax/color/p3.js +1 -1
  76. package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
  77. package/dist/src/lib/syntax/color/rec2020.js +1 -1
  78. package/dist/src/lib/syntax/color/relative-color.js +94 -144
  79. package/dist/src/lib/syntax/color/rgb.js +54 -57
  80. package/dist/src/lib/syntax/color/srgb.js +55 -53
  81. package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
  82. package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
  83. package/dist/src/lib/syntax/color/utils/components.js +19 -20
  84. package/dist/src/lib/syntax/color/utils/distance.js +15 -13
  85. package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
  86. package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
  87. package/dist/src/lib/syntax/color/xyz.js +1 -1
  88. package/dist/src/lib/syntax/color/xyzd50.js +1 -1
  89. package/dist/src/lib/syntax/constants.js +2 -16
  90. package/dist/src/lib/syntax/syntax.js +301 -407
  91. package/dist/src/lib/validation/config.js +1 -1
  92. package/dist/src/lib/validation/match.js +959 -723
  93. package/dist/src/lib/validation/parser/parse.js +230 -112
  94. package/dist/src/lib/validation/parser/typedef.js +1 -1
  95. package/dist/src/lib/validation/utils/list.js +27 -12
  96. package/dist/src/node.js +12 -72
  97. package/dist/src/types.js +1 -1
  98. package/dist/src/utils/sync.js +9 -6
  99. package/dist/src/web.js +11 -73
  100. package/package.json +9 -10
  101. package/dist/index.cjs +0 -35870
  102. package/dist/src/lib/ast/clone.js +0 -51
  103. package/dist/src/lib/ast/expand.js +0 -311
  104. package/dist/src/lib/ast/find.js +0 -192
  105. package/dist/src/lib/ast/node.js +0 -59
@@ -2,79 +2,313 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.1
6
6
  License: MIT
7
7
  */
8
- import { convertColor, getAngle } from '../syntax/color/color.js';
8
+ import { getAngle, convertColor } from '../syntax/color/color.js';
9
9
  import { reduceHexValue } from '../syntax/color/hex.js';
10
- import { EnumToken, ColorType } from '../ast/types.js';
11
- import { expand } from '../ast/expand.js';
10
+ import { EnumToken, ColorType, EnumAstNodeStatus } from '../ast/types.js';
12
11
  import { SourceMap } from './sourcemap/sourcemap.js';
13
- import { pseudoElements, anglePrecision, urlTokenMatcher, PARENT, tokensfuncSet, LOCSTA, LOCSRCID } from '../syntax/constants.js';
14
- import { minifyNumber, toPrecisionAngle, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor, isWhiteSpace, toPrecisionValue } from '../syntax/syntax.js';
12
+ import { pseudoElements, anglePrecision, urlTokenMatcher, tokensfuncSet } from '../syntax/constants.js';
13
+ import { minifyNumber, toPrecisionAngle, toPrecisionValue, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor, isWhiteSpace } from '../syntax/syntax.js';
15
14
  import { equalsIgnoreCase } from '../parser/utils/text.js';
16
15
  import { toDegrees } from '../parser/utils/angle.js';
17
16
  import { LineMap } from '../parser/linesmap.js';
18
17
  import { dirname } from '../fs/resolve.js';
19
18
  import { SourceFile } from '../parser/source.js';
20
- import { cloneNode } from '../ast/clone.js';
19
+ import { TokenField } from '../ast/arena.js';
20
+ import { StructuralMemo } from '../ast/cache.js';
21
21
 
22
22
  /**
23
- * render ast
24
- * @param data
23
+ * Rendered strings shared by the renderer.
24
+ *
25
+ * Identical declarations are very common (`display: block`, `color: #fff`, `width: 10px`, ...): a declaration that has
26
+ * the same name and the same value tokens as a declaration that has already been rendered reuses the rendered string.
27
+ * The same applies to the constructs of the values: colors, functions, parentheses, binary expressions, ... The
28
+ * individual tokens (identifiers, numbers, dimensions, ...) are not memoized: they are rendered when the declaration
29
+ * or the construct that contains them is rendered for the first time.
30
+ *
31
+ * Rendering only depends on the token structure and on the render options: a memo must only be shared by renders that
32
+ * use the same options.
33
+ *
34
+ * The source map locations are not memoized: they are computed for every node from the rendered string and the location
35
+ * of the node.
36
+ *
37
+ * @internal
38
+ */
39
+ class RenderMemo {
40
+ /**
41
+ * An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
42
+ * collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
43
+ */
44
+ static shape = new RenderMemo();
45
+ /**
46
+ * rendered declarations (`name:value`), by name and value tokens
47
+ */
48
+ declarations = new StructuralMemo();
49
+ /**
50
+ * rendered constructs: colors and tokens with sub tokens (functions, parentheses, binary expressions, ...)
51
+ */
52
+ constructs = new StructuralMemo();
53
+ /**
54
+ * cleared memos, reused by the next renders: their buffers are not allocated again. read by the library
55
+ * (see {@link acquire})
56
+ *
57
+ * @internal
58
+ */
59
+ static free = [];
60
+ /**
61
+ * a cleared memo
62
+ */
63
+ static acquire() {
64
+ return RenderMemo.free.pop() ?? new RenderMemo();
65
+ }
66
+ /**
67
+ * clear a memo that is no longer used and keep it for the next renders. the large memos (more than 4 MB) are not
68
+ * kept: they are reclaimed by the garbage collector
69
+ * @param memo
70
+ */
71
+ static release(memo) {
72
+ if (RenderMemo.free.length < 4 && memo.declarations.footprint + memo.constructs.footprint <= 1 << 20) {
73
+ memo.declarations.clear();
74
+ memo.constructs.clear();
75
+ RenderMemo.free.push(memo);
76
+ }
77
+ }
78
+ }
79
+ /**
80
+ * Render state shared by the nodes and the tokens of a render
81
+ *
82
+ * @internal
83
+ */
84
+ class RenderCache {
85
+ memo;
86
+ options;
87
+ /**
88
+ * An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
89
+ * collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
90
+ */
91
+ static shape = new RenderCache(new RenderMemo(), {});
92
+ /**
93
+ * resolved source file names (source maps)
94
+ */
95
+ paths = Object.create(null);
96
+ /**
97
+ * @param memo rendered values
98
+ * @param options options the memo is valid for
99
+ */
100
+ constructor(memo, options) {
101
+ this.memo = memo;
102
+ this.options = options;
103
+ }
104
+ }
105
+ /**
106
+ * render memo of the cache, if the tokens are rendered with the options the memo was created for. the memo is not used
107
+ * when a token is rendered with other options (a copy of the options, the default options, ...)
108
+ * @param cache
25
109
  * @param options
26
- * @param mapping
27
- * @private
28
110
  */
29
- function doRender(data, options = {}, mapping) {
111
+ function getRenderMemo(cache, options) {
112
+ return cache instanceof RenderCache && cache.options === options ? cache.memo : null;
113
+ }
114
+ /**
115
+ * legacy color functions: comma separated values and an alias with the `a` suffix
116
+ */
117
+ const legacyColorFunctions = new Set(["rgb", "rgba", "hsl", "hsla"]);
118
+ /**
119
+ * color functions that accept an alpha component
120
+ */
121
+ const alphaColorFunctions = new Set(["rgb", "rgba", "hsl", "hsla", "hwb", "oklab", "oklch", "lab", "lch"]);
122
+ /**
123
+ * color function value ending with a separator
124
+ */
125
+ const trailingSeparator = /[,/]\s*$/;
126
+ /**
127
+ * candidate angle units, in the order they are tested
128
+ */
129
+ const angleUnits = ["deg", "turn", "rad", "grad"];
130
+ /**
131
+ * record a source id
132
+ * @param sourcemaps
133
+ * @param srcId
134
+ */
135
+ function addSource(sourcemaps, srcId) {
136
+ if (!sourcemaps.sourceIds.has(srcId)) {
137
+ sourcemaps.sourceIds.add(srcId);
138
+ sourcemaps.sources.push(srcId);
139
+ }
140
+ }
141
+ /**
142
+ * copy of the options with the defaults of the missing options
143
+ * @param options
144
+ *
145
+ * @internal
146
+ */
147
+ function resolveRenderOptions(options) {
30
148
  const minify = options.minify ?? true;
31
149
  const beautify = options.beautify ?? !minify;
32
- options = {
33
- ...(beautify
34
- ? {
35
- indent: " ",
36
- newLine: "\n",
150
+ // a copy of the options (a spread into an empty object is a fast clone), then the defaults of the missing
151
+ // options. a spread after other properties copies the properties one by one: it is much slower
152
+ options = { ...options };
153
+ if (!("indent" in options)) {
154
+ options.indent = beautify ? " " : "";
155
+ }
156
+ if (!("newLine" in options)) {
157
+ options.newLine = beautify ? "\n" : "";
158
+ }
159
+ if (!("removeEmpty" in options)) {
160
+ options.removeEmpty = minify;
161
+ }
162
+ if (!("removeComments" in options)) {
163
+ options.removeComments = minify;
164
+ }
165
+ if (minify && !("minify" in options)) {
166
+ options.minify = true;
167
+ }
168
+ if (!("minifyValues" in options)) {
169
+ options.minifyValues = true;
170
+ }
171
+ if (!("sourcemap" in options)) {
172
+ options.sourcemap = false;
173
+ }
174
+ if (!("convertColor" in options)) {
175
+ options.convertColor = true;
176
+ }
177
+ if (!("expandNestingRules" in options)) {
178
+ options.expandNestingRules = false;
179
+ }
180
+ if (!("preserveLicense" in options)) {
181
+ options.preserveLicense = false;
182
+ }
183
+ return options;
184
+ }
185
+ /**
186
+ * 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
188
+ *
189
+ * @internal
190
+ */
191
+ class NodeRenderer {
192
+ /**
193
+ * An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
194
+ * collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
195
+ */
196
+ static shape = new NodeRenderer({});
197
+ /**
198
+ * options
199
+ */
200
+ options;
201
+ /**
202
+ * resolved options
203
+ */
204
+ resolved;
205
+ /**
206
+ * render state. the memo keys contain interned string ids: the memo is valid for one arena
207
+ */
208
+ cache;
209
+ /**
210
+ * errors
211
+ */
212
+ errors = [];
213
+ /**
214
+ * token reducer, bound to an arena
215
+ */
216
+ reducer = null;
217
+ /**
218
+ * arena of the reducer
219
+ */
220
+ arena = null;
221
+ /**
222
+ * indentation by level
223
+ */
224
+ indents = [];
225
+ /**
226
+ * output location, not used: there is no source map
227
+ */
228
+ location = { srcId: 0, sta: 0, end: 0 };
229
+ /**
230
+ * the nodes are rendered without doRender(): no nesting expansion, no parents, no source map
231
+ */
232
+ direct;
233
+ /**
234
+ * @param options render options
235
+ */
236
+ constructor(options) {
237
+ this.options = options;
238
+ this.resolved = resolveRenderOptions(options);
239
+ this.cache = new RenderCache(new RenderMemo(), this.resolved);
240
+ this.direct =
241
+ !this.resolved.expandNestingRules &&
242
+ !this.resolved.withParents &&
243
+ !this.resolved.sourcemap &&
244
+ this.resolved.output == null;
245
+ }
246
+ /**
247
+ * render a node
248
+ * @param arena
249
+ * @param node
250
+ */
251
+ render(arena, node) {
252
+ if (this.arena !== arena) {
253
+ if (this.arena != null) {
254
+ this.cache = new RenderCache(new RenderMemo(), this.resolved);
37
255
  }
38
- : {
39
- indent: "",
40
- newLine: "",
41
- }),
42
- ...(minify
43
- ? {
44
- removeEmpty: true,
45
- removeComments: true,
46
- minify: true,
256
+ this.arena = arena;
257
+ this.reducer = createReducer(arena, this.resolved, this.cache);
258
+ }
259
+ if (!this.direct) {
260
+ return doRender(arena, node, this.options, null, this.cache.memo).code;
261
+ }
262
+ return renderAstNode(arena, node, this.resolved, null, this.location, null, this.errors, this.reducer, this.cache, 0, this.indents);
263
+ }
264
+ }
265
+ /**
266
+ * render ast
267
+ * @param arena
268
+ * @param data node id
269
+ * @param options
270
+ * @param mapping
271
+ * @param memo rendered values shared with other renders that use the same options (see {@link NodeRenderer})
272
+ * @private
273
+ */
274
+ function doRender(arena, data, options = {}, mapping, memo) {
275
+ options = resolveRenderOptions(options);
276
+ const expandNesting = options.expandNestingRules &&
277
+ [EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(arena.dataTypes[data]) &&
278
+ (arena.flags[data] & EnumAstNodeStatus.HasChildren) != 0;
279
+ // temporary stylesheet holding the rendered copy
280
+ let sheet = 0;
281
+ if (options.withParents || expandNesting) {
282
+ // render a copy: the original tree is not modified. the copy is allocated in the same arena,
283
+ // under a temporary stylesheet node that is not attached to the tree
284
+ let parent = arena.parents[data];
285
+ sheet = arena.createStyleSheet();
286
+ data = arena.clone(data, true);
287
+ if (options.withParents) {
288
+ // wrap the node into shallow copies of its ancestors
289
+ while (parent != 0) {
290
+ const clone = arena.clone(parent, false);
291
+ arena.appendChild(clone, data);
292
+ data = clone;
293
+ parent = arena.parents[parent];
47
294
  }
48
- : {
49
- removeEmpty: false,
50
- removeComments: false,
51
- }),
52
- minifyValues: true,
53
- sourcemap: false,
54
- convertColor: true,
55
- expandNestingRules: false,
56
- preserveLicense: false,
57
- ...options,
58
- };
59
- if (options.withParents) {
60
- let parent;
61
- while (data[PARENT] != null) {
62
- // @ts-ignore
63
- // parent = { ...data[PARENT], chi: [data] };
64
- parent = cloneNode(data[PARENT]);
65
- // @ts-ignore
66
- parent.chi = [data];
67
- // @ts-ignore
68
- parent[PARENT] = data[PARENT][PARENT];
69
- // @ts-ignore
70
- data = parent;
295
+ }
296
+ arena.appendChild(sheet, data);
297
+ if (expandNesting) {
298
+ data = arena.expand(data);
71
299
  }
72
300
  }
73
301
  const startTime = performance.now();
74
302
  const errors = [];
75
303
  const sourcemap = options.sourcemap ? new SourceMap() : null;
76
- const sourcemaps = options.sourcemap ? { sources: [], maps: [] } : null;
77
- const cache = Object.create(null);
304
+ const sourcemaps = options.sourcemap
305
+ ? { sources: [], sourceIds: new Set(), maps: [] }
306
+ : null;
307
+ // identical values are rendered once. the memo is valid for these options only. a memo that is not provided is
308
+ // reused by the next renders
309
+ const ownMemo = memo == null;
310
+ // inlined: RenderMemo.acquire()
311
+ const cache = new RenderCache(memo ?? (RenderMemo.free.pop() ?? new RenderMemo()), options);
78
312
  const sourceLocation = {
79
313
  end: 0,
80
314
  };
@@ -102,29 +336,26 @@ function doRender(data, options = {}, mapping) {
102
336
  }
103
337
  const result = {
104
338
  code: code +
105
- renderAstNode(options.expandNestingRules &&
106
- [EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(data.typ) &&
107
- "chi" in data
108
- ? expand(data)
109
- : data, options, sourcemaps, sourceLocation, linesMap, errors, function reducer(acc, curr) {
110
- if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
111
- if (!options.preserveLicense || !curr.val.startsWith("/*!")) {
112
- return acc;
113
- }
114
- return acc + curr.val;
115
- }
116
- return acc + renderValue(curr, options, cache, reducer);
117
- }, cache),
339
+ renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMap, errors, createReducer(arena, options, cache), cache),
118
340
  errors,
119
341
  stats: {
120
342
  total: `${(performance.now() - startTime).toFixed(2)}ms`,
121
343
  },
122
344
  };
345
+ if (ownMemo) {
346
+ RenderMemo.release(cache.memo);
347
+ }
348
+ if (sheet != 0) {
349
+ // drop the temporary stylesheet. the tokens allocated while rendering (color conversion, gradients, ...)
350
+ // are not attached to the tree either: the memory is not reclaimed, the ids are never reused
351
+ arena.detach(data);
352
+ }
123
353
  if (sourcemap != null) {
124
354
  let source;
125
355
  for (const sourceId of sourcemaps.sources) {
126
356
  source = options.sourcesMap.get(sourceId);
127
- sourcemap.addSourceContent(source.id, source.getFileName(), source.getContent());
357
+ // inlined: source.getFileName(), source.getContent()
358
+ sourcemap.addSourceContent(source.id, source.file, source.content);
128
359
  }
129
360
  sourcemap.add(sourcemaps.maps);
130
361
  result.map = sourcemap;
@@ -136,6 +367,7 @@ function doRender(data, options = {}, mapping) {
136
367
  }
137
368
  /**
138
369
  * Update source map
370
+ * @param arena
139
371
  * @param node
140
372
  * @param options
141
373
  * @param cache
@@ -146,7 +378,7 @@ function doRender(data, options = {}, mapping) {
146
378
  *
147
379
  * @internal
148
380
  */
149
- function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, linesMap, str) {
381
+ function updateSourceMap(arena, node, options, cache, sourcemaps, sourceLocation, linesMap, str) {
150
382
  let offset = 0;
151
383
  // eat leanding whitespace
152
384
  while (offset < str.length && isWhiteSpace(str.charCodeAt(offset))) {
@@ -155,14 +387,20 @@ function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, lines
155
387
  if (offset > 0) {
156
388
  move(sourceLocation, linesMap, str, 0, offset + 1);
157
389
  }
158
- if (node[LOCSTA] != null) {
159
- const source = options.sourcesMap.get(node[LOCSRCID]);
390
+ if (arena.flags[node] & EnumAstNodeStatus.HasLocation) {
391
+ // inlined: arena.getSource(node)
392
+ let srcId = arena.sources[arena.getSourceId(node)] ?? 0;
393
+ const source = options.sourcesMap.get(srcId);
160
394
  const inputSourceMap = source.getInputSourceMap();
161
- const offsets = source.getOffsets(node[LOCSTA]);
162
- const [newLine, newColumn] = linesMap.getOffsets(sourceLocation.end);
395
+ // inlined: source.getOffsets(arena.getSpanStart(node))
396
+ const offsets = source.lineStarts.getOffsets(arena.getSpanStart(node));
397
+ // generated position, see LineMap.getOffsets()
398
+ const line = linesMap.search(sourceLocation.end);
399
+ const newLine = line + 1;
400
+ const newColumn = sourceLocation.end - linesMap.lineStarts[line] + (line == 0 ? 1 : 0);
163
401
  let records = null;
164
- let srcId = node[LOCSRCID];
165
- let sourceFileName = source.getFileName() || null;
402
+ // inlined: source.getFileName()
403
+ let sourceFileName = source.file || null;
166
404
  let sourceContent; // = (source.getContent() as string) || null;
167
405
  if (inputSourceMap != null && (records = inputSourceMap.find(offsets[0], offsets[1])) != null) {
168
406
  let newId = null;
@@ -176,23 +414,25 @@ function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, lines
176
414
  offsets[1] = record[2];
177
415
  sourceContent = record[3] || null;
178
416
  if (sourceFileName != null && options.output != null && !sourceFileName.startsWith("data:")) {
179
- if (cache[sourceFileName] == null) {
417
+ if (cache.paths[sourceFileName] == null) {
180
418
  const absolute = options.resolve(dirname(options.output), options.cwd)
181
419
  .absolute;
182
420
  const absoluteSourcePath = options.resolve(dirname(options.src || ""), options.cwd).absolute;
183
421
  // resolution is relative to the source file
184
422
  const absoluteSourceFileName = options.resolve(sourceFileName, absoluteSourcePath)
185
423
  .absolute;
186
- cache[sourceFileName] = options.resolve(absoluteSourceFileName, absolute).relative;
424
+ cache.paths[sourceFileName] = options.resolve(absoluteSourceFileName, absolute).relative;
187
425
  }
188
- sourceFileName = cache[sourceFileName];
426
+ sourceFileName = cache.paths[sourceFileName];
189
427
  }
190
428
  for (const [id, file] of options.sourcesMap.entries()) {
191
- if (file.getFileName() === sourceFileName) {
429
+ // inlined: file.getFileName()
430
+ if (file.file === sourceFileName) {
192
431
  newId = id;
193
432
  break;
194
433
  }
195
- if (sourceFileName == null && file.getContent() === sourceContent) {
434
+ // inlined: file.getContent()
435
+ if (sourceFileName == null && file.content === sourceContent) {
196
436
  newId = id;
197
437
  break;
198
438
  }
@@ -203,9 +443,7 @@ function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, lines
203
443
  newId = source.id;
204
444
  }
205
445
  srcId = newId;
206
- if (!sourcemaps.sources.includes(srcId)) {
207
- sourcemaps.sources.push(srcId);
208
- }
446
+ addSource(sourcemaps, srcId);
209
447
  sourcemaps.maps.push([newLine, newColumn, srcId, offsets[0], offsets[1]]);
210
448
  }
211
449
  }
@@ -220,9 +458,7 @@ function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, lines
220
458
  // }
221
459
  // sourceFileName = cache[sourceFileName] as string;
222
460
  // }
223
- if (!sourcemaps.sources.includes(srcId)) {
224
- sourcemaps.sources.push(srcId);
225
- }
461
+ addSource(sourcemaps, srcId);
226
462
  sourcemaps.maps.push([newLine, newColumn, srcId, offsets[0], offsets[1]]);
227
463
  }
228
464
  }
@@ -237,14 +473,12 @@ function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, lines
237
473
  * @param end
238
474
  */
239
475
  function move(sourceLocation, linesMap, str, start, end) {
476
+ const j = end ?? str.length;
240
477
  let i = start ?? 0;
241
- let j = end ?? str.length;
242
478
  let codepoint;
243
- let char;
244
479
  for (; i < j; i++) {
245
- char = str.charAt(i);
246
- codepoint = char.charCodeAt(0);
247
- sourceLocation.end += char.length;
480
+ codepoint = str.charCodeAt(i);
481
+ sourceLocation.end++;
248
482
  if (codepoint == 0xa || // \n
249
483
  codepoint == 0xb || // \v
250
484
  codepoint == 0xc || // \f
@@ -255,14 +489,16 @@ function move(sourceLocation, linesMap, str, start, end) {
255
489
  if (codepoint == 0xd && i + 1 < str.length && str.charCodeAt(i + 1) == 0xa) ;
256
490
  else if (codepoint == 0xa && i > 0 && str.charCodeAt(i - 1) == 0xd) ;
257
491
  else {
258
- linesMap.addLineStart(sourceLocation.end);
492
+ // inlined: linesMap.addLineStart(sourceLocation.end)
493
+ linesMap.lineStarts.push(sourceLocation.end);
259
494
  }
260
495
  }
261
496
  }
262
497
  }
263
498
  /**
264
499
  * render ast node
265
- * @param data
500
+ * @param arena
501
+ * @param data node id
266
502
  * @param options
267
503
  * @param sourcemaps
268
504
  * @param sourceLocation
@@ -275,7 +511,7 @@ function move(sourceLocation, linesMap, str, start, end) {
275
511
  *
276
512
  * @internal
277
513
  */
278
- function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level = 0, indents = []) {
514
+ function renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level = 0, indents = []) {
279
515
  if (indents.length < level + 1) {
280
516
  indents.push(options.indent.repeat(level));
281
517
  }
@@ -287,23 +523,22 @@ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, erro
287
523
  let str = "";
288
524
  const indent = indents[level];
289
525
  const indentSub = indents[level + 1];
290
- switch (data.typ) {
526
+ const typ = arena.dataTypes[data];
527
+ switch (typ) {
291
528
  case EnumToken.DeclarationNodeType:
292
- return `${data.nam}:${options.indent}${(options.minifyValues
293
- ? filterValues(data.val)
294
- : data.val).reduce(reducer, "")}`;
529
+ return `${arena.strings[arena.name[data]]}:${options.indent}${renderDeclarationValue(arena, data, options.minifyValues, reducer)}`;
295
530
  case EnumToken.CommentNodeType:
296
- case EnumToken.CDOCOMMNodeType:
297
- if (data.val.startsWith("/*# sourceMappingURL=")) {
531
+ case EnumToken.CDOCOMMNodeType: {
532
+ const val = arena.strings[arena.stringValue[data]];
533
+ if (val.startsWith("/*# sourceMappingURL=")) {
298
534
  // ignore sourcemap comment
299
535
  return "";
300
536
  }
301
- return !options.removeComments || (options.preserveLicense && data.val.startsWith("/*!"))
302
- ? data.val
303
- : "";
537
+ return !options.removeComments || (options.preserveLicense && val.startsWith("/*!")) ? val : "";
538
+ }
304
539
  case EnumToken.StyleSheetNodeType:
305
- for (const node of data.chi) {
306
- str = renderAstNode(node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level, indents);
540
+ for (let node = arena.firstChild[data]; node != 0; node = arena.nextSibling[node]) {
541
+ str = renderAstNode(arena, node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level, indents);
307
542
  if (str === "") {
308
543
  continue;
309
544
  }
@@ -319,43 +554,53 @@ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, erro
319
554
  case EnumToken.AtRuleNodeType:
320
555
  case EnumToken.RuleNodeType:
321
556
  case EnumToken.KeyframesRuleNodeType:
322
- case EnumToken.KeyframesAtRuleNodeType:
323
- if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !("chi" in data)) {
324
- return `${(indent.length > 0 ? options.newLine : "") + indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`;
557
+ case EnumToken.KeyframesAtRuleNodeType: {
558
+ const isAtRule = typ == EnumToken.AtRuleNodeType || typ == EnumToken.KeyframesAtRuleNodeType;
559
+ const nam = arena.strings[arena.name[data]];
560
+ const val = arena.strings[arena.stringValue[data]];
561
+ if (isAtRule && !(arena.flags[data] & EnumAstNodeStatus.HasChildren)) {
562
+ return `${(indent.length > 0 ? options.newLine : "") + indent}@${nam}${val === "" ? "" : options.indent || " "}${val};`;
325
563
  }
326
- const lineMapLength = linesMap ? linesMap.getLineStarts().length : 0;
564
+ // inlined: linesMap.getLineStarts()
565
+ const lineMapLength = linesMap ? linesMap.lineStarts.length : 0;
327
566
  const prelude = (indent.length > 0 ? options.newLine : "") +
328
567
  indent +
329
- ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ)
330
- ? `@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val}${options.indent}{`
331
- : data.sel + `${options.indent}{`);
568
+ (isAtRule
569
+ ? `@${nam}${val === "" ? "" : options.indent || " "}${val}${options.indent}{`
570
+ : nam + `${options.indent}{`);
332
571
  if (sourcemaps != null) {
333
- updateSourceMap(data, options, cache, sourcemaps, sourceLocation, linesMap, prelude);
572
+ updateSourceMap(arena, data, options, cache, sourcemaps, sourceLocation, linesMap, prelude);
334
573
  }
335
574
  let node;
336
- let recordDeclarationSourceMap = data.typ == EnumToken.AtRuleNodeType;
337
- for (let i = 0; i < data.chi.length; i++) {
338
- node = data.chi[i];
339
- if (node.typ == EnumToken.CommentNodeType) {
575
+ let nodeType;
576
+ let recordDeclarationSourceMap = typ == EnumToken.AtRuleNodeType;
577
+ // rendering of the last child, without its trailing semicolon, appended to the children when the next child
578
+ // is rendered. the trailing semicolon of the last child is dropped: it is known from the type of the child,
579
+ // the rendered strings are not read (reading a concatenated string flattens it)
580
+ let last = "";
581
+ let semicolon = false;
582
+ for (node = arena.firstChild[data]; node != 0; node = arena.nextSibling[node]) {
583
+ nodeType = arena.dataTypes[node];
584
+ if (nodeType == EnumToken.CommentNodeType) {
585
+ const comment = arena.strings[arena.stringValue[node]];
340
586
  str =
341
- options.removeComments &&
342
- (!options.preserveLicense || !node.val.startsWith("/*!"))
587
+ options.removeComments && (!options.preserveLicense || !comment.startsWith("/*!"))
343
588
  ? ""
344
- : node.val;
589
+ : comment;
345
590
  }
346
- else if (node.typ == EnumToken.DeclarationNodeType) {
347
- str = `${node.nam}:${options.indent}${(options.minifyValues
348
- ? filterValues(node.val)
349
- : node.val)
350
- .reduce(reducer, "")
351
- .trimEnd()};`;
591
+ else if (nodeType == EnumToken.DeclarationNodeType) {
592
+ // the semicolon is not part of the rendered declaration
593
+ str = renderDeclaration(arena, node, options, cache, reducer);
352
594
  }
353
595
  else {
354
- str = renderAstNode(node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level + 1, indents);
596
+ str = renderAstNode(arena, node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level + 1, indents);
355
597
  if (str === "") {
356
598
  continue;
357
599
  }
358
- children += str;
600
+ children += semicolon ? last + ";" : last;
601
+ // at-rules without a block ('@layer name;') end with a semicolon
602
+ semicolon = endsWithSemicolon(arena, node, str);
603
+ last = semicolon ? str.slice(0, -1) : str;
359
604
  str = "";
360
605
  continue;
361
606
  }
@@ -363,31 +608,45 @@ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, erro
363
608
  continue;
364
609
  }
365
610
  str = options.newLine + indentSub + str;
366
- children += str;
367
- if (sourcemaps != null && str !== "") {
368
- if (node.typ == EnumToken.DeclarationNodeType && recordDeclarationSourceMap) {
611
+ children += semicolon ? last + ";" : last;
612
+ if (nodeType == EnumToken.DeclarationNodeType) {
613
+ last = str;
614
+ semicolon = true;
615
+ }
616
+ else {
617
+ // comments are flat strings
618
+ semicolon = str.charCodeAt(str.length - 1) == 0x3b;
619
+ last = semicolon ? str.slice(0, -1) : str;
620
+ }
621
+ if (sourcemaps != null) {
622
+ if (nodeType == EnumToken.DeclarationNodeType && recordDeclarationSourceMap) {
369
623
  // if declaration is child of at-rule, then record it
370
624
  // .rule {
371
625
  // @media screen {
372
626
  // color: red;
373
627
  // }
374
628
  // }
375
- // @ts-ignore
376
- updateSourceMap(node, options, cache, sourcemaps, sourceLocation, linesMap, str);
629
+ updateSourceMap(arena, node, options, cache, sourcemaps, sourceLocation, linesMap, str);
377
630
  }
378
631
  else {
379
632
  move(sourceLocation, linesMap, str);
380
633
  }
634
+ if (nodeType == EnumToken.DeclarationNodeType) {
635
+ // the semicolon
636
+ sourceLocation.end++;
637
+ }
381
638
  }
382
639
  }
383
- if (children.endsWith(";")) {
384
- children = children.slice(0, -1);
640
+ // the semicolon that ends the last child is removed
641
+ if (semicolon) {
385
642
  sourceLocation.end--;
386
643
  }
644
+ children += last;
387
645
  if (options.removeEmpty && children === "") {
388
646
  if (sourcemaps != null) {
389
647
  sourceLocation.end -= prelude.length;
390
- linesMap.getLineStarts().length = lineMapLength;
648
+ // inlined: linesMap!.getLineStarts()
649
+ linesMap.lineStarts.length = lineMapLength;
391
650
  }
392
651
  return "";
393
652
  }
@@ -396,12 +655,275 @@ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, erro
396
655
  move(sourceLocation, linesMap, end);
397
656
  }
398
657
  return prelude + children + end;
658
+ }
399
659
  default:
400
660
  return "";
401
661
  }
402
662
  }
403
663
  /**
404
- * render ast token
664
+ * true if a rendered child node ends with a semicolon
665
+ * @param arena
666
+ * @param node child node
667
+ * @param rendered rendered child node, not empty
668
+ */
669
+ function endsWithSemicolon(arena, node, rendered) {
670
+ switch (arena.dataTypes[node]) {
671
+ case EnumToken.AtRuleNodeType:
672
+ case EnumToken.KeyframesAtRuleNodeType:
673
+ // '@rule prelude;' or '@rule prelude { ... }'
674
+ return !(arena.flags[node] & EnumAstNodeStatus.HasChildren);
675
+ case EnumToken.RuleNodeType:
676
+ case EnumToken.KeyframesRuleNodeType:
677
+ // '... }'
678
+ return false;
679
+ default:
680
+ // comments: flat strings
681
+ return rendered.charCodeAt(rendered.length - 1) == 0x3b;
682
+ }
683
+ }
684
+ function renderTokenList(tokens, reducer) {
685
+ let result = "";
686
+ for (const token of tokens) {
687
+ result = reducer(result, token);
688
+ }
689
+ return result;
690
+ }
691
+ /**
692
+ * render a declaration of a block: `name:value`, without the semicolon. identical declarations (same name, same value
693
+ * tokens) are rendered once per render
694
+ * @param arena
695
+ * @param node declaration
696
+ * @param options
697
+ * @param cache
698
+ * @param reducer
699
+ */
700
+ function renderDeclaration(arena, node, options, cache, reducer) {
701
+ const memo = getRenderMemo(cache, options);
702
+ if (memo == null) {
703
+ return `${arena.strings[arena.name[node]]}:${options.indent}${renderDeclarationValue(arena, node, options.minifyValues, reducer).trimEnd()}`;
704
+ }
705
+ // the name is part of the key
706
+ const slot = memo.declarations.list(arena, arena.firstToken[node], arena.name[node]);
707
+ // inlined: memo.declarations.get(slot)
708
+ let rendered = memo.declarations.values[slot];
709
+ if (rendered === undefined) {
710
+ rendered = `${arena.strings[arena.name[node]]}:${options.indent}${renderDeclarationValue(arena, node, options.minifyValues, reducer).trimEnd()}`;
711
+ // inlined: memo.declarations.set(slot, rendered)
712
+ memo.declarations.values[slot] = rendered;
713
+ }
714
+ return rendered;
715
+ }
716
+ /**
717
+ * render the value tokens of a declaration with the reducer. with minifyValues, the whitespace that is not needed is
718
+ * skipped like {@link filterValues} does, without copying the tokens into an array: the whitespace before
719
+ * `!important` (the token that follows `!important` is not checked) and the whitespace after a function
720
+ * @param arena
721
+ * @param node declaration
722
+ * @param minifyValues
723
+ * @param reducer
724
+ */
725
+ function renderDeclarationValue(arena, node, minifyValues, reducer) {
726
+ let rendered = "";
727
+ let token = arena.firstToken[node];
728
+ if (!minifyValues) {
729
+ for (; token != 0; token = arena.nextSibling[token]) {
730
+ rendered = reducer(rendered, token);
731
+ }
732
+ return rendered;
733
+ }
734
+ const dataTypes = arena.dataTypes;
735
+ // last kept token, rendered when the next token is kept: a whitespace before !important is dropped
736
+ let pending = 0;
737
+ let check = true;
738
+ let dropWhitespace = false;
739
+ for (; token != 0; token = arena.nextSibling[token]) {
740
+ const type = dataTypes[token];
741
+ if (dropWhitespace) {
742
+ dropWhitespace = false;
743
+ if (type == EnumToken.WhitespaceTokenType) {
744
+ continue;
745
+ }
746
+ }
747
+ if (!check) {
748
+ check = true;
749
+ }
750
+ else if (type == EnumToken.ImportantTokenType &&
751
+ pending != 0 &&
752
+ dataTypes[pending] === EnumToken.WhitespaceTokenType) {
753
+ pending = 0;
754
+ check = false;
755
+ }
756
+ else if (tokensfuncSet.has(type) &&
757
+ // color keywords and hex colors are not functions
758
+ (type != EnumToken.ColorTokenType || arena.firstToken[token] != 0) &&
759
+ type != EnumToken.WildCardFunctionTokenType &&
760
+ arena.nextSibling[token] != 0 &&
761
+ dataTypes[arena.nextSibling[token]] == EnumToken.WhitespaceTokenType) {
762
+ dropWhitespace = true;
763
+ }
764
+ if (pending != 0) {
765
+ rendered = reducer(rendered, pending);
766
+ }
767
+ pending = token;
768
+ }
769
+ return pending == 0 ? rendered : reducer(rendered, pending);
770
+ }
771
+ /**
772
+ * shared default arguments of {@link renderValue}: no object is created per call. the cache is only written when
773
+ * a source map is generated, with the cache of {@link doRender}
774
+ */
775
+ const defaultRenderOptions = {};
776
+ const defaultCache = Object.create(null);
777
+ /**
778
+ * reducer of the rendered tokens: comments are removed according to the options
779
+ * @param arena
780
+ * @param options
781
+ * @param cache
782
+ * @param errors
783
+ */
784
+ function createReducer(arena, options, cache, errors) {
785
+ const reducer = (acc, curr) => {
786
+ if (arena.dataTypes[curr] == EnumToken.CommentTokenType && options.removeComments) {
787
+ if (!options.preserveLicense || !arena.strings[arena.stringValue[curr]].startsWith("/*!")) {
788
+ return acc;
789
+ }
790
+ return acc + arena.strings[arena.stringValue[curr]];
791
+ }
792
+ return acc + renderValue(arena, curr, options, cache, reducer);
793
+ };
794
+ return reducer;
795
+ }
796
+ /**
797
+ * render the tokens of a linked token list, starting at the first token, with the reducer. without a reducer, the
798
+ * comments are handled like {@link createReducer} does: no closure is created
799
+ * @param arena
800
+ * @param first first token of the list
801
+ * @param options
802
+ * @param cache
803
+ * @param reducer
804
+ * @param errors
805
+ */
806
+ function renderReduced(arena, first, options, cache, reducer, errors) {
807
+ let result = "";
808
+ for (let token = first; token != 0; token = arena.nextSibling[token]) {
809
+ if (reducer != null) {
810
+ result = reducer(result, token);
811
+ }
812
+ else if (arena.dataTypes[token] == EnumToken.CommentTokenType && options.removeComments) {
813
+ if (options.preserveLicense && arena.strings[arena.stringValue[token]].startsWith("/*!")) {
814
+ result += arena.strings[arena.stringValue[token]];
815
+ }
816
+ }
817
+ else {
818
+ result += renderValue(arena, token, options, cache, null);
819
+ }
820
+ }
821
+ return result;
822
+ }
823
+ /**
824
+ * render the tokens of a linked token list, starting at the first token (no array, no closure)
825
+ * @param arena
826
+ * @param first first token of the list
827
+ * @param options
828
+ * @param cache
829
+ * @param reducer
830
+ * @param errors
831
+ */
832
+ function renderList(arena, first, options, cache, reducer, errors) {
833
+ let result = "";
834
+ for (let token = first; token != 0; token = arena.nextSibling[token]) {
835
+ result += renderValue(arena, token, options, cache, reducer);
836
+ }
837
+ return result;
838
+ }
839
+ /**
840
+ * render the tokens of a field of a structured token, see {@link renderList}
841
+ * @param arena
842
+ * @param token
843
+ * @param field
844
+ * @param options
845
+ * @param cache
846
+ * @param reducer
847
+ * @param errors
848
+ */
849
+ function renderField(arena, token, field, options, cache, reducer, errors) {
850
+ const list = arena.getChildAt(token, field);
851
+ return list == null ? "" : renderList(arena, arena.firstToken[list], options, cache, reducer);
852
+ }
853
+ /**
854
+ * render a color token
855
+ * @param arena
856
+ * @param token
857
+ * @param options
858
+ * @param cache
859
+ * @param reducer
860
+ * @param errors
861
+ */
862
+ function renderColor(arena, token, options, cache, reducer, errors) {
863
+ if (options.convertColor !== false) {
864
+ const value = convertColor(arena, token, typeof options.convertColor === "boolean"
865
+ ? ColorType.HEX
866
+ : (ColorType[ColorType[options.convertColor ?? "HEX"]
867
+ ?.toUpperCase?.()
868
+ .replaceAll?.("-", "_")] ?? ColorType.HEX));
869
+ if (value != null) {
870
+ token = value;
871
+ }
872
+ if (arena.colorType[token] == ColorType.HEX || arena.colorType[token] == ColorType.LIT) {
873
+ return reduceHexValue(arena.strings[arena.stringValue[token]]);
874
+ }
875
+ }
876
+ if (arena.colorType[token] === ColorType.SYS ||
877
+ arena.colorType[token] === ColorType.DPSYS ||
878
+ arena.colorType[token] === ColorType.NON_STD) {
879
+ return arena.strings[arena.stringValue[token]];
880
+ }
881
+ if (arena.colorType[token] == ColorType.HEX || arena.colorType[token] == ColorType.LIT) {
882
+ return arena.strings[arena.stringValue[token]];
883
+ }
884
+ if (arena.firstToken[token] == 0) {
885
+ // rendered like a function (no argument)
886
+ return ((arena.strings[arena.stringValue[token]] ?? "") +
887
+ "(" +
888
+ renderReduced(arena, arena.firstToken[token], options, cache, reducer) +
889
+ ")");
890
+ }
891
+ const fnName = arena.strings[arena.stringValue[token]];
892
+ const count = arena.tokenCount(token);
893
+ const isLegacy = legacyColorFunctions.has(fnName);
894
+ const hasAlpha = alphaColorFunctions.has(fnName);
895
+ const useAlpha = (hasAlpha && count == (isLegacy ? 7 : 4)) || ("color" == fnName && count == 5);
896
+ let result = "";
897
+ let index = 0;
898
+ for (let curr = arena.firstToken[token]; curr != 0; curr = arena.nextSibling[curr], index++) {
899
+ if (trailingSeparator.test(result)) {
900
+ if (arena.dataTypes[curr] == EnumToken.WhitespaceTokenType) {
901
+ result = result.trimEnd();
902
+ continue;
903
+ }
904
+ result = result.trimStart() + renderValue(arena, curr, options, cache);
905
+ continue;
906
+ }
907
+ if (isLegacy && arena.dataTypes[curr] == EnumToken.CommaTokenType) {
908
+ result = result.trimEnd() + " ";
909
+ }
910
+ else if (arena.dataTypes[curr] == EnumToken.WhitespaceTokenType) {
911
+ result = result.trimEnd() + " ";
912
+ }
913
+ else if (arena.dataTypes[curr] == EnumToken.CommaTokenType ||
914
+ (arena.dataTypes[curr] == EnumToken.LiteralTokenType && arena.strings[arena.stringValue[curr]] == "/")) {
915
+ result = result.trimEnd() + (arena.dataTypes[curr] == EnumToken.CommaTokenType ? "," : "/");
916
+ }
917
+ else {
918
+ result =
919
+ result.trimEnd() + (useAlpha && index == count - 1 ? "/" : " ") + renderValue(arena, curr, options, cache);
920
+ }
921
+ }
922
+ return (hasAlpha && fnName.endsWith("a") ? fnName.slice(0, -1) : fnName) + "(" + result.trimStart() + ")";
923
+ }
924
+ /**
925
+ * render ast token. identical constructs (colors and tokens with sub tokens: functions, parentheses, binary
926
+ * expressions, ...) are rendered once per render, see {@link RenderMemo}
405
927
  * @param token
406
928
  * @param options
407
929
  * @param cache
@@ -409,25 +931,44 @@ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, erro
409
931
  * @param errors
410
932
  * @private
411
933
  */
412
- function renderValue(token, options = {}, cache = Object.create(null), reducer, errors) {
413
- if (token.typ === EnumToken.WhenElseFunctionTokenType &&
414
- equalsIgnoreCase(token.val, "supports")) {
934
+ function renderValue(arena, token, options = defaultRenderOptions, cache = defaultCache, reducer, errors) {
935
+ if (arena.dataTypes[token] == EnumToken.ColorTokenType ||
936
+ arena.firstToken[token] != 0 ||
937
+ arena.firstChild[token] != 0) {
938
+ const memo = getRenderMemo(cache, options);
939
+ if (memo != null) {
940
+ const slot = memo.constructs.token(arena, token);
941
+ // inlined: memo.constructs.get(slot)
942
+ let rendered = memo.constructs.values[slot];
943
+ if (rendered === undefined) {
944
+ rendered = renderToken(arena, token, options, cache, reducer);
945
+ // inlined: memo.constructs.set(slot, rendered)
946
+ memo.constructs.values[slot] = rendered;
947
+ }
948
+ return rendered;
949
+ }
950
+ }
951
+ return renderToken(arena, token, options, cache, reducer);
952
+ }
953
+ /**
954
+ * render ast token, see {@link renderValue}
955
+ * @param arena
956
+ * @param token
957
+ * @param options
958
+ * @param cache
959
+ * @param reducer
960
+ * @param errors
961
+ */
962
+ function renderToken(arena, token, options, cache, reducer, errors) {
963
+ if (arena.dataTypes[token] === EnumToken.WhenElseFunctionTokenType &&
964
+ equalsIgnoreCase(arena.strings[arena.stringValue[token]], "supports")) {
415
965
  options = { ...options, minifyValues: false, minify: false, convertColor: false };
416
966
  reducer = null;
417
967
  }
418
968
  options.minifyValues ??= options.minify;
419
- if (reducer == null) {
420
- reducer = function (acc, curr) {
421
- if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
422
- if (!options.preserveLicense || !curr.val.startsWith("/*!")) {
423
- return acc;
424
- }
425
- return acc + curr.val;
426
- }
427
- return acc + renderValue(curr, options, cache, reducer);
428
- };
429
- }
430
- switch (token.typ) {
969
+ // without a reducer, the default reducer (see createReducer()) is used: it is created only when the rendering of
970
+ // a child requires it with other options. the children rendered with the same options create their own
971
+ switch (arena.dataTypes[token]) {
431
972
  case EnumToken.UrlFunctionTokenDefType:
432
973
  case EnumToken.FunctionTokenDefType:
433
974
  case EnumToken.MathFunctionTokenDefType:
@@ -444,42 +985,52 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
444
985
  case EnumToken.GeneralEnclosedFunctionTokenDefType:
445
986
  case EnumToken.CustomFunctionTokenDefType:
446
987
  case EnumToken.WildCardFunctionTokenDefType:
447
- return token.val + "(";
988
+ return arena.strings[arena.stringValue[token]] + "(";
448
989
  case EnumToken.ListToken:
449
- return token.chi.reduce((acc, curr) => acc + renderValue(curr, options, cache), "");
450
- case EnumToken.BinaryExpressionTokenType:
451
- if (EnumToken.Mul === token.op ||
452
- EnumToken.Div === token.op) {
990
+ return renderList(arena, arena.firstToken[token], options, cache);
991
+ case EnumToken.BinaryExpressionTokenType: {
992
+ const op = arena.numericValue[token];
993
+ // inlined: arena.getLeft(token)
994
+ const l = (arena.getChildAt(token, TokenField.L) == null ? null : arena.fieldTokens(token, TokenField.L))[0];
995
+ // inlined: arena.getRight(token)
996
+ const r = (arena.getChildAt(token, TokenField.R) == null ? null : arena.fieldTokens(token, TokenField.R))[0];
997
+ if (EnumToken.Mul === op || EnumToken.Div === op) {
453
998
  let result = "";
454
- if (token.l.typ == EnumToken.BinaryExpressionTokenType &&
455
- [EnumToken.Add, EnumToken.Sub].includes(token.l.op)) {
456
- result = "(" + renderValue(token.l, options, cache) + ")";
999
+ if (arena.dataTypes[l] == EnumToken.BinaryExpressionTokenType &&
1000
+ (arena.numericValue[l] == EnumToken.Add || arena.numericValue[l] == EnumToken.Sub)) {
1001
+ result = "(" + renderValue(arena, l, options, cache) + ")";
457
1002
  }
458
1003
  else {
459
- result = renderValue(token.l, options, cache);
1004
+ result = renderValue(arena, l, options, cache);
460
1005
  }
461
- result += token.op == EnumToken.Mul ? "*" : "/";
462
- if (token.r.typ == EnumToken.BinaryExpressionTokenType &&
463
- [EnumToken.Add, EnumToken.Sub].includes(token.r.op)) {
464
- result += "(" + renderValue(token.r, options, cache) + ")";
1006
+ result += op == EnumToken.Mul ? "*" : "/";
1007
+ if (arena.dataTypes[r] == EnumToken.BinaryExpressionTokenType &&
1008
+ (arena.numericValue[r] == EnumToken.Add || arena.numericValue[r] == EnumToken.Sub)) {
1009
+ result += "(" + renderValue(arena, r, options, cache) + ")";
465
1010
  }
466
1011
  else {
467
- result += renderValue(token.r, options, cache);
1012
+ result += renderValue(arena, r, options, cache);
468
1013
  }
469
1014
  return result;
470
1015
  }
471
- return (renderValue(token.l, options, cache) +
472
- (token.op == EnumToken.Add ? " + " : " - ") +
473
- renderValue(token.r, options, cache));
474
- case EnumToken.FractionTokenType:
475
- const fraction = renderValue(token.l) + "/" + renderValue(token.r);
476
- if (+token.r.val != 0) {
477
- const value = minifyNumber(+token.l.val / +token.r.val);
1016
+ return (renderValue(arena, l, options, cache) +
1017
+ (op == EnumToken.Add ? " + " : " - ") +
1018
+ renderValue(arena, r, options, cache));
1019
+ }
1020
+ case EnumToken.FractionTokenType: {
1021
+ // inlined: arena.numerator(token)
1022
+ const numerator = arena.firstChild[token];
1023
+ // inlined: arena.denominator(token)
1024
+ const denominator = arena.lastChild[token];
1025
+ const fraction = renderValue(arena, numerator) + "/" + renderValue(arena, denominator);
1026
+ if (arena.numericValue[denominator] != 0) {
1027
+ const value = minifyNumber(arena.numericValue[numerator] / arena.numericValue[denominator]);
478
1028
  if (value.length <= fraction.length) {
479
1029
  return value;
480
1030
  }
481
1031
  }
482
1032
  return fraction;
1033
+ }
483
1034
  case EnumToken.Add:
484
1035
  return " + ";
485
1036
  case EnumToken.Sub:
@@ -491,99 +1042,35 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
491
1042
  case EnumToken.Div:
492
1043
  return "/";
493
1044
  case EnumToken.WrappedValuesTokenType:
494
- return ("{" +
495
- token.chi.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
496
- "}");
497
- case EnumToken.ColorTokenType:
498
- if (token.kin == ColorType.LIGHT_DARK ||
499
- ("chi" in token && options.convertColor === false && token.chi.length == 2)) {
500
- return (token.val +
501
- "(" +
502
- token.chi.reduce((acc, curr) => {
503
- if (curr.typ === EnumToken.IdenTokenType) {
504
- parseColor(curr);
505
- }
506
- return acc + renderValue(curr, options, cache);
507
- }, "") +
508
- ")");
509
- }
510
- if (options.convertColor !== false) {
511
- const value = convertColor(token, typeof options.convertColor === "boolean"
512
- ? ColorType.HEX
513
- : (ColorType[ColorType[options.convertColor ?? "HEX"]
514
- ?.toUpperCase?.()
515
- .replaceAll?.("-", "_")] ?? ColorType.HEX));
516
- if (value != null) {
517
- token = value;
518
- }
519
- if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
520
- return reduceHexValue(token.val);
1045
+ return "{" + renderList(arena, arena.firstToken[token], options, cache) + "}";
1046
+ case EnumToken.ColorTokenType: {
1047
+ if (arena.colorType[token] == ColorType.LIGHT_DARK ||
1048
+ (arena.firstToken[token] != 0 && options.convertColor === false && arena.tokenCount(token) == 2)) {
1049
+ let result = "";
1050
+ for (let curr = arena.firstToken[token]; curr != 0; curr = arena.nextSibling[curr]) {
1051
+ if (arena.dataTypes[curr] === EnumToken.IdenTokenType) {
1052
+ // the token is updated in place
1053
+ parseColor(arena, curr);
1054
+ }
1055
+ result += renderValue(arena, curr, options, cache);
521
1056
  }
1057
+ return arena.strings[arena.stringValue[token]] + "(" + result + ")";
522
1058
  }
523
- if (token.kin === ColorType.SYS ||
524
- token.kin === ColorType.DPSYS ||
525
- token.kin === ColorType.NON_STD) {
526
- return token.val;
527
- }
528
- if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
529
- return token.val;
530
- }
531
- if (Array.isArray(token.chi)) {
532
- const fnName = token.val;
533
- const isLegacy = ["rgb", "rgba", "hsl", "hsla"].includes(token.val);
534
- const hasAlpha = [
535
- "rgb",
536
- "rgba",
537
- "hsl",
538
- "hsla",
539
- "hwb",
540
- "oklab",
541
- "oklch",
542
- "lab",
543
- "lch",
544
- ].includes(fnName);
545
- const useAlpha = (hasAlpha && token.chi.length == (isLegacy ? 7 : 4)) ||
546
- ("color" == token.val && token.chi.length == 5);
547
- return ((hasAlpha && token.val.endsWith("a") ? fnName.slice(0, -1) : fnName) +
548
- "(" +
549
- token
550
- .chi.reduce((acc, curr, index, array) => {
551
- if (/[,/]\s*$/.test(acc)) {
552
- if (curr.typ == EnumToken.WhitespaceTokenType) {
553
- return acc.trimEnd();
554
- }
555
- return acc.trimStart() + renderValue(curr, options, cache);
556
- }
557
- if (isLegacy && curr.typ == EnumToken.CommaTokenType) {
558
- return acc.trimEnd() + " ";
559
- }
560
- if (curr.typ == EnumToken.WhitespaceTokenType) {
561
- return acc.trimEnd() + " ";
562
- }
563
- if (curr.typ == EnumToken.CommaTokenType ||
564
- (curr.typ == EnumToken.LiteralTokenType && curr.val == "/")) {
565
- return acc.trimEnd() + (curr.typ == EnumToken.CommaTokenType ? "," : "/");
566
- }
567
- return (acc.trimEnd() +
568
- (useAlpha && index == array.length - 1 ? "/" : " ") +
569
- renderValue(curr, options, cache));
570
- }, "")
571
- .trimStart() +
572
- ")");
573
- }
1059
+ return renderColor(arena, token, options, cache, reducer);
1060
+ }
574
1061
  case EnumToken.UrlFunctionTokenType:
575
- if (options.minifyValues && token.typ === EnumToken.UrlFunctionTokenType) {
576
- if (token.chi[0]?.typ === EnumToken.BadUrlTokenType) {
1062
+ if (options.minifyValues && arena.dataTypes[token] === EnumToken.UrlFunctionTokenType) {
1063
+ // inlined: arena.typeOf(arena.firstToken[token])
1064
+ if (arena.dataTypes[arena.firstToken[token]] === EnumToken.BadUrlTokenType) {
577
1065
  return "url()";
578
1066
  }
579
- for (const child of token.chi) {
580
- if (child.typ === EnumToken.StringTokenType) {
581
- if (child.val.slice(1, 5) !== "data:" &&
582
- urlTokenMatcher.test(child.val)) {
583
- Object.assign(child, {
584
- typ: EnumToken.UrlTokenTokenType,
585
- val: child.val.slice(1, -1),
586
- });
1067
+ for (let child = arena.firstToken[token]; child != 0; child = arena.nextSibling[child]) {
1068
+ if (arena.dataTypes[child] === EnumToken.StringTokenType) {
1069
+ const val = arena.strings[arena.stringValue[child]];
1070
+ if (val.slice(1, 5) !== "data:" && urlTokenMatcher.test(val)) {
1071
+ arena.dataTypes[child] = EnumToken.UrlTokenTokenType;
1072
+ // inlined: arena.setStr(child, val.slice(1, -1))
1073
+ arena.stringValue[child] = arena.internString(val.slice(1, -1));
587
1074
  }
588
1075
  break;
589
1076
  }
@@ -593,9 +1080,9 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
593
1080
  case EnumToken.FunctionTokenType:
594
1081
  case EnumToken.MathFunctionTokenType:
595
1082
  case EnumToken.ImageFunctionTokenType:
596
- if (options.minifyValues && token.typ === EnumToken.ImageFunctionTokenType) {
597
- const slice = token.chi.slice();
598
- switch (token.val) {
1083
+ if (options.minifyValues && arena.dataTypes[token] === EnumToken.ImageFunctionTokenType) {
1084
+ const slice = arena.tokens(token);
1085
+ switch (arena.strings[arena.stringValue[token]]) {
599
1086
  case "linear-gradient":
600
1087
  case "repeating-linear-gradient":
601
1088
  {
@@ -604,43 +1091,37 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
604
1091
  let j;
605
1092
  let key;
606
1093
  for (i = 0; i < slice.length; i++) {
607
- if (slice[i].typ == EnumToken.ColorTokenType ||
608
- slice[i].typ === EnumToken.CommaTokenType) {
1094
+ if (arena.dataTypes[slice[i]] == EnumToken.ColorTokenType ||
1095
+ arena.dataTypes[slice[i]] === EnumToken.CommaTokenType) {
609
1096
  break;
610
1097
  }
611
- if (slice[i].typ == EnumToken.IdenTokenType &&
612
- equalsIgnoreCase(slice[i].val, "to")) {
1098
+ if (arena.dataTypes[slice[i]] == EnumToken.IdenTokenType &&
1099
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "to")) {
613
1100
  k = i + 1;
614
1101
  while (k < slice.length &&
615
- (slice[k].typ === EnumToken.WhitespaceTokenType ||
616
- slice[k].typ === EnumToken.CommentTokenType)) {
1102
+ (arena.dataTypes[slice[k]] === EnumToken.WhitespaceTokenType ||
1103
+ arena.dataTypes[slice[k]] === EnumToken.CommentTokenType)) {
617
1104
  k++;
618
1105
  }
619
- if (k < slice.length && slice[k].typ === EnumToken.IdenTokenType) {
1106
+ if (k < slice.length && arena.dataTypes[slice[k]] === EnumToken.IdenTokenType) {
620
1107
  // convert keyword to angle
621
- key = slice[k].val.toLowerCase();
1108
+ key = arena.strings[arena.stringValue[slice[k]]].toLowerCase();
622
1109
  switch (key) {
623
1110
  case "right":
624
- slice.splice(i, k - i + 1, {
625
- typ: EnumToken.AngleTokenType,
626
- val: 90,
627
- });
1111
+ slice.splice(i, k - i + 1, arena.createToken(EnumToken.AngleTokenType, 90, "deg"));
628
1112
  break;
629
1113
  case "left":
630
- slice.splice(i, k - i + 1, {
631
- typ: EnumToken.AngleTokenType,
632
- val: 270,
633
- });
1114
+ slice.splice(i, k - i + 1, arena.createToken(EnumToken.AngleTokenType, 270, "deg"));
634
1115
  break;
635
1116
  case "top":
636
1117
  case "bottom":
637
1118
  j = k + 1;
638
1119
  while (j < slice.length &&
639
- (slice[j].typ === EnumToken.WhitespaceTokenType ||
640
- slice[j].typ === EnumToken.CommentTokenType)) {
1120
+ (arena.dataTypes[slice[j]] === EnumToken.WhitespaceTokenType ||
1121
+ arena.dataTypes[slice[j]] === EnumToken.CommentTokenType)) {
641
1122
  j++;
642
1123
  }
643
- if (j < slice.length && slice[j].typ !== EnumToken.CommaTokenType) {
1124
+ if (j < slice.length && arena.dataTypes[slice[j]] !== EnumToken.CommaTokenType) {
644
1125
  i = j - 1;
645
1126
  break;
646
1127
  }
@@ -648,44 +1129,41 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
648
1129
  if (key === "bottom") {
649
1130
  // to bottom -> 180deg is the default angle and will be removed
650
1131
  while (k < slice.length &&
651
- slice[k].typ != EnumToken.CommaTokenType) {
1132
+ arena.dataTypes[slice[k]] != EnumToken.CommaTokenType) {
652
1133
  k++;
653
1134
  }
654
1135
  slice.splice(i, k - i + 1);
655
1136
  i--;
656
1137
  continue;
657
1138
  }
658
- slice.splice(i, k - i + 1, {
659
- typ: EnumToken.AngleTokenType,
660
- val: key === "top" ? 0 : 180,
661
- });
1139
+ slice.splice(i, k - i + 1, arena.createToken(EnumToken.AngleTokenType, key === "top" ? 0 : 180, "deg"));
662
1140
  i--;
663
1141
  break;
664
1142
  }
665
1143
  }
666
1144
  }
667
- else if (slice[i].typ == EnumToken.AngleTokenType) {
668
- if ((180 - toDegrees(slice[i]).val) % 360 === 0) {
1145
+ else if (arena.dataTypes[slice[i]] == EnumToken.AngleTokenType) {
1146
+ if ((180 - arena.numericValue[toDegrees(arena, slice[i])]) % 360 === 0) {
669
1147
  k = i + 1;
670
- // while (k < slice.length && slice[k].typ !== EnumToken.CommaTokenType) {
1148
+ // while (k < slice.length && arena.dataTypes[slice[k]] !== EnumToken.CommaTokenType) {
671
1149
  // k++;
672
1150
  // }
673
1151
  slice.splice(i, k - i + 1);
674
1152
  }
675
1153
  }
676
1154
  }
677
- if (slice[i]?.typ === EnumToken.CommaTokenType) {
1155
+ if (arena.dataTypes[slice[i]] === EnumToken.CommaTokenType) {
678
1156
  i++;
679
1157
  // while (
680
1158
  // i < slice.length &&
681
- // (slice[i].typ === EnumToken.WhitespaceTokenType ||
682
- // slice[i].typ === EnumToken.CommentTokenType)
683
- // ) {
1159
+ // (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1160
+ // arena.dataTypes[slice[i]] === EnumToken.CommentTokenType)
1161
+ //) {
684
1162
  // i++;
685
1163
  // }
686
1164
  }
687
- if (slice[i]?.typ === EnumToken.ColorTokenType) {
688
- for (const token of reduceColorStops(slice.splice(i, slice.length - i))) {
1165
+ if (arena.dataTypes[slice[i]] === EnumToken.ColorTokenType) {
1166
+ for (const token of reduceColorStops(arena, slice.splice(i, slice.length - i))) {
689
1167
  slice.push(token);
690
1168
  }
691
1169
  }
@@ -697,80 +1175,80 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
697
1175
  let i = 0;
698
1176
  // while (
699
1177
  // i < slice.length &&
700
- // (slice[i].typ === EnumToken.WhitespaceTokenType ||
701
- // slice[i].typ === EnumToken.CommentTokenType)
702
- // ) {
1178
+ // (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1179
+ // arena.dataTypes[slice[i]] === EnumToken.CommentTokenType)
1180
+ //) {
703
1181
  // i++;
704
1182
  // }
705
1183
  const positions = [];
706
1184
  const form = [];
707
1185
  const size = [];
708
1186
  const colorSpaceDef = [];
709
- if (slice[i]?.typ === EnumToken.IdenTokenType) {
710
- if (equalsIgnoreCase(slice[i].val, "farthest-corner") ||
711
- equalsIgnoreCase(slice[i].val, "ellipse")) {
1187
+ if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType) {
1188
+ if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "farthest-corner") ||
1189
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "ellipse")) {
712
1190
  i++;
713
1191
  }
714
- else if (equalsIgnoreCase(slice[i].val, "closest-side") ||
715
- equalsIgnoreCase(slice[i].val, "closest-corner") ||
716
- equalsIgnoreCase(slice[i].val, "farthest-side")) {
1192
+ else if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "closest-side") ||
1193
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "closest-corner") ||
1194
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "farthest-side")) {
717
1195
  size.push(slice[i++]);
718
1196
  }
719
- else if (equalsIgnoreCase(slice[i].val, "circle")) {
1197
+ else if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "circle")) {
720
1198
  form.push(slice[i++]);
721
1199
  }
722
- while (slice[i]?.typ === EnumToken.WhitespaceTokenType ||
723
- slice[i]?.typ === EnumToken.CommentTokenType) {
1200
+ while (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1201
+ arena.dataTypes[slice[i]] === EnumToken.CommentTokenType) {
724
1202
  i++;
725
1203
  }
726
1204
  }
727
- if (slice[i]?.typ === EnumToken.IdenTokenType) {
728
- if (equalsIgnoreCase(slice[i].val, "farthest-corner") ||
729
- equalsIgnoreCase(slice[i].val, "ellipse")) {
1205
+ if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType) {
1206
+ if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "farthest-corner") ||
1207
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "ellipse")) {
730
1208
  i++;
731
1209
  }
732
- else if (equalsIgnoreCase(slice[i].val, "closest-side") ||
733
- equalsIgnoreCase(slice[i].val, "closest-corner") ||
734
- equalsIgnoreCase(slice[i].val, "farthest-side")) {
1210
+ else if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "closest-side") ||
1211
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "closest-corner") ||
1212
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "farthest-side")) {
735
1213
  size.push(slice[i++]);
736
1214
  }
737
- else if (equalsIgnoreCase(slice[i].val, "circle")) {
1215
+ else if (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "circle")) {
738
1216
  form.push(slice[i++]);
739
1217
  }
740
- while (slice[i]?.typ === EnumToken.WhitespaceTokenType ||
741
- slice[i]?.typ === EnumToken.CommentTokenType) {
1218
+ while (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1219
+ arena.dataTypes[slice[i]] === EnumToken.CommentTokenType) {
742
1220
  i++;
743
1221
  }
744
1222
  }
745
- if (slice[i]?.typ === EnumToken.IdenTokenType &&
746
- equalsIgnoreCase(slice[i].val, "at")) {
1223
+ if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1224
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "at")) {
747
1225
  i++;
748
1226
  }
749
- while (slice[i]?.typ === EnumToken.WhitespaceTokenType ||
750
- slice[i]?.typ === EnumToken.CommentTokenType) {
1227
+ while (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1228
+ arena.dataTypes[slice[i]] === EnumToken.CommentTokenType) {
751
1229
  i++;
752
1230
  }
753
- if (slice[i]?.typ === EnumToken.PercentageTokenType ||
754
- (slice[i]?.typ === EnumToken.NumberTokenType && 0 === slice[i].val) ||
755
- (slice[i]?.typ === EnumToken.IdenTokenType &&
756
- (equalsIgnoreCase(slice[i].val, "center") ||
757
- equalsIgnoreCase(slice[i].val, "top") ||
758
- equalsIgnoreCase(slice[i].val, "bottom") ||
759
- equalsIgnoreCase(slice[i].val, "left") ||
760
- equalsIgnoreCase(slice[i].val, "right")))) {
1231
+ if (arena.dataTypes[slice[i]] === EnumToken.PercentageTokenType ||
1232
+ (arena.dataTypes[slice[i]] === EnumToken.NumberTokenType && 0 === arena.numericValue[slice[i]]) ||
1233
+ (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1234
+ (equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "center") ||
1235
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "top") ||
1236
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "bottom") ||
1237
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "left") ||
1238
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "right")))) {
761
1239
  do {
762
- if (slice[i]?.typ === EnumToken.IdenTokenType &&
763
- "in" === slice[i].val) {
1240
+ if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1241
+ "in" === arena.strings[arena.stringValue[slice[i]]]) {
764
1242
  break;
765
1243
  }
766
1244
  positions.push(slice[i]);
767
1245
  i++;
768
- } while (slice[i]?.typ !== EnumToken.CommaTokenType);
1246
+ } while (i < slice.length && arena.dataTypes[slice[i]] !== EnumToken.CommaTokenType);
769
1247
  }
770
- while (slice[i]?.typ !== EnumToken.CommaTokenType) {
1248
+ while (i < slice.length && arena.dataTypes[slice[i]] !== EnumToken.CommaTokenType) {
771
1249
  colorSpaceDef.push(slice[i++]);
772
1250
  }
773
- if (slice[i]?.typ === EnumToken.CommaTokenType) {
1251
+ if (arena.dataTypes[slice[i]] === EnumToken.CommaTokenType) {
774
1252
  i++;
775
1253
  }
776
1254
  if (positions.length > 0) {
@@ -778,21 +1256,21 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
778
1256
  let position1 = "";
779
1257
  let position2 = "";
780
1258
  for (k = 0; k < positions.length; k++) {
781
- if (positions[k].typ === EnumToken.IdenTokenType) {
1259
+ if (arena.dataTypes[positions[k]] === EnumToken.IdenTokenType) {
782
1260
  if (position1.length === 0) {
783
- position1 = positions[k].val;
1261
+ position1 = arena.strings[arena.stringValue[positions[k]]];
784
1262
  }
785
1263
  else {
786
- position2 = positions[k].val;
1264
+ position2 = arena.strings[arena.stringValue[positions[k]]];
787
1265
  }
788
1266
  }
789
- else if (positions[k].typ === EnumToken.PercentageTokenType ||
790
- positions[k].typ === EnumToken.NumberTokenType) {
1267
+ else if (arena.dataTypes[positions[k]] === EnumToken.PercentageTokenType ||
1268
+ arena.dataTypes[positions[k]] === EnumToken.NumberTokenType) {
791
1269
  if (position1.length === 0) {
792
- position1 = positions[k].val + "%";
1270
+ position1 = arena.numericValue[positions[k]] + "%";
793
1271
  }
794
1272
  else {
795
- position2 = positions[k].val + "%";
1273
+ position2 = arena.numericValue[positions[k]] + "%";
796
1274
  }
797
1275
  }
798
1276
  if (position2.length > 0) {
@@ -811,65 +1289,41 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
811
1289
  key === "0 0" ||
812
1290
  key === "top left" ||
813
1291
  key === "left top") {
814
- positions.splice(0, positions.length, {
815
- typ: EnumToken.PercentageTokenType,
816
- val: 0,
817
- });
1292
+ positions.splice(0, positions.length, arena.createToken(EnumToken.PercentageTokenType, 0));
818
1293
  }
819
1294
  else if (key === "50% 0%" ||
820
1295
  key === "50% 0" ||
821
1296
  key === "center top" ||
822
1297
  key === "top center") {
823
- positions.splice(0, positions.length, {
824
- typ: EnumToken.IdenTokenType,
825
- val: "top",
826
- });
1298
+ positions.splice(0, positions.length, arena.createToken(EnumToken.IdenTokenType, "top"));
827
1299
  }
828
1300
  else if (key === "top right" || key === "right top") {
829
- positions.splice(0, positions.length, {
830
- typ: EnumToken.PercentageTokenType,
831
- val: 100,
832
- }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.PercentageTokenType, val: 0 });
1301
+ positions.splice(0, positions.length, arena.createToken(EnumToken.PercentageTokenType, 100), arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.PercentageTokenType, 0));
833
1302
  }
834
1303
  else if (key === "0 50%" ||
835
1304
  key === "0% 50%" ||
836
1305
  key === "left center" ||
837
1306
  key === "center left") {
838
- positions.splice(0, positions.length, {
839
- typ: EnumToken.IdenTokenType,
840
- val: "left",
841
- });
1307
+ positions.splice(0, positions.length, arena.createToken(EnumToken.IdenTokenType, "left"));
842
1308
  }
843
1309
  else if (key === "100% 50%" || key === "right center" || key === "center right") {
844
- positions.splice(0, positions.length, {
845
- typ: EnumToken.IdenTokenType,
846
- val: "right",
847
- });
1310
+ positions.splice(0, positions.length, arena.createToken(EnumToken.IdenTokenType, "right"));
848
1311
  }
849
1312
  else if (key === "0 100%" ||
850
1313
  key === "0% 100%" ||
851
1314
  key === "left bottom" ||
852
1315
  key === "bottom left") {
853
- positions.splice(0, positions.length, {
854
- typ: EnumToken.IdenTokenType,
855
- val: "bottom",
856
- });
1316
+ positions.splice(0, positions.length, arena.createToken(EnumToken.IdenTokenType, "bottom"));
857
1317
  }
858
1318
  else if (key === "50% 100%" ||
859
1319
  key === "center bottom" ||
860
1320
  key === "bottom center") {
861
- positions.splice(0, positions.length, {
862
- typ: EnumToken.IdenTokenType,
863
- val: "bottom",
864
- });
1321
+ positions.splice(0, positions.length, arena.createToken(EnumToken.IdenTokenType, "bottom"));
865
1322
  }
866
1323
  else if (key === "100% 100%" ||
867
1324
  key === "right bottom" ||
868
1325
  key === "bottom right") {
869
- positions.splice(0, positions.length, {
870
- typ: EnumToken.PercentageTokenType,
871
- val: 100,
872
- });
1326
+ positions.splice(0, positions.length, arena.createToken(EnumToken.PercentageTokenType, 100));
873
1327
  }
874
1328
  else if (position1 === position2) {
875
1329
  positions.splice(0, positions.length, positions[k]);
@@ -884,7 +1338,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
884
1338
  }
885
1339
  if (size.length > 0) {
886
1340
  if (result.length > 0) {
887
- result.push({ typ: EnumToken.WhitespaceTokenType });
1341
+ result.push(arena.createToken(EnumToken.WhitespaceTokenType));
888
1342
  }
889
1343
  for (const token of size) {
890
1344
  result.push(token);
@@ -892,25 +1346,25 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
892
1346
  }
893
1347
  if (positions.length > 0) {
894
1348
  if (result.length > 0) {
895
- result.push({ typ: EnumToken.WhitespaceTokenType });
1349
+ result.push(arena.createToken(EnumToken.WhitespaceTokenType));
896
1350
  }
897
- result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType });
1351
+ result.push(arena.createToken(EnumToken.IdenTokenType, "at"), arena.createToken(EnumToken.WhitespaceTokenType));
898
1352
  for (const token of positions) {
899
1353
  result.push(token);
900
1354
  }
901
1355
  }
902
1356
  if (colorSpaceDef.length > 0) {
903
1357
  if (result.length > 0) {
904
- result.push({ typ: EnumToken.WhitespaceTokenType });
1358
+ result.push(arena.createToken(EnumToken.WhitespaceTokenType));
905
1359
  }
906
1360
  for (const token of colorSpaceDef) {
907
1361
  result.push(token);
908
1362
  }
909
1363
  }
910
1364
  if (result.length > 0) {
911
- result.push({ typ: EnumToken.CommaTokenType });
1365
+ result.push(arena.createToken(EnumToken.CommaTokenType));
912
1366
  }
913
- for (const token of reduceColorStops(slice.slice(i))) {
1367
+ for (const token of reduceColorStops(arena, slice.slice(i))) {
914
1368
  result.push(token);
915
1369
  }
916
1370
  slice.length = 0;
@@ -928,100 +1382,100 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
928
1382
  const colorSpaceDef = [];
929
1383
  // while (
930
1384
  // i < slice.length &&
931
- // (slice[i].typ === EnumToken.WhitespaceTokenType ||
932
- // slice[i].typ === EnumToken.CommentTokenType)
933
- // ) {
1385
+ // (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1386
+ // arena.dataTypes[slice[i]] === EnumToken.CommentTokenType)
1387
+ //) {
934
1388
  // i++;
935
1389
  // }
936
- if (slice[i]?.typ === EnumToken.IdenTokenType &&
937
- equalsIgnoreCase(slice[i].val, "from")) {
1390
+ if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1391
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "from")) {
938
1392
  angles.push(slice[i++]);
939
1393
  while (i < slice.length &&
940
- (slice[i].typ === EnumToken.WhitespaceTokenType ||
941
- slice[i].typ === EnumToken.CommentTokenType)) {
1394
+ (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1395
+ arena.dataTypes[slice[i]] === EnumToken.CommentTokenType)) {
942
1396
  angles.push(slice[i++]);
943
1397
  }
944
- if ((slice[i]?.typ === EnumToken.NumberTokenType ||
945
- slice[i]?.typ === EnumToken.AngleTokenType) &&
946
- 0 === toDegrees(slice[i]).val) {
1398
+ if ((arena.dataTypes[slice[i]] === EnumToken.NumberTokenType ||
1399
+ arena.dataTypes[slice[i]] === EnumToken.AngleTokenType) &&
1400
+ 0 === arena.numericValue[toDegrees(arena, slice[i])]) {
947
1401
  angles.length = 0;
948
1402
  i++;
949
1403
  }
950
- else if (slice[i]?.typ !== EnumToken.CommaTokenType &&
951
- slice[i].typ != EnumToken.IdenTokenType) {
1404
+ else if (arena.dataTypes[slice[i]] !== EnumToken.CommaTokenType &&
1405
+ arena.dataTypes[slice[i]] != EnumToken.IdenTokenType) {
952
1406
  angles.push(slice[i++]);
953
1407
  }
954
1408
  while (i < slice.length &&
955
- (slice[i].typ === EnumToken.WhitespaceTokenType ||
956
- slice[i].typ === EnumToken.CommentTokenType)) {
1409
+ (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1410
+ arena.dataTypes[slice[i]] === EnumToken.CommentTokenType)) {
957
1411
  i++;
958
1412
  }
959
1413
  }
960
- if (slice[i]?.typ === EnumToken.IdenTokenType &&
961
- equalsIgnoreCase(slice[i].val, "at")) {
1414
+ if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1415
+ equalsIgnoreCase(arena.strings[arena.stringValue[slice[i]]], "at")) {
962
1416
  i++;
963
1417
  while (i < slice.length &&
964
- (slice[i].typ === EnumToken.WhitespaceTokenType ||
965
- slice[i].typ === EnumToken.CommentTokenType)) {
1418
+ (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1419
+ arena.dataTypes[slice[i]] === EnumToken.CommentTokenType)) {
966
1420
  i++;
967
1421
  }
968
1422
  let position1 = "";
969
1423
  let position2 = "";
970
- if (slice[i]?.typ === EnumToken.IdenTokenType &&
971
- !equalsIgnoreCase("in", slice[i].val)) {
972
- position1 = slice[i].val;
1424
+ if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1425
+ !equalsIgnoreCase("in", arena.strings[arena.stringValue[slice[i]]])) {
1426
+ position1 = arena.strings[arena.stringValue[slice[i]]];
973
1427
  positions.push(slice[i++]);
974
1428
  }
975
- else if (slice[i]?.typ === EnumToken.PercentageTokenType ||
976
- slice[i]?.typ === EnumToken.NumberTokenType) {
977
- position1 = slice[i].val + "%";
1429
+ else if (arena.dataTypes[slice[i]] === EnumToken.PercentageTokenType ||
1430
+ arena.dataTypes[slice[i]] === EnumToken.NumberTokenType) {
1431
+ position1 = arena.numericValue[slice[i]] + "%";
978
1432
  positions.push(slice[i++]);
979
1433
  }
980
1434
  while (i < slice.length &&
981
- (slice[i].typ === EnumToken.WhitespaceTokenType ||
982
- slice[i].typ === EnumToken.CommentTokenType)) {
1435
+ (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1436
+ arena.dataTypes[slice[i]] === EnumToken.CommentTokenType)) {
983
1437
  positions.push(slice[i++]);
984
1438
  }
985
- if (slice[i]?.typ === EnumToken.IdenTokenType &&
986
- !equalsIgnoreCase("in", slice[i].val)) {
987
- position2 = slice[i].val;
1439
+ if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1440
+ !equalsIgnoreCase("in", arena.strings[arena.stringValue[slice[i]]])) {
1441
+ position2 = arena.strings[arena.stringValue[slice[i]]];
988
1442
  positions.push(slice[i++]);
989
1443
  }
990
- else if (slice[i]?.typ === EnumToken.PercentageTokenType ||
991
- slice[i]?.typ === EnumToken.NumberTokenType) {
992
- position2 = slice[i].val + "%";
1444
+ else if (arena.dataTypes[slice[i]] === EnumToken.PercentageTokenType ||
1445
+ arena.dataTypes[slice[i]] === EnumToken.NumberTokenType) {
1446
+ position2 = arena.numericValue[slice[i]] + "%";
993
1447
  positions.push(slice[i++]);
994
1448
  }
995
1449
  while (i < slice.length &&
996
- (slice[i].typ === EnumToken.WhitespaceTokenType ||
997
- slice[i].typ === EnumToken.CommentTokenType)) {
1450
+ (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1451
+ arena.dataTypes[slice[i]] === EnumToken.CommentTokenType)) {
998
1452
  i++;
999
1453
  }
1000
1454
  if (position1.length > 0) {
1001
- reducegradientBackgroundPosition(positions, `${position1} ${position2}`.trim());
1455
+ reducegradientBackgroundPosition(arena, positions, `${position1} ${position2}`.trim());
1002
1456
  }
1003
1457
  }
1004
1458
  while (i < slice.length &&
1005
- (slice[i].typ === EnumToken.WhitespaceTokenType ||
1006
- slice[i].typ === EnumToken.CommentTokenType)) {
1459
+ (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
1460
+ arena.dataTypes[slice[i]] === EnumToken.CommentTokenType)) {
1007
1461
  i++;
1008
1462
  }
1009
- if (slice[i]?.typ === EnumToken.IdenTokenType &&
1010
- equalsIgnoreCase("in", slice[i].val)) {
1011
- while (i < slice.length && slice[i].typ !== EnumToken.CommaTokenType) {
1463
+ if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
1464
+ equalsIgnoreCase("in", arena.strings[arena.stringValue[slice[i]]])) {
1465
+ while (i < slice.length && arena.dataTypes[slice[i]] !== EnumToken.CommaTokenType) {
1012
1466
  colorSpaceDef.push(slice[i++]);
1013
1467
  }
1014
1468
  }
1015
- if (slice[i]?.typ === EnumToken.CommaTokenType) {
1469
+ if (arena.dataTypes[slice[i]] === EnumToken.CommaTokenType) {
1016
1470
  i++;
1017
1471
  }
1018
1472
  const result = [];
1019
1473
  if (positions.length > 0) {
1020
1474
  if (positions.length > 0) {
1021
1475
  if (angles.length > 0) {
1022
- angles.push({ typ: EnumToken.WhitespaceTokenType });
1476
+ angles.push(arena.createToken(EnumToken.WhitespaceTokenType));
1023
1477
  }
1024
- angles.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType });
1478
+ angles.push(arena.createToken(EnumToken.IdenTokenType, "at"), arena.createToken(EnumToken.WhitespaceTokenType));
1025
1479
  for (const position of positions) {
1026
1480
  angles.push(position);
1027
1481
  }
@@ -1031,20 +1485,20 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1031
1485
  for (const angle of angles) {
1032
1486
  result.push(angle);
1033
1487
  }
1034
- result.push({ typ: EnumToken.CommaTokenType });
1488
+ result.push(arena.createToken(EnumToken.CommaTokenType));
1035
1489
  }
1036
1490
  if (colorSpaceDef.length > 0) {
1037
1491
  if (colorSpaceDef.length > 0) {
1038
1492
  if (result.length > 0) {
1039
- result.push({ typ: EnumToken.WhitespaceTokenType });
1493
+ result.push(arena.createToken(EnumToken.WhitespaceTokenType));
1040
1494
  }
1041
1495
  for (const token of colorSpaceDef) {
1042
1496
  result.push(token);
1043
1497
  }
1044
1498
  }
1045
- result.push({ typ: EnumToken.CommaTokenType });
1499
+ result.push(arena.createToken(EnumToken.CommaTokenType));
1046
1500
  }
1047
- for (const token of reduceConicColorStops(slice.slice(i))) {
1501
+ for (const token of reduceConicColorStops(arena, slice.slice(i))) {
1048
1502
  result.push(token);
1049
1503
  }
1050
1504
  slice.length = 0;
@@ -1054,7 +1508,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1054
1508
  }
1055
1509
  break;
1056
1510
  }
1057
- return token.val + "(" + slice.reduce(reducer, "") + ")";
1511
+ return arena.strings[arena.stringValue[token]] + "(" + renderTokenList(slice, reducer ?? createReducer(arena, options, cache)) + ")";
1058
1512
  }
1059
1513
  case EnumToken.TimingFunctionTokenType:
1060
1514
  case EnumToken.PseudoClassFuncTokenType:
@@ -1067,19 +1521,23 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1067
1521
  case EnumToken.GeneralEnclosedFunctionTokenType:
1068
1522
  case EnumToken.CustomFunctionTokenType:
1069
1523
  case EnumToken.WildCardFunctionTokenType:
1070
- if (token.typ == EnumToken.MathFunctionTokenType &&
1071
- token.chi.length == 1 &&
1072
- ![EnumToken.BinaryExpressionTokenType, EnumToken.FractionTokenType, EnumToken.IdenTokenType].includes(token.chi[0].typ) &&
1073
- // @ts-ignore
1074
- token.chi[0].val
1075
- ?.typ != EnumToken.FractionTokenType) {
1076
- return (token.val +
1077
- "(" +
1078
- token.chi.reduce((acc, curr) => acc +
1079
- renderValue(curr, token.typ == EnumToken.FunctionTokenType ? { minify: false } : options, cache, reducer), "") +
1080
- ")");
1524
+ if (arena.dataTypes[token] == EnumToken.MathFunctionTokenType && arena.tokenCount(token) == 1) {
1525
+ // a single argument
1526
+ const first = arena.firstToken[token];
1527
+ const firstType = arena.dataTypes[first];
1528
+ if (firstType != EnumToken.BinaryExpressionTokenType &&
1529
+ firstType != EnumToken.FractionTokenType &&
1530
+ firstType != EnumToken.IdenTokenType &&
1531
+ !arena.hasFraction(first)) {
1532
+ return (arena.strings[arena.stringValue[token]] +
1533
+ "(" +
1534
+ renderValue(arena, first, options, cache,
1535
+ // the reducer renders with the options of this token
1536
+ (reducer ??= createReducer(arena, options, cache))) +
1537
+ ")");
1538
+ }
1081
1539
  }
1082
- return (token.val ?? "") + "(" + token.chi.reduce(reducer, "") + ")";
1540
+ return (arena.strings[arena.stringValue[token]] ?? "") + "(" + renderReduced(arena, arena.firstToken[token], options, cache, reducer) + ")";
1083
1541
  case EnumToken.BlockStartTokenType:
1084
1542
  return "{";
1085
1543
  case EnumToken.BlockEndTokenType:
@@ -1139,7 +1597,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1139
1597
  return "|";
1140
1598
  case EnumToken.AttrTokenType:
1141
1599
  case EnumToken.IdenListTokenType:
1142
- return "[" + token.chi.reduce(reducer, "") + "]";
1600
+ return "[" + renderReduced(arena, arena.firstToken[token], options, cache, reducer) + "]";
1143
1601
  case EnumToken.TimeTokenType:
1144
1602
  case EnumToken.AngleTokenType:
1145
1603
  case EnumToken.LengthTokenType:
@@ -1147,16 +1605,16 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1147
1605
  case EnumToken.FrequencyTokenType:
1148
1606
  case EnumToken.ResolutionTokenType:
1149
1607
  case EnumToken.UserDefinedDimensionTokenType:
1150
- let val = token.val.typ == EnumToken.FractionTokenType
1151
- ? renderValue(token.val, options, cache)
1152
- : minifyNumber(token.val);
1153
- let unit = token.unit;
1154
- if (token.typ == EnumToken.AngleTokenType && !val.includes("/")) {
1155
- const angle = getAngle(token);
1608
+ let val = arena.hasFraction(token)
1609
+ ? renderValue(arena, arena.getFraction(token), options, cache)
1610
+ : minifyNumber(arena.numericValue[token]);
1611
+ let unit = arena.strings[arena.unit[token]];
1612
+ if (arena.dataTypes[token] == EnumToken.AngleTokenType && !val.includes("/")) {
1613
+ const angle = getAngle(arena, token);
1156
1614
  let v;
1157
1615
  let value = val + unit;
1158
- for (const u of ["deg", "turn", "rad", "grad"]) {
1159
- if (token.unit == u) {
1616
+ for (const u of angleUnits) {
1617
+ if (arena.strings[arena.unit[token]] == u) {
1160
1618
  continue;
1161
1619
  }
1162
1620
  switch (u) {
@@ -1196,19 +1654,19 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1196
1654
  }
1197
1655
  }
1198
1656
  if (val === "0") {
1199
- if (token.typ == EnumToken.TimeTokenType) {
1657
+ if (arena.dataTypes[token] == EnumToken.TimeTokenType) {
1200
1658
  return "0s";
1201
1659
  }
1202
- if (token.typ == EnumToken.FrequencyTokenType) {
1660
+ if (arena.dataTypes[token] == EnumToken.FrequencyTokenType) {
1203
1661
  return "0Hz";
1204
1662
  }
1205
1663
  // @ts-ignore
1206
- if (token.typ == EnumToken.ResolutionTokenType) {
1664
+ if (arena.dataTypes[token] == EnumToken.ResolutionTokenType) {
1207
1665
  return "0x";
1208
1666
  }
1209
1667
  return "0";
1210
1668
  }
1211
- if (token.typ == EnumToken.TimeTokenType) {
1669
+ if (arena.dataTypes[token] == EnumToken.TimeTokenType) {
1212
1670
  if (unit == "ms") {
1213
1671
  // @ts-ignore
1214
1672
  const v = minifyNumber(val / 1000);
@@ -1219,25 +1677,25 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1219
1677
  }
1220
1678
  return val + "s";
1221
1679
  }
1222
- if (token.typ == EnumToken.ResolutionTokenType && unit == "dppx") {
1680
+ if (arena.dataTypes[token] == EnumToken.ResolutionTokenType && unit == "dppx") {
1223
1681
  unit = "x";
1224
1682
  }
1225
1683
  return val.includes("/") ? val.replace("/", unit + "/") : minifyNumber(toPrecisionValue(val)) + unit;
1226
1684
  case EnumToken.FlexTokenType:
1227
1685
  case EnumToken.PercentageTokenType:
1228
- const uni = token.typ == EnumToken.PercentageTokenType ? "%" : "fr";
1229
- const perc = token.val.typ == EnumToken.FractionTokenType
1230
- ? renderValue(token.val, options, cache)
1231
- : minifyNumber(token.val);
1686
+ const uni = arena.dataTypes[token] == EnumToken.PercentageTokenType ? "%" : "fr";
1687
+ const perc = arena.hasFraction(token)
1688
+ ? renderValue(arena, arena.getFraction(token), options, cache)
1689
+ : minifyNumber(arena.numericValue[token]);
1232
1690
  return options.minifyValues && perc == "0"
1233
1691
  ? "0"
1234
1692
  : perc.includes("/")
1235
1693
  ? perc.replace("/", uni + "/")
1236
1694
  : perc + uni;
1237
1695
  case EnumToken.NumberTokenType:
1238
- return token.val.typ == EnumToken.FractionTokenType
1239
- ? renderValue(token.val, options, cache)
1240
- : minifyNumber(token.val);
1696
+ return arena.hasFraction(token)
1697
+ ? renderValue(arena, arena.getFraction(token), options, cache)
1698
+ : minifyNumber(arena.numericValue[token]);
1241
1699
  case EnumToken.InfinityTokenType:
1242
1700
  return "1/0";
1243
1701
  case EnumToken.NegativeInfinityTokenType:
@@ -1245,19 +1703,19 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1245
1703
  case EnumToken.NaNTokenType:
1246
1704
  return "NaN";
1247
1705
  case EnumToken.AtRuleTokenType:
1248
- return "@" + token.nam;
1706
+ return "@" + arena.strings[arena.name[token]];
1249
1707
  case EnumToken.CommentTokenType:
1250
1708
  case EnumToken.CDOCOMMNodeType:
1251
1709
  if (options.removeComments &&
1252
- (!options.preserveLicense || !token.val.startsWith("/*!"))) {
1710
+ (!options.preserveLicense || !arena.strings[arena.stringValue[token]].startsWith("/*!"))) {
1253
1711
  return "";
1254
1712
  }
1255
1713
  case EnumToken.PseudoClassTokenType:
1256
1714
  case EnumToken.PseudoElementTokenType:
1257
1715
  // https://www.w3.org/TR/selectors-4/#single-colon-pseudos
1258
- if (token.typ == EnumToken.PseudoElementTokenType &&
1259
- pseudoElements.includes(token.val.slice(1))) {
1260
- return token.val.slice(1);
1716
+ if (arena.dataTypes[token] == EnumToken.PseudoElementTokenType &&
1717
+ pseudoElements.includes(arena.strings[arena.stringValue[token]].slice(1))) {
1718
+ return arena.strings[arena.stringValue[token]].slice(1);
1261
1719
  }
1262
1720
  case EnumToken.CssTypeTokenType:
1263
1721
  case EnumToken.UrlTokenTokenType:
@@ -1268,68 +1726,66 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1268
1726
  case EnumToken.DashedIdenTokenType:
1269
1727
  case EnumToken.PseudoPageTokenType:
1270
1728
  case EnumToken.ClassSelectorTokenType:
1271
- return token.val;
1729
+ return arena.strings[arena.stringValue[token]];
1272
1730
  case EnumToken.NestingSelectorTokenType:
1273
1731
  return "&";
1274
- case EnumToken.InvalidAttrTokenType:
1275
- return ("[" +
1276
- token.chi.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
1277
- case EnumToken.InvalidClassSelectorTokenType:
1278
- return token.val;
1279
1732
  case EnumToken.SupportsQueryUnaryConditionTokenType:
1280
1733
  case EnumToken.WhenElseUnaryConditionTokenType:
1281
- return (renderValue(token.l, options, cache, reducer) +
1734
+ return (renderValue(arena, arena.field(token, TokenField.L), options, cache, reducer) +
1282
1735
  " " +
1283
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1736
+ renderField(arena, token, TokenField.R, options, cache, reducer));
1284
1737
  case EnumToken.SupportsQueryConditionTokenType:
1285
1738
  case EnumToken.WhenElseQueryConditionTokenType:
1286
- return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
1739
+ return (renderField(arena, token, TokenField.L, options, cache, reducer) +
1287
1740
  " " +
1288
- renderValue(token.op, options, cache, reducer) +
1741
+ renderValue(arena, arena.field(token, TokenField.Op), options, cache, reducer) +
1289
1742
  " " +
1290
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1291
- case EnumToken.IfConditionTokenType:
1292
- return token.l.length == 0
1743
+ renderField(arena, token, TokenField.R, options, cache, reducer));
1744
+ case EnumToken.IfConditionTokenType: {
1745
+ return !arena.hasField(token, TokenField.L)
1293
1746
  ? ""
1294
- : token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
1747
+ : renderField(arena, token, TokenField.L, options, cache, reducer) +
1295
1748
  ":" +
1296
1749
  (options.indent ?? "") +
1297
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "");
1750
+ renderField(arena, token, TokenField.R, options, cache, reducer);
1751
+ }
1298
1752
  case EnumToken.IfElseConditionTokenType:
1299
- return (renderValue(token.l, options, cache, reducer) +
1753
+ return (renderValue(arena, arena.field(token, TokenField.L), options, cache, reducer) +
1300
1754
  (options.indent ?? "") +
1301
- renderValue(token.r, options, cache, reducer));
1755
+ renderValue(arena, arena.field(token, TokenField.R), options, cache, reducer));
1302
1756
  case EnumToken.DeclarationNodeType:
1303
- return (token.nam +
1757
+ return (arena.strings[arena.name[token]] +
1304
1758
  ":" +
1305
- (options.minifyValues ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1759
+ renderTokenList(options.minifyValues ? filterValues(arena, arena.tokens(token)) : arena.tokens(token), (acc, curr) => acc + renderValue(arena, curr, options, cache, reducer)));
1306
1760
  case EnumToken.MediaQueryUnaryFeatureTokenType:
1307
- return (renderValue(token.l, options, cache, reducer) +
1761
+ return (renderValue(arena, arena.field(token, TokenField.L), options, cache, reducer) +
1308
1762
  " " +
1309
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1763
+ renderField(arena, token, TokenField.R, options, cache, reducer));
1310
1764
  case EnumToken.MediaQueryConditionTokenType: {
1311
- const indent = token.op.typ == EnumToken.LtTokenType ||
1312
- token.op.typ == EnumToken.GtTokenType ||
1313
- token.op.typ == EnumToken.ColonTokenType ||
1314
- token.op.typ == EnumToken.DelimTokenType ||
1315
- token.op.typ == EnumToken.LteTokenType ||
1316
- token.op.typ == EnumToken.GteTokenType
1765
+ const op = arena.field(token, TokenField.Op);
1766
+ const opType = arena.dataTypes[op];
1767
+ const indent = opType == EnumToken.LtTokenType ||
1768
+ opType == EnumToken.GtTokenType ||
1769
+ opType == EnumToken.ColonTokenType ||
1770
+ opType == EnumToken.DelimTokenType ||
1771
+ opType == EnumToken.LteTokenType ||
1772
+ opType == EnumToken.GteTokenType
1317
1773
  ? ""
1318
1774
  : " ";
1319
- return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
1775
+ return (renderField(arena, token, TokenField.L, options, cache, reducer) +
1320
1776
  indent +
1321
- renderValue(token.op, options, cache, reducer) +
1777
+ renderValue(arena, op, options, cache, reducer) +
1322
1778
  indent +
1323
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1779
+ renderField(arena, token, TokenField.R, options, cache, reducer));
1324
1780
  }
1325
1781
  case EnumToken.MediaRangeQueryTokenType:
1326
- return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
1327
- renderValue(token.op1, options, cache, reducer) +
1328
- token.val.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
1329
- renderValue(token.op2, options, cache, reducer) +
1330
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1782
+ return (renderField(arena, token, TokenField.L, options, cache, reducer) +
1783
+ renderValue(arena, arena.field(token, TokenField.Op), options, cache, reducer) +
1784
+ renderField(arena, token, TokenField.Val, options, cache, reducer) +
1785
+ renderValue(arena, arena.field(token, TokenField.Op2), options, cache, reducer) +
1786
+ renderField(arena, token, TokenField.R, options, cache, reducer));
1331
1787
  case EnumToken.MediaFeatureTokenType:
1332
- return token.val;
1788
+ return arena.strings[arena.stringValue[token]];
1333
1789
  case EnumToken.NotTokenType:
1334
1790
  return "not";
1335
1791
  case EnumToken.OnlyTokenType:
@@ -1338,8 +1794,6 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1338
1794
  return "and";
1339
1795
  case EnumToken.OrTokenType:
1340
1796
  return "or";
1341
- case EnumToken.InvalidMediaQueryTokenType:
1342
- case EnumToken.InvalidCommentTokenType:
1343
1797
  case EnumToken.BadCommentTokenType:
1344
1798
  case EnumToken.BadCdoTokenType:
1345
1799
  case EnumToken.BadStringTokenType:
@@ -1347,10 +1801,9 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1347
1801
  case EnumToken.EOFTokenType:
1348
1802
  return "";
1349
1803
  case EnumToken.ComposesSelectorNodeType:
1350
- return `${token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "")}${token.r == null ? "" : " from " + renderValue(token.r, options, cache, reducer)}`;
1804
+ return `${renderField(arena, token, TokenField.L, options, cache, reducer)}${!arena.hasField(token, TokenField.R) ? "" : " from " + renderValue(arena, arena.field(token, TokenField.R), options, cache, reducer)}`;
1351
1805
  default:
1352
- console.error({ token });
1353
- throw new Error(`Unsupported token type for ${EnumToken[token.typ]}`);
1806
+ throw new Error(`Unsupported token type for ${EnumToken[arena.dataTypes[token]]}`);
1354
1807
  }
1355
1808
  // errors?.push({ action: "ignore", message: `render: unexpected token ${JSON.stringify(token, null, 1)}` });
1356
1809
  // return "";
@@ -1361,20 +1814,21 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1361
1814
  *
1362
1815
  * @internal
1363
1816
  */
1364
- function filterValues(values) {
1817
+ function filterValues(arena, values) {
1365
1818
  let i = 0;
1366
1819
  for (; i < values.length; i++) {
1367
- if (values[i].typ == EnumToken.ImportantTokenType && values[i - 1]?.typ === EnumToken.WhitespaceTokenType) {
1820
+ if (arena.dataTypes[values[i]] == EnumToken.ImportantTokenType && arena.dataTypes[values[i - 1]] === EnumToken.WhitespaceTokenType) {
1368
1821
  values.splice(i - 1, 1);
1369
1822
  }
1370
- else if (tokensfuncSet.has(values[i].typ) &&
1371
- "chi" in values[i] &&
1372
- values[i].typ != EnumToken.WildCardFunctionTokenType &&
1373
- values[i + 1]?.typ == EnumToken.WhitespaceTokenType) {
1823
+ else if (tokensfuncSet.has(arena.dataTypes[values[i]]) &&
1824
+ // color keywords and hex colors are not functions
1825
+ (arena.dataTypes[values[i]] != EnumToken.ColorTokenType || arena.firstToken[values[i]] != 0) &&
1826
+ arena.dataTypes[values[i]] != EnumToken.WildCardFunctionTokenType &&
1827
+ arena.dataTypes[values[i + 1]] == EnumToken.WhitespaceTokenType) {
1374
1828
  values.splice(i + 1, 1);
1375
1829
  }
1376
1830
  }
1377
1831
  return values;
1378
1832
  }
1379
1833
 
1380
- export { doRender, filterValues, move, renderValue };
1834
+ export { NodeRenderer, RenderMemo, doRender, filterValues, move, renderValue };