@tbela99/css-parser 1.8.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27382 -24086
  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/features/calc.js +85 -76
  11. package/dist/src/lib/ast/features/if.js +155 -115
  12. package/dist/src/lib/ast/features/index.js +1 -1
  13. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  14. package/dist/src/lib/ast/features/prefix.js +184 -183
  15. package/dist/src/lib/ast/features/shorthand.js +40 -46
  16. package/dist/src/lib/ast/features/transform.js +24 -24
  17. package/dist/src/lib/ast/features/type.js +1 -1
  18. package/dist/src/lib/ast/fingerprint.js +333 -0
  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 -1662
  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 -35874
  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,34 +2,27 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.1
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
- import { eq } from '../parser/utils/eq.js';
9
- import { renderValue, doRender } from '../printer/render.js';
8
+ import { eq, eqToken } from '../parser/utils/eq.js';
9
+ import { renderValue, NodeRenderer } from '../printer/render.js';
10
10
  import * as index from './features/index.js';
11
- import { walkValues } from './walk.js';
12
- import { EnumToken } from './types.js';
11
+ import { EnumToken, EnumAstNodeStatus } from './types.js';
13
12
  import { isWhiteSpace, isIdent, isFunction, isIdentStart } from '../syntax/syntax.js';
14
13
  import { FeatureWalkMode } from './features/type.js';
15
14
  import { trimArray } from '../validation/match.js';
16
- import { TOKENS, PARENT, OPTIMIZED, RAW, combinators, LOCEND, LOCSTA, LOCSRCID } from '../syntax/constants.js';
17
- import { replaceNodeOrValue } from '../parser/utils/token.js';
15
+ import { combinators } from '../syntax/constants.js';
18
16
  import { parseString } from '../parser/parse.js';
19
- import { replaceCompound } from './expand.js';
20
17
  import { equalsIgnoreCase } from '../parser/utils/text.js';
18
+ import { ValueWalker } from './walk.js';
21
19
 
22
20
  const notEndingWith = ["(", "["].concat(combinators);
23
- const rules = [
24
- EnumToken.AtRuleNodeType,
25
- EnumToken.RuleNodeType,
26
- EnumToken.AtRuleTokenType,
27
- EnumToken.KeyframesRuleNodeType,
28
- ];
29
21
  // @ts-ignore
30
22
  const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
31
23
  /**
32
24
  * Apply minification rules to the ast tree
25
+ * @param arena
33
26
  * @param ast
34
27
  * @param options
35
28
  * @param recursive
@@ -39,11 +32,9 @@ const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
39
32
  * @param context
40
33
  * @private
41
34
  */
42
- function minify(ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
35
+ function minify(arena, ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
43
36
  let preprocess = false;
44
37
  let postprocess = false;
45
- let parents;
46
- let replacement;
47
38
  let { sourcemap, module, ...options2 } = options;
48
39
  if (!(options2.features != null)) {
49
40
  options2 = {
@@ -67,188 +58,67 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
67
58
  postprocess = true;
68
59
  }
69
60
  }
70
- if (preprocess) {
71
- parents = new Set([ast]);
72
- for (const parent of parents) {
73
- if (parent.typ == EnumToken.CommentTokenType || parent.typ == EnumToken.CDOCOMMTokenType) {
61
+ /**
62
+ * run the features on every node of the tree
63
+ * @param mode
64
+ */
65
+ const runFeatures = (mode) => {
66
+ // breadth first traversal
67
+ const queue = [ast];
68
+ let node;
69
+ let typ;
70
+ let replacement;
71
+ for (let k = 0; k < queue.length; k++) {
72
+ node = queue[k];
73
+ typ = arena.dataTypes[node];
74
+ if (typ == EnumToken.CommentTokenType || typ == EnumToken.CDOCOMMTokenType) {
74
75
  continue;
75
76
  }
76
- replacement = parent;
77
+ replacement = node;
77
78
  for (const feature of options2.features) {
78
- if ((feature.processMode & FeatureWalkMode.Pre) === 0 ||
79
- (feature.accept != null && !feature.accept.has(parent.typ))) {
79
+ if ((feature.processMode & mode) === 0 || (feature.accept != null && !feature.accept.has(typ))) {
80
80
  continue;
81
81
  }
82
- if (rules.includes(replacement.typ) && !Array.isArray(replacement[TOKENS])) {
83
- replacement[TOKENS] = parseString(replacement.typ == EnumToken.RuleNodeType || replacement.typ === EnumToken.KeyframesRuleNodeType
84
- ? replacement.sel
85
- : // @ts-ignore
86
- replacement.nam);
82
+ if (Array.isArray(replacement)) {
83
+ break;
87
84
  }
88
- const result = feature.run(replacement, options2,
89
- // @ts-ignore
90
- parent[PARENT] ?? ast, context, FeatureWalkMode.Pre);
85
+ // the prelude tokens of the rules whose selector has been rewritten are not parsed again:
86
+ // the features and the renderer use the selector / prelude string
87
+ const result = feature.run(arena, replacement, options2, arena.parents[node] == 0 ? ast : arena.parents[node], context, mode);
91
88
  if (result != null) {
92
89
  replacement = result;
93
90
  }
94
91
  }
95
92
  if (replacement != null &&
96
93
  (!Array.isArray(replacement) || replacement.length > 0) &&
97
- replacement != parent &&
98
- parent[PARENT] != null) {
99
- // @ts-ignore
100
- replaceNodeOrValue(parent[PARENT], parent, replacement);
94
+ replacement != node &&
95
+ arena.parents[node] != 0) {
96
+ arena.replaceChild(node, ...(Array.isArray(replacement) ? replacement : [replacement]));
101
97
  }
102
- // @ts-ignore
103
- if (replacement.chi != null) {
104
- // @ts-ignore
105
- for (const node of replacement.chi) {
106
- node[PARENT] = replacement;
107
- parents.add(node);
98
+ if (!Array.isArray(replacement) && arena.flags[replacement] & EnumAstNodeStatus.HasChildren) {
99
+ for (let child = arena.firstChild[replacement]; child != 0; child = arena.nextSibling[child]) {
100
+ queue.push(child);
108
101
  }
109
102
  }
110
103
  }
111
104
  for (const feature of options2.features) {
112
- if (feature.processMode & FeatureWalkMode.Pre && "cleanup" in feature) {
113
- // @ts-ignore
114
- feature.cleanup(ast, options2, context, FeatureWalkMode.Pre);
115
- }
116
- }
117
- }
118
- doMinify(ast, options2, recursive, errors, nestingContent, context);
119
- parents = new Set([ast]);
120
- for (const parent of parents) {
121
- if (parent.typ == EnumToken.CommentTokenType || parent.typ == EnumToken.CDOCOMMTokenType) {
122
- continue;
123
- }
124
- replacement = parent;
125
- if (postprocess) {
126
- for (const feature of options2.features) {
127
- if ((feature.processMode & FeatureWalkMode.Post) === 0 ||
128
- (feature.accept != null && !feature.accept.has(parent.typ))) {
129
- continue;
130
- }
131
- const result = feature.run(replacement, options2,
105
+ if (feature.processMode & mode && "cleanup" in feature) {
132
106
  // @ts-ignore
133
- parent[PARENT] ?? ast, context, FeatureWalkMode.Post);
134
- if (result != null) {
135
- replacement = result;
136
- }
137
- }
138
- }
139
- if (replacement != null &&
140
- (!Array.isArray(replacement) || replacement.length > 0) &&
141
- replacement != parent &&
142
- parent[PARENT] != null) {
143
- // @ts-ignore
144
- replaceNodeOrValue(parent[PARENT], parent, replacement);
145
- }
146
- // @ts-ignore
147
- if (replacement.chi != null) {
148
- // @ts-ignore
149
- for (const node of replacement.chi) {
150
- node[PARENT] = replacement;
151
- parents.add(node);
107
+ feature.cleanup(arena, ast, options2, context, mode);
152
108
  }
153
109
  }
110
+ };
111
+ if (preprocess) {
112
+ runFeatures(FeatureWalkMode.Pre);
154
113
  }
114
+ doMinify(arena, ast, options2, recursive, errors, nestingContent, context);
155
115
  if (postprocess) {
156
- for (const feature of options2.features) {
157
- if (feature.processMode & FeatureWalkMode.Post && "cleanup" in feature) {
158
- // @ts-ignore
159
- feature.cleanup(ast, options2, context, FeatureWalkMode.Post);
160
- }
161
- }
116
+ runFeatures(FeatureWalkMode.Post);
162
117
  }
163
118
  return ast;
164
119
  }
165
- function transformAtRuleMediaPrelude(values) {
166
- let hasUpdates = false;
167
- for (let { value, parent, parents } of walkValues(values)) {
168
- if (value.typ === EnumToken.MediaQueryConditionTokenType) {
169
- if (value.op.typ == EnumToken.AndTokenType &&
170
- // @ts-ignore
171
- value.l.typ === EnumToken.IdenTokenType &&
172
- // @ts-ignore
173
- value.l.val.toLowerCase() === "all") {
174
- if (parent === null) {
175
- // @ts-ignore
176
- values[values.indexOf(value)] = value.l;
177
- }
178
- else {
179
- // @ts-ignore
180
- replaceNodeOrValue(parent, value, value.l);
181
- // @ts-ignore
182
- value = value.l;
183
- }
184
- hasUpdates = true;
185
- }
186
- }
187
- // range operator
188
- if (parent != null &&
189
- parent.typ === EnumToken.MediaQueryConditionTokenType &&
190
- parent.op.typ == EnumToken.AndTokenType &&
191
- // @ts-ignore
192
- parent.l.typ == EnumToken.ParensTokenType) {
193
- let token = parent.r.find((t) => t.typ !== EnumToken.WhitespaceTokenType && t.typ !== EnumToken.CommentTokenType);
194
- if (token?.typ === EnumToken.ParensTokenType) {
195
- // @ts-ignore
196
- const node1 = parent.l.chi.find((t) => t.typ !== EnumToken.WhitespaceTokenType && t.typ !== EnumToken.CommentTokenType);
197
- const node2 = token.chi.find((t) => t.typ !== EnumToken.WhitespaceTokenType && t.typ !== EnumToken.CommentTokenType);
198
- if (node1?.typ === EnumToken.MediaQueryConditionTokenType &&
199
- node2?.typ === EnumToken.MediaQueryConditionTokenType &&
200
- node1.op.typ == EnumToken.ColonTokenType &&
201
- node2.op.typ == EnumToken.ColonTokenType &&
202
- // @ts-ignore
203
- node1.l.typ == EnumToken.IdenTokenType &&
204
- // @ts-ignore
205
- node2.l.typ == EnumToken.IdenTokenType &&
206
- // @ts-ignore
207
- node1.l.val.startsWith("min-") &&
208
- // @ts-ignore
209
- node2.l.val.startsWith("max-") &&
210
- // @ts-ignore
211
- node1.l.val.slice(4) ==
212
- // @ts-ignore
213
- node2.l.val.slice(4)) {
214
- const val1 = node1.r.find((t) => t.typ !== EnumToken.WhitespaceTokenType && t.typ !== EnumToken.CommentTokenType);
215
- const val2 = node2.r.find((t) => t.typ !== EnumToken.WhitespaceTokenType && t.typ !== EnumToken.CommentTokenType);
216
- const replacement = {
217
- typ: EnumToken.ParensTokenType,
218
- chi: [
219
- // @ts-ignore
220
- {
221
- typ: EnumToken.MediaRangeQueryTokenType,
222
- op: {
223
- typ: EnumToken.IdenTokenType,
224
- // @ts-ignore
225
- val: node1.l.val.slice(4),
226
- },
227
- l: val1,
228
- r: val2,
229
- [LOCSRCID]: value[LOCSRCID],
230
- [LOCSTA]: value[LOCSTA],
231
- [LOCEND]: value[LOCEND],
232
- },
233
- ],
234
- };
235
- // @ts-expect-error
236
- const p = parents?.[parents?.indexOf?.(parent) + 1];
237
- if (p != null) {
238
- // @ts-ignore
239
- replaceNodeOrValue(p, parent, replacement);
240
- }
241
- else {
242
- // @ts-ignore
243
- values.splice(values.indexOf(parent), 1, replacement);
244
- }
245
- hasUpdates = true;
246
- value = replacement;
247
- }
248
- }
249
- }
250
- }
251
- return { hasUpdates, values: trimArray(values) };
120
+ function transformAtRuleMediaPrelude(arena, values) {
121
+ return { hasUpdates: false, values: trimArray(arena, values) };
252
122
  }
253
123
  /**
254
124
  * Minify at-rule media
@@ -258,48 +128,33 @@ function transformAtRuleMediaPrelude(values) {
258
128
  * @private
259
129
  * @param tokens
260
130
  */
261
- function minifyAtRuleMedia(tokens) {
262
- let hasUpdates = false;
263
- const sections = tokens
264
- .reduce((acc, t) => {
265
- if (t.typ === EnumToken.CommaTokenType) {
266
- acc.push([]);
131
+ function minifyAtRuleMedia(arena, tokens) {
132
+ const parts = [[]];
133
+ for (const token of tokens) {
134
+ if (arena.dataTypes[token] === EnumToken.CommaTokenType) {
135
+ parts.push([]);
267
136
  }
268
137
  else {
269
- acc[acc.length - 1].push(t);
138
+ parts[parts.length - 1].push(token);
270
139
  }
271
- return acc;
272
- }, [[]])
273
- .reduce((acc, values) => {
274
- if (acc.has("all")) {
275
- return acc;
140
+ }
141
+ const sections = new Map();
142
+ for (const values of parts) {
143
+ if (sections.has("all")) {
144
+ break;
276
145
  }
277
- const result = transformAtRuleMediaPrelude(values);
146
+ const result = transformAtRuleMediaPrelude(arena, values);
278
147
  if (result.values.length === 0) {
279
- return acc;
148
+ continue;
280
149
  }
281
- if (result.hasUpdates) {
282
- hasUpdates = true;
150
+ let key = "";
151
+ for (const token of values) {
152
+ key += renderValue(arena, token);
283
153
  }
284
- acc.set(values.reduce((acc, t) => acc + renderValue(t), ""), result.values);
285
- return acc;
286
- }, new Map());
287
- if (sections.has("all")) {
288
- tokens.length = 0;
154
+ sections.set(key, result.values);
289
155
  }
290
- else if (hasUpdates) {
156
+ if (sections.has("all")) {
291
157
  tokens.length = 0;
292
- tokens.push(...[...sections.values()].reduce((acc, t) => {
293
- if (acc.length > 0) {
294
- acc.push({
295
- typ: EnumToken.CommaTokenType,
296
- });
297
- }
298
- for (const token of t) {
299
- acc.push(token);
300
- }
301
- return acc;
302
- }, []));
303
158
  }
304
159
  // return ast;
305
160
  return tokens;
@@ -331,6 +186,7 @@ function reduce(acc, curr) {
331
186
  }
332
187
  /**
333
188
  * Apply minification rules to the ast tree
189
+ * @param arena
334
190
  * @param ast
335
191
  * @param options
336
192
  * @param recursive
@@ -340,7 +196,7 @@ function reduce(acc, curr) {
340
196
  *
341
197
  * @private
342
198
  */
343
- function doMinify(ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
199
+ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
344
200
  if (!("nodes" in context)) {
345
201
  context.nodes = new Set();
346
202
  }
@@ -348,263 +204,279 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
348
204
  return ast;
349
205
  }
350
206
  context.nodes.add(ast);
351
- // @ts-ignore
352
- if ("chi" in ast && ast.chi.length > 0) {
207
+ if (arena.flags[ast] & EnumAstNodeStatus.HasChildren && arena.firstChild[ast] != 0) {
353
208
  const reducer = reduce.bind(ast);
354
209
  if (!nestingContent) {
355
- nestingContent = options.nestingRules && ast.typ == EnumToken.RuleNodeType;
210
+ nestingContent = options.nestingRules && arena.dataTypes[ast] == EnumToken.RuleNodeType;
356
211
  }
357
- let i = 0;
212
+ // the children list is modified while it is visited: every branch sets the next child to visit
213
+ let cursor = arena.firstChild[ast];
358
214
  let previous = null;
359
215
  let node = null;
360
- let nodeIndex = -1;
361
- for (; i < ast.chi.length; i++) {
362
- if (ast.chi[i].typ === EnumToken.CommentNodeType) {
216
+ let nodeType;
217
+ let previousType;
218
+ /**
219
+ * the node located `count` positions before a child of ast (the end of the list when 0). null when out of range
220
+ * @param child
221
+ * @param count
222
+ */
223
+ const before = (child, count) => {
224
+ let result = child == 0 ? arena.lastChild[ast] : arena.previousSibling[child];
225
+ while (--count > 0 && result != 0) {
226
+ result = arena.previousSibling[result];
227
+ }
228
+ return result == 0 ? null : result;
229
+ };
230
+ while (cursor != 0) {
231
+ if (arena.dataTypes[cursor] === EnumToken.CommentNodeType) {
232
+ cursor = arena.nextSibling[cursor];
363
233
  continue;
364
234
  }
365
- while (previous?.typ === EnumToken.CommentNodeType) {
366
- // @ts-ignore
367
- previous = ast.chi[--nodeIndex];
235
+ while (previous != null && arena.dataTypes[previous] === EnumToken.CommentNodeType) {
236
+ previous = arena.previousSibling[previous] || null;
368
237
  }
369
- node = ast.chi[i];
370
- if (node.typ === EnumToken.AtRuleNodeType) {
371
- if (node.nam === "font-face") {
238
+ node = cursor;
239
+ nodeType = arena.dataTypes[node];
240
+ previousType = previous == null ? null : arena.dataTypes[previous];
241
+ if (nodeType === EnumToken.AtRuleNodeType) {
242
+ const nam = arena.strings[arena.name[node]];
243
+ if (nam === "font-face") {
244
+ cursor = arena.nextSibling[node];
372
245
  continue;
373
246
  }
374
247
  // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example
375
- if (equalsIgnoreCase(node.nam, "function")) {
376
- const chi = node.chi;
377
- let k = chi?.length;
248
+ if (equalsIgnoreCase(nam, "function")) {
249
+ let k = arena.lastChild[node];
378
250
  let slice = false;
379
- while (k--) {
380
- if (chi[k].typ === EnumToken.CommentNodeType ||
381
- chi[k].typ === EnumToken.WhitespaceTokenType) {
251
+ while (k != 0) {
252
+ if (arena.dataTypes[k] === EnumToken.CommentNodeType ||
253
+ arena.dataTypes[k] === EnumToken.WhitespaceTokenType) {
254
+ k = arena.previousSibling[k];
382
255
  continue;
383
256
  }
384
- if (chi[k].typ === EnumToken.DeclarationNodeType &&
385
- equalsIgnoreCase(chi[k].nam, "result")) {
257
+ if (arena.dataTypes[k] === EnumToken.DeclarationNodeType &&
258
+ equalsIgnoreCase(arena.strings[arena.name[k]], "result")) {
386
259
  slice = true;
387
260
  break;
388
261
  }
262
+ k = arena.previousSibling[k];
389
263
  }
390
- if (k > 0 && slice) {
391
- while (k--) {
392
- if (chi[k].typ === EnumToken.AtRuleNodeType) {
393
- chi.splice(k, 1);
394
- continue;
264
+ if (k != 0 && slice) {
265
+ k = arena.previousSibling[k];
266
+ while (k != 0) {
267
+ const previousNode = arena.previousSibling[k];
268
+ if (arena.dataTypes[k] === EnumToken.AtRuleNodeType) {
269
+ arena.detach(k);
395
270
  }
271
+ k = previousNode;
396
272
  }
397
273
  }
398
274
  }
399
275
  }
400
- if (node.typ === EnumToken.KeyframesAtRuleNodeType) {
401
- if (previous?.typ === EnumToken.KeyframesAtRuleNodeType &&
402
- node.nam === previous.nam &&
403
- node.val === previous.val) {
404
- ast.chi?.splice(nodeIndex--, 1);
405
- previous = ast?.chi?.[nodeIndex] ?? null;
406
- i = nodeIndex;
276
+ if (nodeType === EnumToken.KeyframesAtRuleNodeType) {
277
+ if (previousType === EnumToken.KeyframesAtRuleNodeType &&
278
+ arena.name[node] === arena.name[previous] &&
279
+ arena.stringValue[node] === arena.stringValue[previous]) {
280
+ // the previous keyframes are dropped, the node is visited again
281
+ const previousNode = arena.previousSibling[previous];
282
+ cursor = arena.nextSibling[previous];
283
+ arena.detach(previous);
284
+ previous = previousNode || null;
407
285
  continue;
408
286
  }
409
287
  }
410
- else if (node.typ === EnumToken.KeyframesRuleNodeType) {
411
- if (previous?.typ === EnumToken.KeyframesRuleNodeType &&
412
- node.sel === previous.sel) {
288
+ else if (nodeType === EnumToken.KeyframesRuleNodeType) {
289
+ if (previousType === EnumToken.KeyframesRuleNodeType &&
290
+ arena.name[node] === arena.name[previous]) {
413
291
  // do not merge keyframes
414
292
  // https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@keyframes#resolving_duplicates
415
- for (const child of node.chi) {
416
- previous.chi.push(child);
417
- }
418
- // @ts-ignore
419
- ast.chi.splice(i, 1);
420
- previous = ast?.chi?.[nodeIndex] ?? null;
421
- i = nodeIndex;
293
+ arena.moveChildren(node, previous);
294
+ options.cache?.delete(previous);
295
+ arena.detach(node);
296
+ cursor = arena.nextSibling[previous];
422
297
  continue;
423
298
  }
424
- let k;
425
- for (k = 0; k < node.chi.length; k++) {
426
- if (node.chi[k].typ == EnumToken.DeclarationNodeType) {
427
- let l = node.chi[k].val.length;
428
- while (l--) {
429
- if (node.chi[k].val[l].typ ==
430
- EnumToken.ImportantTokenType) {
431
- node.chi.splice(k--, 1);
299
+ let k = arena.firstChild[node];
300
+ while (k != 0) {
301
+ const next = arena.nextSibling[k];
302
+ if (arena.dataTypes[k] == EnumToken.DeclarationNodeType) {
303
+ let l = arena.lastToken[k];
304
+ while (l != 0) {
305
+ if (arena.dataTypes[l] == EnumToken.ImportantTokenType) {
306
+ arena.detach(k);
432
307
  break;
433
308
  }
434
- if ([EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(node.chi[k].val[l].typ)) {
309
+ if (arena.dataTypes[l] == EnumToken.WhitespaceTokenType ||
310
+ arena.dataTypes[l] == EnumToken.CommentTokenType) {
311
+ l = arena.previousSibling[l];
435
312
  continue;
436
313
  }
437
314
  break;
438
315
  }
439
316
  }
317
+ k = next;
440
318
  }
441
319
  }
442
- else if (node.typ == EnumToken.AtRuleNodeType) {
443
- if (node.nam == "media") {
444
- if (Array.isArray(node[TOKENS])) {
445
- const slice = node[TOKENS].slice();
446
- minifyAtRuleMedia(slice);
447
- if (slice.length !== node[TOKENS].length) {
448
- node[TOKENS].length = 0;
449
- for (const token of slice) {
450
- node[TOKENS].push(token);
451
- }
452
- node.val = slice.reduce((acc, curr, index, arr) => acc +
453
- (curr.typ === EnumToken.CommentTokenType ||
454
- (curr.typ === EnumToken.WhitespaceTokenType &&
455
- arr[index + 1]?.typ === EnumToken.CommentTokenType &&
456
- (index + 3 < arr.length ||
457
- arr[index + 2]?.typ === EnumToken.WhitespaceTokenType))
458
- ? ""
459
- : renderValue(curr)), "");
320
+ else if (nodeType == EnumToken.AtRuleNodeType) {
321
+ const nam = arena.strings[arena.name[node]];
322
+ const tokens = arena.firstToken[node] == 0 ? null : arena.tokens(node);
323
+ const renderPrelude = (acc, curr, index, arr) => acc +
324
+ (arena.dataTypes[curr] === EnumToken.CommentTokenType ||
325
+ (arena.dataTypes[curr] === EnumToken.WhitespaceTokenType &&
326
+ // inlined: arena.typeOf(arr[index + 1])
327
+ arena.dataTypes[arr[index + 1]] === EnumToken.CommentTokenType &&
328
+ // inlined: arena.typeOf(arr[index + 2])
329
+ (index + 3 < arr.length || arena.dataTypes[arr[index + 2]] === EnumToken.WhitespaceTokenType))
330
+ ? ""
331
+ : renderValue(arena, curr));
332
+ if (nam == "media") {
333
+ if (tokens != null) {
334
+ const slice = tokens.slice();
335
+ minifyAtRuleMedia(arena, slice);
336
+ if (slice.length !== tokens.length) {
337
+ arena.setTokenList(node, slice);
338
+ // inlined: arena.setValueString(node, slice.reduce(renderPrelude, ""))
339
+ arena.stringValue[node] = arena.internString(slice.reduce(renderPrelude, ""));
460
340
  }
461
341
  }
462
- if (["all", "", null].includes(node.val)) {
463
- ast.chi?.splice(i--, 1, ...node.chi);
342
+ if (["all", ""].includes(arena.strings[arena.stringValue[node]])) {
343
+ // the node is replaced by its children
344
+ cursor = arena.firstChild[node] || arena.nextSibling[node];
345
+ arena.moveChildren(node, ast, node);
346
+ arena.detach(node);
464
347
  continue;
465
348
  }
466
349
  }
467
- else if (node.nam === "import" && Array.isArray(node[TOKENS])) {
350
+ else if (nam === "import" && tokens != null) {
468
351
  let l = 0;
469
352
  let token;
470
- for (; l < node[TOKENS].length; l++) {
471
- token = node[TOKENS][l];
472
- if (token.typ === EnumToken.ParensTokenType ||
473
- token.typ === EnumToken.MediaQueryConditionTokenType ||
474
- (token.typ === EnumToken.IdenTokenType && "layer" !== token.val)) {
353
+ for (; l < tokens.length; l++) {
354
+ token = tokens[l];
355
+ if (arena.dataTypes[token] === EnumToken.ParensTokenType ||
356
+ arena.dataTypes[token] === EnumToken.MediaQueryConditionTokenType ||
357
+ (arena.dataTypes[token] === EnumToken.IdenTokenType &&
358
+ "layer" !== arena.strings[arena.stringValue[token]])) {
475
359
  break;
476
360
  }
477
361
  }
478
- if (l < node[TOKENS].length) {
479
- const slice = node[TOKENS]?.slice(l);
480
- node[TOKENS].splice(l, slice.length, ...minifyAtRuleMedia(slice));
481
- node.val = trimArray(node[TOKENS]).reduce((acc, curr, index, arr) => acc +
482
- (curr.typ === EnumToken.CommentTokenType ||
483
- (curr.typ === EnumToken.WhitespaceTokenType &&
484
- arr[index + 1]?.typ === EnumToken.CommentTokenType &&
485
- (index + 3 < arr.length || arr[index + 2].typ === EnumToken.WhitespaceTokenType))
486
- ? ""
487
- : renderValue(curr)), "");
362
+ if (l < tokens.length) {
363
+ const slice = tokens.slice(l);
364
+ tokens.splice(l, slice.length, ...minifyAtRuleMedia(arena, slice));
365
+ arena.setTokenList(node, tokens);
366
+ // inlined: arena.setValueString(node, trimArray(arena, tokens).reduce(renderPrelude, ""))
367
+ arena.stringValue[node] = arena.internString(trimArray(arena, tokens).reduce(renderPrelude, ""));
488
368
  }
489
369
  }
490
- else if (ast.typ === node.typ &&
491
- ast.nam === node.nam &&
492
- ast.val === node.val) {
493
- // @ts-ignore
494
- replaceNodeOrValue(ast, node, node.chi);
495
- i--;
370
+ else if (arena.dataTypes[ast] === nodeType &&
371
+ arena.name[ast] === arena.name[node] &&
372
+ arena.stringValue[ast] === arena.stringValue[node]) {
373
+ // the node is replaced by its children
374
+ cursor = arena.firstChild[node] || arena.nextSibling[node];
375
+ arena.moveChildren(node, ast, node);
376
+ arena.detach(node);
496
377
  continue;
497
378
  }
498
- if (previous?.typ == EnumToken.AtRuleNodeType &&
499
- node.nam != "font-face" &&
500
- previous.nam === node.nam &&
501
- previous.val === node.val) {
502
- if ("chi" in node) {
503
- for (const child of node.chi) {
504
- previous.chi.push(child);
505
- }
506
- if (!hasDeclaration(previous)) {
379
+ if (previousType == EnumToken.AtRuleNodeType &&
380
+ nam != "font-face" &&
381
+ arena.name[previous] === arena.name[node] &&
382
+ arena.stringValue[previous] === arena.stringValue[node]) {
383
+ if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
384
+ arena.moveChildren(node, previous);
385
+ options.cache?.delete(previous);
386
+ if (!hasDeclaration(arena, previous)) {
507
387
  context.nodes.delete(previous);
508
- doMinify(previous, options, recursive, errors, nestingContent, context);
388
+ doMinify(arena, previous, options, recursive, errors, nestingContent, context);
509
389
  }
510
390
  }
511
- ast?.chi?.splice(i--, 1);
391
+ cursor = arena.nextSibling[node];
392
+ arena.detach(node);
512
393
  continue;
513
394
  }
514
- // if (!hasDeclaration(node as AstAtRule)) {
515
- // doMinify(node, options, recursive, errors, nestingContent, context);
516
- // }
517
- if ("chi" in node) {
518
- doMinify(node, options, recursive, errors, nestingContent, context);
395
+ if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
396
+ doMinify(arena, node, options, recursive, errors, nestingContent, context);
519
397
  }
520
398
  previous = node;
521
- nodeIndex = i;
399
+ cursor = arena.nextSibling[node];
522
400
  continue;
523
401
  }
524
- // @ts-ignore
525
- else if (node.typ === EnumToken.RuleNodeType) {
526
- reduceRuleSelector(node);
402
+ else if (nodeType === EnumToken.RuleNodeType) {
403
+ reduceRuleSelector(arena, node);
527
404
  let wrapper = null;
528
405
  let match;
529
406
  if (options.nestingRules) {
530
- if (previous?.typ == EnumToken.RuleNodeType) {
531
- reduceRuleSelector(previous);
532
- // @ts-ignore
533
- match = matchSelectors(previous[RAW], node[RAW]);
407
+ if (previousType == EnumToken.RuleNodeType) {
408
+ reduceRuleSelector(arena, previous);
409
+ match = matchSelectors(arena.raw[previous], arena.raw[node]);
534
410
  if (match != null) {
535
- wrapper = wrapNodes(previous, node, match, ast, reducer, i, nodeIndex);
536
- nodeIndex = i - 1;
537
- previous = ast.chi[nodeIndex];
411
+ wrapper = wrapNodes(arena, previous, node, match, reducer);
538
412
  }
539
413
  }
540
414
  if (wrapper != null) {
541
- while (i < ast.chi.length) {
542
- const nextNode = ast.chi[i];
543
- if (nextNode.typ != EnumToken.RuleNodeType) {
544
- break;
545
- }
546
- reduceRuleSelector(nextNode);
547
- match = matchSelectors(wrapper[RAW], nextNode[RAW]);
415
+ // wrap the following rules that match the wrapper selector
416
+ let nextNode = arena.nextSibling[wrapper];
417
+ while (nextNode != 0 && arena.dataTypes[nextNode] == EnumToken.RuleNodeType) {
418
+ reduceRuleSelector(arena, nextNode);
419
+ match = matchSelectors(arena.raw[wrapper], arena.raw[nextNode]);
548
420
  if (match == null) {
549
421
  break;
550
422
  }
551
- wrapper = wrapNodes(wrapper, nextNode, match, ast, reducer, i, nodeIndex);
423
+ wrapper = wrapNodes(arena, wrapper, nextNode, match, reducer);
424
+ nextNode = arena.nextSibling[wrapper];
552
425
  }
553
- nodeIndex = --i;
554
- previous = ast.chi[nodeIndex];
555
- doMinify(wrapper, options, recursive, errors, nestingContent, context);
426
+ previous = wrapper;
427
+ cursor = arena.nextSibling[wrapper];
428
+ doMinify(arena, wrapper, options, recursive, errors, nestingContent, context);
556
429
  continue;
557
430
  }
558
- // @ts-ignore
559
- else if (node[OPTIMIZED] != null &&
560
- // @ts-ignore
561
- node[OPTIMIZED].match &&
562
- // @ts-ignore
563
- node[OPTIMIZED].selector.length > 1) {
564
- // @ts-ignore
565
- wrapper = {
566
- ...node,
567
- chi: [],
568
- sel: node[OPTIMIZED].optimized[0],
569
- [RAW]: [[node[OPTIMIZED].optimized[0]]],
570
- };
571
- // @ts-ignore
572
- node.sel = node[OPTIMIZED].selector.reduce(reducer, []).join(",");
573
- // @ts-ignore
574
- node[RAW] = node[OPTIMIZED].selector.slice();
575
- node[TOKENS] = null;
576
- // @ts-ignore
577
- wrapper.chi.push(node);
578
- // @ts-ignore
579
- ast.chi.splice(i, 1, wrapper);
431
+ else if (arena.optimized[node] != null &&
432
+ arena.optimized[node].match &&
433
+ arena.optimized[node].selector.length > 1) {
434
+ const optimized = arena.optimized[node];
435
+ wrapper = arena.clone(node);
436
+ // inlined: arena.setSelector(wrapper, optimized.optimized[0])
437
+ arena.name[wrapper] = arena.internString(optimized.optimized[0]);
438
+ arena.raw[wrapper] = [[optimized.optimized[0]]];
439
+ // inlined: arena.setTokens(wrapper)
440
+ arena.setTokenList(wrapper, []);
441
+ // inlined: arena.setSelector(node, optimized.selector.reduce(reducer, []).join(","))
442
+ arena.name[node] = arena.internString(optimized.selector.reduce(reducer, []).join(","));
443
+ arena.raw[node] = optimized.selector.slice();
444
+ // inlined: arena.setTokens(node)
445
+ arena.setTokenList(node, []);
446
+ // the wrapper takes the place of the node
447
+ arena.insertBefore(node, wrapper);
448
+ arena.appendChild(wrapper, node);
580
449
  node = wrapper;
450
+ nodeType = EnumToken.RuleNodeType;
581
451
  }
582
- else if (node[OPTIMIZED]?.reducible) {
583
- if (node[OPTIMIZED].optimized.length === 1) {
584
- const sel1 = node[OPTIMIZED].optimized[0] +
452
+ else if (arena.optimized[node]?.reducible) {
453
+ const optimized = arena.optimized[node];
454
+ if (optimized.optimized.length === 1) {
455
+ const sel1 = optimized.optimized[0] +
585
456
  ":is(" +
586
- node[OPTIMIZED].selector.reduce(reducer, []).join(",") +
457
+ optimized.selector.reduce(reducer, []).join(",") +
587
458
  ")";
588
- const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) =>
589
- // @ts-ignore
590
- (acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
591
- node.sel = sel1.length < sel2.length ? sel1 : sel2;
592
- node[TOKENS] = null;
459
+ const sel2 = optimized.selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") + optimized.optimized[0] + curr.join(""), "");
460
+ // inlined: arena.setSelector(node, sel1.length < sel2.length ? sel1 : sel2)
461
+ arena.name[node] = arena.internString((sel1.length < sel2.length ? sel1 : sel2));
462
+ // inlined: arena.setTokens(node)
463
+ arena.setTokenList(node, []);
593
464
  }
594
- else if (node[OPTIMIZED].optimized.length === 0) {
465
+ else if (optimized.optimized.length === 0) {
595
466
  const testIdent = /^[a-zA-Z]/;
596
- node.sel = node[OPTIMIZED].selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") +
467
+ // inlined: arena.setSelector(node, optimized.selector.reduce((acc: string, curr: string[]) => (acc.length > 0 ? acc + "," : "") + (nestingContent && testIdent.test(curr[0]) ? "& " : "") + curr.join(""), ""))
468
+ arena.name[node] = arena.internString(optimized.selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") +
597
469
  (nestingContent && testIdent.test(curr[0]) ? "& " : "") +
598
- curr.join(""), "");
599
- node[TOKENS] = null;
470
+ curr.join(""), ""));
471
+ // inlined: arena.setTokens(node)
472
+ arena.setTokenList(node, []);
600
473
  }
601
474
  }
602
475
  }
603
- // @ts-ignore
604
- else if (node[OPTIMIZED]?.match) {
476
+ else if (arena.optimized[node]?.match) {
477
+ const optimizedSelector = arena.optimized[node];
605
478
  let wrap = true;
606
- // @ts-ignore
607
- const selector = node[OPTIMIZED].selector.reduce((acc, curr) => {
479
+ const selector = optimizedSelector.selector.reduce((acc, curr) => {
608
480
  if (curr[0] == "&" && curr.length > 1) {
609
481
  if (curr[1] == " ") {
610
482
  curr.splice(0, 2);
@@ -624,7 +496,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
624
496
  wrap = selector.some((s) => s[0] != "&");
625
497
  }
626
498
  let rule = null;
627
- const optimized = node[OPTIMIZED].optimized.slice();
499
+ const optimized = optimizedSelector.optimized.slice();
628
500
  if (optimized.length > 1) {
629
501
  const check = optimized.at(-2);
630
502
  if (!combinators.includes(check)) {
@@ -670,7 +542,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
670
542
  rule = selector
671
543
  .map((s) => {
672
544
  if (s[0] == "&") {
673
- const replacement = node[OPTIMIZED].optimized;
545
+ const replacement = optimizedSelector.optimized;
674
546
  const all = new Array(Math.max(s.length - 1 + replacement.length, 0));
675
547
  let write = 0;
676
548
  for (let read = 0; read < replacement.length; read++) {
@@ -689,175 +561,176 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
689
561
  })
690
562
  .join(",");
691
563
  }
692
- let sel = wrap ? node[OPTIMIZED].optimized.join("") + `:is(${rule})` : rule;
693
- if (sel.length < node.sel.length) {
694
- node.sel = sel;
695
- node[TOKENS] = null;
564
+ let sel = wrap ? optimizedSelector.optimized.join("") + `:is(${rule})` : rule;
565
+ if (sel.length < arena.strings[arena.name[node]].length) {
566
+ // inlined: arena.setSelector(node, sel)
567
+ arena.name[node] = arena.internString(sel);
568
+ // inlined: arena.setTokens(node)
569
+ arena.setTokenList(node, []);
696
570
  }
697
571
  }
698
- else if (node[OPTIMIZED]?.reducible) {
699
- if (node[OPTIMIZED].optimized.length === 1) {
700
- const sel1 = node[OPTIMIZED].optimized[0] +
701
- ":is(" +
702
- node[OPTIMIZED].selector.reduce(reducer, []).join(",") +
703
- ")";
704
- const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) =>
705
- // @ts-ignore
706
- (acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
707
- node.sel = sel1.length < sel2.length ? sel1 : sel2;
708
- node[TOKENS] = null;
572
+ else if (arena.optimized[node]?.reducible) {
573
+ const optimized = arena.optimized[node];
574
+ if (optimized.optimized.length === 1) {
575
+ const sel1 = optimized.optimized[0] + ":is(" + optimized.selector.reduce(reducer, []).join(",") + ")";
576
+ const sel2 = optimized.selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") + optimized.optimized[0] + curr.join(""), "");
577
+ // inlined: arena.setSelector(node, sel1.length < sel2.length ? sel1 : sel2)
578
+ arena.name[node] = arena.internString((sel1.length < sel2.length ? sel1 : sel2));
579
+ // inlined: arena.setTokens(node)
580
+ arena.setTokenList(node, []);
709
581
  }
710
- else if (node[OPTIMIZED].optimized.length === 0) {
582
+ else if (optimized.optimized.length === 0) {
711
583
  const testIdent = /^[a-zA-Z]/;
712
- node.sel = node[OPTIMIZED].selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") +
584
+ // inlined: arena.setSelector(node, optimized.selector.reduce((acc: string, curr: string[]) => (acc.length > 0 ? acc + "," : "") + (nestingContent && testIdent.test(curr[0]) ? "& " : "") + curr.join(""), ""))
585
+ arena.name[node] = arena.internString(optimized.selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") +
713
586
  (nestingContent && testIdent.test(curr[0]) ? "& " : "") +
714
- curr.join(""), "");
715
- node[TOKENS] = null;
587
+ curr.join(""), ""));
588
+ // inlined: arena.setTokens(node)
589
+ arena.setTokenList(node, []);
716
590
  }
717
- // @ts-ignore
718
591
  }
719
- else if (node[OPTIMIZED]?.optimized.length > 0) {
720
- // @ts-ignore
721
- const sel = node[OPTIMIZED].optimized.join("");
722
- if (sel.length < node.sel.length) {
723
- node.sel = sel;
724
- // @ts-ignore
725
- node[RAW] = [node[OPTIMIZED].optimized.slice()];
726
- node[TOKENS] = null;
592
+ else if (arena.optimized[node]?.optimized.length > 0) {
593
+ const optimized = arena.optimized[node];
594
+ const sel = optimized.optimized.join("");
595
+ if (sel.length < arena.strings[arena.name[node]].length) {
596
+ // inlined: arena.setSelector(node, sel)
597
+ arena.name[node] = arena.internString(sel);
598
+ arena.raw[node] = [optimized.optimized.slice()];
599
+ // inlined: arena.setTokens(node)
600
+ arena.setTokenList(node, []);
727
601
  }
728
602
  }
729
- doMinify(node, options, recursive, errors, nestingContent, context);
603
+ doMinify(arena, node, options, recursive, errors, nestingContent, context);
730
604
  }
731
605
  if (previous != null) {
732
- if ("chi" in previous && "chi" in node) {
733
- if (previous.typ === node.typ) {
606
+ previousType = arena.dataTypes[previous];
607
+ if (arena.flags[previous] & EnumAstNodeStatus.HasChildren &&
608
+ arena.flags[node] & EnumAstNodeStatus.HasChildren) {
609
+ if (previousType === nodeType) {
734
610
  let shouldMerge = true;
735
- let k = previous.chi.length;
736
- while (k-- > 0) {
737
- if (previous.chi[k].typ === EnumToken.CommentNodeType ||
738
- previous.chi[k].typ === EnumToken.InvalidRuleNodeType ||
739
- previous.chi[k].typ === EnumToken.InvalidRuleNodeType) {
611
+ let k = arena.lastChild[previous];
612
+ while (k != 0) {
613
+ if (arena.dataTypes[k] === EnumToken.CommentNodeType) {
614
+ k = arena.previousSibling[k];
740
615
  continue;
741
616
  }
742
- shouldMerge = previous.chi[k].typ === EnumToken.DeclarationNodeType;
617
+ shouldMerge = arena.dataTypes[k] === EnumToken.DeclarationNodeType;
743
618
  break;
744
619
  }
745
620
  if (shouldMerge) {
746
- if (((node.typ === EnumToken.RuleNodeType ||
747
- node.typ === EnumToken.KeyframesRuleNodeType) &&
748
- node.sel === previous.sel) ||
749
- // @ts-ignore
750
- (node.typ == EnumToken.AtRuleNodeType &&
751
- node.nam !== "font-face" &&
752
- // @ts-ignore
753
- node.nam === previous.nam)) {
754
- const array = [];
755
- for (let i = 0; i < previous.chi.length; i++) {
756
- array.push(previous.chi[i]);
757
- }
758
- for (let i = 0; i < node.chi.length; i++) {
759
- array.push(node.chi[i]);
760
- }
761
- // @ts-ignore
762
- node.chi = array;
763
- doMinify(node, options, recursive, errors, nestingContent, context);
764
- ast.chi.splice(nodeIndex, 1);
765
- previous = ast.chi[--i];
766
- nodeIndex = i;
621
+ if (((nodeType === EnumToken.RuleNodeType ||
622
+ nodeType === EnumToken.KeyframesRuleNodeType) &&
623
+ arena.name[node] === arena.name[previous]) ||
624
+ (nodeType == EnumToken.AtRuleNodeType &&
625
+ arena.strings[arena.name[node]] !== "font-face" &&
626
+ arena.name[node] === arena.name[previous])) {
627
+ arena.moveChildren(previous, node, arena.firstChild[node]);
628
+ options.cache?.delete(node);
629
+ doMinify(arena, node, options, recursive, errors, nestingContent, context);
630
+ arena.detach(previous);
631
+ previous = node;
632
+ cursor = arena.nextSibling[node];
767
633
  continue;
768
634
  }
769
- else if (node.typ == previous?.typ &&
770
- [EnumToken.KeyframesRuleNodeType, EnumToken.RuleNodeType].includes(node.typ)) {
771
- const intersect = diff(previous, node, options);
635
+ else if (nodeType == previousType &&
636
+ [EnumToken.KeyframesRuleNodeType, EnumToken.RuleNodeType].includes(nodeType)) {
637
+ const intersect = diff(arena, previous, node, options, context);
772
638
  if (intersect != null) {
773
- if (intersect.node1.chi.length == 0) {
774
- ast.chi.splice(i--, 1);
639
+ // [result, node2] take the place of node1 (previous), node1 takes the place of node2 (node)
640
+ const { node1, node2, result } = intersect;
641
+ const after = arena.nextSibling[node2];
642
+ const keep2 = arena.firstChild[node2] != 0;
643
+ if (result != null) {
644
+ arena.insertBefore(node1, result);
645
+ }
646
+ if (keep2) {
647
+ arena.insertBefore(node1, node2);
775
648
  }
776
649
  else {
777
- ast.chi.splice(i--, 1, intersect.node1);
650
+ arena.detach(node2);
778
651
  }
779
- if (intersect.node2.chi.length == 0) {
780
- if (intersect.result != null) {
781
- ast.chi.splice(nodeIndex, 1, intersect.result);
782
- }
783
- else {
784
- ast.chi.splice(nodeIndex, 1);
785
- }
786
- i--;
787
- if (nodeIndex == i) {
788
- nodeIndex = i;
789
- }
652
+ if (arena.firstChild[node1] == 0) {
653
+ arena.detach(node1);
790
654
  }
791
- else {
792
- if (intersect.result != null) {
793
- ast.chi.splice(nodeIndex, 1, intersect.result, intersect.node2);
794
- }
795
- else {
796
- ast.chi.splice(nodeIndex, 1, intersect.node2);
797
- }
798
- i = (nodeIndex ?? 0) + 1;
655
+ else if (after == 0) {
656
+ arena.appendChild(ast, node1);
799
657
  }
800
- if (node != ast.chi[i]) {
801
- node = ast.chi[i];
658
+ else {
659
+ arena.insertBefore(after, node1);
802
660
  }
803
- previous = intersect.result;
804
- nodeIndex = i;
661
+ // next node to visit
662
+ node = keep2
663
+ ? arena.nextSibling[result ?? node2] || null
664
+ : before(arena.parents[node1] == ast ? node1 : after, result == null ? 1 : 2);
665
+ previous = result;
805
666
  }
806
667
  }
807
668
  }
808
669
  }
809
670
  if (recursive && previous != null && previous != node) {
810
- if (!hasDeclaration(previous)) {
811
- doMinify(previous, options, recursive, errors, nestingContent, context);
671
+ if (!hasDeclaration(arena, previous)) {
672
+ doMinify(arena, previous, options, recursive, errors, nestingContent, context);
812
673
  }
813
674
  }
814
675
  }
815
676
  }
816
677
  if (!nestingContent &&
817
678
  previous != null &&
818
- previous.typ == EnumToken.RuleNodeType &&
819
- previous.sel.includes("&")) {
820
- fixSelector(previous);
679
+ arena.dataTypes[previous] == EnumToken.RuleNodeType &&
680
+ arena.strings[arena.name[previous]].includes("&")) {
681
+ fixSelector(arena, previous);
821
682
  }
822
683
  previous = node;
823
- nodeIndex = i;
684
+ cursor = node == null ? arena.firstChild[ast] : arena.nextSibling[node];
824
685
  }
825
- // @ts-ignore
826
- if (recursive && node != null && node.chi != null) {
827
- if (node.typ == EnumToken.KeyframesAtRuleNodeType ||
828
- !node.chi.some((n) => n.typ == EnumToken.DeclarationNodeType)) {
829
- if (!(node.typ == EnumToken.AtRuleNodeType && node.nam != "font-face")) {
830
- doMinify(node, options, recursive, errors, nestingContent, context);
686
+ if (recursive && node != null && arena.flags[node] & EnumAstNodeStatus.HasChildren) {
687
+ if (arena.dataTypes[node] == EnumToken.KeyframesAtRuleNodeType || !hasDeclarationChild(arena, node)) {
688
+ if (!(arena.dataTypes[node] == EnumToken.AtRuleNodeType &&
689
+ arena.strings[arena.name[node]] != "font-face")) {
690
+ doMinify(arena, node, options, recursive, errors, nestingContent, context);
831
691
  }
832
692
  }
833
693
  }
834
694
  if (!nestingContent &&
835
695
  node != null &&
836
- node.typ == EnumToken.RuleNodeType &&
837
- node.sel.includes("&")) {
838
- fixSelector(node);
696
+ arena.dataTypes[node] == EnumToken.RuleNodeType &&
697
+ arena.strings[arena.name[node]].includes("&")) {
698
+ fixSelector(arena, node);
839
699
  }
840
700
  }
841
701
  return ast;
842
702
  }
843
703
  /**
844
- * Check if a rule has a declaration
704
+ * Check if the first non comment child is a declaration
705
+ * @param arena
845
706
  * @param node
846
707
  *
847
708
  * @private
848
709
  */
849
- function hasDeclaration(node) {
850
- // @ts-ignore
851
- for (let i = 0; i < node.chi?.length; i++) {
852
- // @ts-ignore
853
- if (node.chi[i].typ == EnumToken.CommentNodeType) {
710
+ function hasDeclaration(arena, node) {
711
+ for (let child = arena.firstChild[node]; child != 0; child = arena.nextSibling[child]) {
712
+ if (arena.dataTypes[child] == EnumToken.CommentNodeType) {
854
713
  continue;
855
714
  }
856
- // @ts-ignore
857
- return node.chi[i].typ == EnumToken.DeclarationNodeType;
715
+ return arena.dataTypes[child] == EnumToken.DeclarationNodeType;
858
716
  }
859
717
  return true;
860
718
  }
719
+ /**
720
+ * Check if the node has a declaration child
721
+ * @param arena
722
+ * @param node
723
+ *
724
+ * @private
725
+ */
726
+ function hasDeclarationChild(arena, node) {
727
+ for (let child = arena.firstChild[node]; child != 0; child = arena.nextSibling[child]) {
728
+ if (arena.dataTypes[child] == EnumToken.DeclarationNodeType) {
729
+ return true;
730
+ }
731
+ }
732
+ return false;
733
+ }
861
734
  /**
862
735
  * Optimize selector
863
736
  * @param selector
@@ -1257,105 +1130,126 @@ function matchSelectors(selector1, selector2) {
1257
1130
  }
1258
1131
  /**
1259
1132
  * Fix selector
1133
+ * @param arena
1260
1134
  * @param node
1261
1135
  *
1262
1136
  * @private
1263
1137
  */
1264
- function fixSelector(node) {
1265
- if (node.sel.includes("&")) {
1266
- const attributes = parseString(node.sel);
1267
- for (const attr of walkValues(attributes)) {
1268
- if (attr.value.typ == EnumToken.PseudoClassFuncTokenType &&
1269
- attr.value.val == ":is") {
1270
- let i = attr.value.chi.length;
1271
- while (i--) {
1272
- if (attr.value.chi[i].typ == EnumToken.NestingSelectorTokenType) {
1273
- attr.value.chi.splice(i, 1);
1138
+ function fixSelector(arena, node) {
1139
+ const sel = arena.strings[arena.name[node]];
1140
+ if (sel.includes("&")) {
1141
+ const attributes = parseString(arena, sel);
1142
+ // inlined: arena.walkValues(attributes)
1143
+ for (const attr of new ValueWalker(arena, attributes)) {
1144
+ if (arena.dataTypes[attr] == EnumToken.PseudoClassFuncTokenType &&
1145
+ arena.strings[arena.stringValue[attr]] == ":is") {
1146
+ let child = arena.lastToken[attr];
1147
+ while (child != 0) {
1148
+ const previous = arena.previousSibling[child];
1149
+ if (arena.dataTypes[child] == EnumToken.NestingSelectorTokenType) {
1150
+ arena.detach(child);
1274
1151
  }
1152
+ child = previous;
1275
1153
  }
1276
1154
  }
1277
1155
  }
1278
- node.sel = attributes.reduce((acc, curr) => acc + renderValue(curr), "");
1279
- node[TOKENS] = null;
1156
+ // inlined: arena.setSelector(node, attributes.reduce((acc: string, curr: Token) => acc + renderValue(arena, curr), ""))
1157
+ arena.name[node] = arena.internString(attributes.reduce((acc, curr) => acc + renderValue(arena, curr), ""));
1158
+ // inlined: arena.setTokens(node)
1159
+ arena.setTokenList(node, []);
1280
1160
  }
1281
1161
  }
1282
1162
  /**
1283
- * Wrap nodes
1163
+ * Wrap two sibling rules into a new rule. the wrapper takes the place of node, previous is removed
1164
+ * @param arena
1284
1165
  * @param previous
1285
1166
  * @param node
1286
1167
  * @param match
1287
- * @param ast
1288
1168
  * @param reducer
1289
- * @param i
1290
- * @param nodeIndex
1291
1169
  *
1292
1170
  * @private
1293
1171
  */
1294
- function wrapNodes(previous, node, match, ast, reducer, i, nodeIndex) {
1172
+ function wrapNodes(arena, previous, node, match, reducer) {
1295
1173
  // @ts-ignore
1296
1174
  let pSel = match.selector1.reduce(reducer, []).join(",");
1297
1175
  // @ts-ignore
1298
1176
  let nSel = match.selector2.reduce(reducer, []).join(",");
1299
- const wrapper = {
1300
- ...previous,
1301
- chi: [],
1302
- // @ts-ignore
1303
- sel: match.match.reduce(reducer, []).join(","),
1304
- [RAW]: match.match.map((t) => t.slice()),
1305
- };
1177
+ const wrapper = arena.clone(previous);
1178
+ // inlined: arena.setSelector(wrapper, match.match.reduce(reducer, []).join(","))
1179
+ // @ts-ignore
1180
+ arena.name[wrapper] = arena.internString(match.match.reduce(reducer, []).join(","));
1181
+ arena.raw[wrapper] = match.match.map((t) => t.slice());
1182
+ arena.optimized[wrapper] = null;
1183
+ // inlined: arena.setTokens(wrapper)
1184
+ arena.setTokenList(wrapper, []);
1185
+ const parent = arena.parents[node];
1186
+ // the wrapper takes the place of node
1187
+ arena.insertBefore(node, wrapper);
1306
1188
  if (pSel == "&" || pSel === "") {
1307
- for (const child of previous.chi) {
1308
- wrapper.chi.push(child);
1309
- }
1189
+ arena.moveChildren(previous, wrapper);
1310
1190
  if (nSel == "&" || nSel === "") {
1311
- for (const child of node.chi) {
1312
- wrapper.chi.push(child);
1313
- }
1191
+ arena.moveChildren(node, wrapper);
1314
1192
  }
1315
1193
  else {
1316
- wrapper.chi.push(node);
1194
+ arena.appendChild(wrapper, node);
1317
1195
  }
1318
1196
  }
1319
1197
  else {
1320
- wrapper.chi.push(previous, node);
1198
+ arena.appendChild(wrapper, previous);
1199
+ arena.appendChild(wrapper, node);
1200
+ }
1201
+ // the nodes whose children were moved into the wrapper are dropped
1202
+ if (arena.parents[previous] == parent) {
1203
+ arena.detach(previous);
1321
1204
  }
1322
- ast.chi.splice(i, 1, wrapper);
1323
- ast.chi.splice(nodeIndex, 1);
1324
- previous.sel = pSel;
1325
- previous[RAW] = match.selector1;
1326
- previous[TOKENS] = null;
1327
- node.sel = nSel;
1328
- node[RAW] = match.selector2;
1329
- node[TOKENS] = null;
1330
- reduceRuleSelector(wrapper);
1331
- wrapper[TOKENS] = null;
1205
+ if (arena.parents[node] == parent) {
1206
+ arena.detach(node);
1207
+ }
1208
+ // update the selectors of the wrapped nodes
1209
+ if (arena.parents[previous] == wrapper) {
1210
+ // inlined: arena.setSelector(previous, pSel)
1211
+ arena.name[previous] = arena.internString(pSel);
1212
+ arena.raw[previous] = match.selector1;
1213
+ // inlined: arena.setTokens(previous)
1214
+ arena.setTokenList(previous, []);
1215
+ }
1216
+ if (arena.parents[node] == wrapper) {
1217
+ // inlined: arena.setSelector(node, nSel)
1218
+ arena.name[node] = arena.internString(nSel);
1219
+ arena.raw[node] = match.selector2;
1220
+ // inlined: arena.setTokens(node)
1221
+ arena.setTokenList(node, []);
1222
+ }
1223
+ reduceRuleSelector(arena, wrapper);
1224
+ // inlined: arena.setTokens(wrapper)
1225
+ arena.setTokenList(wrapper, []);
1332
1226
  return wrapper;
1333
1227
  }
1334
1228
  /**
1335
- * Diff nodes
1229
+ * Diff nodes: move the declarations shared by both rules into a new rule
1230
+ * @param arena
1336
1231
  * @param n1
1337
1232
  * @param n2
1338
1233
  * @param options
1234
+ * @param context minification context
1339
1235
  *
1340
1236
  * @private
1341
1237
  */
1342
- function diff(n1, n2, options = {}) {
1238
+ function diff(arena, n1, n2, options, context) {
1343
1239
  if (!("cache" in options)) {
1344
- options.cache = new WeakMap();
1240
+ options.cache = new Map();
1345
1241
  }
1346
1242
  let node1 = n1;
1347
1243
  let node2 = n2;
1348
1244
  let exchanged = false;
1349
- if (node1.chi.length > node2.chi.length) {
1245
+ if (arena.childCount(node1) > arena.childCount(node2)) {
1350
1246
  const t = node1;
1351
1247
  node1 = node2;
1352
1248
  node2 = t;
1353
1249
  exchanged = true;
1354
1250
  }
1355
- let i = node1.chi.length;
1356
- let j = node2.chi.length;
1357
- const raw1 = node1[RAW];
1358
- const raw2 = node2[RAW];
1251
+ const raw1 = arena.raw[node1];
1252
+ const raw2 = arena.raw[node2];
1359
1253
  if (raw1 != null && raw2 != null) {
1360
1254
  const prefixes1 = new Set();
1361
1255
  const prefixes2 = new Set();
@@ -1402,136 +1296,183 @@ function diff(n1, n2, options = {}) {
1402
1296
  }
1403
1297
  }
1404
1298
  }
1405
- const css1 = options.cache.get(node1);
1406
- const css2 = options.cache.get(node2);
1407
- node1 = { ...node1, chi: node1.chi.slice() };
1408
- node2 = { ...node2, chi: node2.chi.slice() };
1409
- if (css1 != null) {
1410
- options.cache.set(node1, css1);
1411
- }
1412
- if (css2 != null) {
1413
- options.cache.set(node2, css2);
1414
- }
1415
- if (raw1 != null) {
1416
- node1[RAW] = raw1;
1417
- }
1418
- if (raw2 != null) {
1419
- node2[RAW] = raw2;
1420
- }
1421
- const intersect = [];
1422
- while (i--) {
1423
- if (node1.chi[i].typ == EnumToken.CommentNodeType) {
1299
+ // match the declarations of both rules, from the last to the first
1300
+ let matches = 0;
1301
+ for (let child1 = arena.lastChild[node1]; child1 != 0; child1 = arena.previousSibling[child1]) {
1302
+ if (arena.dataTypes[child1] == EnumToken.CommentNodeType) {
1424
1303
  continue;
1425
1304
  }
1426
- j = node2.chi.length;
1427
- while (j--) {
1428
- if (node2.chi[j].typ == EnumToken.CommentNodeType) {
1305
+ for (let child2 = arena.lastChild[node2]; child2 != 0; child2 = arena.previousSibling[child2]) {
1306
+ if (arena.dataTypes[child2] == EnumToken.CommentNodeType ||
1307
+ arena.flags[child2] & EnumAstNodeStatus.Matched) {
1429
1308
  continue;
1430
1309
  }
1431
- if (node1.chi[i].nam == node2.chi[j].nam) {
1432
- if (node1.chi[i].typ == node2.chi[j].typ && eq(node1.chi[i], node2.chi[j])) {
1433
- intersect.push(node1.chi[i]);
1434
- node1.chi.splice(i, 1);
1435
- node2.chi.splice(j, 1);
1436
- options.cache.delete(node1);
1437
- options.cache.delete(node2);
1438
- break;
1439
- }
1310
+ if (arena.name[child1] == arena.name[child2] && eqNode(arena, child1, child2)) {
1311
+ arena.flags[child1] |= EnumAstNodeStatus.Matched;
1312
+ arena.flags[child2] |= EnumAstNodeStatus.Matched;
1313
+ matches++;
1314
+ break;
1440
1315
  }
1441
1316
  }
1442
1317
  }
1443
- const result = intersect.length === 0 && (node1.chi.length > 0 || node2.chi.length > 0)
1444
- ? null
1445
- : {
1446
- ...node1,
1447
- // @ts-ignore
1448
- sel: [
1449
- ...new Set(splitRule(node1.sel)
1450
- .concat(splitRule(node2.sel))
1451
- .map((s) => s.join(""))),
1452
- ].join(","),
1453
- // @ts-ignore
1454
- chi: intersect.reverse(),
1455
- };
1456
- let op = { level: 0, ...options };
1457
- if (result == null ||
1458
- [n1, n2].reduce((acc, curr) => {
1459
- let css = options.cache.get(curr);
1460
- if (css == null) {
1461
- let level = 0;
1462
- let parent = curr[PARENT];
1463
- while (parent != null && parent.typ != EnumToken.StyleSheetNodeType) {
1464
- level++;
1465
- parent = parent[PARENT];
1466
- }
1467
- op.level = level;
1468
- css = doRender(curr, op).code;
1469
- options.cache.set(curr, css);
1318
+ if (matches == 0 && (arena.firstChild[node1] != 0 || arena.firstChild[node2] != 0)) {
1319
+ return null;
1320
+ }
1321
+ // the rules are rendered with the same options during the minification: the options are resolved once and
1322
+ // identical values are rendered once
1323
+ const renderer = (context.renderer ??= new NodeRenderer(options));
1324
+ const renderedSize = (node, cache) => {
1325
+ let css = cache ? options.cache.get(node) : undefined;
1326
+ if (css == null) {
1327
+ css = renderer.render(arena, node);
1328
+ if (cache) {
1329
+ options.cache.set(node, css);
1470
1330
  }
1471
- return curr.chi.length == 0 ? acc : acc + css.length;
1472
- }, 0) <=
1473
- [node1, node2, result].reduce((acc, curr) => {
1474
- let css = options.cache.get(curr);
1475
- if (css != null) {
1476
- return curr.chi.length == 0 ? acc : acc + css.length;
1331
+ }
1332
+ return arena.firstChild[node] == 0 ? 0 : css.length;
1333
+ };
1334
+ // size of the original nodes
1335
+ const size = renderedSize(n1, true) + renderedSize(n2, true);
1336
+ // the shared declarations are moved into the result node
1337
+ const result = arena.clone(node1);
1338
+ // inlined: arena.setSelector(result, [ ...new Set(splitRule(arena.strings[arena.name[node1]]) .concat(splitRule(arena.strings[arena.name[node2]])) .map((s) => s.join(""))), ].join(","))
1339
+ arena.name[result] = arena.internString([
1340
+ ...new Set(splitRule(arena.strings[arena.name[node1]])
1341
+ .concat(splitRule(arena.strings[arena.name[node2]]))
1342
+ .map((s) => s.join(""))),
1343
+ ].join(","));
1344
+ // the matched declarations of node1 are moved into the result, the duplicates in node2 are dropped.
1345
+ // the moves are recorded (node, parent, next sibling) to be undone when the result is not smaller
1346
+ const moves = [];
1347
+ const moveMatched = (parent, target) => {
1348
+ let child = arena.firstChild[parent];
1349
+ while (child != 0) {
1350
+ const next = arena.nextSibling[child];
1351
+ if (arena.flags[child] & EnumAstNodeStatus.Matched) {
1352
+ arena.flags[child] &= ~EnumAstNodeStatus.Matched;
1353
+ moves.push(child, parent, next);
1354
+ if (target == null) {
1355
+ arena.detach(child);
1477
1356
  }
1478
- let level = 0;
1479
- let parent = curr[PARENT];
1480
- while (parent != null && parent.typ != EnumToken.StyleSheetNodeType) {
1481
- level++;
1482
- parent = parent[PARENT];
1357
+ else {
1358
+ arena.appendChild(target, child);
1483
1359
  }
1484
- op.level = level;
1485
- css = doRender(curr, op).code;
1486
- return curr.chi.length == 0 ? acc : acc + css.length;
1487
- }, 0)) {
1488
- if (node1.chi.length != 0 && node2.chi.length != 0) {
1489
- return null;
1360
+ }
1361
+ child = next;
1490
1362
  }
1363
+ };
1364
+ moveMatched(node1, result);
1365
+ moveMatched(node2, null);
1366
+ arena.insertBefore(n1, result);
1367
+ options.cache.delete(node1);
1368
+ options.cache.delete(node2);
1369
+ if (size <= renderedSize(node1, false) + renderedSize(node2, false) + renderedSize(result, false) &&
1370
+ arena.firstChild[node1] != 0 &&
1371
+ arena.firstChild[node2] != 0) {
1372
+ // restore the nodes: undo the moves in reverse order
1373
+ for (let k = moves.length - 3; k >= 0; k -= 3) {
1374
+ if (moves[k + 2] == 0) {
1375
+ arena.appendChild(moves[k + 1], moves[k]);
1376
+ }
1377
+ else {
1378
+ arena.insertBefore(moves[k + 2], moves[k]);
1379
+ }
1380
+ }
1381
+ arena.detach(result);
1382
+ return null;
1491
1383
  }
1492
- if (result != null) {
1493
- result[TOKENS] = null;
1494
- result[RAW] = null;
1495
- const optimized = optimizeSelector(splitRule(result.sel));
1496
- if (optimized?.match) {
1497
- const rule = optimized.selector.reduce((acc, curr) => {
1498
- if (acc.length > 0) {
1499
- acc += ",";
1500
- }
1501
- if (curr.length > 2 && curr[0] === "&" && curr[1] === " ") {
1502
- return acc + curr.slice(2).join("");
1503
- }
1504
- else if (curr.length > 1 && curr[0] === "&") {
1505
- return acc + curr.slice(1).join("");
1506
- }
1507
- return acc + curr.join("");
1508
- }, "");
1509
- const match = optimized.optimized.join("");
1510
- const sel = match + ":is(" + replaceCompound(rule, match) + ")";
1511
- if (sel.length < result.sel.length) {
1512
- result.sel = sel;
1513
- result[TOKENS] = null;
1384
+ // inlined: arena.setTokens(result)
1385
+ arena.setTokenList(result, []);
1386
+ arena.raw[result] = null;
1387
+ arena.optimized[result] = null;
1388
+ const optimized = optimizeSelector(splitRule(arena.strings[arena.name[result]]));
1389
+ if (optimized?.match) {
1390
+ const rule = optimized.selector.reduce((acc, curr) => {
1391
+ if (acc.length > 0) {
1392
+ acc += ",";
1514
1393
  }
1394
+ if (curr.length > 2 && curr[0] === "&" && curr[1] === " ") {
1395
+ return acc + curr.slice(2).join("");
1396
+ }
1397
+ else if (curr.length > 1 && curr[0] === "&") {
1398
+ return acc + curr.slice(1).join("");
1399
+ }
1400
+ return acc + curr.join("");
1401
+ }, "");
1402
+ const match = optimized.optimized.join("");
1403
+ const sel = match + ":is(" + arena.replaceCompound(rule, match) + ")";
1404
+ if (sel.length < arena.strings[arena.name[result]].length) {
1405
+ // inlined: arena.setSelector(result, sel)
1406
+ arena.name[result] = arena.internString(sel);
1407
+ // inlined: arena.setTokens(result)
1408
+ arena.setTokenList(result, []);
1515
1409
  }
1516
1410
  }
1411
+ arena.detach(result);
1517
1412
  return { result, node1: exchanged ? node2 : node1, node2: exchanged ? node1 : node2 };
1518
1413
  }
1414
+ /**
1415
+ * compare two nodes
1416
+ * @param arena
1417
+ * @param a
1418
+ * @param b
1419
+ *
1420
+ * @private
1421
+ */
1422
+ function eqNode(arena, a, b) {
1423
+ if (a == b) {
1424
+ return true;
1425
+ }
1426
+ if (arena.dataTypes[a] != arena.dataTypes[b] ||
1427
+ arena.name[a] != arena.name[b] ||
1428
+ arena.stringValue[a] != arena.stringValue[b]) {
1429
+ return false;
1430
+ }
1431
+ if (arena.flags[a] & EnumAstNodeStatus.HasValues) {
1432
+ let token1 = arena.firstToken[a];
1433
+ let token2 = arena.firstToken[b];
1434
+ while (token1 != 0 && token2 != 0) {
1435
+ if (!eqToken(arena, token1, token2)) {
1436
+ return false;
1437
+ }
1438
+ token1 = arena.nextSibling[token1];
1439
+ token2 = arena.nextSibling[token2];
1440
+ }
1441
+ if (token1 != token2) {
1442
+ return false;
1443
+ }
1444
+ }
1445
+ if (arena.flags[a] & EnumAstNodeStatus.HasChildren) {
1446
+ let child1 = arena.firstChild[a];
1447
+ let child2 = arena.firstChild[b];
1448
+ while (child1 != 0 && child2 != 0) {
1449
+ if (!eqNode(arena, child1, child2)) {
1450
+ return false;
1451
+ }
1452
+ child1 = arena.nextSibling[child1];
1453
+ child2 = arena.nextSibling[child2];
1454
+ }
1455
+ return child1 == child2;
1456
+ }
1457
+ return true;
1458
+ }
1519
1459
  /**
1520
1460
  * Reduce rule selector
1461
+ * @param arena
1521
1462
  * @param node
1522
1463
  *
1523
1464
  * @private
1524
1465
  */
1525
- function reduceRuleSelector(node) {
1526
- if (node[RAW] == null) {
1527
- node[RAW] = splitRule(node.sel);
1466
+ function reduceRuleSelector(arena, node) {
1467
+ if (arena.raw[node] == null) {
1468
+ arena.raw[node] = splitRule(arena.strings[arena.name[node]]);
1528
1469
  }
1529
- let optimized = optimizeSelector(node[RAW].reduce((acc, curr) => {
1470
+ let optimized = optimizeSelector(arena.raw[node].reduce((acc, curr) => {
1530
1471
  acc.push(curr.slice());
1531
1472
  return acc;
1532
1473
  }, []));
1533
1474
  if (optimized != null) {
1534
- node[OPTIMIZED] = optimized;
1475
+ arena.optimized[node] = optimized;
1535
1476
  }
1536
1477
  if (optimized != null && optimized.match && optimized.reducible && optimized.selector.length > 1) {
1537
1478
  for (const selector of optimized.selector) {
@@ -1567,10 +1508,12 @@ function reduceRuleSelector(node) {
1567
1508
  [optimized.optimized[0], reduced.length === 1 ? reduced.join("") : ":is("].concat(reduced).concat(")"),
1568
1509
  ];
1569
1510
  const sel = raw[0].join("");
1570
- if (sel.length < node.sel.length) {
1571
- node.sel = sel;
1572
- node[RAW] = raw;
1573
- node[TOKENS] = null;
1511
+ if (sel.length < arena.strings[arena.name[node]].length) {
1512
+ // inlined: arena.setSelector(node, sel)
1513
+ arena.name[node] = arena.internString(sel);
1514
+ arena.raw[node] = raw;
1515
+ // inlined: arena.setTokens(node)
1516
+ arena.setTokenList(node, []);
1574
1517
  }
1575
1518
  }
1576
1519
  }