@tbela99/css-parser 2.0.1 → 2.2.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 (128) hide show
  1. package/CHANGELOG.md +86 -1
  2. package/README.md +15 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +4848 -3750
  6. package/dist/index-umd-web.js +18234 -9498
  7. package/dist/index.d.ts +1251 -373
  8. package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
  9. package/dist/{src/lib → lib}/ast/arena.js +1034 -655
  10. package/dist/{src/lib → lib}/ast/cache.js +36 -31
  11. package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
  12. package/dist/{src/lib → lib}/ast/features/if.js +24 -19
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
  21. package/dist/{src/lib → lib}/ast/math/math.js +8 -8
  22. package/dist/{src/lib → lib}/ast/minify.js +932 -354
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +7 -15
  37. package/dist/{src/lib → lib}/ast/walk.js +19 -19
  38. package/dist/{src/lib → lib}/fs/resolve.js +15 -15
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
  42. package/dist/lib/parser/input.js +85 -0
  43. package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
  44. package/dist/{src/lib → lib}/parser/parse.js +852 -619
  45. package/dist/lib/parser/source.js +71 -0
  46. package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
  64. package/dist/lib/parser/utils/selector.js +720 -0
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
  68. package/dist/{src/lib → lib}/printer/render.js +536 -243
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
  77. package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
  99. package/dist/{src/lib → lib}/syntax/constants.js +30 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
  101. package/dist/lib/validation/config.js +173 -0
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +1256 -579
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +92 -80
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +13 -8
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +80 -75
  113. package/package.json +13 -13
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -87
  116. package/dist/src/lib/ast/features/transform.js +0 -85
  117. package/dist/src/lib/ast/transform/compute.js +0 -358
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/source.js +0 -127
  126. package/dist/src/lib/parser/utils/cache.js +0 -54
  127. package/dist/src/lib/parser/utils/selector.js +0 -440
  128. package/dist/src/lib/validation/config.js +0 -120
@@ -0,0 +1,720 @@
1
+ /*
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.2.0
6
+ License: MIT
7
+ */
8
+ import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
9
+ import { StringBuilder } from '../../ast/strings.js';
10
+ import { renderValue } from '../../printer/render.js';
11
+ import { isFunctionDefType, tokensfuncDefMap, pseudoElements, combinators } from '../../syntax/constants.js';
12
+ import { isHash, isIdent } from '../../syntax/syntax.js';
13
+ import { getParsedSyntax, getSyntaxRule, getSyntaxConfig } from '../../validation/config.js';
14
+ import { matchAllSyntaxes, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
15
+ import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
16
+ import { splitTokenList } from '../../validation/utils/list.js';
17
+ import { trimWhiteSpace } from '../parse.js';
18
+ import { equalsIgnoreCase } from './text.js';
19
+
20
+ /**
21
+ * minify the an+b expression of a pseudo class function
22
+ * @param arena
23
+ * @param func
24
+ */
25
+ function minifyNthExpression(arena, func) {
26
+ const chi = arena.tokens(func);
27
+ const list = [];
28
+ let index;
29
+ for (index = 0; index < chi.length; index++) {
30
+ if (arena.dataTypes[chi[index]] === EnumToken.CommentTokenType ||
31
+ arena.dataTypes[chi[index]] === EnumToken.WhitespaceTokenType) {
32
+ continue;
33
+ }
34
+ if (arena.dataTypes[chi[index]] === EnumToken.IdenTokenType &&
35
+ equalsIgnoreCase("of", arena.resolveString(arena.stringValue[chi[index]]))) {
36
+ index--;
37
+ break;
38
+ }
39
+ list.push(chi[index]);
40
+ }
41
+ if (list.length === 2) {
42
+ if (arena.dataTypes[list[1]] === EnumToken.NumberTokenType) {
43
+ if (arena.numericValue[list[1]] === 0) {
44
+ list.length = 1;
45
+ if (arena.dataTypes[list[0]] === EnumToken.DimensionTokenType && arena.numericValue[list[0]] === -2) {
46
+ arena.numericValue[list[0]] = 2;
47
+ }
48
+ }
49
+ else {
50
+ const sign = Math.sign(arena.numericValue[list[1]]);
51
+ arena.numericValue[list[1]] *= sign;
52
+ list.splice(1, 0, arena.createToken(EnumToken.LiteralTokenType, sign > 0 ? "+" : "-"));
53
+ }
54
+ }
55
+ if (list.length === 3 &&
56
+ arena.dataTypes[list[2]] === EnumToken.NumberTokenType &&
57
+ arena.dataTypes[list[0]] === EnumToken.DimensionTokenType &&
58
+ (arena.numericValue[list[0]] === 2 || arena.numericValue[list[0]] === -2)) {
59
+ if (1 === arena.numericValue[list[2]]) {
60
+ const ident = arena.createToken(EnumToken.IdenTokenType, "odd");
61
+ arena.copyLocation(ident, list[0]);
62
+ list.splice(0, 3, ident);
63
+ }
64
+ else if (0 === arena.numericValue[list[2]]) {
65
+ const ident = arena.createToken(EnumToken.IdenTokenType, "even");
66
+ arena.copyLocation(ident, list[0]);
67
+ list.splice(0, 3, ident);
68
+ }
69
+ }
70
+ chi.splice(0, index, ...list);
71
+ }
72
+ if (list.length === 1) {
73
+ if (arena.dataTypes[list[0]] === EnumToken.IdenTokenType &&
74
+ equalsIgnoreCase("-n", arena.resolveString(arena.stringValue[list[0]]))) {
75
+ // inlined: arena.setStr(list[0], "n")
76
+ arena.stringValue[list[0]] = arena.internString("n");
77
+ }
78
+ }
79
+ if (list.length === 3) {
80
+ if (arena.dataTypes[list[0]] === EnumToken.IdenTokenType &&
81
+ ("n" === arena.resolveString(arena.stringValue[list[0]]) ||
82
+ "-n" === arena.resolveString(arena.stringValue[list[0]]) ||
83
+ "+n" === arena.resolveString(arena.stringValue[list[0]]))) {
84
+ if (arena.dataTypes[list[1]] === EnumToken.NextSiblingCombinatorTokenType) {
85
+ if (arena.dataTypes[list[2]] === EnumToken.NumberTokenType && 0 === arena.numericValue[list[2]]) {
86
+ // inlined: arena.setStr(list[0], "n")
87
+ arena.stringValue[list[0]] = arena.internString("n");
88
+ chi.splice(0, index, list[0]);
89
+ arena.setTokenList(func, chi);
90
+ return;
91
+ }
92
+ }
93
+ }
94
+ }
95
+ const token = chi.find((t) => arena.dataTypes[t] !== EnumToken.WhitespaceTokenType && arena.dataTypes[t] !== EnumToken.CommentTokenType);
96
+ if (token != null) {
97
+ if (arena.dataTypes[token] === EnumToken.IdenTokenType ||
98
+ arena.dataTypes[token] === EnumToken.LiteralTokenType) {
99
+ const val = arena.resolveString(arena.stringValue[token]);
100
+ if (arena.dataTypes[token] === EnumToken.IdenTokenType && (val === "odd" || val === "even")) {
101
+ if (val === "even") {
102
+ arena.resetToken(token, EnumToken.DimensionTokenType, 2, "n");
103
+ }
104
+ }
105
+ else {
106
+ const matches = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(val);
107
+ if (matches != null) {
108
+ const a1 = matches[2] === "" ? 1 : matches[2] === "-" ? -1 : +matches[2];
109
+ const b1 = +matches[3];
110
+ if (b1 === 0) {
111
+ if (Math.abs(a1) === 1) {
112
+ arena.resetToken(token, EnumToken.IdenTokenType, "n");
113
+ }
114
+ else {
115
+ arena.resetToken(token, EnumToken.DimensionTokenType, a1 * Math.sign(a1), "n");
116
+ }
117
+ }
118
+ }
119
+ }
120
+ }
121
+ else if (arena.dataTypes[token] === EnumToken.DimensionTokenType) {
122
+ const index = chi.indexOf(token);
123
+ let num = null;
124
+ let i = index + 1;
125
+ for (; i < chi.length; i++) {
126
+ if (arena.dataTypes[chi[i]] === EnumToken.WhitespaceTokenType ||
127
+ arena.dataTypes[chi[i]] === EnumToken.CommentTokenType) {
128
+ continue;
129
+ }
130
+ if (arena.dataTypes[chi[i]] === EnumToken.NumberTokenType) {
131
+ num = chi[i];
132
+ break;
133
+ }
134
+ }
135
+ if (num != null) {
136
+ if (arena.numericValue[num] === 0) {
137
+ chi.splice(index + 1, i - index);
138
+ if (arena.numericValue[token] < 0) {
139
+ arena.numericValue[token] *= -1;
140
+ }
141
+ }
142
+ else if (arena.numericValue[token] === 2 && Math.abs(arena.numericValue[num]) === 1) {
143
+ arena.resetToken(token, EnumToken.IdenTokenType, "odd");
144
+ chi.splice(index + 1, i - index);
145
+ }
146
+ }
147
+ }
148
+ }
149
+ arena.setTokenList(func, chi);
150
+ }
151
+ /**
152
+ * split the unicode ranges of a selector: `u+a` is the element `u` followed by the element `a`, not a unicode range
153
+ * @param arena
154
+ * @param tokens
155
+ */
156
+ function splitUnicodeRanges(arena, tokens) {
157
+ for (let i = 0; i < tokens.length; i++) {
158
+ const token = tokens[i];
159
+ if (arena.dataTypes[token] === EnumToken.UniCodeRange) {
160
+ const text = arena.resolveString(arena.stringValue[token]);
161
+ const rest = text.slice(2);
162
+ const split = [
163
+ arena.createToken(EnumToken.IdenTokenType, text.charAt(0)),
164
+ arena.createToken(EnumToken.Plus, "+"),
165
+ arena.createToken(isIdent(rest) ? EnumToken.IdenTokenType : EnumToken.LiteralTokenType, rest),
166
+ ];
167
+ for (const t of split) {
168
+ arena.copyLocation(t, token);
169
+ }
170
+ tokens.splice(i, 1, ...split);
171
+ i += split.length - 1;
172
+ }
173
+ else if (arena.firstToken[token] !== 0) {
174
+ // `:is(u+a)`
175
+ const children = arena.tokens(token);
176
+ if (children.some((child) => arena.dataTypes[child] === EnumToken.UniCodeRange || arena.firstToken[child] !== 0)) {
177
+ const length = children.length;
178
+ splitUnicodeRanges(arena, children);
179
+ if (children.length !== length) {
180
+ arena.setTokenList(token, children);
181
+ }
182
+ }
183
+ }
184
+ }
185
+ }
186
+ /**
187
+ * parse selector
188
+ * @param arena
189
+ * @param tokens
190
+ * @param context parent node id
191
+ * @param options
192
+ * @param errors
193
+ */
194
+ function parseSelector(arena, tokens, context, options, errors) {
195
+ const contextType = context == null ? null : arena.dataTypes[context];
196
+ splitUnicodeRanges(arena, tokens);
197
+ if (contextType === EnumToken.KeyframesAtRuleNodeType) {
198
+ const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(arena, tokens), options);
199
+ const parts = splitTokenList(arena, tokens);
200
+ for (const part of parts) {
201
+ trimArray(arena, part);
202
+ if (options.minify) {
203
+ const filtered = part.filter((token) => arena.dataTypes[token] !== EnumToken.WhitespaceTokenType &&
204
+ arena.dataTypes[token] !== EnumToken.CommentTokenType);
205
+ if (filtered.length === 1) {
206
+ if (arena.dataTypes[filtered[0]] === EnumToken.IdenTokenType &&
207
+ "from" === arena.resolveString(arena.stringValue[filtered[0]]).toLowerCase()) {
208
+ arena.resetToken(filtered[0], EnumToken.PercentageTokenType, 0);
209
+ }
210
+ else if (arena.dataTypes[filtered[0]] === EnumToken.PercentageTokenType &&
211
+ 100 === arena.numericValue[filtered[0]]) {
212
+ arena.resetToken(filtered[0], EnumToken.IdenTokenType, "to");
213
+ }
214
+ part.splice(0, part.length, ...filtered);
215
+ }
216
+ }
217
+ }
218
+ tokens.splice(0, tokens.length, ...parts.reduce((acc, curr) => {
219
+ if (acc.length > 0) {
220
+ acc.push(arena.createToken(EnumToken.CommaTokenType));
221
+ }
222
+ for (const c of curr) {
223
+ acc.push(c);
224
+ }
225
+ return acc;
226
+ }, []));
227
+ const node = arena.createRule(EnumToken.KeyframesRuleNodeType, [
228
+ ...splitTokenList(arena, trimArray(arena, tokens)).reduce((acc, curr) => {
229
+ acc.add(curr.reduce((acc, curr) => acc + renderValue(arena, curr, { minify: false }), ""));
230
+ return acc;
231
+ }, new Set()),
232
+ ].join(), tokens.length === 0 ? null : tokens, result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid, result.errors);
233
+ // inlined: arena.getSource(tokens[0])
234
+ arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
235
+ return node;
236
+ }
237
+ const stack = [];
238
+ let allowed = true;
239
+ let index;
240
+ let parent = context;
241
+ let nested = false;
242
+ let val;
243
+ if (contextType !== EnumToken.StyleSheetNodeType && contextType !== EnumToken.RuleNodeType) {
244
+ allowed = false;
245
+ if (contextType === EnumToken.AtRuleNodeType) {
246
+ const syntaxRule = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.resolveString(arena.name[context]));
247
+ allowed = syntaxRule?.acceptAnyRule ?? false;
248
+ if (!allowed) {
249
+ const rules = syntaxRule?.getBlockRules?.();
250
+ if (rules != null) {
251
+ for (const rule of rules) {
252
+ if (ValidationTokenEnum.PropertyType === rule.typ) {
253
+ if ("block-contents" === rule.val ||
254
+ "rule-list" === rule.val) {
255
+ allowed = true;
256
+ break;
257
+ }
258
+ }
259
+ }
260
+ }
261
+ }
262
+ }
263
+ }
264
+ while (parent != null && parent > 0) {
265
+ if (arena.dataTypes[parent] === EnumToken.AtRuleNodeType && "media" === arena.resolveString(arena.name[parent])) {
266
+ parent = arena.parents[parent];
267
+ continue;
268
+ }
269
+ // the rules of a mixin body are nested in the rule the mixin is applied to:
270
+ // '&' represents the rule that contains the '@apply' rule
271
+ nested =
272
+ arena.dataTypes[parent] === EnumToken.RuleNodeType ||
273
+ (arena.dataTypes[parent] === EnumToken.AtRuleNodeType &&
274
+ "mixin" === arena.resolveString(arena.name[parent]));
275
+ if (nested) {
276
+ break;
277
+ }
278
+ parent = arena.parents[parent];
279
+ }
280
+ const all = new Array(tokens.length);
281
+ let write = 0;
282
+ for (let read = 0; read < tokens.length; read++) {
283
+ let token = tokens[read];
284
+ if (arena.dataTypes[token] === EnumToken.ColonTokenType) {
285
+ const next = tokens[read + 1];
286
+ if (arena.dataTypes[next] === EnumToken.IdenTokenType) {
287
+ arena.dataTypes[token] = EnumToken.PseudoElementTokenType;
288
+ // ":" + name, no string is created
289
+ arena.stringValue[token] = arena.internBuilder(arena.appendString(pseudoBuilder.reset().appendCode(0x3a), arena.stringValue[next]));
290
+ arena.spanLocation(token, token, next);
291
+ all[write++] = token;
292
+ read++;
293
+ continue;
294
+ }
295
+ if (arena.dataTypes[next] === EnumToken.FunctionTokenDefType) {
296
+ val = ":" + arena.resolveString(arena.stringValue[next]);
297
+ arena.dataTypes[token] =
298
+ val + "()" in getSyntaxConfig().selectors
299
+ ? EnumToken.PseudoClassFunctionTokenDefType
300
+ : arena.dataTypes[next];
301
+ // inlined: arena.setStr(token, val)
302
+ arena.stringValue[token] = arena.internString(val);
303
+ arena.spanLocation(token, token, next);
304
+ all[write++] = token;
305
+ read++;
306
+ continue;
307
+ }
308
+ }
309
+ if (arena.dataTypes[token] === EnumToken.DoubleColonTokenType) {
310
+ const next = tokens[read + 1];
311
+ if (arena.dataTypes[next] === EnumToken.IdenTokenType) {
312
+ arena.dataTypes[token] = EnumToken.PseudoClassTokenType;
313
+ // ":" + name for the pseudo elements with a single colon syntax, "::" + name otherwise. no string is
314
+ // created
315
+ pseudoBuilder.reset().appendCode(0x3a);
316
+ if (!isSingleColonPseudoElement(arena, arena.stringValue[next])) {
317
+ pseudoBuilder.appendCode(0x3a);
318
+ }
319
+ arena.stringValue[token] = arena.internBuilder(arena.appendString(pseudoBuilder, arena.stringValue[next]));
320
+ arena.spanLocation(token, token, next);
321
+ all[write++] = token;
322
+ read++;
323
+ continue;
324
+ }
325
+ if (arena.dataTypes[next] === EnumToken.FunctionTokenDefType) {
326
+ val = "::" + arena.resolveString(arena.stringValue[next]);
327
+ arena.dataTypes[token] =
328
+ val + "()" in getSyntaxConfig().selectors
329
+ ? EnumToken.PseudoClassFunctionTokenDefType
330
+ : EnumToken.FunctionTokenDefType;
331
+ // inlined: arena.setStr(token, val)
332
+ arena.stringValue[token] = arena.internString(val);
333
+ arena.spanLocation(token, token, next);
334
+ all[write++] = token;
335
+ read++;
336
+ continue;
337
+ }
338
+ }
339
+ if (arena.dataTypes[token] === EnumToken.ColorTokenType) {
340
+ if (isHash(arena.resolveString(arena.stringValue[token]))) {
341
+ arena.dataTypes[token] = EnumToken.HashTokenType;
342
+ all[write++] = token;
343
+ continue;
344
+ }
345
+ const node = arena.createRule(EnumToken.RuleNodeType, renderSelector(arena, tokens), tokens, EnumAstNodeStatus.Invalid, [
346
+ {
347
+ action: "drop",
348
+ node: token,
349
+ message: "invalid hash id",
350
+ },
351
+ ]);
352
+ // inlined: arena.getSource(tokens[0])
353
+ arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
354
+ return node;
355
+ }
356
+ all[write++] = token;
357
+ }
358
+ if (write !== tokens.length) {
359
+ for (let index = 0; index < write; index++) {
360
+ tokens[index] = all[index];
361
+ }
362
+ tokens.length = write;
363
+ }
364
+ const result = matchSelectorSyntax(arena, tokens, errors, options, nested);
365
+ trimArray(arena, tokens);
366
+ if (result.success) {
367
+ for (let i = 0; i < tokens.length; i++) {
368
+ const token = tokens[i];
369
+ switch (arena.dataTypes[token]) {
370
+ case EnumToken.AttrStartTokenType:
371
+ stack.push(token);
372
+ break;
373
+ case EnumToken.AttrEndTokenType:
374
+ // inlined: arena.typeOf(stack.at(-1))
375
+ if (arena.dataTypes[stack.at(-1)] === EnumToken.AttrStartTokenType) {
376
+ const attr = stack[stack.length - 1];
377
+ index = tokens.indexOf(attr);
378
+ arena.spanLocation(attr, attr, token);
379
+ tokens.splice(i, 1);
380
+ arena.dataTypes[attr] = EnumToken.AttrTokenType;
381
+ arena.stringValue[attr] = 0;
382
+ arena.setTokenList(attr, tokens.splice(index + 1, i - index - 1));
383
+ i = index;
384
+ stack.pop();
385
+ }
386
+ break;
387
+ case EnumToken.PseudoClassFunctionTokenDefType:
388
+ stack.push(token);
389
+ break;
390
+ case EnumToken.EndParensTokenType:
391
+ // inlined: arena.typeOf(stack.at(-1))
392
+ if (arena.dataTypes[stack.at(-1)] === EnumToken.PseudoClassFunctionTokenDefType) {
393
+ const func = stack[stack.length - 1];
394
+ index = tokens.indexOf(func);
395
+ arena.spanLocation(func, func, token);
396
+ tokens.splice(i, 1);
397
+ if (isFunctionDefType(arena.dataTypes[func])) {
398
+ arena.dataTypes[func] = tokensfuncDefMap.get(arena.dataTypes[func]);
399
+ arena.setTokenList(func, trimArray(arena, tokens.splice(index + 1, i - index - 1)));
400
+ }
401
+ if (result.success && options.minify) {
402
+ // parse an+b
403
+ // 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]
404
+ const name = arena.resolveString(arena.stringValue[func]);
405
+ if (name === ":nth-child" ||
406
+ name === ":nth-last-child" ||
407
+ name === ":nth-of-type" ||
408
+ name === ":nth-last-of-type") {
409
+ minifyNthExpression(arena, func);
410
+ }
411
+ }
412
+ i = index;
413
+ stack.pop();
414
+ }
415
+ break;
416
+ }
417
+ }
418
+ }
419
+ const node = arena.createRule(EnumToken.RuleNodeType, renderSelector(arena, tokens), tokens, result.success && allowed
420
+ ? EnumAstNodeStatus.Validated
421
+ : allowed
422
+ ? EnumAstNodeStatus.Invalid
423
+ : EnumAstNodeStatus.Disallowed, result.success ? [] : result.errors);
424
+ // inlined: arena.getSource(tokens[0])
425
+ arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
426
+ return node;
427
+ }
428
+ // options of the rendering of the selector tokens
429
+ const selectorRenderOptions = { minify: false };
430
+ /**
431
+ * builder of the pseudo classes and pseudo elements
432
+ */
433
+ const pseudoBuilder = new StringBuilder();
434
+ /**
435
+ * true if ":" + the string is a pseudo element with a single colon syntax (`:before`). the string is not created
436
+ * @param arena
437
+ * @param id
438
+ */
439
+ function isSingleColonPseudoElement(arena, id) {
440
+ const length = arena.stringLength(id);
441
+ for (let i = 0; i < pseudoElements.length; i++) {
442
+ const pseudo = pseudoElements[i];
443
+ if (pseudo.length === length + 1 && pseudo.charCodeAt(0) === 0x3a) {
444
+ let k = 0;
445
+ while (k < length && arena.stringCodeAt(id, k) === pseudo.charCodeAt(k + 1)) {
446
+ k++;
447
+ }
448
+ if (k === length) {
449
+ return true;
450
+ }
451
+ }
452
+ }
453
+ return false;
454
+ }
455
+ /**
456
+ * builder of the rendered tokens of the current selector
457
+ */
458
+ const partBuilder = new StringBuilder();
459
+ /**
460
+ * builder of the selector of a rule
461
+ */
462
+ const selectorBuilder = new StringBuilder();
463
+ /**
464
+ * offsets of the rendered tokens of the current selector in {@link partBuilder}: start and end of each token
465
+ */
466
+ let partOffsets = new Int32Array(64);
467
+ /**
468
+ * offsets of the distinct selectors in {@link selectorBuilder}: start and end of each selector
469
+ */
470
+ let selectorOffsets = new Int32Array(16);
471
+ /**
472
+ * true if the string of a token is a combinator. the string is not created
473
+ * @param arena
474
+ * @param token
475
+ */
476
+ function isCombinatorString(arena, token) {
477
+ if (token === undefined) {
478
+ return false;
479
+ }
480
+ const id = arena.stringValue[token];
481
+ const length = arena.stringLength(id);
482
+ if (length === 0 || length > 2) {
483
+ return false;
484
+ }
485
+ for (let i = 0; i < combinators.length; i++) {
486
+ if (arena.stringEquals(id, combinators[i])) {
487
+ return true;
488
+ }
489
+ }
490
+ return false;
491
+ }
492
+ /**
493
+ * append the rendering of a selector token to the builder. the strings of the tokens are copied, no string is created:
494
+ * the tokens that are rendered as their string value, the symbols. the other tokens are rendered with renderValue()
495
+ * @param arena
496
+ * @param token
497
+ * @param builder
498
+ */
499
+ function appendSelectorToken(arena, token, builder) {
500
+ if (arena.firstToken[token] === 0 && arena.firstChild[token] === 0) {
501
+ switch (arena.dataTypes[token]) {
502
+ case EnumToken.HashTokenType:
503
+ case EnumToken.IdenTokenType:
504
+ case EnumToken.LiteralTokenType:
505
+ case EnumToken.DashedIdenTokenType:
506
+ case EnumToken.ClassSelectorTokenType:
507
+ case EnumToken.PseudoClassTokenType:
508
+ arena.appendString(builder, arena.stringValue[token]);
509
+ return;
510
+ case EnumToken.WhitespaceTokenType:
511
+ case EnumToken.DescendantCombinatorTokenType:
512
+ builder.appendCode(0x20);
513
+ return;
514
+ case EnumToken.CommaTokenType:
515
+ builder.appendCode(0x2c);
516
+ return;
517
+ case EnumToken.Star:
518
+ case EnumToken.UniversalSelectorTokenType:
519
+ builder.appendCode(0x2a);
520
+ return;
521
+ case EnumToken.NestingSelectorTokenType:
522
+ builder.appendCode(0x26);
523
+ return;
524
+ case EnumToken.GtTokenType:
525
+ case EnumToken.ChildCombinatorTokenType:
526
+ builder.appendCode(0x3e);
527
+ return;
528
+ case EnumToken.Plus:
529
+ case EnumToken.NextSiblingCombinatorTokenType:
530
+ builder.appendCode(0x2b);
531
+ return;
532
+ case EnumToken.Tilda:
533
+ case EnumToken.SubsequentSiblingCombinatorTokenType:
534
+ builder.appendCode(0x7e);
535
+ return;
536
+ }
537
+ }
538
+ builder.appendString(renderValue(arena, token, selectorRenderOptions));
539
+ }
540
+ /**
541
+ * true if two ranges of code units are equal
542
+ * @param codes
543
+ * @param start1
544
+ * @param end1
545
+ * @param start2
546
+ * @param end2
547
+ */
548
+ function equalCodes(codes, start1, end1, start2, end2) {
549
+ if (end1 - start1 !== end2 - start2) {
550
+ return false;
551
+ }
552
+ for (let i = 0; i < end1 - start1; i++) {
553
+ if (codes[start1 + i] !== codes[start2 + i]) {
554
+ return false;
555
+ }
556
+ }
557
+ return true;
558
+ }
559
+ /**
560
+ * true if a rendered token of the current selector is a single code unit
561
+ * @param index token index
562
+ * @param count number of tokens
563
+ * @param code
564
+ */
565
+ function isPartCode(index, count, code) {
566
+ if (index < 0 || index >= count) {
567
+ return false;
568
+ }
569
+ const start = partOffsets[index * 2];
570
+ return partOffsets[index * 2 + 1] - start === 1 && partBuilder.codes[start] === code;
571
+ }
572
+ /**
573
+ * true if the rendered token is a simple selector that is not a type selector: a class (`.a`), an id (`#a`), an
574
+ * attribute (`[a]`), a pseudo-class (`:hover`) or a pseudo-element (`::before`)
575
+ * @param index index of the rendered token
576
+ * @param count number of rendered tokens
577
+ */
578
+ function isSubclassPart(index, count) {
579
+ if (index < 0 || index >= count || partOffsets[index * 2 + 1] === partOffsets[index * 2]) {
580
+ return false;
581
+ }
582
+ const code = partBuilder.codes[partOffsets[index * 2]];
583
+ // '.', '#', ':', '['
584
+ return code === 0x2e || code === 0x23 || code === 0x3a || code === 0x5b;
585
+ }
586
+ /**
587
+ * true if the rendered token is a type selector other than `html`: `span`, `svg|circle`
588
+ * @param index index of the rendered token
589
+ * @param count number of rendered tokens
590
+ */
591
+ function isTypePart(index, count) {
592
+ if (index >= count) {
593
+ return false;
594
+ }
595
+ const start = partOffsets[index * 2];
596
+ const end = partOffsets[index * 2 + 1];
597
+ const code = partBuilder.codes[start];
598
+ // a namespace prefix: `ns|span`
599
+ if (isPartCode(index + 1, count, 0x7c)) {
600
+ return false;
601
+ }
602
+ if (!((code >= 0x61 && code <= 0x7a) || (code >= 0x41 && code <= 0x5a) || code === 0x5f || code >= 0x80)) {
603
+ return false;
604
+ }
605
+ // `html`, case-insensitive
606
+ return !(end - start === 4 &&
607
+ (partBuilder.codes[start] | 0x20) === 0x68 &&
608
+ (partBuilder.codes[start + 1] | 0x20) === 0x74 &&
609
+ (partBuilder.codes[start + 2] | 0x20) === 0x6d &&
610
+ (partBuilder.codes[start + 3] | 0x20) === 0x6c);
611
+ }
612
+ /**
613
+ * add the current selector (the rendered tokens in {@link partBuilder}) to the selectors of the rule when it is not
614
+ * already present. a universal selector is removed before a compound selector
615
+ * @param count number of rendered tokens
616
+ * @param selectors number of selectors
617
+ * @returns the number of selectors
618
+ */
619
+ function addSelectorCodes(count, selectors) {
620
+ // `*` is removed when it is followed by another simple selector of the same compound selector:
621
+ // `*.a` -> `.a`, `*:hover` -> `:hover`, `*[a]` -> `[a]`, `*::before` -> `::before`.
622
+ // it is kept when it is the only simple selector of the compound selector: it is followed by a combinator
623
+ // (`.a * .b`, `.a *+.b`) or it ends the selector. it is kept in a namespace selector: `*|*`, `ns|*.a`
624
+ for (let i = 0; i + 1 < count; i++) {
625
+ if (isPartCode(i, count, 0x2a) && !isPartCode(i - 1, count, 0x7c) && isSubclassPart(i + 1, count)) {
626
+ partOffsets.copyWithin(i * 2, (i + 1) * 2, count * 2);
627
+ count--;
628
+ }
629
+ }
630
+ // a leading `* ` is removed before a type selector: `* span` -> `span`. every element but the root element has a
631
+ // parent element, the root element is `html`. it is kept before the other selectors: `* .dark` does not match
632
+ // `html.dark`
633
+ if (count > 2 && isPartCode(0, count, 0x2a) && isPartCode(1, count, 0x20) && isTypePart(2, count)) {
634
+ partOffsets.copyWithin(0, 4, count * 2);
635
+ count -= 2;
636
+ }
637
+ // the selector: the rendered tokens, at the end of the selector builder
638
+ const start = selectorBuilder.length + (selectors > 0 ? 1 : 0);
639
+ if (selectors > 0) {
640
+ selectorBuilder.appendCode(0x2c);
641
+ }
642
+ for (let i = 0; i < count; i++) {
643
+ const tokenStart = partOffsets[i * 2];
644
+ const tokenEnd = partOffsets[i * 2 + 1];
645
+ for (let k = tokenStart; k < tokenEnd; k++) {
646
+ selectorBuilder.appendCode(partBuilder.codes[k]);
647
+ }
648
+ }
649
+ const end = selectorBuilder.length;
650
+ // distinct selectors
651
+ for (let k = 0; k < selectors; k++) {
652
+ if (equalCodes(selectorBuilder.codes, selectorOffsets[k * 2], selectorOffsets[k * 2 + 1], start, end)) {
653
+ selectorBuilder.length = selectors > 0 ? start - 1 : start;
654
+ return selectors;
655
+ }
656
+ }
657
+ if ((selectors + 1) * 2 > selectorOffsets.length) {
658
+ const offsets = new Int32Array(selectorOffsets.length * 2);
659
+ offsets.set(selectorOffsets);
660
+ selectorOffsets = offsets;
661
+ }
662
+ selectorOffsets[selectors * 2] = start;
663
+ selectorOffsets[selectors * 2 + 1] = end;
664
+ return selectors + 1;
665
+ }
666
+ /**
667
+ * selector of a rule: the distinct comma separated selectors, the whitespace around the combinators is removed.
668
+ * a universal selector is removed before a compound selector. the selector is built from the code units of the
669
+ * tokens and interned: no string is created
670
+ * @param arena
671
+ * @param tokens
672
+ * @returns the interned string id of the selector
673
+ */
674
+ function renderSelector(arena, tokens) {
675
+ // number of rendered tokens of the current selector, number of distinct selectors
676
+ let count = 0;
677
+ let selectors = 0;
678
+ partBuilder.reset();
679
+ selectorBuilder.reset();
680
+ for (let index = 0; index < tokens.length; index++) {
681
+ const curr = tokens[index];
682
+ const type = arena.dataTypes[curr];
683
+ if (type === EnumToken.WhitespaceTokenType) {
684
+ if (trimWhiteSpace.includes(arena.dataTypes[tokens[index - 1]]) ||
685
+ trimWhiteSpace.includes(arena.dataTypes[tokens[index + 1]]) ||
686
+ isCombinatorString(arena, tokens[index - 1]) ||
687
+ isCombinatorString(arena, tokens[index + 1])) {
688
+ continue;
689
+ }
690
+ }
691
+ if (type === EnumToken.CommaTokenType) {
692
+ selectors = addSelectorCodes(count, selectors);
693
+ count = 0;
694
+ partBuilder.reset();
695
+ continue;
696
+ }
697
+ const start = partBuilder.length;
698
+ appendSelectorToken(arena, curr, partBuilder);
699
+ // a token rendered as `,`
700
+ if (partBuilder.length - start === 1 && partBuilder.codes[start] === 0x2c) {
701
+ partBuilder.length = start;
702
+ selectors = addSelectorCodes(count, selectors);
703
+ count = 0;
704
+ partBuilder.reset();
705
+ continue;
706
+ }
707
+ if ((count + 1) * 2 > partOffsets.length) {
708
+ const offsets = new Int32Array(partOffsets.length * 2);
709
+ offsets.set(partOffsets);
710
+ partOffsets = offsets;
711
+ }
712
+ partOffsets[count * 2] = start;
713
+ partOffsets[count * 2 + 1] = partBuilder.length;
714
+ count++;
715
+ }
716
+ addSelectorCodes(count, selectors);
717
+ return arena.internBuilder(selectorBuilder);
718
+ }
719
+
720
+ export { parseSelector };