@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,17 +2,18 @@
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
8
  import { EnumToken } from '../../ast/types.js';
9
- import { pseudoElements, LOCEND, tokensfuncDefMap, LOCSTA, LOCSRCID } from '../../syntax/constants.js';
9
+ import { pseudoElements, tokensfuncDefMap } from '../../syntax/constants.js';
10
10
  import { getSyntaxConfig, getParsedSyntax } from '../../validation/config.js';
11
11
  import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
12
12
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
13
13
  import { parseDeclaration } from './declaration.js';
14
+ import { TokenField } from '../../ast/arena.js';
14
15
 
15
- function parseAtRuleSupportSyntax(stream, context, options = {}) {
16
+ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
16
17
  const tokens = [];
17
18
  const stack = [];
18
19
  let i = 0;
@@ -32,38 +33,40 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
32
33
  ]);
33
34
  for (; i < stream.length; i++) {
34
35
  tokens.push(stream[i]);
35
- if (stream[i].typ == EnumToken.ColonTokenType) {
36
- if (stream[i + 1]?.typ == EnumToken.IdenTokenType) {
37
- const val = stream[i + 1].val;
38
- Object.assign(stream[i], {
39
- typ: pseudoElements.includes(val)
40
- ? EnumToken.PseudoElementTokenType
41
- : EnumToken.PseudoClassTokenType,
42
- val: ":" + val,
43
- });
44
- stream[i][LOCEND] = stream[i + 1][LOCEND];
36
+ if (arena.dataTypes[stream[i]] == EnumToken.ColonTokenType) {
37
+ if (arena.dataTypes[stream[i + 1]] == EnumToken.IdenTokenType) {
38
+ const val = arena.strings[arena.stringValue[stream[i + 1]]];
39
+ arena.dataTypes[stream[i]] = pseudoElements.includes(val)
40
+ ? EnumToken.PseudoElementTokenType
41
+ : EnumToken.PseudoClassTokenType;
42
+ // inlined: arena.setStr(stream[i], ":" + val)
43
+ arena.stringValue[stream[i]] = arena.internString((":" + val));
44
+ // inlined: arena.setSpanEnd(stream[i], arena.getSpanEnd(stream[i + 1]))
45
+ arena.setSpan(stream[i], arena.getSourceId(stream[i]), arena.getSpanStart(stream[i]), arena.getSpanEnd(stream[i + 1]));
45
46
  stream.splice(i + 1, 1);
46
47
  continue;
47
48
  }
48
- if (stream[i + 1]?.typ == EnumToken.FunctionTokenDefType) {
49
- val = ":" + stream[i + 1].val;
50
- Object.assign(stream[i], {
51
- typ: val + "()" in getSyntaxConfig().selectors
49
+ if (arena.dataTypes[stream[i + 1]] == EnumToken.FunctionTokenDefType) {
50
+ val = ":" + arena.strings[arena.stringValue[stream[i + 1]]];
51
+ arena.dataTypes[stream[i]] =
52
+ val + "()" in getSyntaxConfig().selectors
52
53
  ? EnumToken.PseudoClassFunctionTokenDefType
53
- : stream[i + 1].typ,
54
- val,
55
- });
54
+ : arena.dataTypes[stream[i + 1]];
55
+ // inlined: arena.setStr(stream[i], val)
56
+ arena.stringValue[stream[i]] = arena.internString(val);
56
57
  stack.push(stream[i]);
57
- stream[i][LOCEND] = stream[i + 1][LOCEND];
58
+ // inlined: arena.setSpanEnd(stream[i], arena.getSpanEnd(stream[i + 1]))
59
+ arena.setSpan(stream[i], arena.getSourceId(stream[i]), arena.getSpanStart(stream[i]), arena.getSpanEnd(stream[i + 1]));
58
60
  stream.splice(i + 1, 1);
59
61
  continue;
60
62
  }
61
63
  }
62
- if (trimWhiteSpace.has(stream[i].typ)) {
63
- if (tokens.at(-2)?.typ === EnumToken.WhitespaceTokenType) {
64
+ if (trimWhiteSpace.has(arena.dataTypes[stream[i]])) {
65
+ // inlined: arena.typeOf(tokens.at(-2))
66
+ if (arena.dataTypes[tokens.at(-2)] === EnumToken.WhitespaceTokenType) {
64
67
  tokens.splice(tokens.length - 2, 1);
65
68
  }
66
- if (stream[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
69
+ if (arena.dataTypes[stream[i + 1]] === EnumToken.WhitespaceTokenType) {
67
70
  i++;
68
71
  continue;
69
72
  }
@@ -71,62 +74,53 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
71
74
  if (expectAndOr) {
72
75
  let k = i;
73
76
  while (k < stream.length &&
74
- (stream[k]?.typ === EnumToken.WhitespaceTokenType || stream[k]?.typ === EnumToken.CommentTokenType)) {
77
+ (arena.dataTypes[stream[k]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[k]] === EnumToken.CommentTokenType)) {
75
78
  tokens.push(stream[k]);
76
79
  k++;
77
80
  }
78
81
  expectAndOr = false;
79
82
  }
80
- if (stream[i].typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stream[i].typ)) {
83
+ if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(arena.dataTypes[stream[i]])) {
81
84
  scopes.push((scope = new Set()));
82
85
  stack.push(stream[i]);
83
86
  continue;
84
87
  }
85
- switch (stream[i].typ) {
88
+ switch (arena.dataTypes[stream[i]]) {
86
89
  case EnumToken.EndParensTokenType:
87
90
  {
88
- if (stack[stack.length - 1].typ === EnumToken.ColonTokenType) {
91
+ if (arena.dataTypes[stack[stack.length - 1]] === EnumToken.ColonTokenType) {
89
92
  // match declaration
90
93
  const index = tokens.indexOf(stack[stack.length - 2]);
91
- const slice = trimArray(tokens.splice(index + 1, tokens.length - index - 2));
92
- const declaration = parseDeclaration(slice, context, { ...options, validation: false }, errors);
94
+ const slice = trimArray(arena, tokens.splice(index + 1, tokens.length - index - 2));
95
+ const declaration = parseDeclaration(arena, slice, context, { ...options, validation: false }, errors);
93
96
  tokens.splice(index + 1, 0, declaration);
94
97
  stack.pop();
95
98
  }
96
99
  // match <support-condition-name>
97
100
  // @supports (--condition-name) {}
98
- if (stack.at(-1)?.typ === EnumToken.StartParensTokenType) {
99
- const index = tokens.indexOf(stack.at(-1));
100
- const slice = trimArray(tokens.splice(index + 1, tokens.length - index - 2));
101
- slice.filter((token) => {
102
- return (token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType);
103
- });
104
- tokens[index] = {
105
- typ: EnumToken.ParensTokenType,
106
- chi: slice,
107
- [LOCSRCID]: stack.at(-1)[LOCSRCID],
108
- [LOCSTA]: stack.at(-1)[LOCSTA],
109
- [LOCEND]: stream[i]?.[LOCEND],
110
- };
101
+ // inlined: arena.typeOf(stack.at(-1))
102
+ if (arena.dataTypes[stack.at(-1)] === EnumToken.StartParensTokenType) {
103
+ const parens = stack[stack.length - 1];
104
+ const index = tokens.indexOf(parens);
105
+ const slice = trimArray(arena, tokens.splice(index + 1, tokens.length - index - 2));
106
+ arena.dataTypes[parens] = EnumToken.ParensTokenType;
107
+ arena.setTokenList(parens, slice);
108
+ arena.spanLocation(parens, parens, stream[i]);
111
109
  stack.pop();
112
110
  tokens.pop();
113
111
  scopes.pop();
114
112
  scope = scopes[scopes.length - 1];
115
113
  expectAndOr = true;
116
114
  }
117
- else if (tokensfuncDefMap.has(stack.at(-1)?.typ)) {
118
- const index = tokens.indexOf(stack.at(-1));
119
- tokens[index] = {
120
- typ: tokensfuncDefMap.get(stack.at(-1)?.typ),
121
- val: stack.at(-1).val,
122
- chi: trimArray(tokens.splice(index + 1, tokens.length - index - 2)),
123
- [LOCSRCID]: stack.at(-1)[LOCSRCID],
124
- [LOCSTA]: stack.at(-1)[LOCSTA],
125
- [LOCEND]: stream[i]?.[LOCEND],
126
- };
127
- if (tokens[index].typ === EnumToken.PseudoClassFuncTokenType) {
115
+ else if (stack.length > 0 && tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]])) {
116
+ const func = stack[stack.length - 1];
117
+ const index = tokens.indexOf(func);
118
+ arena.dataTypes[func] = tokensfuncDefMap.get(arena.dataTypes[func]);
119
+ arena.setTokenList(func, trimArray(arena, tokens.splice(index + 1, tokens.length - index - 2)));
120
+ arena.spanLocation(func, func, stream[i]);
121
+ if (arena.dataTypes[tokens[index]] === EnumToken.PseudoClassFuncTokenType) {
128
122
  // not a declaration
129
- const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, tokens[index].val + "()")?.[0]?.chi, createValidationContext(tokens[index].chi), options);
123
+ const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, arena.strings[arena.stringValue[tokens[index]]] + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
130
124
  if (!result.success) {
131
125
  return {
132
126
  success: false,
@@ -139,7 +133,7 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
139
133
  }
140
134
  //
141
135
  let k = stack.length - 1;
142
- while (tokensfuncDefMap.has(stack[k]?.typ)) {
136
+ while (tokensfuncDefMap.has(arena.dataTypes[stack[k]])) {
143
137
  k--;
144
138
  }
145
139
  stack.pop();
@@ -147,33 +141,34 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
147
141
  scopes.pop();
148
142
  scope = scopes[scopes.length - 1];
149
143
  }
150
- if (stack.at(-1)?.typ === EnumToken.NotTokenType) {
151
- const index = tokens.indexOf(stack.at(-1));
152
- tokens[index] = {
153
- typ: EnumToken.SupportsQueryUnaryConditionTokenType,
154
- l: stack.at(-1),
155
- r: trimArray(tokens.splice(index + 1, i - index - 1)),
156
- [LOCSRCID]: stack.at(-1)[LOCSRCID],
157
- [LOCSTA]: stack.at(-1)[LOCSTA],
158
- [LOCEND]: stream[i]?.[LOCEND],
159
- };
144
+ // inlined: arena.typeOf(stack.at(-1))
145
+ if (arena.dataTypes[stack.at(-1)] === EnumToken.NotTokenType) {
146
+ const not = stack[stack.length - 1];
147
+ const index = tokens.indexOf(not);
148
+ const unary = arena.createToken(EnumToken.SupportsQueryUnaryConditionTokenType);
149
+ // inlined: arena.setField(unary, TokenField.R, ...trimArray(arena, tokens.splice(index + 1, i - index - 1)))
150
+ arena.setTokenList(arena.fieldList(unary, TokenField.R), trimArray(arena, tokens.splice(index + 1, i - index - 1)));
151
+ // inlined: arena.setField(unary, TokenField.L, not)
152
+ arena.setTokenList(arena.fieldList(unary, TokenField.L), [not]);
153
+ arena.spanLocation(unary, not, stream[i]);
154
+ tokens[index] = unary;
160
155
  stack.pop();
161
156
  }
162
- if (stack.at(-1)?.typ === EnumToken.AndTokenType || stack.at(-1)?.typ === EnumToken.OrTokenType) {
157
+ // inlined: arena.typeOf(stack.at(-1))
158
+ if (arena.dataTypes[stack.at(-1)] === EnumToken.AndTokenType || arena.dataTypes[stack.at(-1)] === EnumToken.OrTokenType) {
163
159
  const index = tokens.indexOf(stack.at(-1));
164
160
  const index2 = stack.length > 1 ? tokens.indexOf(stack.at(-2)) + 1 : 0;
165
- const left = trimArray(tokens.slice(index2, index));
166
- left.find((t) => t.typ === EnumToken.SupportsQueryUnaryConditionTokenType &&
167
- t.l.typ === EnumToken.NotTokenType);
168
- tokens[index2] = {
169
- typ: EnumToken.SupportsQueryConditionTokenType,
170
- op: stack.at(-1),
171
- l: left,
172
- r: trimArray(tokens.slice(index + 1)),
173
- [LOCSRCID]: stack.at(-1)[LOCSRCID],
174
- [LOCSTA]: stack.at(-1)[LOCSTA],
175
- [LOCEND]: stream[i]?.[LOCEND],
176
- };
161
+ const op = stack[stack.length - 1];
162
+ const left = trimArray(arena, tokens.slice(index2, index));
163
+ const condition = arena.createToken(EnumToken.SupportsQueryConditionTokenType);
164
+ // inlined: arena.setField(condition, TokenField.L, ...left)
165
+ arena.setTokenList(arena.fieldList(condition, TokenField.L), left);
166
+ // inlined: arena.setField(condition, TokenField.R, ...trimArray(arena, tokens.slice(index + 1)))
167
+ arena.setTokenList(arena.fieldList(condition, TokenField.R), trimArray(arena, tokens.slice(index + 1)));
168
+ // inlined: arena.setField(condition, TokenField.Op, op)
169
+ arena.setTokenList(arena.fieldList(condition, TokenField.Op), [op]);
170
+ arena.spanLocation(condition, op, stream[i]);
171
+ tokens[index2] = condition;
177
172
  tokens.length = index2 + 1;
178
173
  stack.pop();
179
174
  }
@@ -184,16 +179,18 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
184
179
  break;
185
180
  case EnumToken.IdenTokenType:
186
181
  {
187
- const val = stream[i].val.toLowerCase();
182
+ const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
188
183
  if ("not" === val) {
189
184
  stack.push(stream[i]);
190
- Object.assign(stream[i], { typ: EnumToken.NotTokenType });
185
+ arena.dataTypes[stream[i]] = EnumToken.NotTokenType;
191
186
  break;
192
187
  }
193
188
  if ("and" === val || "or" === val) {
194
189
  if ("or" === val && scopes.length === 1 && scopes[0].size > 0) {
195
- const fileName = options.source.getFileName() ?? "";
196
- const [line, column] = options.source.getOffsets(stream[i]?.[LOCSTA]);
190
+ // inlined: options.source!.getFileName()
191
+ const fileName = options.source.file ?? "";
192
+ // inlined: options.source!.getOffsets(arena.getSpanStart(stream[i]))
193
+ const [line, column] = options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]));
197
194
  return {
198
195
  success: false,
199
196
  errors: [
@@ -205,10 +202,8 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
205
202
  ],
206
203
  };
207
204
  }
208
- Object.assign(stream[i], {
209
- typ: "or" === val ? EnumToken.OrTokenType : EnumToken.AndTokenType,
210
- });
211
- scope.add(stream[i].typ);
205
+ arena.dataTypes[stream[i]] = "or" === val ? EnumToken.OrTokenType : EnumToken.AndTokenType;
206
+ scope.add(arena.dataTypes[stream[i]]);
212
207
  stack.push(stream[i]);
213
208
  break;
214
209
  }
@@ -217,7 +212,7 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
217
212
  }
218
213
  }
219
214
  stream.length = 0;
220
- for (const token of trimArray(tokens)) {
215
+ for (const token of trimArray(arena, tokens)) {
221
216
  stream.push(token);
222
217
  }
223
218
  return { success, errors };
@@ -2,16 +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.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 { trimArray } from '../../validation/match.js';
10
- import { tokensfuncDefMap, LOCEND, LOCSTA, LOCSRCID } from '../../syntax/constants.js';
11
+ import { tokensfuncDefMap } from '../../syntax/constants.js';
11
12
  import { parseMediaqueryList } from './at-rule-media.js';
12
13
  import { parseAtRuleSupportSyntax } from './at-rule-support.js';
13
14
 
14
- function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
15
+ function matchAtRuleWhenElseSyntax(arena, stream, context, options = {}) {
15
16
  const tokens = [];
16
17
  const stack = [];
17
18
  let i = 0;
@@ -25,21 +26,19 @@ function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
25
26
  if (expectAndOr) {
26
27
  let k = i;
27
28
  while (k < stream.length &&
28
- (stream[k]?.typ === EnumToken.WhitespaceTokenType || stream[k]?.typ === EnumToken.CommentTokenType)) {
29
+ (arena.dataTypes[stream[k]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[k]] === EnumToken.CommentTokenType)) {
29
30
  tokens.push(stream[k]);
30
31
  k++;
31
32
  }
32
33
  expectAndOr = false;
33
34
  }
34
- switch (stream[i].typ) {
35
+ switch (arena.dataTypes[stream[i]]) {
35
36
  case EnumToken.IdenTokenType:
36
37
  {
37
- const val = stream[i].val.toLowerCase();
38
+ const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
38
39
  if ("and" === val || "or" === val) {
39
- Object.assign(stream[i], {
40
- typ: "or" === val ? EnumToken.OrTokenType : EnumToken.AndTokenType,
41
- });
42
- // scope.add(stream[i].typ);
40
+ arena.dataTypes[stream[i]] = "or" === val ? EnumToken.OrTokenType : EnumToken.AndTokenType;
41
+ // scope.add(arena.dataTypes[stream[i]]);
43
42
  stack.push(stream[i]);
44
43
  // break;
45
44
  }
@@ -51,66 +50,67 @@ function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
51
50
  let matchCount = 1;
52
51
  let j = i + 1;
53
52
  for (; j < stream.length; j++) {
54
- if (stream[j].typ === EnumToken.EndParensTokenType) {
53
+ if (arena.dataTypes[stream[j]] === EnumToken.EndParensTokenType) {
55
54
  matchCount--;
56
55
  if (matchCount === 0) {
57
56
  break;
58
57
  }
59
58
  }
60
- else if (stream[j].typ === EnumToken.StartParensTokenType ||
61
- tokensfuncDefMap.has(stream[j].typ)) {
59
+ else if (arena.dataTypes[stream[j]] === EnumToken.StartParensTokenType ||
60
+ tokensfuncDefMap.has(arena.dataTypes[stream[j]])) {
62
61
  matchCount++;
63
62
  }
64
63
  }
65
64
  const slice = stream.slice(i, j + 1);
66
- const funcName = stream[i].val.toLowerCase();
67
- const tokenList = [
68
- {
69
- typ: EnumToken.StartParensTokenType,
70
- [LOCSRCID]: stream[i][LOCSRCID],
71
- [LOCSTA]: stream[i][LOCSTA],
72
- [LOCEND]: stream[j]?.[LOCEND],
73
- },
74
- // @ts-expect-error
75
- ].concat(slice.slice(1));
65
+ const funcName = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
66
+ // the function token may be converted by the sub parsers
67
+ const isSupportsFunction = arena.dataTypes[stream[i]] === EnumToken.SupportsFunctionTokenDefType;
68
+ const funcType = tokensfuncDefMap.get(arena.dataTypes[stream[i]]);
69
+ const parens = arena.createToken(EnumToken.StartParensTokenType);
70
+ arena.spanLocation(parens, stream[i], stream[j]);
71
+ const tokenList = [parens].concat(slice.slice(1));
76
72
  if ("media" === funcName) {
77
- const result = parseMediaqueryList(tokenList, options);
73
+ const result = parseMediaqueryList(arena, tokenList, options);
78
74
  if (!result.success) {
79
75
  return result;
80
76
  }
81
77
  }
82
78
  else if ("supports" === funcName) {
83
- const result = parseAtRuleSupportSyntax(tokenList, context, options);
79
+ const result = parseAtRuleSupportSyntax(arena, tokenList, context, options);
84
80
  if (!result.success) {
85
81
  return result;
86
82
  }
87
83
  }
88
- else if (stream[i].typ === EnumToken.SupportsFunctionTokenDefType) {
84
+ else if (isSupportsFunction) {
89
85
  tokenList.splice(0, tokenList.length, ...slice);
90
- const result = parseAtRuleSupportSyntax(stream.slice(i, j + 1), context, options);
86
+ const result = parseAtRuleSupportSyntax(arena, stream.slice(i, j + 1), context, options);
91
87
  if (!result.success) {
92
88
  return result;
93
89
  }
94
90
  }
95
- stream[i][LOCEND] = stream[j]?.[LOCEND];
96
- Object.assign(stream[i], {
97
- typ: tokensfuncDefMap.get(stream[i].typ),
98
- chi: stream[i].typ === EnumToken.SupportsFunctionTokenDefType
99
- ? trimArray(slice.slice(1, -1))
100
- : tokenList[0].chi,
101
- });
102
- if (stack.at(-1)?.typ === EnumToken.AndTokenType || stack.at(-1)?.typ === EnumToken.OrTokenType) {
103
- const index = tokens.indexOf(stack.at(-1));
104
- const index2 = stack.length > 1 ? tokens.indexOf(stack.at(-2)) + 1 : 0;
105
- tokens[index2] = {
106
- typ: EnumToken.WhenElseQueryConditionTokenType,
107
- op: stack.at(-1),
108
- l: trimArray(tokens.slice(index2, index)),
109
- r: trimArray(tokens.slice(index + 1)),
110
- [LOCSRCID]: stack.at(-1)[LOCSRCID],
111
- [LOCSTA]: stack.at(-1)[LOCSTA],
112
- [LOCEND]: stream[i]?.[LOCEND],
113
- };
91
+ arena.spanLocation(stream[i], stream[i], stream[j]);
92
+ const chi = isSupportsFunction
93
+ ? arena.firstToken[stream[i]] != 0
94
+ ? arena.tokens(stream[i])
95
+ : trimArray(arena, slice.slice(1, -1))
96
+ : arena.tokens(tokenList[0]);
97
+ arena.dataTypes[stream[i]] = funcType;
98
+ arena.setTokenList(stream[i], chi);
99
+ if (stack.length > 0 &&
100
+ (arena.dataTypes[stack[stack.length - 1]] === EnumToken.AndTokenType ||
101
+ arena.dataTypes[stack[stack.length - 1]] === EnumToken.OrTokenType)) {
102
+ const op = stack[stack.length - 1];
103
+ const index = tokens.indexOf(op);
104
+ const index2 = stack.length > 1 ? tokens.indexOf(stack[stack.length - 2]) + 1 : 0;
105
+ const condition = arena.createToken(EnumToken.WhenElseQueryConditionTokenType);
106
+ // inlined: arena.setField(condition, TokenField.L, ...trimArray(arena, tokens.slice(index2, index)))
107
+ arena.setTokenList(arena.fieldList(condition, TokenField.L), trimArray(arena, tokens.slice(index2, index)));
108
+ // inlined: arena.setField(condition, TokenField.R, ...trimArray(arena, tokens.slice(index + 1)))
109
+ arena.setTokenList(arena.fieldList(condition, TokenField.R), trimArray(arena, tokens.slice(index + 1)));
110
+ // inlined: arena.setField(condition, TokenField.Op, op)
111
+ arena.setTokenList(arena.fieldList(condition, TokenField.Op), [op]);
112
+ arena.spanLocation(condition, op, stream[i]);
113
+ tokens[index2] = condition;
114
114
  tokens.length = index2 + 1;
115
115
  stack.pop();
116
116
  }
@@ -121,7 +121,7 @@ function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
121
121
  }
122
122
  }
123
123
  stream.length = 0;
124
- for (const token of trimArray(tokens)) {
124
+ for (const token of trimArray(arena, tokens)) {
125
125
  stream.push(token);
126
126
  }
127
127
  return { success, errors };
@@ -2,21 +2,21 @@
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
8
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
9
9
  import { getSyntaxRule } from '../../validation/config.js';
10
10
  import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
11
11
 
12
- function matchAtRuleSyntax(atRule, stream, options) {
13
- const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + atRule.nam);
12
+ function matchAtRuleSyntax(arena, atRule, stream, options) {
13
+ const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.strings[arena.name[atRule]]);
14
14
  const syntax = syntaxRules?.getPreludeRules()?.slice?.(1);
15
- trimArray(stream);
15
+ trimArray(arena, stream);
16
16
  if (syntax.length === 0) {
17
17
  return { success: true, errors: [] };
18
18
  }
19
- const { success, errors } = matchAllSyntaxes(syntax, createValidationContext(stream), options);
19
+ const { success, errors } = matchAllSyntaxes(arena, syntax, createValidationContext(arena, stream), options);
20
20
  return { success, errors };
21
21
  }
22
22
 
@@ -2,7 +2,7 @@
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
8
  /**
@@ -11,7 +11,7 @@
11
11
  * @returns
12
12
  */
13
13
  function toBase64(value) {
14
- let result = '';
14
+ let result = "";
15
15
  for (const c of new TextEncoder().encode(value)) {
16
16
  result += String.fromCharCode(c);
17
17
  }
@@ -2,7 +2,7 @@
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
8
  /**
@@ -2,7 +2,7 @@
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
8
  import config from '../../../data/properties.json.js';