@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,12 +2,12 @@
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, EnumAstNodeStatus } from '../../ast/types.js';
9
9
  import { renderValue } from '../../printer/render.js';
10
- import { LOCEND, LOCSTA, LOCSRCID, ERRORS, STATE, TOKENS, pseudoElements, combinators, tokensfuncDefMap, PARENT } from '../../syntax/constants.js';
10
+ import { pseudoElements, tokensfuncDefMap, combinators } from '../../syntax/constants.js';
11
11
  import { isHash } from '../../syntax/syntax.js';
12
12
  import { getParsedSyntax, getSyntaxRule, getSyntaxConfig } from '../../validation/config.js';
13
13
  import { matchAllSyntaxes, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
@@ -16,37 +16,161 @@ import { splitTokenList } from '../../validation/utils/list.js';
16
16
  import { trimWhiteSpace } from '../parse.js';
17
17
  import { equalsIgnoreCase } from './text.js';
18
18
 
19
+ /**
20
+ * minify the an+b expression of a pseudo class function
21
+ * @param arena
22
+ * @param func
23
+ */
24
+ function minifyNthExpression(arena, func) {
25
+ const chi = arena.tokens(func);
26
+ const list = [];
27
+ let index;
28
+ for (index = 0; index < chi.length; index++) {
29
+ if (arena.dataTypes[chi[index]] == EnumToken.CommentTokenType ||
30
+ arena.dataTypes[chi[index]] == EnumToken.WhitespaceTokenType) {
31
+ continue;
32
+ }
33
+ if (arena.dataTypes[chi[index]] == EnumToken.IdenTokenType &&
34
+ equalsIgnoreCase("of", arena.strings[arena.stringValue[chi[index]]])) {
35
+ index--;
36
+ break;
37
+ }
38
+ list.push(chi[index]);
39
+ }
40
+ if (list.length == 2) {
41
+ if (arena.dataTypes[list[1]] == EnumToken.NumberTokenType) {
42
+ if (arena.numericValue[list[1]] == 0) {
43
+ list.length = 1;
44
+ if (arena.dataTypes[list[0]] == EnumToken.DimensionTokenType && arena.numericValue[list[0]] == -2) {
45
+ arena.numericValue[list[0]] = 2;
46
+ }
47
+ }
48
+ else {
49
+ const sign = Math.sign(arena.numericValue[list[1]]);
50
+ arena.numericValue[list[1]] *= sign;
51
+ list.splice(1, 0, arena.createToken(EnumToken.LiteralTokenType, sign > 0 ? "+" : "-"));
52
+ }
53
+ }
54
+ if (list.length == 3 &&
55
+ arena.dataTypes[list[2]] == EnumToken.NumberTokenType &&
56
+ arena.dataTypes[list[0]] == EnumToken.DimensionTokenType &&
57
+ (arena.numericValue[list[0]] == 2 || arena.numericValue[list[0]] == -2)) {
58
+ if (1 == arena.numericValue[list[2]]) {
59
+ const ident = arena.createToken(EnumToken.IdenTokenType, "odd");
60
+ arena.copyLocation(ident, list[0]);
61
+ list.splice(0, 3, ident);
62
+ }
63
+ else if (0 == arena.numericValue[list[2]]) {
64
+ const ident = arena.createToken(EnumToken.IdenTokenType, "even");
65
+ arena.copyLocation(ident, list[0]);
66
+ list.splice(0, 3, ident);
67
+ }
68
+ }
69
+ chi.splice(0, index, ...list);
70
+ }
71
+ if (list.length == 1) {
72
+ if (arena.dataTypes[list[0]] == EnumToken.IdenTokenType &&
73
+ equalsIgnoreCase("-n", arena.strings[arena.stringValue[list[0]]])) {
74
+ // inlined: arena.setStr(list[0], "n")
75
+ arena.stringValue[list[0]] = arena.internString("n");
76
+ }
77
+ }
78
+ if (list.length == 3) {
79
+ if (arena.dataTypes[list[0]] == EnumToken.IdenTokenType &&
80
+ ("n" == arena.strings[arena.stringValue[list[0]]] ||
81
+ "-n" == arena.strings[arena.stringValue[list[0]]] ||
82
+ "+n" == arena.strings[arena.stringValue[list[0]]])) {
83
+ if (arena.dataTypes[list[1]] == EnumToken.NextSiblingCombinatorTokenType) {
84
+ if (arena.dataTypes[list[2]] == EnumToken.NumberTokenType && 0 == arena.numericValue[list[2]]) {
85
+ // inlined: arena.setStr(list[0], "n")
86
+ arena.stringValue[list[0]] = arena.internString("n");
87
+ chi.splice(0, index, list[0]);
88
+ arena.setTokenList(func, chi);
89
+ return;
90
+ }
91
+ }
92
+ }
93
+ }
94
+ const token = chi.find((t) => arena.dataTypes[t] != EnumToken.WhitespaceTokenType && arena.dataTypes[t] != EnumToken.CommentTokenType);
95
+ if (token != null) {
96
+ if (arena.dataTypes[token] == EnumToken.IdenTokenType || arena.dataTypes[token] == EnumToken.LiteralTokenType) {
97
+ const val = arena.strings[arena.stringValue[token]];
98
+ if (arena.dataTypes[token] == EnumToken.IdenTokenType && (val == "odd" || val == "even")) {
99
+ if (val == "even") {
100
+ arena.resetToken(token, EnumToken.DimensionTokenType, 2, "n");
101
+ }
102
+ }
103
+ else {
104
+ const matches = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(val);
105
+ if (matches != null) {
106
+ const a1 = matches[2] === "" ? 1 : matches[2] === "-" ? -1 : +matches[2];
107
+ const b1 = +matches[3];
108
+ if (b1 === 0) {
109
+ if (Math.abs(a1) === 1) {
110
+ arena.resetToken(token, EnumToken.IdenTokenType, "n");
111
+ }
112
+ else {
113
+ arena.resetToken(token, EnumToken.DimensionTokenType, a1 * Math.sign(a1), "n");
114
+ }
115
+ }
116
+ }
117
+ }
118
+ }
119
+ else if (arena.dataTypes[token] === EnumToken.DimensionTokenType) {
120
+ const index = chi.indexOf(token);
121
+ let num = null;
122
+ let i = index + 1;
123
+ for (; i < chi.length; i++) {
124
+ if (arena.dataTypes[chi[i]] == EnumToken.WhitespaceTokenType ||
125
+ arena.dataTypes[chi[i]] == EnumToken.CommentTokenType) {
126
+ continue;
127
+ }
128
+ if (arena.dataTypes[chi[i]] == EnumToken.NumberTokenType) {
129
+ num = chi[i];
130
+ break;
131
+ }
132
+ }
133
+ if (num != null) {
134
+ if (arena.numericValue[num] === 0) {
135
+ chi.splice(index + 1, i - index);
136
+ if (arena.numericValue[token] < 0) {
137
+ arena.numericValue[token] *= -1;
138
+ }
139
+ }
140
+ else if (arena.numericValue[token] === 2 && Math.abs(arena.numericValue[num]) === 1) {
141
+ arena.resetToken(token, EnumToken.IdenTokenType, "odd");
142
+ chi.splice(index + 1, i - index);
143
+ }
144
+ }
145
+ }
146
+ }
147
+ arena.setTokenList(func, chi);
148
+ }
19
149
  /**
20
150
  * parse selector
151
+ * @param arena
152
+ * @param tokens
153
+ * @param context parent node id
154
+ * @param options
155
+ * @param errors
21
156
  */
22
- function parseSelector(tokens, context, options, errors) {
23
- if (context?.typ === EnumToken.KeyframesAtRuleNodeType) {
24
- const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(tokens), options);
25
- const parts = splitTokenList(tokens);
157
+ function parseSelector(arena, tokens, context, options, errors) {
158
+ const contextType = context == null ? null : arena.dataTypes[context];
159
+ if (contextType === EnumToken.KeyframesAtRuleNodeType) {
160
+ const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(arena, tokens), options);
161
+ const parts = splitTokenList(arena, tokens);
26
162
  for (const part of parts) {
27
- trimArray(part);
163
+ trimArray(arena, part);
28
164
  if (options.minify) {
29
- const filtered = part.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType);
165
+ const filtered = part.filter((token) => arena.dataTypes[token] !== EnumToken.WhitespaceTokenType && arena.dataTypes[token] !== EnumToken.CommentTokenType);
30
166
  if (filtered.length === 1) {
31
- if (filtered[0].typ === EnumToken.IdenTokenType &&
32
- "from" === filtered[0].val.toLowerCase()) {
33
- filtered[0] = {
34
- typ: EnumToken.PercentageTokenType,
35
- val: 0,
36
- [LOCSRCID]: filtered[0][LOCSRCID],
37
- [LOCSTA]: filtered[0][LOCSTA],
38
- [LOCEND]: filtered[0][LOCEND],
39
- };
167
+ if (arena.dataTypes[filtered[0]] === EnumToken.IdenTokenType &&
168
+ "from" === arena.strings[arena.stringValue[filtered[0]]].toLowerCase()) {
169
+ arena.resetToken(filtered[0], EnumToken.PercentageTokenType, 0);
40
170
  }
41
- else if (filtered[0].typ === EnumToken.PercentageTokenType &&
42
- 100 === filtered[0].val) {
43
- filtered[0] = {
44
- typ: EnumToken.IdenTokenType,
45
- val: "to",
46
- [LOCSRCID]: filtered[0][LOCSRCID],
47
- [LOCSTA]: filtered[0][LOCSTA],
48
- [LOCEND]: filtered[0][LOCEND],
49
- };
171
+ else if (arena.dataTypes[filtered[0]] === EnumToken.PercentageTokenType &&
172
+ 100 === arena.numericValue[filtered[0]]) {
173
+ arena.resetToken(filtered[0], EnumToken.IdenTokenType, "to");
50
174
  }
51
175
  part.splice(0, part.length, ...filtered);
52
176
  }
@@ -54,41 +178,33 @@ function parseSelector(tokens, context, options, errors) {
54
178
  }
55
179
  tokens.splice(0, tokens.length, ...parts.reduce((acc, curr) => {
56
180
  if (acc.length > 0) {
57
- acc.push({ typ: EnumToken.CommaTokenType });
181
+ acc.push(arena.createToken(EnumToken.CommaTokenType));
58
182
  }
59
183
  for (const c of curr) {
60
184
  acc.push(c);
61
185
  }
62
186
  return acc;
63
187
  }, []));
64
- return {
65
- typ: EnumToken.KeyframesRuleNodeType,
66
- sel: [
67
- ...splitTokenList(trimArray(tokens)).reduce((acc, curr) => {
68
- acc.add(curr.reduce((acc, curr) => acc + renderValue(curr, { minify: false }), ""));
69
- return acc;
70
- }, new Set()),
71
- ].join(),
72
- chi: [],
73
- [LOCSRCID]: tokens[0]?.[LOCSRCID],
74
- [LOCSTA]: tokens[0]?.[LOCSTA],
75
- [LOCEND]: tokens[tokens.length - 1]?.[LOCEND],
76
- [TOKENS]: tokens.length === 0 ? null : tokens,
77
- [STATE]: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
78
- [ERRORS]: result.errors,
79
- };
188
+ const node = arena.createRule(EnumToken.KeyframesRuleNodeType, [
189
+ ...splitTokenList(arena, trimArray(arena, tokens)).reduce((acc, curr) => {
190
+ acc.add(curr.reduce((acc, curr) => acc + renderValue(arena, curr, { minify: false }), ""));
191
+ return acc;
192
+ }, new Set()),
193
+ ].join(), tokens.length === 0 ? null : tokens, result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid, result.errors);
194
+ // inlined: arena.getSource(tokens[0])
195
+ arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
196
+ return node;
80
197
  }
81
198
  const stack = [];
82
- const uniq = new Map();
83
199
  let allowed = true;
84
200
  let index;
85
201
  let parent = context;
86
202
  let nested = false;
87
203
  let val;
88
- if (context?.typ !== EnumToken.StyleSheetNodeType && context?.typ !== EnumToken.RuleNodeType) {
204
+ if (contextType !== EnumToken.StyleSheetNodeType && contextType !== EnumToken.RuleNodeType) {
89
205
  allowed = false;
90
- if (context?.typ === EnumToken.AtRuleNodeType) {
91
- const syntaxRule = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + context.nam);
206
+ if (contextType === EnumToken.AtRuleNodeType) {
207
+ const syntaxRule = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.strings[arena.name[context]]);
92
208
  allowed = syntaxRule?.acceptAnyRule ?? false;
93
209
  if (!allowed) {
94
210
  const rules = syntaxRule?.getBlockRules?.();
@@ -106,130 +222,88 @@ function parseSelector(tokens, context, options, errors) {
106
222
  }
107
223
  }
108
224
  }
109
- do {
110
- if (parent?.typ === EnumToken.AtRuleNodeType && "media" === parent.nam) {
111
- parent = parent[PARENT];
225
+ while (parent != null && parent > 0) {
226
+ if (arena.dataTypes[parent] === EnumToken.AtRuleNodeType && "media" === arena.strings[arena.name[parent]]) {
227
+ parent = arena.parents[parent];
112
228
  continue;
113
229
  }
114
- nested = parent?.typ == EnumToken.RuleNodeType;
115
- parent = parent?.[PARENT];
116
- } while (!nested && parent != null);
230
+ nested = arena.dataTypes[parent] == EnumToken.RuleNodeType;
231
+ if (nested) {
232
+ break;
233
+ }
234
+ parent = arena.parents[parent];
235
+ }
117
236
  const all = new Array(tokens.length);
118
237
  let write = 0;
119
238
  for (let read = 0; read < tokens.length; read++) {
120
239
  let token = tokens[read];
121
- if (token.typ == EnumToken.ColonTokenType) {
240
+ if (arena.dataTypes[token] == EnumToken.ColonTokenType) {
122
241
  const next = tokens[read + 1];
123
- if (next?.typ == EnumToken.IdenTokenType) {
124
- all[write++] = {
125
- ...token,
126
- typ: EnumToken.PseudoElementTokenType,
127
- val: ":" + next.val,
128
- [LOCEND]: next[LOCEND],
129
- };
242
+ if (arena.dataTypes[next] == EnumToken.IdenTokenType) {
243
+ arena.dataTypes[token] = EnumToken.PseudoElementTokenType;
244
+ // inlined: arena.setStr(token, ":" + arena.strings[arena.stringValue[next]])
245
+ arena.stringValue[token] = arena.internString((":" + arena.strings[arena.stringValue[next]]));
246
+ arena.spanLocation(token, token, next);
247
+ all[write++] = token;
130
248
  read++;
131
249
  continue;
132
250
  }
133
- if (next?.typ == EnumToken.FunctionTokenDefType) {
134
- val = ":" + next.val;
135
- all[write++] = {
136
- ...token,
137
- typ: val + "()" in getSyntaxConfig().selectors
251
+ if (arena.dataTypes[next] == EnumToken.FunctionTokenDefType) {
252
+ val = ":" + arena.strings[arena.stringValue[next]];
253
+ arena.dataTypes[token] =
254
+ val + "()" in getSyntaxConfig().selectors
138
255
  ? EnumToken.PseudoClassFunctionTokenDefType
139
- : next.typ,
140
- val,
141
- [LOCEND]: next[LOCEND],
142
- };
256
+ : arena.dataTypes[next];
257
+ // inlined: arena.setStr(token, val)
258
+ arena.stringValue[token] = arena.internString(val);
259
+ arena.spanLocation(token, token, next);
260
+ all[write++] = token;
143
261
  read++;
144
262
  continue;
145
263
  }
146
264
  }
147
- if (token.typ == EnumToken.DoubleColonTokenType) {
265
+ if (arena.dataTypes[token] == EnumToken.DoubleColonTokenType) {
148
266
  const next = tokens[read + 1];
149
- val = ":" + next.val;
150
- if (next?.typ == EnumToken.IdenTokenType) {
151
- all[write++] = {
152
- ...token,
153
- typ: EnumToken.PseudoClassTokenType,
154
- val: (pseudoElements.includes(val) ? "" : ":") + val,
155
- [LOCEND]: next[LOCEND],
156
- };
267
+ val = ":" + arena.strings[arena.stringValue[next]];
268
+ if (arena.dataTypes[next] == EnumToken.IdenTokenType) {
269
+ arena.dataTypes[token] = EnumToken.PseudoClassTokenType;
270
+ // inlined: arena.setStr(token, (pseudoElements.includes(val) ? "" : ":") + val)
271
+ arena.stringValue[token] = arena.internString(((pseudoElements.includes(val) ? "" : ":") + val));
272
+ arena.spanLocation(token, token, next);
273
+ all[write++] = token;
157
274
  read++;
158
275
  continue;
159
276
  }
160
- if (next?.typ == EnumToken.FunctionTokenDefType) {
161
- val = "::" + next.val;
162
- all[write++] = {
163
- ...token,
164
- typ: val + "()" in getSyntaxConfig().selectors
277
+ if (arena.dataTypes[next] == EnumToken.FunctionTokenDefType) {
278
+ val = "::" + arena.strings[arena.stringValue[next]];
279
+ arena.dataTypes[token] =
280
+ val + "()" in getSyntaxConfig().selectors
165
281
  ? EnumToken.PseudoClassFunctionTokenDefType
166
- : EnumToken.FunctionTokenDefType,
167
- val,
168
- [LOCEND]: next[LOCEND],
169
- };
282
+ : EnumToken.FunctionTokenDefType;
283
+ // inlined: arena.setStr(token, val)
284
+ arena.stringValue[token] = arena.internString(val);
285
+ arena.spanLocation(token, token, next);
286
+ all[write++] = token;
170
287
  read++;
171
288
  continue;
172
289
  }
173
290
  }
174
- if (token.typ == EnumToken.ColorTokenType) {
175
- if (isHash(token.val)) {
176
- token.typ = EnumToken.HashTokenType;
291
+ if (arena.dataTypes[token] == EnumToken.ColorTokenType) {
292
+ if (isHash(arena.strings[arena.stringValue[token]])) {
293
+ arena.dataTypes[token] = EnumToken.HashTokenType;
177
294
  all[write++] = token;
178
295
  continue;
179
296
  }
180
- return {
181
- typ: EnumToken.RuleNodeType,
182
- sel: [
183
- ...tokens
184
- .reduce((acc, curr, index, array) => {
185
- if (curr.typ == EnumToken.WhitespaceTokenType) {
186
- if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
187
- trimWhiteSpace.includes(array[index + 1]?.typ) ||
188
- combinators.includes(array[index - 1]?.val) ||
189
- combinators.includes(array[index + 1]?.val)) {
190
- return acc;
191
- }
192
- }
193
- let t = renderValue(curr, { minify: false });
194
- if (t == ",") {
195
- acc.push([]);
196
- }
197
- else {
198
- acc[acc.length - 1].push(t);
199
- }
200
- return acc;
201
- }, [[]])
202
- .reduce((acc, curr) => {
203
- let i = 0;
204
- for (; i < curr.length; i++) {
205
- if (i + 1 < curr.length && curr[i] == "*") {
206
- if (curr[i] == "*") {
207
- let index = curr[i + 1] == " " ? 2 : 1;
208
- if (![">", "~", "+"].includes(curr[index])) {
209
- curr.splice(i, index);
210
- }
211
- }
212
- }
213
- }
214
- acc.set(curr.join(""), curr);
215
- return acc;
216
- }, uniq)
217
- .keys(),
218
- ].join(","),
219
- chi: [],
220
- [LOCSRCID]: tokens[0][LOCSRCID],
221
- [LOCSTA]: tokens[0][LOCSTA],
222
- [LOCEND]: tokens[tokens.length - 1][LOCEND],
223
- [TOKENS]: tokens,
224
- [STATE]: EnumAstNodeStatus.Invalid,
225
- [ERRORS]: [
226
- {
227
- action: "drop",
228
- node: token,
229
- message: "invalid hash id",
230
- },
231
- ],
232
- };
297
+ const node = arena.createRule(EnumToken.RuleNodeType, renderSelector(arena, tokens), tokens, EnumAstNodeStatus.Invalid, [
298
+ {
299
+ action: "drop",
300
+ node: token,
301
+ message: "invalid hash id",
302
+ },
303
+ ]);
304
+ // inlined: arena.getSource(tokens[0])
305
+ arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
306
+ return node;
233
307
  }
234
308
  all[write++] = token;
235
309
  }
@@ -239,29 +313,25 @@ function parseSelector(tokens, context, options, errors) {
239
313
  }
240
314
  tokens.length = write;
241
315
  }
242
- const result = matchSelectorSyntax(tokens, errors, options, nested);
243
- trimArray(tokens);
316
+ const result = matchSelectorSyntax(arena, tokens, errors, options, nested);
317
+ trimArray(arena, tokens);
244
318
  if (result.success) {
245
319
  for (let i = 0; i < tokens.length; i++) {
246
320
  const token = tokens[i];
247
- switch (token.typ) {
321
+ switch (arena.dataTypes[token]) {
248
322
  case EnumToken.AttrStartTokenType:
249
323
  stack.push(token);
250
324
  break;
251
325
  case EnumToken.AttrEndTokenType:
252
- if (stack.at(-1)?.typ == EnumToken.AttrStartTokenType) {
253
- index = tokens.indexOf(stack.at(-1));
254
- // @ts-expect-error
255
- const { val, ...attr } = stack.at(-1);
256
- attr[LOCSRCID] = stack.at(-1)[LOCSRCID];
257
- attr[LOCSTA] = stack.at(-1)[LOCSTA];
258
- attr[LOCEND] = token[LOCEND];
326
+ // inlined: arena.typeOf(stack.at(-1))
327
+ if (arena.dataTypes[stack.at(-1)] == EnumToken.AttrStartTokenType) {
328
+ const attr = stack[stack.length - 1];
329
+ index = tokens.indexOf(attr);
330
+ arena.spanLocation(attr, attr, token);
259
331
  tokens.splice(i, 1);
260
- Object.assign(attr, {
261
- typ: EnumToken.AttrTokenType,
262
- chi: tokens.splice(index + 1, i - index - 1),
263
- });
264
- tokens[index] = attr;
332
+ arena.dataTypes[attr] = EnumToken.AttrTokenType;
333
+ arena.stringValue[attr] = 0;
334
+ arena.setTokenList(attr, tokens.splice(index + 1, i - index - 1));
265
335
  i = index;
266
336
  stack.pop();
267
337
  }
@@ -270,186 +340,25 @@ function parseSelector(tokens, context, options, errors) {
270
340
  stack.push(token);
271
341
  break;
272
342
  case EnumToken.EndParensTokenType:
273
- if (stack.at(-1)?.typ == EnumToken.PseudoClassFunctionTokenDefType) {
274
- const func = stack.at(-1);
343
+ // inlined: arena.typeOf(stack.at(-1))
344
+ if (arena.dataTypes[stack.at(-1)] == EnumToken.PseudoClassFunctionTokenDefType) {
345
+ const func = stack[stack.length - 1];
275
346
  index = tokens.indexOf(func);
276
- stack.at(-1)[LOCEND] = token[LOCEND];
347
+ arena.spanLocation(func, func, token);
277
348
  tokens.splice(i, 1);
278
- if (tokensfuncDefMap.has(func.typ)) {
279
- // @ts-expect-error
280
- func.typ = tokensfuncDefMap.get(func.typ);
281
- func.chi = trimArray(tokens.splice(index + 1, i - index - 1));
349
+ if (tokensfuncDefMap.has(arena.dataTypes[func])) {
350
+ arena.dataTypes[func] = tokensfuncDefMap.get(arena.dataTypes[func]);
351
+ arena.setTokenList(func, trimArray(arena, tokens.splice(index + 1, i - index - 1)));
282
352
  }
283
353
  if (result.success && options.minify) {
284
354
  // parse an+b
285
355
  // an+b produces an ident such as 'n-0', a literal such as '+2n-0' or a list of tokens such as[2n\s?[+-]\s?3]
286
- if (func.val == ":nth-child" ||
287
- func.val == ":nth-last-child" ||
288
- func.val == ":nth-of-type" ||
289
- func.val == ":nth-last-of-type") {
290
- const list = [];
291
- let index;
292
- for (index = 0; index < func.chi.length; index++) {
293
- if (func.chi[index].typ == EnumToken.CommentTokenType ||
294
- func.chi[index].typ == EnumToken.WhitespaceTokenType) {
295
- continue;
296
- }
297
- if (func.chi[index].typ == EnumToken.IdenTokenType &&
298
- equalsIgnoreCase("of", func.chi[index].val)) {
299
- index--;
300
- break;
301
- }
302
- list.push(func.chi[index]);
303
- }
304
- if (list.length == 2) {
305
- if (list[1].typ == EnumToken.NumberTokenType) {
306
- if (list[1].val == 0) {
307
- list.length = 1;
308
- if (list[0].typ == EnumToken.DimensionTokenType &&
309
- list[0].val == -2) {
310
- list[0].val = 2;
311
- }
312
- }
313
- else {
314
- const sign = Math.sign(list[1].val);
315
- // @ts-ignore
316
- list[1].val *= sign;
317
- list.splice(1, 0, {
318
- typ: EnumToken.LiteralTokenType,
319
- val: sign > 0 ? "+" : "-",
320
- });
321
- }
322
- }
323
- if (list.length == 3 &&
324
- list[2].typ == EnumToken.NumberTokenType &&
325
- list[0].typ == EnumToken.DimensionTokenType &&
326
- (list[0].val == 2 ||
327
- list[0].val == -2)) {
328
- if (1 == list[2].val) {
329
- list.splice(0, 3, {
330
- typ: EnumToken.IdenTokenType,
331
- val: "odd",
332
- [LOCSRCID]: list[0][LOCSRCID],
333
- [LOCSTA]: list[0][LOCSTA],
334
- [LOCEND]: list[0][LOCEND],
335
- });
336
- }
337
- else if (0 == list[2].val) {
338
- list.splice(0, 3, {
339
- typ: EnumToken.IdenTokenType,
340
- val: "even",
341
- [LOCSRCID]: list[0][LOCSRCID],
342
- [LOCSTA]: list[0][LOCSTA],
343
- [LOCEND]: list[0][LOCEND],
344
- });
345
- }
346
- }
347
- func.chi.splice(0, index, ...list);
348
- }
349
- if (list.length == 1) {
350
- if (list[0].typ == EnumToken.IdenTokenType &&
351
- equalsIgnoreCase("-n", list[0].val)) {
352
- list[0].val = "n";
353
- }
354
- }
355
- if (list.length == 3) {
356
- if (list[0].typ == EnumToken.IdenTokenType &&
357
- ("n" == list[0].val ||
358
- "-n" == list[0].val ||
359
- "+n" == list[0].val)) {
360
- if (list[1].typ == EnumToken.NextSiblingCombinatorTokenType) {
361
- if (list[2].typ == EnumToken.NumberTokenType &&
362
- 0 == list[2].val) {
363
- list[0].val = "n";
364
- func.chi.splice(0, index, list[0]);
365
- break;
366
- }
367
- }
368
- }
369
- }
370
- const token = func.chi.find((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ != EnumToken.CommentTokenType);
371
- if (token?.typ == EnumToken.IdenTokenType || token?.typ == EnumToken.LiteralTokenType) {
372
- if (token.typ == EnumToken.IdenTokenType &&
373
- (token.val == "odd" || token.val == "even")) {
374
- if (token.val == "even") {
375
- Object.assign(token, {
376
- typ: EnumToken.DimensionTokenType,
377
- val: 2,
378
- unit: "n",
379
- });
380
- }
381
- }
382
- else {
383
- const matches = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(token.val);
384
- if (matches != null) {
385
- const a1 = matches[2] === "" ? 1 : matches[2] === "-" ? -1 : +matches[2];
386
- const b1 = +matches[3];
387
- if (b1 === 0) {
388
- Object.assign(token, Math.abs(a1) === 1
389
- ? {
390
- typ: EnumToken.IdenTokenType,
391
- val: "n",
392
- }
393
- : {
394
- typ: EnumToken.DimensionTokenType,
395
- val: a1 * Math.sign(a1),
396
- unit: "n",
397
- });
398
- }
399
- }
400
- }
401
- }
402
- else if (token?.typ === EnumToken.DimensionTokenType) {
403
- const index = func.chi.indexOf(token);
404
- let num = null;
405
- let i = index + 1;
406
- for (; i < func.chi.length; i++) {
407
- if (func.chi[i].typ == EnumToken.WhitespaceTokenType ||
408
- func.chi[i].typ == EnumToken.CommentTokenType) {
409
- continue;
410
- }
411
- if (func.chi[i].typ == EnumToken.NumberTokenType) {
412
- num = func.chi[i];
413
- break;
414
- }
415
- }
416
- if (num != null) {
417
- if (num.val === 0) {
418
- func.chi.splice(index + 1, i - index);
419
- if (token.val < 0) {
420
- token.val *= -1;
421
- }
422
- break;
423
- }
424
- else if (token.val === 2 &&
425
- Math.abs(num.val) === 1) {
426
- Object.assign(token, {
427
- typ: EnumToken.IdenTokenType,
428
- val: "odd",
429
- });
430
- func.chi.splice(index + 1, i - index);
431
- break;
432
- }
433
- // if (((token as DimensionToken).val as number) === 1) {
434
- // Object.assign(token, {
435
- // typ: EnumToken.IdenTokenType,
436
- // val:
437
- // Math.sign((token as DimensionToken).val as number) === 1
438
- // ? "n"
439
- // : "-n",
440
- // });
441
- // }
442
- // if (sign == null) {
443
- // func.chi.splice(index + 1, i - index - 1);
444
- // if (Math.sign(num.val as number) === 1) {
445
- // func.chi.splice(index + 1, 0, {
446
- // typ: EnumToken.LiteralTokenType,
447
- // val: "+",
448
- // });
449
- // }
450
- // }
451
- }
452
- }
356
+ const name = arena.strings[arena.stringValue[func]];
357
+ if (name == ":nth-child" ||
358
+ name == ":nth-last-child" ||
359
+ name == ":nth-of-type" ||
360
+ name == ":nth-last-of-type") {
361
+ minifyNthExpression(arena, func);
453
362
  }
454
363
  }
455
364
  i = index;
@@ -459,60 +368,73 @@ function parseSelector(tokens, context, options, errors) {
459
368
  }
460
369
  }
461
370
  }
462
- return {
463
- typ: EnumToken.RuleNodeType,
464
- sel: [
465
- ...tokens
466
- .reduce((acc, curr, index, array) => {
467
- // if (curr.typ == EnumToken.CommentTokenType) {
468
- // return acc;
469
- // }
470
- if (curr.typ == EnumToken.WhitespaceTokenType) {
471
- if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
472
- trimWhiteSpace.includes(array[index + 1]?.typ) ||
473
- combinators.includes(array[index - 1]?.val) ||
474
- combinators.includes(array[index + 1]?.val)) {
475
- return acc;
476
- }
477
- }
478
- let t = renderValue(curr, { minify: false });
479
- if (t == ",") {
480
- acc.push([]);
481
- }
482
- else {
483
- acc[acc.length - 1].push(t);
484
- }
485
- return acc;
486
- }, [[]])
487
- .reduce((acc, curr) => {
488
- let i = 0;
489
- for (; i < curr.length; i++) {
490
- if (i + 1 < curr.length && curr[i] == "*") {
491
- if (curr[i] == "*") {
492
- let index = curr[i + 1] == " " ? 2 : 1;
493
- if (![">", "~", "+"].includes(curr[index])) {
494
- curr.splice(i, index);
495
- }
496
- }
497
- }
498
- }
499
- acc.set(curr.join(""), curr);
500
- return acc;
501
- }, uniq)
502
- .keys(),
503
- ].join(","),
504
- chi: [],
505
- [LOCSRCID]: tokens[0][LOCSRCID],
506
- [LOCSTA]: tokens[0][LOCSTA],
507
- [LOCEND]: tokens[tokens.length - 1][LOCEND],
508
- [TOKENS]: tokens,
509
- [STATE]: result.success && allowed
510
- ? EnumAstNodeStatus.Validated
511
- : allowed
512
- ? EnumAstNodeStatus.Invalid
513
- : EnumAstNodeStatus.Disallowed,
514
- [ERRORS]: result.success ? [] : result.errors,
515
- };
371
+ const node = arena.createRule(EnumToken.RuleNodeType, renderSelector(arena, tokens), tokens, result.success && allowed
372
+ ? EnumAstNodeStatus.Validated
373
+ : allowed
374
+ ? EnumAstNodeStatus.Invalid
375
+ : EnumAstNodeStatus.Disallowed, result.success ? [] : result.errors);
376
+ // inlined: arena.getSource(tokens[0])
377
+ arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
378
+ return node;
379
+ }
380
+ // options of the rendering of the selector tokens
381
+ const selectorRenderOptions = { minify: false };
382
+ /**
383
+ * selector of a rule: the distinct comma separated selectors, the whitespace around the combinators is removed.
384
+ * a universal selector is removed before a compound selector
385
+ * @param arena
386
+ * @param tokens
387
+ */
388
+ function renderSelector(arena, tokens) {
389
+ // the distinct selectors, in order
390
+ const selectors = new Set();
391
+ // rendered tokens of the current selector
392
+ let part = [];
393
+ for (let index = 0; index < tokens.length; index++) {
394
+ const curr = tokens[index];
395
+ if (arena.dataTypes[curr] == EnumToken.WhitespaceTokenType) {
396
+ if (trimWhiteSpace.includes(arena.dataTypes[tokens[index - 1]]) ||
397
+ trimWhiteSpace.includes(arena.dataTypes[tokens[index + 1]]) ||
398
+ // inlined: arena.str(tokens[index - 1])
399
+ combinators.includes(arena.strings[arena.stringValue[tokens[index - 1]]]) ||
400
+ // inlined: arena.str(tokens[index + 1])
401
+ combinators.includes(arena.strings[arena.stringValue[tokens[index + 1]]])) {
402
+ continue;
403
+ }
404
+ }
405
+ const t = renderValue(arena, curr, selectorRenderOptions);
406
+ if (t == ",") {
407
+ addSelector(selectors, part);
408
+ part = [];
409
+ }
410
+ else {
411
+ part.push(t);
412
+ }
413
+ }
414
+ addSelector(selectors, part);
415
+ let result = "";
416
+ let first = true;
417
+ for (const selector of selectors) {
418
+ result = first ? selector : result + "," + selector;
419
+ first = false;
420
+ }
421
+ return result;
422
+ }
423
+ /**
424
+ * add a selector from its rendered tokens
425
+ * @param selectors
426
+ * @param curr
427
+ */
428
+ function addSelector(selectors, curr) {
429
+ for (let i = 0; i < curr.length; i++) {
430
+ if (i + 1 < curr.length && curr[i] == "*") {
431
+ const index = curr[i + 1] == " " ? 2 : 1;
432
+ if (![">", "~", "+"].includes(curr[index])) {
433
+ curr.splice(i, index);
434
+ }
435
+ }
436
+ }
437
+ selectors.add(curr.join(""));
516
438
  }
517
439
 
518
440
  export { parseSelector };