@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,13 +2,14 @@
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 { TokenField } from '../../ast/arena.js';
8
9
  import { EnumToken } from '../../ast/types.js';
9
10
  import { evaluate } from '../../ast/math/expression.js';
10
11
  import { gcd } from '../../ast/math/math.js';
11
- import { tokensfuncDefMap, mediaTypes, LOCSTA, LOCEND, mFLT, mFGT, LOCSRCID } from '../../syntax/constants.js';
12
+ import { tokensfuncDefMap, mediaTypes, mFLT, mFGT } from '../../syntax/constants.js';
12
13
  import { trimArray, matchAllSyntaxes, createValidationContext, getMFInfo, isMFValue } from '../../validation/match.js';
13
14
  import { ValidationSyntaxGroupEnum, MediaFeatureType } from '../../validation/parser/typedef.js';
14
15
  import { getParsedSyntax } from '../../validation/config.js';
@@ -19,27 +20,25 @@ import { equalsIgnoreCase } from './text.js';
19
20
  // 'or' is not allowed outside of parentheses
20
21
  // https://drafts.csswg.org/mediaqueries/#error-handling
21
22
  // error recovery
22
- function parseMediaqueryList(stream, options) {
23
+ function parseMediaqueryList(arena, stream, options) {
23
24
  let matchCount = 0;
24
25
  const errors = [];
25
26
  let hasErrors = false;
26
- const parts = stream.reduce((acc, t) => {
27
- if (t.typ === EnumToken.CommaTokenType && matchCount === 0) {
28
- acc.push([]);
27
+ const parts = [[]];
28
+ for (const token of stream) {
29
+ const type = arena.dataTypes[token];
30
+ if (type === EnumToken.CommaTokenType && matchCount === 0) {
31
+ parts.push([]);
32
+ continue;
29
33
  }
30
- else {
31
- acc[acc.length - 1].push(t);
32
- if (t.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(t.typ)) {
33
- matchCount++;
34
- }
35
- else if (t.typ === EnumToken.EndParensTokenType) {
36
- if (matchCount > 0) {
37
- matchCount--;
38
- }
39
- }
34
+ parts[parts.length - 1].push(token);
35
+ if (type === EnumToken.StartParensTokenType || tokensfuncDefMap.has(type)) {
36
+ matchCount++;
40
37
  }
41
- return acc;
42
- }, [[]]);
38
+ else if (type === EnumToken.EndParensTokenType && matchCount > 0) {
39
+ matchCount--;
40
+ }
41
+ }
43
42
  {
44
43
  for (const stream of parts.slice()) {
45
44
  let success = true;
@@ -51,14 +50,14 @@ function parseMediaqueryList(stream, options) {
51
50
  let expectAndOrComma = false;
52
51
  i = 0;
53
52
  while (i < stream.length &&
54
- (stream[i]?.typ === EnumToken.WhitespaceTokenType || stream[i]?.typ === EnumToken.CommentTokenType)) {
53
+ (arena.dataTypes[stream[i]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[i]] === EnumToken.CommentTokenType)) {
55
54
  tokens.push(stream[i]);
56
55
  i++;
57
56
  }
58
57
  // [not | only] <media-type> [ and <media-condition-without-or> ]?
59
58
  if (i < stream.length) {
60
- if (stream[i].typ === EnumToken.IdenTokenType) {
61
- const val = stream[i].val.toLowerCase();
59
+ if (arena.dataTypes[stream[i]] === EnumToken.IdenTokenType) {
60
+ const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
62
61
  if (mediaTypes.includes(val)) {
63
62
  tokens.push(stream[i]);
64
63
  i++;
@@ -69,37 +68,39 @@ function parseMediaqueryList(stream, options) {
69
68
  action: "drop",
70
69
  message: `expecting '<media-type>'`,
71
70
  node: stream[i],
72
- location: options.source.getSourceLocation(stream[i][LOCSTA]),
71
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
72
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
73
73
  });
74
74
  }
75
75
  }
76
- else if (stream[i].typ !== EnumToken.StartParensTokenType) {
76
+ else if (arena.dataTypes[stream[i]] !== EnumToken.StartParensTokenType) {
77
77
  success = false;
78
78
  errors.push({
79
79
  action: "drop",
80
80
  message: `expecting '('`,
81
81
  node: stream[i],
82
- location: options.source.getSourceLocation(stream[i][LOCSTA]),
82
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
83
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
83
84
  });
84
85
  }
85
86
  }
86
87
  for (; i < stream.length; i++) {
87
88
  tokens.push(stream[i]);
88
- if (stream[i].typ === EnumToken.WhitespaceTokenType || stream[i].typ === EnumToken.CommentTokenType) {
89
+ if (arena.dataTypes[stream[i]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[i]] === EnumToken.CommentTokenType) {
89
90
  continue;
90
91
  }
91
92
  if (expectAndOrComma) {
92
- if (stream[i].typ === EnumToken.IdenTokenType) {
93
- stream[i].val.toLowerCase();
93
+ if (arena.dataTypes[stream[i]] === EnumToken.IdenTokenType) {
94
+ arena.strings[arena.stringValue[stream[i]]].toLowerCase();
94
95
  }
95
96
  expectAndOrComma = false;
96
97
  }
97
- if (stream[i].typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stream[i].typ)) {
98
+ if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(arena.dataTypes[stream[i]])) {
98
99
  scopes.push((currentScope = new Set()));
99
100
  stack.push(stream[i]);
100
101
  continue;
101
102
  }
102
- switch (stream[i].typ) {
103
+ switch (arena.dataTypes[stream[i]]) {
103
104
  case EnumToken.ColonTokenType:
104
105
  case EnumToken.LtTokenType:
105
106
  case EnumToken.LteTokenType:
@@ -110,18 +111,20 @@ function parseMediaqueryList(stream, options) {
110
111
  break;
111
112
  case EnumToken.IdenTokenType:
112
113
  {
113
- const val = stream[i].val.toLowerCase();
114
+ const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
114
115
  if (val === "and" || val === "or") {
115
- Object.assign(stream[i], {
116
- typ: val === "and" ? EnumToken.AndTokenType : EnumToken.OrTokenType,
117
- });
118
- if (val === "or" && (scopes.length == 1 && scopes[0].size > 0 && !scopes[0].has(EnumToken.OrTokenType))) {
116
+ arena.dataTypes[stream[i]] = val === "and" ? EnumToken.AndTokenType : EnumToken.OrTokenType;
117
+ if (val === "or" &&
118
+ scopes.length == 1 &&
119
+ scopes[0].size > 0 &&
120
+ !scopes[0].has(EnumToken.OrTokenType)) {
119
121
  success = false;
120
122
  errors.push({
121
123
  action: "drop",
122
124
  node: stream[i],
123
125
  message: `<or> is not allowed outside of parentheses`,
124
- location: options.source.getSourceLocation(stream[i][LOCSTA]),
126
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
127
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
125
128
  });
126
129
  break;
127
130
  }
@@ -131,24 +134,23 @@ function parseMediaqueryList(stream, options) {
131
134
  action: "drop",
132
135
  node: stream[i],
133
136
  message: `cannot mix <and> and <or> at the same level`,
134
- location: options.source.getSourceLocation(stream[i][LOCSTA]),
137
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
138
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
135
139
  });
136
140
  }
137
- currentScope.add(stream[i].typ);
141
+ currentScope.add(arena.dataTypes[stream[i]]);
138
142
  stack.push(stream[i]);
139
143
  }
140
144
  }
141
145
  break;
142
146
  case EnumToken.EndParensTokenType:
143
- if (tokensfuncDefMap.has(stack.at(-1)?.typ)) {
144
- const index = tokens.indexOf(stack.at(-1));
145
- tokens[index][LOCEND] = stream[i][LOCEND];
146
- Object.assign(tokens[index], {
147
- typ: tokensfuncDefMap.get(stack.at(-1)?.typ),
148
- chi: trimArray(tokens.slice(index + 1, tokens.length - 1)),
149
- });
147
+ if (stack.length > 0 && tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]])) {
148
+ const index = tokens.indexOf(stack[stack.length - 1]);
149
+ arena.spanLocation(tokens[index], tokens[index], stream[i]);
150
+ arena.dataTypes[tokens[index]] = tokensfuncDefMap.get(arena.dataTypes[tokens[index]]);
151
+ arena.setTokenList(tokens[index], trimArray(arena, tokens.slice(index + 1, tokens.length - 1)));
150
152
  tokens.length = index + 1;
151
- const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, tokens[index].val + "()")?.[0]?.chi, createValidationContext(tokens[index].chi), options);
153
+ const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.strings[arena.stringValue[tokens[index]]] + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
152
154
  stack.pop();
153
155
  scopes.pop();
154
156
  currentScope = scopes.at(-1);
@@ -161,21 +163,23 @@ function parseMediaqueryList(stream, options) {
161
163
  break;
162
164
  }
163
165
  // feature
164
- if (mFLT.has(stack.at(-1)?.typ) || mFGT.has(stack.at(-1)?.typ)) {
166
+ if (stack.length > 0 &&
167
+ (mFLT.has(arena.dataTypes[stack[stack.length - 1]]) ||
168
+ mFGT.has(arena.dataTypes[stack[stack.length - 1]]))) {
165
169
  // <mf-lt> | <mf-gt>
166
170
  const index = tokens.indexOf(stack.at(-1));
167
171
  const prevToken = stack[stack.length - 2];
168
- if (mFLT.has(prevToken?.typ) || mFGT.has(prevToken?.typ)) {
172
+ if (mFLT.has(arena.dataTypes[prevToken]) || mFGT.has(arena.dataTypes[prevToken])) {
169
173
  // const index: number = tokens.indexOf(stack.at(-1)!);
170
174
  // <mf-lt> | <mf-name>
171
175
  const index2 = tokens.indexOf(prevToken);
172
176
  // '('
173
177
  const index3 = tokens.indexOf(stack.at(-3));
174
- const left = trimArray(tokens.slice(index3 + 1, index2));
175
- const right = trimArray(tokens.slice(index + 1, tokens.length - 1));
176
- const names = trimArray(tokens.slice(index2 + 1, index));
177
- const filteredNames = names.filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
178
- const name = filteredNames[0].val;
178
+ const left = trimArray(arena, tokens.slice(index3 + 1, index2));
179
+ const right = trimArray(arena, tokens.slice(index + 1, tokens.length - 1));
180
+ const names = trimArray(arena, tokens.slice(index2 + 1, index));
181
+ const filteredNames = names.filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType && arena.dataTypes[n] !== EnumToken.CommentTokenType);
182
+ const name = arena.strings[arena.stringValue[filteredNames[0]]];
179
183
  const mfInfo = getMFInfo(name);
180
184
  for (const val of [left, right]) {
181
185
  if (options.computeCalcExpression) {
@@ -183,13 +187,11 @@ function parseMediaqueryList(stream, options) {
183
187
  (mfInfo.type != MediaFeatureType.KeywordType &&
184
188
  mfInfo.type != MediaFeatureType.StringType)) {
185
189
  for (let l = 0; l < val.length; l++) {
186
- if (val[l].typ === EnumToken.MathFunctionTokenType &&
187
- val[l].val === "calc") {
188
- const value = evaluate([val[l]]);
190
+ if (arena.dataTypes[val[l]] === EnumToken.MathFunctionTokenType &&
191
+ arena.strings[arena.stringValue[val[l]]] === "calc") {
192
+ const value = evaluate(arena, [val[l]]);
189
193
  if (value.length == 1) {
190
- value[0][LOCSRCID] = val[l][LOCSRCID];
191
- value[0][LOCSTA] = val[l][LOCSTA];
192
- value[0][LOCEND] = val[l][LOCEND];
194
+ arena.copyLocation(value[0], val[l]);
193
195
  val[l] = value[0];
194
196
  }
195
197
  }
@@ -199,48 +201,52 @@ function parseMediaqueryList(stream, options) {
199
201
  }
200
202
  for (const val of [left, right]) {
201
203
  if (mfInfo?.type === MediaFeatureType.RatioType) {
202
- const filteredValues = val.filter((n) => n.typ !== EnumToken.WhitespaceTokenType &&
203
- n.typ !== EnumToken.CommentTokenType);
204
+ const filteredValues = val.filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType &&
205
+ arena.dataTypes[n] !== EnumToken.CommentTokenType);
204
206
  if (filteredValues.length === 3) {
205
207
  if (options.computeCalcExpression) {
206
- const div = gcd(filteredValues[0].val, filteredValues[2].val);
208
+ const div = gcd(arena.numericValue[filteredValues[0]], arena.numericValue[filteredValues[2]]);
207
209
  if (div > 1) {
208
- filteredValues[0].val =
209
- filteredValues[0].val / div;
210
- filteredValues[2].val =
211
- filteredValues[2].val / div;
210
+ arena.numericValue[filteredValues[0]] =
211
+ arena.numericValue[filteredValues[0]] / div;
212
+ arena.numericValue[filteredValues[2]] =
213
+ arena.numericValue[filteredValues[2]] / div;
212
214
  }
213
215
  }
214
216
  val.splice(0, val.length, ...filteredValues);
215
217
  }
216
218
  }
217
219
  }
218
- tokens.splice(index3 + 1, tokens.length - index3 - 2, {
219
- typ: EnumToken.MediaRangeQueryTokenType,
220
- l: left,
221
- val: filteredNames,
222
- op1: prevToken,
223
- op2: stack.at(-1),
224
- r: right,
225
- [LOCSRCID]: left[0][LOCSRCID],
226
- [LOCSTA]: left[0][LOCSTA],
227
- [LOCEND]: right.at(-1)[LOCEND],
228
- });
220
+ const rangeQuery = arena.createToken(EnumToken.MediaRangeQueryTokenType);
221
+ // inlined: arena.setField(rangeQuery, TokenField.L, ...left)
222
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.L), left);
223
+ // inlined: arena.setField(rangeQuery, TokenField.Val, ...filteredNames)
224
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.Val), filteredNames);
225
+ // inlined: arena.setField(rangeQuery, TokenField.Op, prevToken)
226
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.Op), [prevToken]);
227
+ // inlined: arena.setField(rangeQuery, TokenField.Op2, stack[stack.length - 1])
228
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.Op2), [stack[stack.length - 1]]);
229
+ // inlined: arena.setField(rangeQuery, TokenField.R, ...right)
230
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.R), right);
231
+ if (left.length > 0 && right.length > 0) {
232
+ arena.spanLocation(rangeQuery, left[0], right[right.length - 1]);
233
+ }
234
+ tokens.splice(index3 + 1, tokens.length - index3 - 2, rangeQuery);
229
235
  stack.pop();
230
236
  stack.pop();
231
237
  }
232
238
  }
233
239
  if (stack.length > 0 &&
234
- (mFGT.has(stack.at(-1)?.typ) ||
235
- mFLT.has(stack.at(-1)?.typ) ||
236
- stack.at(-1)?.typ === EnumToken.DelimTokenType ||
237
- stack.at(-1)?.typ === EnumToken.ColonTokenType)) {
240
+ (mFGT.has(arena.dataTypes[stack[stack.length - 1]]) ||
241
+ mFLT.has(arena.dataTypes[stack[stack.length - 1]]) ||
242
+ arena.dataTypes[stack[stack.length - 1]] === EnumToken.DelimTokenType ||
243
+ arena.dataTypes[stack[stack.length - 1]] === EnumToken.ColonTokenType)) {
238
244
  const index2 = tokens.indexOf(stack.at(-1));
239
245
  const index3 = tokens.indexOf(stack.at(-2));
240
- let names = trimArray(tokens.slice(index3 + 1, index2));
241
- let values = trimArray(tokens.slice(index2 + 1, tokens.length - 1));
242
- const filteredNames = (names).filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
243
- const name = filteredNames[0].val;
246
+ let names = trimArray(arena, tokens.slice(index3 + 1, index2));
247
+ let values = trimArray(arena, tokens.slice(index2 + 1, tokens.length - 1));
248
+ const filteredNames = (names).filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType && arena.dataTypes[n] !== EnumToken.CommentTokenType);
249
+ const name = arena.strings[arena.stringValue[filteredNames[0]]];
244
250
  const mfInfo = getMFInfo(name);
245
251
  if (options.computeCalcExpression) {
246
252
  if (mfInfo == null ||
@@ -248,167 +254,171 @@ function parseMediaqueryList(stream, options) {
248
254
  mfInfo.type != MediaFeatureType.StringType)) {
249
255
  const val = values;
250
256
  for (let l = 0; l < val.length; l++) {
251
- if (val[l].typ === EnumToken.MathFunctionTokenType &&
252
- val[l].val === "calc") {
253
- const value = evaluate([val[l]]);
257
+ if (arena.dataTypes[val[l]] === EnumToken.MathFunctionTokenType &&
258
+ arena.strings[arena.stringValue[val[l]]] === "calc") {
259
+ const value = evaluate(arena, [val[l]]);
254
260
  if (value.length == 1) {
255
- value[0][LOCSRCID] = val[l][LOCSRCID];
256
- value[0][LOCSTA] = val[l][LOCSTA];
257
- value[0][LOCEND] = val[l][LOCEND];
261
+ arena.copyLocation(value[0], val[l]);
258
262
  val[l] = value[0];
259
263
  }
260
264
  }
261
265
  }
262
266
  }
263
267
  }
264
- const mfValue = isMFValue(name, values, mFGT.has(stack.at(-1)?.typ) ||
265
- mFLT.has(stack.at(-1)?.typ) ||
266
- stack.at(-1)?.typ === EnumToken.DelimTokenType);
268
+ const mfValue = isMFValue(arena, name, values, mFGT.has(arena.dataTypes[stack[stack.length - 1]]) ||
269
+ mFLT.has(arena.dataTypes[stack[stack.length - 1]]) ||
270
+ arena.dataTypes[stack[stack.length - 1]] === EnumToken.DelimTokenType);
267
271
  if (!mfValue.success) {
268
272
  success = false;
269
273
  const arr = values;
270
274
  errors.push({
271
275
  action: "drop",
272
276
  node: arr[0],
273
- location: options.source.getSourceLocation(arr[0]?.[LOCSTA]),
277
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(arr[0]))
278
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(arr[0]))],
274
279
  message: `${mfValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"}`,
275
280
  });
276
281
  break;
277
282
  }
278
283
  if (mfInfo?.type === MediaFeatureType.RatioType) {
279
284
  const val = values;
280
- const filteredValues = val.filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
285
+ const filteredValues = val.filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType && arena.dataTypes[n] !== EnumToken.CommentTokenType);
281
286
  if (filteredValues.length === 3) {
282
287
  if (options.computeCalcExpression) {
283
- const div = gcd(filteredValues[0].val, filteredValues[2].val);
288
+ const div = gcd(arena.numericValue[filteredValues[0]], arena.numericValue[filteredValues[2]]);
284
289
  if (div > 1) {
285
- filteredValues[0].val =
286
- filteredValues[0].val / div;
287
- filteredValues[2].val =
288
- filteredValues[2].val / div;
290
+ arena.numericValue[filteredValues[0]] =
291
+ arena.numericValue[filteredValues[0]] / div;
292
+ arena.numericValue[filteredValues[2]] =
293
+ arena.numericValue[filteredValues[2]] / div;
289
294
  }
290
295
  }
291
296
  val.splice(0, val.length, ...filteredValues);
292
297
  }
293
298
  }
294
- if (stack.at(-1)?.typ === EnumToken.ColonTokenType) {
299
+ // inlined: arena.typeOf(stack.at(-1))
300
+ if (arena.dataTypes[stack.at(-1)] === EnumToken.ColonTokenType) {
295
301
  if (name.startsWith("min-")) {
296
- filteredNames[0].val = name.substring(4);
297
- stack.at(-1).typ = EnumToken.GteTokenType;
302
+ // inlined: arena.setStr(filteredNames[0], name.substring(4))
303
+ arena.stringValue[filteredNames[0]] = arena.internString(name.substring(4));
304
+ arena.dataTypes[stack[stack.length - 1]] = EnumToken.GteTokenType;
298
305
  }
299
306
  else if (name.startsWith("max-")) {
300
- filteredNames[0].val = name.substring(4);
301
- stack.at(-1).typ = EnumToken.LteTokenType;
307
+ // inlined: arena.setStr(filteredNames[0], name.substring(4))
308
+ arena.stringValue[filteredNames[0]] = arena.internString(name.substring(4));
309
+ arena.dataTypes[stack[stack.length - 1]] = EnumToken.LteTokenType;
302
310
  }
303
311
  }
304
- // @ts-expect-error
305
- tokens.splice(index3 + 1, tokens.length - index3 - 2, {
306
- typ: EnumToken.MediaQueryConditionTokenType,
307
- l: names,
308
- op: stack.pop(),
309
- r: values,
310
- [LOCSRCID]: names[0][LOCSRCID],
311
- [LOCSTA]: names[0][LOCSTA],
312
- [LOCEND]: values.at(-1)[LOCEND],
313
- });
312
+ const condition = arena.createToken(EnumToken.MediaQueryConditionTokenType);
313
+ // inlined: arena.setField(condition, TokenField.L, ...names)
314
+ arena.setTokenList(arena.fieldList(condition, TokenField.L), names);
315
+ // inlined: arena.setField(condition, TokenField.Op, stack.pop() as Token)
316
+ arena.setTokenList(arena.fieldList(condition, TokenField.Op), [stack.pop()]);
317
+ // inlined: arena.setField(condition, TokenField.R, ...values)
318
+ arena.setTokenList(arena.fieldList(condition, TokenField.R), values);
319
+ if (names.length > 0 && values.length > 0) {
320
+ arena.spanLocation(condition, names[0], values[values.length - 1]);
321
+ }
322
+ tokens.splice(index3 + 1, tokens.length - index3 - 2, condition);
314
323
  }
315
324
  if (stack.length === 0) {
316
325
  success = false;
317
326
  errors.push({
318
327
  action: "drop",
319
328
  node: stream[i],
320
- location: options.source.getSourceLocation(stream[i]?.[LOCSTA]),
329
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
330
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
321
331
  message: `unmatched ')'`,
322
332
  });
323
333
  break;
324
334
  }
325
335
  {
326
- const index = tokens.indexOf(stack.at(-1));
327
- tokens[index] = {
328
- typ: EnumToken.ParensTokenType,
329
- chi: tokens.slice(index + 1, tokens.length - 1),
330
- [LOCSRCID]: tokens[index][LOCSRCID],
331
- [LOCSTA]: tokens[index][LOCSTA],
332
- [LOCEND]: stream[i][LOCEND],
333
- };
336
+ const index = tokens.indexOf(stack[stack.length - 1]);
337
+ arena.dataTypes[tokens[index]] = EnumToken.ParensTokenType;
338
+ arena.setTokenList(tokens[index], tokens.slice(index + 1, tokens.length - 1));
339
+ arena.spanLocation(tokens[index], tokens[index], stream[i]);
334
340
  tokens.length = index + 1;
335
341
  scopes.pop();
336
342
  currentScope = scopes.at(-1);
337
343
  stack.pop();
338
- if (stack.at(-1)?.typ === EnumToken.AndTokenType ||
339
- stack.at(-1)?.typ === EnumToken.OrTokenType) {
344
+ if (
345
+ // inlined: arena.typeOf(stack.at(-1))
346
+ arena.dataTypes[stack.at(-1)] === EnumToken.AndTokenType ||
347
+ // inlined: arena.typeOf(stack.at(-1))
348
+ arena.dataTypes[stack.at(-1)] === EnumToken.OrTokenType) {
340
349
  const index = tokens.indexOf(stack.at(-1));
341
350
  let l = index - 1;
342
351
  while (l > 0 &&
343
- (tokens[l].typ === EnumToken.WhitespaceTokenType ||
344
- tokens[l].typ === EnumToken.CommentTokenType)) {
352
+ (arena.dataTypes[tokens[l]] === EnumToken.WhitespaceTokenType ||
353
+ arena.dataTypes[tokens[l]] === EnumToken.CommentTokenType)) {
345
354
  l--;
346
355
  }
347
- const left = trimArray(tokens.slice(l, index));
348
- const right = trimArray(tokens.slice(index + 1));
349
- tokens[l] = {
350
- typ: EnumToken.MediaQueryConditionTokenType,
351
- op: stack.pop(),
352
- l: left,
353
- r: right,
354
- [LOCSRCID]: left[0][LOCSRCID],
355
- [LOCSTA]: left[0][LOCSTA],
356
- [LOCEND]: right.at(-1)[LOCEND],
357
- };
356
+ const left = trimArray(arena, tokens.slice(l, index));
357
+ const right = trimArray(arena, tokens.slice(index + 1));
358
+ const op = stack.pop();
359
+ const condition = arena.createToken(EnumToken.MediaQueryConditionTokenType);
360
+ // inlined: arena.setField(condition, TokenField.Op, op)
361
+ arena.setTokenList(arena.fieldList(condition, TokenField.Op), [op]);
362
+ // inlined: arena.setField(condition, TokenField.L, ...left)
363
+ arena.setTokenList(arena.fieldList(condition, TokenField.L), left);
364
+ // inlined: arena.setField(condition, TokenField.R, ...right)
365
+ arena.setTokenList(arena.fieldList(condition, TokenField.R), right);
366
+ if (left.length > 0 && right.length > 0) {
367
+ arena.spanLocation(condition, left[0], right[right.length - 1]);
368
+ }
369
+ tokens[l] = condition;
358
370
  tokens.length = l + 1;
359
371
  // media range query
360
- if (options.minify &&
361
- tokens[l].op.typ === EnumToken.AndTokenType) {
362
- if (left.length === 1 &&
363
- left[0].typ == EnumToken.ParensTokenType &&
364
- left[0].chi.length == 1 &&
365
- left[0].chi[0].typ == EnumToken.MediaQueryConditionTokenType &&
366
- left[0].chi[0].l.length == 1 &&
367
- left[0].chi[0].l[0].typ ==
368
- EnumToken.IdenTokenType &&
369
- (left[0].chi[0].op.typ ==
370
- EnumToken.GtTokenType ||
371
- left[0].chi[0].op.typ ==
372
- EnumToken.GteTokenType) &&
373
- right.length === 1 &&
374
- left[0].typ == EnumToken.ParensTokenType &&
375
- right[0].chi.length == 1 &&
376
- right[0].chi[0].typ ==
377
- EnumToken.MediaQueryConditionTokenType &&
378
- right[0].chi[0].l.length == 1 &&
379
- right[0].chi[0].l[0].typ ==
380
- EnumToken.IdenTokenType &&
381
- (right[0].chi[0].op.typ ==
382
- EnumToken.LtTokenType ||
383
- right[0].chi[0].op.typ ==
384
- EnumToken.LteTokenType) &&
385
- equalsIgnoreCase(left[0].chi[0]
386
- .l[0].val, right[0].chi[0]
387
- .l[0].val)) {
388
- tokens[l] = {
389
- typ: EnumToken.ParensTokenType,
390
- chi: [
391
- {
392
- typ: EnumToken.MediaRangeQueryTokenType,
393
- op1: Object.assign(left[0].chi[0]
394
- .op, {
395
- typ: left[0]
396
- .chi[0].op.typ == EnumToken.GteTokenType
397
- ? EnumToken.LteTokenType
398
- : EnumToken.LtTokenType,
399
- }),
400
- op2: right[0].chi[0]
401
- .op,
402
- l: left[0].chi[0].r,
403
- val: left[0].chi[0]
404
- .l,
405
- r: right[0].chi[0].r,
406
- [LOCSRCID]: left[0][LOCSRCID],
407
- [LOCSTA]: left[0][LOCSTA],
408
- [LOCEND]: right.at(-1)[LOCEND],
409
- },
410
- ],
411
- };
372
+ if (options.minify && arena.dataTypes[op] === EnumToken.AndTokenType) {
373
+ const leftCondition = left.length === 1 &&
374
+ arena.dataTypes[left[0]] == EnumToken.ParensTokenType &&
375
+ arena.tokenCount(left[0]) == 1
376
+ ? arena.firstToken[left[0]]
377
+ : null;
378
+ const rightCondition = right.length === 1 &&
379
+ arena.dataTypes[right[0]] == EnumToken.ParensTokenType &&
380
+ arena.tokenCount(right[0]) == 1
381
+ ? arena.firstToken[right[0]]
382
+ : null;
383
+ if (leftCondition != null &&
384
+ rightCondition != null &&
385
+ arena.dataTypes[leftCondition] == EnumToken.MediaQueryConditionTokenType &&
386
+ arena.dataTypes[rightCondition] == EnumToken.MediaQueryConditionTokenType) {
387
+ const leftNames = arena.fieldTokens(leftCondition, TokenField.L);
388
+ const rightNames = arena.fieldTokens(rightCondition, TokenField.L);
389
+ const leftOp = arena.field(leftCondition, TokenField.Op);
390
+ const rightOp = arena.field(rightCondition, TokenField.Op);
391
+ if (leftNames.length == 1 &&
392
+ arena.dataTypes[leftNames[0]] == EnumToken.IdenTokenType &&
393
+ (arena.dataTypes[leftOp] == EnumToken.GtTokenType ||
394
+ arena.dataTypes[leftOp] == EnumToken.GteTokenType) &&
395
+ rightNames.length == 1 &&
396
+ arena.dataTypes[rightNames[0]] == EnumToken.IdenTokenType &&
397
+ (arena.dataTypes[rightOp] == EnumToken.LtTokenType ||
398
+ arena.dataTypes[rightOp] == EnumToken.LteTokenType) &&
399
+ equalsIgnoreCase(arena.strings[arena.stringValue[leftNames[0]]], arena.strings[arena.stringValue[rightNames[0]]])) {
400
+ const rangeQuery = arena.createToken(EnumToken.MediaRangeQueryTokenType);
401
+ arena.dataTypes[leftOp] =
402
+ arena.dataTypes[leftOp] == EnumToken.GteTokenType
403
+ ? EnumToken.LteTokenType
404
+ : EnumToken.LtTokenType;
405
+ // inlined: arena.setField(rangeQuery, TokenField.L, ...arena.fieldTokens(leftCondition, TokenField.R))
406
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.L), arena.fieldTokens(leftCondition, TokenField.R));
407
+ // inlined: arena.setField(rangeQuery, TokenField.Val, ...leftNames)
408
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.Val), leftNames);
409
+ // inlined: arena.setField(rangeQuery, TokenField.Op, leftOp)
410
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.Op), [leftOp]);
411
+ // inlined: arena.setField(rangeQuery, TokenField.Op2, rightOp)
412
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.Op2), [rightOp]);
413
+ // inlined: arena.setField(rangeQuery, TokenField.R, ...arena.fieldTokens(rightCondition, TokenField.R))
414
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.R), arena.fieldTokens(rightCondition, TokenField.R));
415
+ arena.spanLocation(rangeQuery, left[0], right[right.length - 1]);
416
+ const parens = arena.createToken(EnumToken.ParensTokenType);
417
+ // inlined: arena.setTokens(parens, rangeQuery)
418
+ arena.setTokenList(parens, [rangeQuery]);
419
+ arena.copyLocation(parens, rangeQuery);
420
+ tokens[l] = parens;
421
+ }
412
422
  }
413
423
  }
414
424
  expectAndOrComma = true;
@@ -425,29 +435,30 @@ function parseMediaqueryList(stream, options) {
425
435
  parts.splice(parts.indexOf(stream), 1);
426
436
  }
427
437
  stream.length = 0;
428
- for (const t of trimArray(tokens)) {
438
+ for (const t of trimArray(arena, tokens)) {
429
439
  stream.push(t);
430
440
  }
431
441
  }
432
442
  }
433
443
  stream.length = 0;
434
- stream.push(...parts
435
- .filter((p) => p.length > 0 && p[0].typ !== EnumToken.InvalidMediaQueryTokenType)
436
- .reduce((acc, b) => {
437
- if (acc.length > 0) {
438
- acc.push({ typ: EnumToken.CommaTokenType });
444
+ let hasPart = false;
445
+ for (const part of parts) {
446
+ if (part.length == 0) {
447
+ continue;
439
448
  }
440
- for (const t of b) {
441
- acc.push(t);
449
+ if (hasPart) {
450
+ stream.push(arena.createToken(EnumToken.CommaTokenType));
442
451
  }
443
- return acc;
444
- }, []));
452
+ for (const token of part) {
453
+ stream.push(token);
454
+ }
455
+ hasPart = true;
456
+ }
445
457
  return {
446
458
  success: !hasErrors ||
447
- stream.filter((t) => t.typ !== EnumToken.WhitespaceTokenType &&
448
- t.typ !== EnumToken.CommentTokenType &&
449
- t.typ !== EnumToken.InvalidMediaQueryTokenType &&
450
- t.typ !== EnumToken.CommaTokenType).length !== 0,
459
+ stream.filter((t) => arena.dataTypes[t] !== EnumToken.WhitespaceTokenType &&
460
+ arena.dataTypes[t] !== EnumToken.CommentTokenType &&
461
+ arena.dataTypes[t] !== EnumToken.CommaTokenType).length !== 0,
451
462
  errors,
452
463
  };
453
464
  }