@tbela99/css-parser 1.8.2 → 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 +62 -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/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 -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,27 +2,74 @@
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.0
6
6
  License: MIT
7
7
  */
8
- import { eq } from '../utils/eq.js';
8
+ import { eqToken } from '../utils/eq.js';
9
9
  import { getConfig } from '../utils/config.js';
10
10
  import { matchType } from '../utils/type.js';
11
11
  import { EnumToken } from '../../ast/types.js';
12
12
  import { renderValue } from '../../printer/render.js';
13
13
  import { parseString } from '../parse.js';
14
14
  import { PropertySet } from './set.js';
15
- import { PROPERTYNAME } from '../../syntax/constants.js';
16
- import { cloneNode } from '../../ast/clone.js';
17
15
 
18
16
  const propertiesConfig = getConfig();
17
+ /**
18
+ * token type of a configuration token
19
+ * @param config
20
+ */
21
+ function configType(config) {
22
+ return EnumToken[config.typ];
23
+ }
24
+ /**
25
+ * create a token from the shorthand configuration
26
+ * @param arena
27
+ * @param config
28
+ */
29
+ function configToken(arena, config) {
30
+ if (config == null) {
31
+ return arena.createToken(EnumToken.WhitespaceTokenType);
32
+ }
33
+ return arena.createToken(configType(config), config.val);
34
+ }
35
+ /**
36
+ * true if the token matches the configuration token
37
+ * @param arena
38
+ * @param token
39
+ * @param config
40
+ */
41
+ function matchConfigToken(arena, token, config) {
42
+ return (config != null &&
43
+ arena.dataTypes[token] == configType(config) &&
44
+ (config.val == null || arena.strings[arena.stringValue[token]] == config.val));
45
+ }
46
+ /**
47
+ * map of declarations that can be merged into a single shorthand declaration.
48
+ * declarations are arena node ids
49
+ */
19
50
  class PropertyMap {
20
51
  declarations;
21
52
  config;
53
+ arena;
22
54
  requiredCount;
23
55
  pattern;
24
- constructor(config) {
56
+ /**
57
+ * longhand property name of the matched tokens
58
+ */
59
+ propertyNames = new Map();
60
+ /**
61
+ * results shared by the lists of a minification: the default values are parsed once
62
+ */
63
+ memo;
64
+ /**
65
+ * @param arena
66
+ * @param config
67
+ * @param memo results shared by the lists of a minification
68
+ */
69
+ constructor(arena, config, memo = null) {
70
+ this.memo = memo;
25
71
  const values = Object.values(config.properties);
72
+ this.arena = arena;
26
73
  this.requiredCount =
27
74
  values.reduce((acc, curr) => (curr.required ? ++acc : acc), 0) ||
28
75
  values.length;
@@ -30,50 +77,47 @@ class PropertyMap {
30
77
  this.declarations = new Map();
31
78
  this.pattern = config.pattern.split(/\s/);
32
79
  }
80
+ /**
81
+ * tokens of a value (default values of the properties, ...)
82
+ * @param value
83
+ */
84
+ parse(value) {
85
+ return this.memo == null ? parseString(this.arena, value) : this.memo.parse(this.arena, value);
86
+ }
33
87
  add(declaration) {
34
- if (declaration.nam == this.config.shorthand) {
88
+ const arena = this.arena;
89
+ const nam = arena.strings[arena.name[declaration]];
90
+ if (nam == this.config.shorthand) {
35
91
  this.declarations.clear();
36
- this.declarations.set(declaration.nam, declaration);
92
+ this.declarations.set(nam, declaration);
37
93
  this.matchTypes(declaration);
38
94
  }
39
95
  else {
40
- const separator = this.config.separator != null
41
- ? {
42
- ...this.config.separator,
43
- // @ts-ignore
44
- typ: EnumToken[this.config.separator.typ],
45
- }
46
- : null;
96
+ const separator = this.config.separator ?? null;
47
97
  // expand shorthand
48
98
  if (this.declarations.has(this.config.shorthand)) {
49
99
  const tokens = {};
50
100
  const values = [];
51
101
  const val = [[]];
52
- for (const curr of this.declarations.get(this.config.shorthand).val) {
53
- // @ts-ignore
54
- // @ts-ignore
55
- if (separator != null && separator.typ == curr.typ && separator.val == curr.val) {
102
+ for (const curr of arena.tokens(this.declarations.get(this.config.shorthand))) {
103
+ if (matchConfigToken(arena, curr, separator)) {
56
104
  val.push([]);
57
105
  continue;
58
106
  }
59
- // @ts-ignore
60
- val.at(-1).push(curr);
107
+ val[val.length - 1].push(curr);
61
108
  }
62
109
  for (let current = 0; current < val.length; current++) {
63
110
  values.push(...this.pattern.reduce((acc, property) => {
64
111
  // let current: number = 0;
65
112
  const props = this.config.properties[property];
66
113
  for (let i = 0; i < acc.length; i++) {
67
- if (acc[i].typ == EnumToken.CommentTokenType ||
68
- acc[i].typ == EnumToken.WhitespaceTokenType) {
114
+ if (arena.dataTypes[acc[i]] == EnumToken.CommentTokenType ||
115
+ arena.dataTypes[acc[i]] == EnumToken.WhitespaceTokenType) {
69
116
  acc.splice(i, 1);
70
117
  i--;
71
118
  continue;
72
119
  }
73
- if (
74
- // @ts-ignore
75
- acc[i][PROPERTYNAME] == property ||
76
- matchType(acc[i], props)) {
120
+ if (this.propertyNames.get(acc[i]) == property || matchType(arena, acc[i], props)) {
77
121
  if ("prefix" in props && props.previous != null && !(props.previous in tokens)) {
78
122
  return acc;
79
123
  }
@@ -85,21 +129,16 @@ class PropertyMap {
85
129
  tokens[property].push([acc[i]]);
86
130
  }
87
131
  else {
88
- tokens[property][current].push({ typ: EnumToken.WhitespaceTokenType }, acc[i]);
132
+ tokens[property][current].push(arena.createToken(EnumToken.WhitespaceTokenType), acc[i]);
89
133
  }
90
134
  }
91
135
  acc.splice(i, 1);
92
136
  i--;
93
- // @ts-ignore
94
- if ("prefix" in props && acc[i]?.typ == EnumToken[props.prefix.typ]) {
95
- if (
96
- // @ts-ignore
97
- acc[i].typ == EnumToken[props.prefix.typ] &&
98
- // @ts-ignore
99
- acc[i].val == this.config.properties[property].prefix.val) {
100
- acc.splice(i, 1);
101
- i--;
102
- }
137
+ if ("prefix" in props &&
138
+ i >= 0 &&
139
+ matchConfigToken(arena, acc[i], props.prefix)) {
140
+ acc.splice(i, 1);
141
+ i--;
103
142
  }
104
143
  if (props.multiple) {
105
144
  continue;
@@ -117,7 +156,8 @@ class PropertyMap {
117
156
  }
118
157
  // default
119
158
  if (props.default.length > 0) {
120
- const defaults = parseString(props.default[0]);
159
+ // inlined: this.parse(props.default[0])
160
+ const defaults = this.memo == null ? parseString(this.arena, props.default[0]) : this.memo.parse(this.arena, props.default[0]);
121
161
  if (!(property in tokens)) {
122
162
  tokens[property] = [[...defaults]];
123
163
  }
@@ -129,9 +169,7 @@ class PropertyMap {
129
169
  }
130
170
  }
131
171
  else {
132
- tokens[property][current].push({
133
- typ: EnumToken.WhitespaceTokenType,
134
- });
172
+ tokens[property][current].push(arena.createToken(EnumToken.WhitespaceTokenType));
135
173
  for (let i = 0; i < defaults.length; i++) {
136
174
  tokens[property][current].push(defaults[i]);
137
175
  }
@@ -143,32 +181,29 @@ class PropertyMap {
143
181
  }
144
182
  if (values.length == 0) {
145
183
  this.declarations = Object.entries(tokens).reduce((acc, curr) => {
146
- acc.set(curr[0], {
147
- typ: EnumToken.DeclarationNodeType,
148
- nam: curr[0],
149
- val: curr[1].reduce((acc, curr) => {
150
- if (acc.length > 0) {
151
- acc.push({ ...separator });
152
- }
153
- for (let i = 0; i < curr.length; i++) {
154
- acc.push(curr[i]);
155
- }
156
- return acc;
157
- }, []),
158
- });
184
+ acc.set(curr[0], arena.createDeclaration(EnumToken.DeclarationNodeType, curr[0], curr[1].reduce((acc, curr) => {
185
+ if (acc.length > 0) {
186
+ acc.push(configToken(arena, separator));
187
+ }
188
+ for (let i = 0; i < curr.length; i++) {
189
+ acc.push(this.take(curr[i]));
190
+ }
191
+ return acc;
192
+ }, []), null, null));
159
193
  return acc;
160
194
  }, new Map());
161
195
  }
162
196
  }
163
197
  // @ts-ignore
164
- const config = propertiesConfig.properties[declaration.nam];
165
- let property = declaration.nam;
198
+ const config = propertiesConfig.properties[nam];
199
+ let property = nam;
166
200
  if (config != null) {
167
201
  property = config.shorthand;
168
202
  let value = this.declarations.get(property);
169
203
  if (!(value instanceof PropertySet)) {
204
+ this.declarations.set(property,
170
205
  // @ts-ignore
171
- this.declarations.set(property, new PropertySet(propertiesConfig.properties[config.shorthand]));
206
+ new PropertySet(arena, propertiesConfig.properties[config.shorthand]));
172
207
  // Token[]
173
208
  if (value != null) {
174
209
  // @ts-ignore
@@ -178,12 +213,13 @@ class PropertyMap {
178
213
  this.declarations.get(property).add(declaration);
179
214
  }
180
215
  else {
181
- this.declarations.set(declaration.nam, declaration);
216
+ this.declarations.set(nam, declaration);
182
217
  }
183
218
  }
184
219
  return this;
185
220
  }
186
221
  [Symbol.iterator]() {
222
+ const arena = this.arena;
187
223
  const propertiesMapping = { ...this.config.properties };
188
224
  const patterns = this.config.pattern.split(" ");
189
225
  let hasMapping = false;
@@ -202,26 +238,27 @@ class PropertyMap {
202
238
  const mapped = {};
203
239
  let key;
204
240
  for (const value of declarations.values()) {
205
- key = value.nam;
206
241
  if (value instanceof PropertyMap) {
207
242
  for (const [k, v] of value.declarations) {
208
243
  if (k == value.config.shorthand) {
244
+ const values = arena.tokens(v);
209
245
  for (const [key] of Object.entries(value.config.properties)) {
210
- mapped[key] = cloneNode(v);
211
- for (let i = 0; i < v.val.length; i++) {
212
- // @ts-ignore
213
- if (key == v.val[i][PROPERTYNAME]) {
214
- if (mapped[key].val.length > 0) {
215
- mapped[key].val.push({
216
- typ: EnumToken.WhitespaceTokenType,
217
- });
246
+ mapped[key] = arena.clone(v);
247
+ const mappedValues = [];
248
+ for (let i = 0; i < values.length; i++) {
249
+ if (key == value.propertyNames.get(values[i])) {
250
+ if (mappedValues.length > 0) {
251
+ mappedValues.push(arena.createToken(EnumToken.WhitespaceTokenType));
218
252
  }
219
- mapped[key].val.push(v.val[i]);
253
+ mappedValues.push(value.take(values[i]));
220
254
  }
221
255
  }
222
- if (mapped[key].val.length == 0) {
223
- mapped[key].val.push(...parseString(value.config.properties[key].default[0]));
256
+ if (mappedValues.length == 0) {
257
+ mappedValues.push(
258
+ // inlined: this.parse((value as PropertyMap).config.properties[key].default[0])
259
+ ...(this.memo == null ? parseString(this.arena, value.config.properties[key].default[0]) : this.memo.parse(this.arena, value.config.properties[key].default[0])));
224
260
  }
261
+ arena.setTokenList(mapped[key], mappedValues);
225
262
  }
226
263
  }
227
264
  else {
@@ -230,6 +267,9 @@ class PropertyMap {
230
267
  }
231
268
  }
232
269
  else {
270
+ // a property set has no name
271
+ key =
272
+ value instanceof PropertySet ? "undefined" : arena.strings[arena.name[value]];
233
273
  mapped[key] = value;
234
274
  }
235
275
  }
@@ -273,30 +313,18 @@ class PropertyMap {
273
313
  let map = new Map();
274
314
  let value = [];
275
315
  let values = [];
276
- // @ts-ignore
277
- let typ = (EnumToken[this.config.separator?.typ] ?? EnumToken.CommaTokenType);
278
- // @ts-ignore
279
- const sep = this.config.separator == null
280
- ? null
281
- : {
282
- ...this.config.separator,
283
- typ: EnumToken[this.config.separator.typ],
284
- };
285
- // @ts-ignore
286
- const separator = this.config.separator
287
- ? renderValue({
288
- ...this.config.separator,
289
- typ: EnumToken[this.config.separator.typ],
290
- })
291
- : ",";
316
+ const sep = this.config.separator ?? null;
317
+ const typ = sep == null ? EnumToken.CommaTokenType : configType(sep);
318
+ const separator = sep == null ? "," : (sep.val ?? "");
292
319
  this.matchTypes(declaration);
293
320
  values.push(value);
294
- for (i = 0; i < declaration.val.length; i++) {
295
- t = declaration.val[i];
321
+ const declarationValues = arena.tokens(declaration);
322
+ for (i = 0; i < declarationValues.length; i++) {
323
+ t = declarationValues[i];
296
324
  if (!cache.has(t)) {
297
- cache.set(t, renderValue(t, { minify: true }));
325
+ cache.set(t, renderValue(arena, t, { minify: true }));
298
326
  }
299
- if (t.typ == typ && separator == cache.get(t)) {
327
+ if (arena.dataTypes[t] == typ && separator == cache.get(t)) {
300
328
  this.removeDefaults(map, value);
301
329
  value = [];
302
330
  values.push(value);
@@ -304,40 +332,40 @@ class PropertyMap {
304
332
  continue;
305
333
  }
306
334
  value.push(t);
307
- // @ts-ignore
308
- if (t[PROPERTYNAME] != null) {
309
- // @ts-ignore
310
- if (!map.has(t[PROPERTYNAME])) {
311
- // @ts-ignore
312
- map.set(t[PROPERTYNAME], { t: [t], value: [cache.get(t)] });
335
+ const propertyName = this.propertyNames.get(t);
336
+ if (propertyName != null) {
337
+ if (!map.has(propertyName)) {
338
+ map.set(propertyName, { t: [t], value: [cache.get(t)] });
313
339
  }
314
340
  else {
315
- // @ts-ignore
316
- const v = map.get(t[PROPERTYNAME]);
341
+ const v = map.get(propertyName);
317
342
  v.t.push(t);
318
343
  v.value.push(cache.get(t));
319
344
  }
320
345
  }
321
346
  }
322
347
  this.removeDefaults(map, value);
323
- declaration.val = values.reduce((acc, curr) => {
348
+ const result = values.reduce((acc, curr) => {
324
349
  if (sep != null && acc.length > 0) {
325
- acc.push({ ...sep });
350
+ acc.push(configToken(arena, sep));
326
351
  }
327
352
  for (const cr of curr) {
328
- if (cr.typ == EnumToken.WhitespaceTokenType && acc.at(-1)?.typ == cr.typ) {
353
+ if (arena.dataTypes[cr] == EnumToken.WhitespaceTokenType &&
354
+ acc.length > 0 &&
355
+ arena.dataTypes[acc[acc.length - 1]] == EnumToken.WhitespaceTokenType) {
329
356
  continue;
330
357
  }
331
358
  acc.push(cr);
332
359
  }
333
360
  return acc;
334
361
  }, []);
335
- while (declaration.val.at(-1)?.typ == EnumToken.WhitespaceTokenType) {
336
- declaration.val.pop();
362
+ while (result.length > 0 && arena.dataTypes[result[result.length - 1]] == EnumToken.WhitespaceTokenType) {
363
+ result.pop();
337
364
  }
338
- while (declaration.val.at(0)?.typ == EnumToken.WhitespaceTokenType) {
339
- declaration.val.shift();
365
+ while (result.length > 0 && arena.dataTypes[result[0]] == EnumToken.WhitespaceTokenType) {
366
+ result.shift();
340
367
  }
368
+ arena.setTokenList(declaration, result);
341
369
  return declaration;
342
370
  };
343
371
  const values = [...declarations.values()].reduce((acc, curr) => {
@@ -356,27 +384,23 @@ class PropertyMap {
356
384
  const filtered = [];
357
385
  for (const declaration of values) {
358
386
  dec = removeDefaults(declaration);
359
- for (const t of dec.val) {
360
- if (t.typ == EnumToken.ImportantTokenType) {
387
+ for (const t of arena.tokens(dec)) {
388
+ if (arena.dataTypes[t] == EnumToken.ImportantTokenType) {
361
389
  isImportant = true;
362
390
  }
363
391
  if (filtered.length == 0 &&
364
- t.typ != EnumToken.WhitespaceTokenType &&
365
- t.typ != EnumToken.ImportantTokenType) {
392
+ arena.dataTypes[t] != EnumToken.WhitespaceTokenType &&
393
+ arena.dataTypes[t] != EnumToken.ImportantTokenType) {
366
394
  filtered.push(dec);
367
395
  }
368
396
  }
369
397
  }
370
398
  if (filtered.length == 0 && this.config.default.length > 0) {
371
- filtered.push({
372
- typ: EnumToken.DeclarationNodeType,
373
- nam: this.config.shorthand,
374
- val: parseString(this.config.default[0]),
375
- });
399
+ filtered.push(arena.createDeclaration(EnumToken.DeclarationNodeType, this.config.shorthand,
400
+ // inlined: this.parse(this.config.default[0])
401
+ this.memo == null ? parseString(this.arena, this.config.default[0]) : this.memo.parse(this.arena, this.config.default[0]), null, null));
376
402
  if (isImportant) {
377
- filtered[0].val.push({
378
- typ: EnumToken.ImportantTokenType,
379
- });
403
+ arena.appendToken(filtered[0], arena.createToken(EnumToken.ImportantTokenType));
380
404
  }
381
405
  }
382
406
  return (filtered.length > 0 ? filtered : values)[Symbol.iterator]();
@@ -385,20 +409,22 @@ class PropertyMap {
385
409
  if (declaration instanceof PropertySet || declaration instanceof PropertyMap) {
386
410
  continue;
387
411
  }
388
- const config = declaration.nam == this.config.shorthand
389
- ? this.config
390
- : (propertiesMapping[declaration.nam] ?? this.config);
412
+ const nam = arena.strings[arena.name[declaration]];
413
+ const config = nam == this.config.shorthand ? this.config : (propertiesMapping[nam] ?? this.config);
391
414
  if (!("mapping" in config)) {
392
415
  continue;
393
416
  }
417
+ const declarationValues = arena.tokens(declaration);
394
418
  // @ts-ignore
395
419
  for (const [key, val] of Object.entries(config.mapping)) {
396
- const keys = parseString(key);
397
- if (keys.length != declaration.val.length) {
420
+ // inlined: this.parse(key)
421
+ const keys = this.memo == null ? parseString(this.arena, key) : this.memo.parse(this.arena, key);
422
+ if (keys.length != declarationValues.length) {
398
423
  continue;
399
424
  }
400
- if (eq(declaration.val, keys)) {
401
- declaration.val = parseString(val);
425
+ if (keys.every((k, index) => eqToken(arena, k, declarationValues[index]))) {
426
+ // inlined: this.parse(val as string)
427
+ arena.setTokenList(declaration, this.memo == null ? parseString(this.arena, val) : this.memo.parse(this.arena, val));
402
428
  break;
403
429
  }
404
430
  }
@@ -409,13 +435,7 @@ class PropertyMap {
409
435
  else {
410
436
  let count = 0;
411
437
  let match;
412
- const separator = this.config.separator != null
413
- ? {
414
- ...this.config.separator,
415
- // @ts-ignore
416
- typ: EnumToken[this.config.separator.typ],
417
- }
418
- : null;
438
+ const separator = this.config.separator ?? null;
419
439
  const tokens = {};
420
440
  // @ts-ignore
421
441
  Object.entries(propertiesMapping).reduce((acc, curr) => {
@@ -428,38 +448,34 @@ class PropertyMap {
428
448
  let current = 0;
429
449
  const props = propertiesMapping[curr[0]];
430
450
  const properties = (declarations.get(curr[0]));
431
- for (const declaration of properties instanceof PropertySet || properties instanceof PropertyMap
451
+ for (const declaration of properties instanceof PropertySet ||
452
+ properties instanceof PropertyMap
432
453
  ? [...properties].flat()
433
454
  : [properties]) {
434
- // @ts-ignore
435
- for (const val of declaration.val) {
436
- // @ts-ignore
437
- if (separator != null && separator.typ == val.typ && separator.val == val.val) {
455
+ for (const val of arena.tokens(declaration)) {
456
+ if (matchConfigToken(arena, val, separator)) {
438
457
  current++;
439
458
  if (tokens[curr[0]].length == current) {
440
459
  tokens[curr[0]].push([]);
441
460
  }
442
461
  continue;
443
462
  }
444
- if (val.typ == EnumToken.WhitespaceTokenType || val.typ == EnumToken.CommentTokenType) {
463
+ const valType = arena.dataTypes[val];
464
+ if (valType == EnumToken.WhitespaceTokenType || valType == EnumToken.CommentTokenType) {
445
465
  continue;
446
466
  }
447
- // @ts-ignore
448
467
  if (props.multiple &&
449
468
  props.separator != null &&
450
- // @ts-ignore
451
- EnumToken[props.separator.typ] === val.typ &&
452
- // @ts-ignore
453
- (val.typ === EnumToken.CommaTokenType || props.separator.val == val.val)) {
469
+ configType(props.separator) === valType &&
470
+ (valType === EnumToken.CommaTokenType ||
471
+ props.separator.val == arena.strings[arena.stringValue[val]])) {
454
472
  continue;
455
473
  }
456
- // @ts-ignore
457
- match = val.typ == EnumToken.CommentTokenType || matchType(val, curr[1]);
474
+ match = matchType(arena, val, curr[1]);
458
475
  if (isShorthand) {
459
476
  isShorthand = match;
460
477
  }
461
- // @ts-ignore
462
- if (val[PROPERTYNAME] == property || match) {
478
+ if (this.propertyNames.get(val) == property || match) {
463
479
  if (!(curr[0] in tokens)) {
464
480
  tokens[curr[0]] = [[]];
465
481
  }
@@ -499,18 +515,16 @@ class PropertyMap {
499
515
  result.push(...tokens[patterns[1]][0].slice(j));
500
516
  }
501
517
  const separator = this.config.properties[patterns[2]]?.prefix;
502
- result.push(
503
- // @ts-ignore
504
- { ...separator, typ: EnumToken[separator.typ] }, ...tokens[patterns[2]][0].reduce((acc, curr) => {
518
+ result.push(configToken(arena, separator), ...tokens[patterns[2]][0].reduce((acc, curr) => {
505
519
  if (acc.length > 0) {
506
- acc.push({ typ: EnumToken.WhitespaceTokenType });
520
+ acc.push(arena.createToken(EnumToken.WhitespaceTokenType));
507
521
  }
508
522
  acc.push(curr);
509
523
  return acc;
510
524
  }, []));
511
- const declaration = cloneNode(declarations.get(patterns[0]));
512
- declaration.nam = this.config.shorthand;
513
- declaration.val = result;
525
+ const declaration = arena.clone(declarations.get(patterns[0]));
526
+ arena.name[declaration] = arena.internString(this.config.shorthand);
527
+ arena.setTokenList(declaration, result.map((t) => this.take(t)));
514
528
  return [declaration][Symbol.iterator]();
515
529
  }
516
530
  count++;
@@ -521,40 +535,36 @@ class PropertyMap {
521
535
  }) ||
522
536
  // @ts-ignore
523
537
  !Object.values(tokens).every((v) =>
524
- // @ts-expect-error
525
- v.filter((t) => t.typ != EnumToken.CommentTokenType).length === count)) {
538
+ // `v` holds token groups: only single token groups were type checked by the former
539
+ // `arena.dataTypes[group]` lookup (the group is converted to a string key)
540
+ v.filter((t) => t.length != 1 || arena.dataTypes[t[0]] != EnumToken.CommentTokenType).length === count)) {
526
541
  // @ts-ignore
527
542
  iterable = declarations.values();
528
543
  }
529
544
  else if (hasMapping) {
530
545
  if (patterns.length == Object.keys(tokens).length) {
531
- const declaration = cloneNode(declarations.get(patterns[0]));
532
- declaration.nam = this.config.shorthand;
546
+ const declaration = arena.clone(declarations.get(patterns[0]));
547
+ const declarationValues = arena.tokens(declaration);
548
+ arena.name[declaration] = arena.internString(this.config.shorthand);
533
549
  for (let i = 0; i < patterns.length; i++) {
534
- if (declaration.val.length > 0) {
535
- declaration.val.push(
536
- // @ts-ignore
537
- this.config.separator != null
538
- ? {
539
- ...this.config.separator,
540
- typ: EnumToken[this.config.separator.typ],
541
- }
542
- : { typ: EnumToken.WhitespaceTokenType });
550
+ if (declarationValues.length > 0) {
551
+ declarationValues.push(configToken(arena, separator));
543
552
  }
544
- declaration.val.push(...tokens[patterns[i]].reduce((acc, curr) => {
553
+ declarationValues.push(...tokens[patterns[i]].reduce((acc, curr) => {
545
554
  if (acc.length > 0) {
546
- acc.push({ typ: EnumToken.WhitespaceTokenType });
555
+ acc.push(arena.createToken(EnumToken.WhitespaceTokenType));
547
556
  }
548
557
  acc.push(...curr.reduce((acc, curr) => {
549
558
  if (acc.length > 0) {
550
- acc.push({ typ: EnumToken.WhitespaceTokenType });
559
+ acc.push(arena.createToken(EnumToken.WhitespaceTokenType));
551
560
  }
552
- acc.push(curr);
561
+ acc.push(this.take(curr));
553
562
  return acc;
554
563
  }, []));
555
564
  return acc;
556
565
  }, []));
557
566
  }
567
+ arena.setTokenList(declaration, declarationValues);
558
568
  return [declaration][Symbol.iterator]();
559
569
  }
560
570
  iterable = declarations.values();
@@ -587,9 +597,9 @@ class PropertyMap {
587
597
  // return acc;
588
598
  // },
589
599
  // <Token[]>[],
590
- // );
600
+ //);
591
601
  if (props.default.includes(curr[1][i]
592
- .reduce((acc, curr) => acc + renderValue(curr) + " ", "")
602
+ .reduce((acc, curr) => acc + renderValue(arena, curr) + " ", "")
593
603
  .trimEnd())) {
594
604
  if (!propertiesMapping[curr[0]].required) {
595
605
  continue;
@@ -603,9 +613,9 @@ class PropertyMap {
603
613
  EnumToken.WhitespaceTokenType,
604
614
  EnumToken.CommentTokenType,
605
615
  EnumToken.IdenTokenType,
606
- ].includes(v.typ) ||
607
- (v.typ == EnumToken.IdenTokenType &&
608
- !propertiesMapping[curr[0]].default.includes(v.val))) {
616
+ ].includes(arena.dataTypes[v]) ||
617
+ (arena.dataTypes[v] == EnumToken.IdenTokenType &&
618
+ !propertiesMapping[curr[0]].default.includes(arena.strings[arena.stringValue[v]]))) {
609
619
  doFilterDefault = false;
610
620
  break;
611
621
  }
@@ -613,13 +623,13 @@ class PropertyMap {
613
623
  }
614
624
  // remove default values
615
625
  const filtered = values.filter((val) => {
616
- if (val.typ == EnumToken.WhitespaceTokenType ||
617
- val.typ == EnumToken.CommentTokenType) {
626
+ if (arena.dataTypes[val] == EnumToken.WhitespaceTokenType ||
627
+ arena.dataTypes[val] == EnumToken.CommentTokenType) {
618
628
  return false;
619
629
  }
620
630
  return (!doFilterDefault ||
621
- !(val.typ == EnumToken.IdenTokenType &&
622
- props.default.includes(val.val)));
631
+ !(arena.dataTypes[val] == EnumToken.IdenTokenType &&
632
+ props.default.includes(arena.strings[arena.stringValue[val]])));
623
633
  });
624
634
  if (filtered.length > 0 ||
625
635
  !(this.requiredCount == requiredCount && propertiesMapping[curr[0]].required)) {
@@ -633,36 +643,30 @@ class PropertyMap {
633
643
  values.length <= props.constraints.mapping.max) {
634
644
  let i = values.length;
635
645
  while (i--) {
636
- if (values[i].typ == EnumToken.IdenTokenType &&
646
+ if (arena.dataTypes[values[i]] == EnumToken.IdenTokenType &&
637
647
  // @ts-expect-error
638
- values[i].val in props.mapping) {
639
- // @ts-ignore
640
- values.splice(i, 1, ...parseString(props.mapping[values[i].val]));
648
+ arena.strings[arena.stringValue[values[i]]] in props.mapping) {
649
+ // inlined: this.parse(props.mapping[arena.strings[arena.stringValue[values[i]]]])
650
+ values.splice(i, 1, ...(this.memo == null
651
+ ? // @ts-ignore
652
+ parseString(this.arena, props.mapping[arena.strings[arena.stringValue[values[i]]]])
653
+ : // @ts-ignore
654
+ this.memo.parse(this.arena, props.mapping[arena.strings[arena.stringValue[values[i]]]])));
641
655
  }
642
656
  }
643
657
  }
644
658
  }
645
659
  if ("prefix" in props) {
646
- // @ts-ignore
647
- acc[i].push({ ...props.prefix, typ: EnumToken[props.prefix.typ] });
660
+ acc[i].push(configToken(arena, props.prefix));
648
661
  }
649
662
  else if (acc[i].length > 0) {
650
- // @ts-ignore
651
- acc[i].push({ typ: EnumToken.WhitespaceTokenType });
663
+ acc[i].push(arena.createToken(EnumToken.WhitespaceTokenType));
652
664
  }
653
665
  for (const v of values.reduce((acc, curr) => {
654
666
  if (acc.length > 0) {
655
- // @ts-ignore
656
- acc.push({
657
- ...((props.separator && {
658
- ...props.separator,
659
- // @ts-ignore
660
- typ: EnumToken[props.separator.typ],
661
- }) ?? { typ: EnumToken.WhitespaceTokenType }),
662
- });
667
+ acc.push(configToken(arena, props.separator));
663
668
  }
664
- // @ts-ignore
665
- acc.push(curr);
669
+ acc.push(this.take(curr));
666
670
  return acc;
667
671
  }, [])) {
668
672
  acc[i].push(v);
@@ -673,12 +677,14 @@ class PropertyMap {
673
677
  }, [])
674
678
  .reduce((acc, curr) => {
675
679
  if (acc.length > 0) {
676
- acc.push({ ...separator });
680
+ acc.push(configToken(arena, separator));
677
681
  }
678
682
  if (curr.length == 0 && this.config.default.length > 0) {
679
- curr.push(...parseString(this.config.default[0]).reduce((acc, curr) => {
683
+ curr.push(
684
+ // inlined: this.parse(this.config.default[0])
685
+ ...(this.memo == null ? parseString(this.arena, this.config.default[0]) : this.memo.parse(this.arena, this.config.default[0])).reduce((acc, curr) => {
680
686
  if (acc.length > 0) {
681
- acc.push({ typ: EnumToken.WhitespaceTokenType });
687
+ acc.push(arena.createToken(EnumToken.WhitespaceTokenType));
682
688
  }
683
689
  acc.push(curr);
684
690
  return acc;
@@ -691,36 +697,28 @@ class PropertyMap {
691
697
  }, []);
692
698
  if (this.config.mapping != null) {
693
699
  const val = values.reduce((acc, curr) => acc +
694
- renderValue(curr, {
700
+ renderValue(arena, curr, {
695
701
  removeComments: true,
696
702
  minify: true,
697
703
  }), "");
698
704
  if (val in this.config.mapping) {
699
705
  values.length = 0;
700
- values.push({
701
- typ: ['"', "'"].includes(val.charAt(0))
702
- ? EnumToken.StringTokenType
703
- : EnumToken.IdenTokenType,
704
- // @ts-ignore
705
- val: this.config.mapping[val],
706
- });
706
+ values.push(arena.createToken(['"', "'"].includes(val.charAt(0))
707
+ ? EnumToken.StringTokenType
708
+ : EnumToken.IdenTokenType,
709
+ // @ts-ignore
710
+ this.config.mapping[val]));
707
711
  }
708
712
  }
709
- // @ts-ignore
710
713
  if (values.length == 1 &&
711
- // @ts-ignore
712
- typeof values[0].val == "string" &&
713
- this.config.default.includes(values[0].val.toLowerCase()) &&
714
- this.config.default[0] != values[0].val.toLowerCase()) {
715
- // @ts-ignore/
716
- values = parseString(this.config.default[0]);
714
+ arena.stringValue[values[0]] != 0 &&
715
+ this.config.default.includes(arena.strings[arena.stringValue[values[0]]].toLowerCase()) &&
716
+ this.config.default[0] != arena.strings[arena.stringValue[values[0]]].toLowerCase()) {
717
+ // inlined: this.parse(this.config.default[0])
718
+ values = this.memo == null ? parseString(this.arena, this.config.default[0]) : this.memo.parse(this.arena, this.config.default[0]);
717
719
  }
718
720
  iterable = [
719
- {
720
- typ: EnumToken.DeclarationNodeType,
721
- nam: this.config.shorthand,
722
- val: values,
723
- },
721
+ arena.createDeclaration(EnumToken.DeclarationNodeType, this.config.shorthand, values, null, null),
724
722
  ][Symbol.iterator]();
725
723
  }
726
724
  }
@@ -753,14 +751,31 @@ class PropertyMap {
753
751
  },
754
752
  };
755
753
  }
754
+ /**
755
+ * detach a token from its declaration: tokens are cloned when they are still attached to a declaration
756
+ * @param token
757
+ */
758
+ take(token) {
759
+ if (this.arena.parents[token] == 0) {
760
+ return token;
761
+ }
762
+ const clone = this.arena.clone(token, true);
763
+ const propertyName = this.propertyNames.get(token);
764
+ if (propertyName != null) {
765
+ this.propertyNames.set(clone, propertyName);
766
+ }
767
+ return clone;
768
+ }
756
769
  matchTypes(declaration) {
770
+ const arena = this.arena;
757
771
  const patterns = this.pattern.slice();
758
772
  const values = [];
773
+ const declarationValues = arena.tokens(declaration);
759
774
  let i;
760
775
  let j;
761
776
  const map = new Map();
762
- for (i = 0; i < declaration.val.length; i++) {
763
- values.push(declaration.val[i]);
777
+ for (i = 0; i < declarationValues.length; i++) {
778
+ values.push(declarationValues[i]);
764
779
  }
765
780
  for (i = 0; i < patterns.length; i++) {
766
781
  for (j = 0; j < values.length; j++) {
@@ -769,34 +784,36 @@ class PropertyMap {
769
784
  map.set(patterns[i], this.config.properties?.[patterns[i]]?.constraints?.mapping?.max ?? 1);
770
785
  }
771
786
  let count = map.get(patterns[i]);
772
- if (count > 0 && matchType(values[j], this.config.properties[patterns[i]])) {
773
- // @ts-expect-error
774
- values[j][PROPERTYNAME] = patterns[i];
787
+ if (count > 0 && matchType(arena, values[j], this.config.properties[patterns[i]])) {
788
+ this.propertyNames.set(values[j], patterns[i]);
775
789
  map.set(patterns[i], --count);
776
790
  values.splice(j--, 1);
777
791
  }
778
792
  }
779
793
  }
780
794
  if (this.config.set != null) {
795
+ let modified = false;
781
796
  for (const [key, val] of Object.entries(this.config.set)) {
782
797
  if (map.has(key)) {
783
798
  for (const v of val) {
784
799
  // missing
785
800
  if (map.get(v) == 1) {
786
- let i = declaration.val.length;
801
+ let i = declarationValues.length;
787
802
  while (i--) {
788
- // @ts-expect-error
789
- if (declaration.val[i][PROPERTYNAME] == key) {
790
- const val = { ...declaration.val[i] };
791
- // @ts-expect-error
792
- val[PROPERTYNAME] = v;
793
- declaration.val.splice(i, 0, val, { typ: EnumToken.WhitespaceTokenType });
803
+ if (this.propertyNames.get(declarationValues[i]) == key) {
804
+ const val = arena.clone(declarationValues[i], true);
805
+ this.propertyNames.set(val, v);
806
+ declarationValues.splice(i, 0, val, arena.createToken(EnumToken.WhitespaceTokenType));
807
+ modified = true;
794
808
  }
795
809
  }
796
810
  }
797
811
  }
798
812
  }
799
813
  }
814
+ if (modified) {
815
+ arena.setTokenList(declaration, declarationValues);
816
+ }
800
817
  }
801
818
  }
802
819
  removeDefaults(map, value) {
@@ -813,14 +830,10 @@ class PropertyMap {
813
830
  value.splice(index, 1);
814
831
  if (config.prefix != null) {
815
832
  while (index-- > 0) {
816
- if (value[index].typ == EnumToken.WhitespaceTokenType) {
833
+ if (this.arena.dataTypes[value[index]] == EnumToken.WhitespaceTokenType) {
817
834
  continue;
818
835
  }
819
- if (
820
- // @ts-expect-error
821
- value[index].typ == EnumToken[config.prefix.typ] &&
822
- // @ts-ignore
823
- value[index].val == config.prefix.val) {
836
+ if (matchConfigToken(this.arena, value[index], config.prefix)) {
824
837
  value.splice(index, 1);
825
838
  break;
826
839
  }