@tbela99/css-parser 1.8.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27383 -24083
  4. package/dist/index.d.ts +2737 -2200
  5. package/dist/package.json.js +2 -2
  6. package/dist/src/data/properties.json.js +1 -1
  7. package/dist/src/data/validation.json.js +1 -1
  8. package/dist/src/lib/ast/arena-node.js +548 -0
  9. package/dist/src/lib/ast/arena.js +3198 -0
  10. package/dist/src/lib/ast/cache.js +333 -0
  11. package/dist/src/lib/ast/features/calc.js +85 -76
  12. package/dist/src/lib/ast/features/if.js +155 -115
  13. package/dist/src/lib/ast/features/index.js +1 -1
  14. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  15. package/dist/src/lib/ast/features/prefix.js +184 -183
  16. package/dist/src/lib/ast/features/shorthand.js +40 -46
  17. package/dist/src/lib/ast/features/transform.js +24 -24
  18. package/dist/src/lib/ast/features/type.js +1 -1
  19. package/dist/src/lib/ast/math/expression.js +458 -543
  20. package/dist/src/lib/ast/math/math.js +19 -41
  21. package/dist/src/lib/ast/minify.js +590 -647
  22. package/dist/src/lib/ast/transform/compute.js +82 -85
  23. package/dist/src/lib/ast/transform/matrix.js +21 -38
  24. package/dist/src/lib/ast/transform/minify.js +82 -193
  25. package/dist/src/lib/ast/transform/perspective.js +2 -3
  26. package/dist/src/lib/ast/transform/rotate.js +1 -1
  27. package/dist/src/lib/ast/transform/scale.js +1 -1
  28. package/dist/src/lib/ast/transform/skew.js +1 -1
  29. package/dist/src/lib/ast/transform/translate.js +1 -1
  30. package/dist/src/lib/ast/transform/utils.js +2 -2
  31. package/dist/src/lib/ast/types.js +263 -258
  32. package/dist/src/lib/ast/walk.js +246 -610
  33. package/dist/src/lib/fs/resolve.js +1 -1
  34. package/dist/src/lib/parser/declaration/list.js +223 -139
  35. package/dist/src/lib/parser/declaration/map.js +251 -238
  36. package/dist/src/lib/parser/declaration/set.js +74 -66
  37. package/dist/src/lib/parser/linesmap.js +1 -1
  38. package/dist/src/lib/parser/parse.js +1379 -1658
  39. package/dist/src/lib/parser/source.js +7 -4
  40. package/dist/src/lib/parser/tokenize.js +406 -199
  41. package/dist/src/lib/parser/utils/angle.js +11 -13
  42. package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
  43. package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
  44. package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
  45. package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
  46. package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
  47. package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
  48. package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
  49. package/dist/src/lib/parser/utils/at-rule.js +5 -5
  50. package/dist/src/lib/parser/utils/base64.js +2 -2
  51. package/dist/src/lib/parser/utils/cache.js +1 -1
  52. package/dist/src/lib/parser/utils/config.js +1 -1
  53. package/dist/src/lib/parser/utils/declaration.js +233 -234
  54. package/dist/src/lib/parser/utils/eq.js +50 -3
  55. package/dist/src/lib/parser/utils/hash.js +2 -35
  56. package/dist/src/lib/parser/utils/selector.js +309 -387
  57. package/dist/src/lib/parser/utils/text.js +18 -4
  58. package/dist/src/lib/parser/utils/token.js +16 -69
  59. package/dist/src/lib/parser/utils/type.js +37 -24
  60. package/dist/src/lib/printer/render.js +965 -511
  61. package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
  62. package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
  63. package/dist/src/lib/syntax/color/a98rgb.js +1 -1
  64. package/dist/src/lib/syntax/color/alpha.js +36 -37
  65. package/dist/src/lib/syntax/color/cmyk.js +35 -52
  66. package/dist/src/lib/syntax/color/color-mix.js +96 -132
  67. package/dist/src/lib/syntax/color/color.js +222 -231
  68. package/dist/src/lib/syntax/color/hex.js +45 -55
  69. package/dist/src/lib/syntax/color/hsl.js +54 -62
  70. package/dist/src/lib/syntax/color/hwb.js +60 -68
  71. package/dist/src/lib/syntax/color/lab.js +59 -67
  72. package/dist/src/lib/syntax/color/lch.js +59 -67
  73. package/dist/src/lib/syntax/color/oklab.js +60 -68
  74. package/dist/src/lib/syntax/color/oklch.js +60 -67
  75. package/dist/src/lib/syntax/color/p3.js +1 -1
  76. package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
  77. package/dist/src/lib/syntax/color/rec2020.js +1 -1
  78. package/dist/src/lib/syntax/color/relative-color.js +94 -144
  79. package/dist/src/lib/syntax/color/rgb.js +54 -57
  80. package/dist/src/lib/syntax/color/srgb.js +55 -53
  81. package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
  82. package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
  83. package/dist/src/lib/syntax/color/utils/components.js +19 -20
  84. package/dist/src/lib/syntax/color/utils/distance.js +15 -13
  85. package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
  86. package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
  87. package/dist/src/lib/syntax/color/xyz.js +1 -1
  88. package/dist/src/lib/syntax/color/xyzd50.js +1 -1
  89. package/dist/src/lib/syntax/constants.js +2 -16
  90. package/dist/src/lib/syntax/syntax.js +301 -407
  91. package/dist/src/lib/validation/config.js +1 -1
  92. package/dist/src/lib/validation/match.js +959 -723
  93. package/dist/src/lib/validation/parser/parse.js +230 -112
  94. package/dist/src/lib/validation/parser/typedef.js +1 -1
  95. package/dist/src/lib/validation/utils/list.js +27 -12
  96. package/dist/src/node.js +12 -72
  97. package/dist/src/types.js +1 -1
  98. package/dist/src/utils/sync.js +9 -6
  99. package/dist/src/web.js +11 -73
  100. package/package.json +9 -10
  101. package/dist/index.cjs +0 -35870
  102. package/dist/src/lib/ast/clone.js +0 -51
  103. package/dist/src/lib/ast/expand.js +0 -311
  104. package/dist/src/lib/ast/find.js +0 -192
  105. package/dist/src/lib/ast/node.js +0 -59
@@ -2,17 +2,17 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.1
6
6
  License: MIT
7
7
  */
8
- import { EnumToken, EnumAstNodeStatus, ColorType } from '../../ast/types.js';
9
- import { LOCEND, STATE, ERRORS, LOCSTA, tokensfuncDefMap, COLORS_NAMES, nonStandardColors, systemColors, deprecatedSystemColors, tokensMap, trimTokenSpace, LOCSRCID } from '../../syntax/constants.js';
8
+ import { EnumToken, ColorType, EnumAstNodeStatus } from '../../ast/types.js';
9
+ import { TokenField } from '../../ast/arena.js';
10
+ import { tokensfuncDefMap, COLORS_NAMES, nonStandardColors, systemColors, deprecatedSystemColors, tokensMap, trimTokenSpace } from '../../syntax/constants.js';
10
11
  import { renamedStandardProperties, isColor, parseColor, isWhiteSpace } from '../../syntax/syntax.js';
11
12
  import { getSyntaxRule, getParsedSyntax } from '../../validation/config.js';
12
- import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
13
+ import { matchDeclarationSyntax, trimArray } from '../../validation/match.js';
13
14
  import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
14
- import { walkValues } from '../../ast/walk.js';
15
- import { equalsIgnoreCase } from './text.js';
15
+ import { lowercase, equalsIgnoreCase } from './text.js';
16
16
  import { buildExpression } from '../../ast/math/expression.js';
17
17
  import { splitTokenList } from '../../validation/utils/list.js';
18
18
 
@@ -49,78 +49,122 @@ function parseGridTemplate(template) {
49
49
  }
50
50
  return buffer.length > 0 ? result + buffer : result;
51
51
  }
52
+ /**
53
+ * parse the color of an identifier or a color token, see {@link AstArena.visitValues}
54
+ * @param arena
55
+ * @param value
56
+ */
57
+ function parseColorToken(arena, value) {
58
+ if (arena.dataTypes[value] === EnumToken.IdenTokenType && isColor(arena, value)) {
59
+ parseColor(arena, value);
60
+ }
61
+ else if (arena.dataTypes[value] === EnumToken.ColorTokenType) {
62
+ if (isColor(arena, value)) {
63
+ parseColor(arena, value);
64
+ }
65
+ }
66
+ }
67
+ /**
68
+ * parse the color of an identifier, see {@link AstArena.visitValues}
69
+ * @param arena
70
+ * @param value
71
+ */
72
+ function parseColorIdent(arena, value) {
73
+ if (arena.dataTypes[value] === EnumToken.IdenTokenType && isColor(arena, value)) {
74
+ parseColor(arena, value);
75
+ }
76
+ }
77
+ /**
78
+ * Build the declaration node from the name token and the value tokens.
79
+ * @param arena
80
+ * @param name name token id
81
+ * @param tokens
82
+ * @param state
83
+ * @param errors
84
+ */
85
+ function createDeclarationNode(arena, name, tokens, state, errors) {
86
+ const node = arena.createDeclaration(EnumToken.DeclarationNodeType, arena.strings[arena.stringValue[name]], tokens, state, errors);
87
+ arena.copyLocation(node, name);
88
+ return node;
89
+ }
52
90
  /**
53
91
  * Parse declaration
92
+ * @param arena
54
93
  * @param tokens
55
- * @param parent
94
+ * @param parent parent node id or at-rule token
56
95
  * @param options
57
96
  * @param errors
58
97
  */
59
- function parseDeclaration(tokens, parent, options, errors) {
98
+ function parseDeclaration(arena, tokens, parent, options, errors) {
60
99
  const name = tokens.shift();
61
100
  let i;
101
+ let nam = arena.strings[arena.stringValue[name]];
102
+ // parent at-rule name
103
+ const isAtRule = parent != null && typeof parent == "number" && arena.dataTypes[parent] === EnumToken.AtRuleNodeType;
104
+ const parentNam = isAtRule ? arena.strings[arena.name[parent]] : null;
62
105
  let rules = null;
63
106
  let syntaxRules = null;
64
107
  let success = true;
65
108
  let hasImportantFlag = false;
66
- let validate = typeof options.validation === "boolean" ? options.validation : !!options.validation;
67
- if (renamedStandardProperties.has(name.val)) {
68
- name.val = renamedStandardProperties.get(name.val);
109
+ let validate = !!options.validation;
110
+ if (renamedStandardProperties.has(nam)) {
111
+ nam = renamedStandardProperties.get(nam);
112
+ // inlined: arena.setStr(name, nam)
113
+ arena.stringValue[name] = arena.internString(nam);
69
114
  }
70
115
  for (i = 0; i < tokens.length; i++) {
71
116
  const token = tokens[i];
72
- if (token.typ == EnumToken.WhitespaceTokenType ||
73
- token.typ == EnumToken.CommentTokenType ||
74
- token.typ == EnumToken.InvalidCommentTokenType) {
117
+ if (arena.dataTypes[token] == EnumToken.WhitespaceTokenType ||
118
+ arena.dataTypes[token] == EnumToken.CommentTokenType ||
119
+ arena.dataTypes[token] == EnumToken.BadCdoTokenType) {
75
120
  continue;
76
121
  }
77
122
  break;
78
123
  }
79
- if ((name.typ !== EnumToken.IdenTokenType && name.typ !== EnumToken.DashedIdenTokenType) ||
80
- tokens[i]?.typ !== EnumToken.ColonTokenType) {
81
- if (tokens[tokens.length - 1]?.[LOCEND] != null) {
82
- name[LOCEND] = tokens[tokens.length - 1]?.[LOCEND];
124
+ if (
125
+ // inlined: arena.typeOf(name)
126
+ (arena.dataTypes[name] !== EnumToken.IdenTokenType && arena.dataTypes[name] !== EnumToken.DashedIdenTokenType) ||
127
+ arena.dataTypes[tokens[i]] !== EnumToken.ColonTokenType) {
128
+ if (tokens.length > 0) {
129
+ arena.spanLocation(name, name, tokens[tokens.length - 1]);
83
130
  }
84
- name[STATE] = EnumAstNodeStatus.Unparsed;
85
- name[ERRORS] = [
86
- {
87
- action: "drop",
88
- node: name,
89
- location: options.source.getSourceLocation(name[LOCSTA]),
90
- message: "invalid declaration",
91
- },
92
- ];
93
- return Object.assign({
94
- typ: EnumToken.RawNodeTokenType,
95
- val: tokens,
96
- });
131
+ // unparsed declaration: stored as a raw node
132
+ const node = arena.createDeclaration(EnumToken.RawNodeTokenType, "", tokens, null, null);
133
+ arena.copyLocation(node, name);
134
+ return node;
135
+ }
136
+ // value tokens, without the leading and trailing whitespace: a single copy
137
+ let start = i + 1;
138
+ let end = tokens.length;
139
+ while (start < end && arena.dataTypes[tokens[start]] === EnumToken.WhitespaceTokenType) {
140
+ start++;
97
141
  }
98
- tokens = trimArray(tokens.slice(i + 1));
99
- if (validate && name.typ === EnumToken.IdenTokenType) {
100
- if (parent != null &&
101
- parent.typ === EnumToken.AtRuleNodeType &&
102
- parent.nam != "import" &&
103
- parent.nam != "supports" &&
104
- parent.nam != "media") {
142
+ while (end > start && arena.dataTypes[tokens[end - 1]] === EnumToken.WhitespaceTokenType) {
143
+ end--;
144
+ }
145
+ tokens = tokens.slice(start, end);
146
+ // inlined: arena.typeOf(name)
147
+ if (validate && arena.dataTypes[name] === EnumToken.IdenTokenType) {
148
+ if (isAtRule && parentNam != "import" && parentNam != "supports" && parentNam != "media") {
105
149
  // check @at-rule description
106
150
  // pick up the syntax or reject immediately
107
151
  // else do this
108
- rules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + parent.nam);
152
+ rules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + parentNam);
109
153
  if (rules != null) {
110
154
  const propertyDescriptors = rules.getPropertyDescriptors();
111
155
  if (propertyDescriptors != null) {
112
- syntaxRules = propertyDescriptors[name.val.toLowerCase()];
156
+ syntaxRules = propertyDescriptors[lowercase(nam)];
113
157
  }
114
158
  else {
115
159
  syntaxRules =
116
160
  rules.acceptAnyDeclaration && rules.acceptAnyRule
117
- ? getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, name.val.toLowerCase())
161
+ ? getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(nam))
118
162
  : rules.getBlockRules();
119
163
  }
120
164
  }
121
165
  }
122
166
  else {
123
- syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, name.val.toLowerCase());
167
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(nam));
124
168
  }
125
169
  // <declaration-list> or <declaration-rule-list>
126
170
  // else
@@ -129,43 +173,35 @@ function parseDeclaration(tokens, parent, options, errors) {
129
173
  syntaxRules[0].typ === ValidationTokenEnum.PropertyType &&
130
174
  (syntaxRules[0].val === "declaration-list" ||
131
175
  syntaxRules[0].val === "declaration-rule-list"))) {
132
- if (parent?.typ === EnumToken.AtRuleNodeType &&
133
- (parent.nam === "swash" ||
134
- parent.nam === "annotation" ||
135
- parent.nam === "ornaments" ||
136
- parent.nam === "stylistic" ||
137
- parent.nam === "historical-forms")) {
176
+ if (isAtRule &&
177
+ (parentNam === "swash" ||
178
+ parentNam === "annotation" ||
179
+ parentNam === "ornaments" ||
180
+ parentNam === "stylistic" ||
181
+ parentNam === "historical-forms")) {
138
182
  syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "font-feature-custom-ident");
139
183
  }
140
- else if (parent?.typ === EnumToken.AtRuleNodeType && parent.nam === "styleset") {
184
+ else if (isAtRule && parentNam === "styleset") {
141
185
  syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "font-feature-custom-ident-list");
142
186
  }
143
- else if (parent?.typ === EnumToken.AtRuleNodeType && parent.nam === "character-variant") {
187
+ else if (isAtRule && parentNam === "character-variant") {
144
188
  syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "font-feature-custom-ident-character-variant");
145
189
  }
146
190
  else {
147
- syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, name.val.toLowerCase());
191
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(nam));
148
192
  }
149
193
  }
150
194
  }
151
- if (tokens.length === 0 && name.typ !== EnumToken.DashedIdenTokenType) {
195
+ // inlined: arena.typeOf(name)
196
+ if (tokens.length === 0 && arena.dataTypes[name] !== EnumToken.DashedIdenTokenType) {
152
197
  errors.push({
153
198
  action: "drop",
154
199
  message: "declaration value missing",
155
200
  node: name,
156
- location: options.source.getSourceLocation(name[LOCSTA]),
157
- });
158
- if (tokens[tokens.length - 1]?.[LOCEND] != null) {
159
- name[LOCEND] = tokens[tokens.length - 1][LOCEND];
160
- }
161
- name[STATE] = EnumAstNodeStatus.Invalid;
162
- name[ERRORS] = [errors[errors.length - 1]];
163
- // @ts-expect-error
164
- return Object.assign(name, {
165
- typ: EnumToken.DeclarationNodeType,
166
- nam: name.val,
167
- val: tokens,
201
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(name))
202
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(name))],
168
203
  });
204
+ return createDeclarationNode(arena, name, tokens, EnumAstNodeStatus.Invalid, [errors[errors.length - 1]]);
169
205
  }
170
206
  const stack = [];
171
207
  let result = null;
@@ -173,20 +209,11 @@ function parseDeclaration(tokens, parent, options, errors) {
173
209
  let index;
174
210
  if (syntaxRules != null) {
175
211
  const doNotValidate = options.validation === false;
176
- result = doNotValidate ? null : matchAllSyntaxes(syntaxRules, createValidationContext(tokens), options);
212
+ result = doNotValidate ? null : matchDeclarationSyntax(arena, syntaxRules, tokens, options);
177
213
  if (doNotValidate || result != null) {
178
214
  success = doNotValidate || result?.success;
179
215
  if (success) {
180
- for (const { value } of walkValues(tokens)) {
181
- if (value.typ === EnumToken.IdenTokenType && isColor(value)) {
182
- parseColor(value);
183
- }
184
- else if (value.typ === EnumToken.ColorTokenType) {
185
- if (isColor(value)) {
186
- parseColor(value);
187
- }
188
- }
189
- }
216
+ arena.visitValues(tokens, parseColorToken);
190
217
  }
191
218
  if (!doNotValidate && !result?.success && result.errors.length > 0) {
192
219
  for (index = 0; index < result.errors.length; index++) {
@@ -197,66 +224,64 @@ function parseDeclaration(tokens, parent, options, errors) {
197
224
  }
198
225
  for (i = 0; i < tokens.length; i++) {
199
226
  token = tokens[i];
200
- if (tokensfuncDefMap.has(token.typ) ||
201
- token.typ == EnumToken.StartParensTokenType ||
202
- token.typ == EnumToken.BlockStartTokenType) {
227
+ if (tokensfuncDefMap.has(arena.dataTypes[token]) ||
228
+ arena.dataTypes[token] == EnumToken.StartParensTokenType ||
229
+ arena.dataTypes[token] == EnumToken.BlockStartTokenType) {
203
230
  stack.push(token);
204
231
  continue;
205
232
  }
206
- if (token.typ === EnumToken.ColonTokenType && stack.length == 0) {
233
+ if (arena.dataTypes[token] === EnumToken.ColonTokenType && stack.length == 0) {
207
234
  stack.push(token);
208
235
  continue;
209
236
  }
210
- switch (token.typ) {
237
+ switch (arena.dataTypes[token]) {
211
238
  case EnumToken.Literal:
212
- if (token.val === "/" && stack.at(-1)?.typ == EnumToken.MathFunctionTokenDefType) {
213
- Object.assign(token, {
214
- typ: EnumToken.Div,
215
- });
239
+ // inlined: arena.typeOf(stack.at(-1))
240
+ if (arena.strings[arena.stringValue[token]] === "/" && arena.dataTypes[stack.at(-1)] == EnumToken.MathFunctionTokenDefType) {
241
+ arena.dataTypes[token] = EnumToken.Div;
216
242
  }
217
- else if (token.val === "-" &&
218
- stack.at(-1)?.typ == EnumToken.MathFunctionTokenDefType) {
219
- Object.assign(token, {
220
- typ: EnumToken.Sub,
221
- });
243
+ else if (arena.strings[arena.stringValue[token]] === "-" &&
244
+ // inlined: arena.typeOf(stack.at(-1))
245
+ arena.dataTypes[stack.at(-1)] == EnumToken.MathFunctionTokenDefType) {
246
+ arena.dataTypes[token] = EnumToken.Sub;
222
247
  }
223
248
  break;
224
249
  case EnumToken.Star:
225
250
  case EnumToken.Plus:
226
- Object.assign(token, {
227
- typ: tokensMap.get(token.typ),
228
- });
251
+ arena.dataTypes[token] = tokensMap.get(arena.dataTypes[token]);
229
252
  break;
230
253
  case EnumToken.BlockEndTokenType:
231
- if (stack.at(-1)?.typ === EnumToken.BlockStartTokenType) {
254
+ // inlined: arena.typeOf(stack.at(-1))
255
+ if (arena.dataTypes[stack.at(-1)] === EnumToken.BlockStartTokenType) {
232
256
  // stack.pop();index = tokens.indexOf(stack.at(-1)!);
233
257
  index = tokens.indexOf(stack.at(-1));
234
258
  tokens.splice(i, 1);
235
- Object.assign(tokens[index], {
236
- typ: EnumToken.WrappedValuesTokenType,
237
- chi: trimArray(tokens.splice(index + 1, i - index - 1)),
238
- });
259
+ arena.dataTypes[tokens[index]] = EnumToken.WrappedValuesTokenType;
260
+ arena.setTokenList(tokens[index], trimArray(arena, tokens.splice(index + 1, i - index - 1)));
239
261
  i = index;
240
262
  stack.pop();
241
263
  }
242
264
  break;
243
265
  case EnumToken.EndParensTokenType:
244
- if (stack.at(-1)?.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stack.at(-1)?.typ)) {
266
+ if (stack.length > 0 &&
267
+ (arena.dataTypes[stack[stack.length - 1]] === EnumToken.StartParensTokenType ||
268
+ tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]]))) {
245
269
  index = tokens.indexOf(stack.at(-1));
246
270
  tokens.splice(i, 1);
247
- Object.assign(tokens[index], {
248
- typ: stack.at(-1)?.typ === EnumToken.StartParensTokenType
271
+ arena.dataTypes[tokens[index]] =
272
+ arena.dataTypes[stack[stack.length - 1]] === EnumToken.StartParensTokenType
249
273
  ? EnumToken.ParensTokenType
250
- : tokensfuncDefMap.get(stack.at(-1)?.typ),
251
- chi: trimArray(tokens.splice(index + 1, i - index - 1)),
252
- });
253
- if (tokens[index].typ === EnumToken.WildCardFunctionTokenType &&
254
- equalsIgnoreCase(tokens[index].val, "if")) {
255
- if (tokens[index].chi.at(-1)?.typ === EnumToken.SemiColonTokenType) {
256
- tokens[index].chi.pop();
274
+ : tokensfuncDefMap.get(arena.dataTypes[stack[stack.length - 1]]);
275
+ arena.setTokenList(tokens[index], trimArray(arena, tokens.splice(index + 1, i - index - 1)));
276
+ if (arena.dataTypes[tokens[index]] === EnumToken.WildCardFunctionTokenType &&
277
+ equalsIgnoreCase(arena.strings[arena.stringValue[tokens[index]]], "if")) {
278
+ const last = arena.lastToken[tokens[index]];
279
+ if (last != 0 && arena.dataTypes[last] === EnumToken.SemiColonTokenType) {
280
+ arena.detach(last);
257
281
  }
258
282
  let foundElse = false;
259
- tokens[index].chi = splitTokenList(tokens[index].chi, [EnumToken.SemiColonTokenType], true).reduce((acc, curr) => {
283
+ // inlined: arena.setTokens(tokens[index], ...splitTokenList(...)), the array is passed as is
284
+ arena.setTokenList(tokens[index], splitTokenList(arena, arena.tokens(tokens[index]), [EnumToken.SemiColonTokenType], true).reduce((acc, curr) => {
260
285
  if (foundElse) {
261
286
  // else already found
262
287
  // ignore everything after else
@@ -265,69 +290,67 @@ function parseDeclaration(tokens, parent, options, errors) {
265
290
  }
266
291
  let index = -1;
267
292
  while (++index < curr.length) {
268
- if (curr[index].typ == EnumToken.IdenTokenType &&
269
- equalsIgnoreCase("else", curr[index].val)) {
293
+ if (arena.dataTypes[curr[index]] == EnumToken.IdenTokenType &&
294
+ equalsIgnoreCase("else", arena.strings[arena.stringValue[curr[index]]])) {
270
295
  foundElse = true;
271
296
  }
272
- if (curr[index].typ === EnumToken.ColonTokenType) {
297
+ if (arena.dataTypes[curr[index]] === EnumToken.ColonTokenType) {
273
298
  break;
274
299
  }
275
300
  }
276
301
  if (foundElse && acc.length > 0) {
277
- acc[acc.length - 1] = {
278
- typ: EnumToken.IfElseConditionTokenType,
279
- l: acc[acc.length - 1],
280
- r: {
281
- typ: EnumToken.IfConditionTokenType,
282
- l: trimArray(curr.slice(0, index)),
283
- r: trimArray(curr.at(-1)?.typ === EnumToken.SemiColonTokenType
284
- ? curr.slice(index + 1, -1)
285
- : curr.slice(index + 1)),
286
- },
287
- };
302
+ const condition = arena.createToken(EnumToken.IfConditionTokenType);
303
+ // inlined: arena.setField(condition, TokenField.L, ...trimArray(arena, curr.slice(0, index)))
304
+ arena.setTokenList(arena.fieldList(condition, TokenField.L), trimArray(arena, curr.slice(0, index)));
305
+ // inlined: arena.setField(condition, TokenField.R, ...trimArray(arena, arena.typeOf(curr.at(-1)) === EnumToken.SemiColonTokenType ? curr.slice(index + 1, -1) : curr.slice(index + 1)))
306
+ arena.setTokenList(arena.fieldList(condition, TokenField.R), trimArray(arena, arena.dataTypes[curr.at(-1)] === EnumToken.SemiColonTokenType
307
+ ? curr.slice(index + 1, -1)
308
+ : curr.slice(index + 1)));
309
+ const ifElse = arena.createToken(EnumToken.IfElseConditionTokenType);
310
+ // inlined: arena.setField(ifElse, TokenField.L, acc[acc.length - 1])
311
+ arena.setTokenList(arena.fieldList(ifElse, TokenField.L), [acc[acc.length - 1]]);
312
+ // inlined: arena.setField(ifElse, TokenField.R, condition)
313
+ arena.setTokenList(arena.fieldList(ifElse, TokenField.R), [condition]);
314
+ acc[acc.length - 1] = ifElse;
288
315
  }
289
316
  else {
290
- acc.push({
291
- typ: EnumToken.IfConditionTokenType,
292
- l: trimArray(curr.slice(0, index)),
293
- r: trimArray(curr.slice(index + 1)),
294
- });
317
+ const condition = arena.createToken(EnumToken.IfConditionTokenType);
318
+ // inlined: arena.setField(condition, TokenField.L, ...trimArray(arena, curr.slice(0, index)))
319
+ arena.setTokenList(arena.fieldList(condition, TokenField.L), trimArray(arena, curr.slice(0, index)));
320
+ // inlined: arena.setField(condition, TokenField.R, ...trimArray(arena, curr.slice(index + 1)))
321
+ arena.setTokenList(arena.fieldList(condition, TokenField.R), trimArray(arena, curr.slice(index + 1)));
322
+ acc.push(condition);
295
323
  }
296
324
  return acc;
297
- }, []);
325
+ }, []));
298
326
  }
299
- else if (tokens[index].typ === EnumToken.UrlFunctionTokenType) {
300
- let l = -1;
301
- while (++l < tokens[index].chi.length) {
302
- if (tokens[index].chi[l].typ === EnumToken.StringTokenType) {
303
- break;
304
- }
305
- }
306
- if (tokens[index].chi[l]?.typ === EnumToken.StringTokenType &&
307
- /^[a-zA-Z0-0/_.-]+$/.test(tokens[index].chi[l].val.slice(1, -1))) {
308
- Object.assign(tokens[index].chi[l], {
309
- typ: EnumToken.UrlTokenTokenType,
310
- val: tokens[index].chi[l].val.slice(1, -1),
311
- });
327
+ else if (arena.dataTypes[tokens[index]] === EnumToken.UrlFunctionTokenType) {
328
+ const url = arena.findToken(tokens[index], (arena, t) => arena.dataTypes[t] === EnumToken.StringTokenType);
329
+ if (url != null && /^[a-zA-Z0-0/_.-]+$/.test(arena.strings[arena.stringValue[url]].slice(1, -1))) {
330
+ arena.dataTypes[url] = EnumToken.UrlTokenTokenType;
331
+ // inlined: arena.setStr(url, arena.strings[arena.stringValue[url]].slice(1, -1))
332
+ arena.stringValue[url] = arena.internString(arena.strings[arena.stringValue[url]].slice(1, -1));
312
333
  }
313
334
  }
314
335
  i = index;
315
- if (tokens[index].typ === EnumToken.MathFunctionTokenType &&
316
- equalsIgnoreCase("calc", tokens[index].val)) {
317
- tokens[index].chi = [buildExpression(tokens[index].chi)];
336
+ if (arena.dataTypes[tokens[index]] === EnumToken.MathFunctionTokenType &&
337
+ equalsIgnoreCase("calc", arena.strings[arena.stringValue[tokens[index]]])) {
338
+ // inlined: arena.setTokens(tokens[index], buildExpression(arena, arena.tokens(tokens[index])))
339
+ arena.setTokenList(tokens[index], [buildExpression(arena, arena.tokens(tokens[index]))]);
318
340
  }
319
- else if (tokens[index].typ === EnumToken.ColorTokenType) {
320
- if (isColor(tokens[index])) {
321
- parseColor(tokens[index]);
341
+ else if (arena.dataTypes[tokens[index]] === EnumToken.ColorTokenType) {
342
+ if (isColor(arena, tokens[index])) {
343
+ parseColor(arena, tokens[index]);
322
344
  }
323
345
  else {
324
346
  success = false;
325
- tokens[index].typ = EnumToken.FunctionTokenType;
347
+ arena.dataTypes[tokens[index]] = EnumToken.FunctionTokenType;
326
348
  errors.push({
327
349
  action: "drop",
328
350
  message: `invalid color`,
329
351
  node: tokens[index],
330
- location: options.source.getSourceLocation(tokens[index][LOCSTA]),
352
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(tokens[index]))
353
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[index]))],
331
354
  });
332
355
  }
333
356
  }
@@ -336,31 +359,28 @@ function parseDeclaration(tokens, parent, options, errors) {
336
359
  break;
337
360
  case EnumToken.IdenTokenType:
338
361
  {
339
- const val = token.val.toLowerCase();
362
+ const val = lowercase(arena.strings[arena.stringValue[token]]);
340
363
  if (val in COLORS_NAMES || val === "currentcolor") {
341
- Object.assign(token, {
342
- val,
343
- typ: EnumToken.ColorTokenType,
344
- kin: ColorType.LIT,
345
- });
364
+ // inlined: arena.setStr(token, val)
365
+ arena.stringValue[token] = arena.internString(val);
366
+ arena.dataTypes[token] = EnumToken.ColorTokenType;
367
+ // inlined: arena.setColorType(token, ColorType.LIT)
368
+ arena.colorType[token] = ColorType.LIT;
346
369
  }
347
370
  else if (nonStandardColors.has(val)) {
348
- Object.assign(token, {
349
- typ: EnumToken.ColorTokenType,
350
- kin: ColorType.NON_STD,
351
- });
371
+ arena.dataTypes[token] = EnumToken.ColorTokenType;
372
+ // inlined: arena.setColorType(token, ColorType.NON_STD)
373
+ arena.colorType[token] = ColorType.NON_STD;
352
374
  }
353
375
  else if (systemColors.has(val)) {
354
- Object.assign(token, {
355
- typ: EnumToken.ColorTokenType,
356
- kin: ColorType.SYS,
357
- });
376
+ arena.dataTypes[token] = EnumToken.ColorTokenType;
377
+ // inlined: arena.setColorType(token, ColorType.SYS)
378
+ arena.colorType[token] = ColorType.SYS;
358
379
  }
359
380
  else if (deprecatedSystemColors.has(val)) {
360
- Object.assign(token, {
361
- typ: EnumToken.ColorTokenType,
362
- kin: ColorType.DPSYS,
363
- });
381
+ arena.dataTypes[token] = EnumToken.ColorTokenType;
382
+ // inlined: arena.setColorType(token, ColorType.DPSYS)
383
+ arena.colorType[token] = ColorType.DPSYS;
364
384
  }
365
385
  }
366
386
  break;
@@ -368,8 +388,8 @@ function parseDeclaration(tokens, parent, options, errors) {
368
388
  hasImportantFlag = true;
369
389
  break;
370
390
  }
371
- if (trimTokenSpace.has(token.typ)) {
372
- if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
391
+ if (trimTokenSpace.has(arena.dataTypes[token])) {
392
+ if (arena.dataTypes[tokens[i + 1]] === EnumToken.WhitespaceTokenType) {
373
393
  for (let index = i + 1; index < tokens.length - 1; index++) {
374
394
  tokens[index] = tokens[index + 1];
375
395
  }
@@ -377,7 +397,7 @@ function parseDeclaration(tokens, parent, options, errors) {
377
397
  i--;
378
398
  continue;
379
399
  }
380
- if (tokens[i - 1]?.typ == EnumToken.WhitespaceTokenType) {
400
+ if (arena.dataTypes[tokens[i - 1]] == EnumToken.WhitespaceTokenType) {
381
401
  for (let index = i - 1; index < tokens.length - 1; index++) {
382
402
  tokens[index] = tokens[index + 1];
383
403
  }
@@ -391,97 +411,76 @@ function parseDeclaration(tokens, parent, options, errors) {
391
411
  action: "drop",
392
412
  message: "unbalanced token",
393
413
  node: stack[stack.length - 1],
394
- location: options.source.getSourceLocation(stack[stack.length - 1][LOCSTA]),
395
- });
396
- if (tokens[tokens.length - 1][LOCEND] != null) {
397
- name[LOCEND] = tokens[tokens.length - 1][LOCEND];
398
- }
399
- name[STATE] = EnumAstNodeStatus.Invalid;
400
- name[ERRORS] = result?.errors ?? [];
401
- //@ts-expect-error
402
- return Object.assign(name, {
403
- typ: EnumToken.DeclarationNodeType,
404
- nam: name.val,
405
- val: tokens,
414
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stack[stack.length - 1]))
415
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stack[stack.length - 1]))],
406
416
  });
407
- }
408
- for (const { value } of walkValues(tokens)) {
409
- if (value.typ === EnumToken.IdenTokenType && isColor(value)) {
410
- parseColor(value);
417
+ if (tokens.length > 0) {
418
+ arena.spanLocation(name, name, tokens[tokens.length - 1]);
411
419
  }
420
+ return createDeclarationNode(arena, name, tokens, EnumAstNodeStatus.Invalid, result?.errors ?? null);
412
421
  }
413
- if (name.val === "grid" ||
414
- name.val === "grid-template-areas" ||
415
- name.val === "grid-template-rows" ||
416
- name.val === "grid-template-columns") {
422
+ arena.visitValues(tokens, parseColorIdent);
423
+ if (nam === "grid" ||
424
+ nam === "grid-template-areas" ||
425
+ nam === "grid-template-rows" ||
426
+ nam === "grid-template-columns") {
417
427
  let i = 0;
418
428
  for (; i < tokens.length; i++) {
419
- if (tokens[i].typ === EnumToken.StringTokenType) {
420
- if (/[\s.]/.test(tokens[i].val)) {
421
- tokens[i].val = parseGridTemplate(tokens[i].val);
429
+ if (arena.dataTypes[tokens[i]] === EnumToken.StringTokenType) {
430
+ if (/[\s.]/.test(arena.strings[arena.stringValue[tokens[i]]])) {
431
+ // inlined: arena.setStr(tokens[i], parseGridTemplate(arena.strings[arena.stringValue[tokens[i]]]))
432
+ arena.stringValue[tokens[i]] = arena.internString(parseGridTemplate(arena.strings[arena.stringValue[tokens[i]]]));
422
433
  }
423
- if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType &&
424
- tokens[i + 2]?.typ === EnumToken.StringTokenType) {
434
+ if (arena.dataTypes[tokens[i + 1]] === EnumToken.WhitespaceTokenType &&
435
+ arena.dataTypes[tokens[i + 2]] === EnumToken.StringTokenType) {
425
436
  tokens.splice(i + 1, 1);
426
437
  }
427
438
  }
428
439
  }
429
440
  }
430
- if (validate && syntaxRules == null && name.typ === EnumToken.IdenTokenType) {
431
- if (tokens[tokens.length - 1]?.[LOCEND] != null) {
432
- name[LOCEND] = tokens[tokens.length - 1][LOCEND];
441
+ // inlined: arena.typeOf(name)
442
+ if (validate && syntaxRules == null && arena.dataTypes[name] === EnumToken.IdenTokenType) {
443
+ if (tokens.length > 0) {
444
+ arena.spanLocation(name, name, tokens[tokens.length - 1]);
433
445
  }
434
- name[STATE] = EnumAstNodeStatus.Unknown;
435
- name[ERRORS] = result?.errors ?? [];
436
- if (hasImportantFlag) {
437
- name[STATE] |= EnumAstNodeStatus.ImportantDeclaration;
438
- }
439
- // @ts-expect-error
440
- return Object.assign(name, {
441
- typ: EnumToken.DeclarationNodeType,
442
- nam: name.val,
443
- val: tokens,
444
- });
446
+ return createDeclarationNode(arena, name, tokens, hasImportantFlag ? EnumAstNodeStatus.Unknown | EnumAstNodeStatus.ImportantDeclaration : EnumAstNodeStatus.Unknown, result?.errors ?? null);
445
447
  }
446
- if (equalsIgnoreCase("composes", name.val)) {
448
+ if (equalsIgnoreCase("composes", nam)) {
447
449
  let index = -1;
448
450
  for (let i = 0; i < tokens.length; i++) {
449
- if (tokens[i].typ === EnumToken.IdenTokenType && equalsIgnoreCase("from", tokens[i].val)) {
451
+ if (arena.dataTypes[tokens[i]] === EnumToken.IdenTokenType && equalsIgnoreCase("from", arena.strings[arena.stringValue[tokens[i]]])) {
450
452
  index = i;
451
453
  break;
452
454
  }
453
455
  }
454
- const left = trimArray(index == -1 ? tokens : tokens.slice(0, index));
455
- const right = index == -1 ? null : trimArray(tokens.slice(index + 1));
456
- tokens = [
457
- {
458
- typ: EnumToken.ComposesSelectorNodeType,
459
- l: left,
460
- r: right?.[0] ?? null,
461
- [LOCSRCID]: tokens[0][LOCSRCID],
462
- [LOCSTA]: tokens[0][LOCSTA],
463
- [LOCEND]: index != -1 ? right[right.length - 1]?.[LOCEND] : left[left.length - 1][LOCEND],
464
- },
465
- ];
456
+ const left = trimArray(arena, index == -1 ? tokens : tokens.slice(0, index));
457
+ const right = index == -1 ? null : trimArray(arena, tokens.slice(index + 1));
458
+ const composes = arena.createToken(EnumToken.ComposesSelectorNodeType);
459
+ const first = tokens[0];
460
+ const last = index != -1 && right.length > 0 ? right[right.length - 1] : left[left.length - 1];
461
+ // inlined: arena.setField(composes, TokenField.L, ...left)
462
+ arena.setTokenList(arena.fieldList(composes, TokenField.L), left);
463
+ if (right != null && right.length > 0) {
464
+ // inlined: arena.setField(composes, TokenField.R, right[0])
465
+ arena.setTokenList(arena.fieldList(composes, TokenField.R), [right[0]]);
466
+ }
467
+ if (first != null && last != null) {
468
+ arena.spanLocation(composes, first, last);
469
+ }
470
+ tokens = [composes];
466
471
  }
467
- if (tokens[tokens.length - 1]?.[LOCEND] != null) {
468
- name[LOCEND] = tokens[tokens.length - 1][LOCEND];
472
+ if (tokens.length > 0) {
473
+ arena.spanLocation(name, name, tokens[tokens.length - 1]);
469
474
  }
470
- name[STATE] = success
475
+ let state = success
471
476
  ? result == null
472
477
  ? EnumAstNodeStatus.Unvalidated
473
478
  : EnumAstNodeStatus.Validated
474
479
  : EnumAstNodeStatus.ValidationFailed;
475
- name[ERRORS] = result?.errors ?? [];
476
480
  if (hasImportantFlag) {
477
- name[STATE] |= EnumAstNodeStatus.ImportantDeclaration;
481
+ state |= EnumAstNodeStatus.ImportantDeclaration;
478
482
  }
479
- // @ts-expect-error
480
- return Object.assign(name, {
481
- typ: EnumToken.DeclarationNodeType,
482
- nam: name.val,
483
- val: tokens,
484
- });
483
+ return createDeclarationNode(arena, name, tokens, state, result?.errors ?? null);
485
484
  }
486
485
 
487
486
  export { parseDeclaration };