@tbela99/css-parser 1.4.2 → 1.4.4

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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -0,0 +1,393 @@
1
+ import { EnumToken } from '../../ast/types.js';
2
+ import { getSyntaxRule } from '../../validation/config.js';
3
+ import { trimArray } from '../../validation/match.js';
4
+ import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
5
+ import { tokensfuncDefMap, definedPropertySettings } from '../../syntax/constants.js';
6
+ import { isColor, parseColor } from '../../syntax/syntax.js';
7
+ import { parseMediaqueryList } from './at-rule-media.js';
8
+ import { parseAtRuleSupportSyntax } from './at-rule-support.js';
9
+
10
+ function matchAtRuleImportSyntax(atRule, stream, context, options) {
11
+ let success = true;
12
+ let index = 0;
13
+ let i = 0;
14
+ let matchCount;
15
+ const errors = [];
16
+ const tokens = [];
17
+ const stack = [];
18
+ const prelude = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@import")
19
+ .getPreludeRules()
20
+ .slice(1);
21
+ trimArray(stream);
22
+ // <string> | <url>
23
+ if (stream[index]?.typ === EnumToken.StringTokenType) {
24
+ tokens.push(stream[index++]);
25
+ }
26
+ else if (stream[index]?.typ === EnumToken.UrlFunctionTokenDefType) {
27
+ // match ending ')'
28
+ let matchCount = 0;
29
+ let k = 0;
30
+ let stringCount = 0;
31
+ let urlTokenCount = 0;
32
+ for (; k < stream.length; k++) {
33
+ if (stream[k]?.typ === EnumToken.EndParensTokenType) {
34
+ matchCount--;
35
+ }
36
+ else if (stream[k]?.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stream[k]?.typ)) {
37
+ matchCount++;
38
+ }
39
+ else if (stream[k]?.typ === EnumToken.StringTokenType) {
40
+ stringCount++;
41
+ }
42
+ else if (stream[k]?.typ === EnumToken.UrlFunctionTokenDefType) {
43
+ urlTokenCount++;
44
+ }
45
+ else if (!(stream[k]?.typ === EnumToken.WhitespaceTokenType || stream[k]?.typ === EnumToken.CommentTokenType)) {
46
+ return {
47
+ success: false,
48
+ errors: [
49
+ {
50
+ action: "drop",
51
+ message: "Expected string or <url-token>",
52
+ syntax: "@import",
53
+ node: stream[k],
54
+ location: stream[k]?.loc,
55
+ },
56
+ ],
57
+ };
58
+ }
59
+ if (matchCount === 0) {
60
+ break;
61
+ }
62
+ }
63
+ if (stringCount + urlTokenCount != 1) {
64
+ return {
65
+ success: false,
66
+ errors: [
67
+ {
68
+ action: "drop",
69
+ syntax: "@import",
70
+ message: "could not match syntax <url>",
71
+ node: stream[0],
72
+ location: stream[0].loc,
73
+ },
74
+ ],
75
+ };
76
+ }
77
+ const slice = stream.slice(index + 1, k);
78
+ tokens.push(Object.defineProperties(Object.assign({}, stream[0], {
79
+ typ: tokensfuncDefMap.get(stream[0].typ),
80
+ chi: trimArray(slice),
81
+ }), {
82
+ loc: {
83
+ ...definedPropertySettings,
84
+ value: {
85
+ ...stream[0].loc,
86
+ end: stream[1].loc.end,
87
+ },
88
+ },
89
+ }));
90
+ index = k + 1;
91
+ }
92
+ else {
93
+ return {
94
+ success: false,
95
+ errors: [
96
+ {
97
+ action: "drop",
98
+ message: "Expected string or url()",
99
+ syntax: "@import",
100
+ node: stream[0],
101
+ location: stream[0]?.loc,
102
+ },
103
+ ],
104
+ };
105
+ }
106
+ while (index + 1 < prelude.length &&
107
+ (stream[index]?.typ == EnumToken.WhitespaceTokenType || stream[index]?.typ == EnumToken.CommentTokenType)) {
108
+ tokens.push(stream[index++]);
109
+ }
110
+ // layer | layer(<layer-name>)
111
+ if (stream[index]?.typ === EnumToken.IdenTokenType &&
112
+ "layer".localeCompare(stream[index].val) === 0) {
113
+ tokens.push(stream[index++]);
114
+ }
115
+ else if (tokensfuncDefMap.has(stream[index]?.typ) &&
116
+ "layer".localeCompare(stream[index].val) === 0) {
117
+ stack.push(stream[index]);
118
+ tokens.push(stream[index++]);
119
+ while (stream[index]?.typ === EnumToken.WhitespaceTokenType ||
120
+ stream[index]?.typ === EnumToken.CommentTokenType) {
121
+ tokens.push(stream[index++]);
122
+ }
123
+ // <layer-name">
124
+ if (stream[index]?.typ === EnumToken.EndParensTokenType) {
125
+ i = tokens.indexOf(stack.at(-1));
126
+ tokens.splice(index, 1);
127
+ return {
128
+ success: false,
129
+ errors: [
130
+ {
131
+ action: "drop",
132
+ message: `Expected <layer-name> at ${stream[index]?.loc?.src}:${stream[index]?.loc?.sta.lin}:${stream[index]?.loc?.sta.col}`,
133
+ syntax: "@import",
134
+ node: stream[index],
135
+ location: stream[index]?.loc,
136
+ },
137
+ ],
138
+ };
139
+ }
140
+ else if (stream[index]?.typ === EnumToken.IdenTokenType) {
141
+ tokens.push(stream[index++]);
142
+ while (stream[index]?.typ == EnumToken.ClassSelectorTokenType) {
143
+ tokens.push(stream[index++]);
144
+ }
145
+ while (stream[index]?.typ == EnumToken.WhitespaceTokenType ||
146
+ stream[index]?.typ == EnumToken.CommentTokenType) {
147
+ tokens.push(stream[index++]);
148
+ }
149
+ if (stream[index]?.typ !== EnumToken.EndParensTokenType) {
150
+ return {
151
+ success: false,
152
+ errors: [
153
+ {
154
+ action: "drop",
155
+ message: "Expected ')'",
156
+ syntax: "@import",
157
+ node: stream[index],
158
+ location: stream[index]?.loc,
159
+ },
160
+ ],
161
+ };
162
+ }
163
+ i = tokens.indexOf(stack.at(-1));
164
+ tokens.splice(index, 1);
165
+ Object.assign(stack.at(-1), {
166
+ typ: EnumToken.FunctionTokenType,
167
+ chi: trimArray(tokens.splice(i + 1, index++ - i - 1)),
168
+ });
169
+ stack.pop();
170
+ }
171
+ else {
172
+ return {
173
+ success: false,
174
+ errors: [
175
+ {
176
+ action: "drop",
177
+ message: `Expected <layer-name> at ${stream[index]?.loc?.src}:${stream[index]?.loc?.sta.lin}:${stream[index]?.loc?.sta.col}`,
178
+ syntax: "@import",
179
+ node: stream[index],
180
+ location: stream[index]?.loc,
181
+ },
182
+ ],
183
+ };
184
+ }
185
+ }
186
+ while (stream[index]?.typ === EnumToken.WhitespaceTokenType || stream[index]?.typ === EnumToken.CommentTokenType) {
187
+ tokens.push(stream[index++]);
188
+ }
189
+ // supports([ <supports-condition> | <declaration> ] )
190
+ if (index < stream.length &&
191
+ tokensfuncDefMap.has(stream[index].typ) &&
192
+ "supports".localeCompare(stream[index].val) === 0) {
193
+ stack.push(stream[index]);
194
+ tokens.push(stream[index++]);
195
+ matchCount = 1;
196
+ let isDecl = false;
197
+ while (stream[index]?.typ === EnumToken.WhitespaceTokenType ||
198
+ stream[index]?.typ === EnumToken.CommentTokenType) {
199
+ tokens.push(stream[index++]);
200
+ }
201
+ // <declaration>
202
+ if (stream[index]?.typ === EnumToken.IdenTokenType &&
203
+ !["and", "or", "not", "only"].includes(stream[index].val.toLowerCase())) {
204
+ tokens.push(stream[index++]);
205
+ while (stream[index]?.typ === EnumToken.WhitespaceTokenType ||
206
+ stream[index]?.typ === EnumToken.CommentTokenType) {
207
+ tokens.push(stream[index++]);
208
+ }
209
+ if (stream[index]?.typ !== EnumToken.ColonTokenType) {
210
+ return {
211
+ success: false,
212
+ errors: [
213
+ {
214
+ action: "drop",
215
+ message: "Expected ':'",
216
+ syntax: "@import",
217
+ node: stream[index],
218
+ location: stream[index]?.loc,
219
+ },
220
+ ],
221
+ };
222
+ }
223
+ else {
224
+ isDecl = true;
225
+ tokens.push(stream[index++]);
226
+ // match the next ')'
227
+ while (index < stream.length && matchCount > 0) {
228
+ if (stream[index]?.typ === EnumToken.StartParensTokenType ||
229
+ tokensfuncDefMap.has(stream[index]?.typ)) {
230
+ matchCount++;
231
+ // stack.push(stream[index] as Token);
232
+ }
233
+ else if (stream[index]?.typ === EnumToken.EndParensTokenType) {
234
+ matchCount--;
235
+ if (matchCount === 0) {
236
+ i = tokens.indexOf(stack.at(-1));
237
+ tokens.splice(index, 1);
238
+ Object.assign(stack.at(-1), {
239
+ typ: tokensfuncDefMap.get(stack.at(-1).typ),
240
+ chi: trimArray(tokens.splice(i + 1, index++ - i - 1)),
241
+ });
242
+ stack.pop();
243
+ break;
244
+ }
245
+ }
246
+ else if (stream[index]?.typ === EnumToken.ImportantTokenType) {
247
+ tokens.push(stream[index++]);
248
+ while (stream[index]?.typ === EnumToken.WhitespaceTokenType ||
249
+ stream[index]?.typ === EnumToken.CommentTokenType) {
250
+ tokens.push(stream[index++]);
251
+ }
252
+ // next token is ')'
253
+ if (stream[index]?.typ !== EnumToken.EndParensTokenType) {
254
+ return {
255
+ success: false,
256
+ errors: [
257
+ {
258
+ action: "drop",
259
+ message: "Expected ')'",
260
+ syntax: "@import",
261
+ node: stream[index],
262
+ location: stream[index]?.loc,
263
+ },
264
+ ],
265
+ };
266
+ }
267
+ matchCount--;
268
+ i = tokens.indexOf(stack.at(-1));
269
+ tokens.splice(index, 1);
270
+ Object.assign(stack.at(-1), {
271
+ typ: tokensfuncDefMap.get(stack.at(-1).typ),
272
+ chi: trimArray(tokens.splice(i + 1, index++ - i - 1)),
273
+ });
274
+ stack.pop();
275
+ break;
276
+ }
277
+ tokens.push(stream[index++]);
278
+ }
279
+ }
280
+ }
281
+ // <supports-condition>
282
+ else {
283
+ // match the next ')'
284
+ while (index < stream.length && matchCount > 0) {
285
+ if (stream[index]?.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stream[index]?.typ)) {
286
+ matchCount++;
287
+ }
288
+ else if (stream[index]?.typ === EnumToken.EndParensTokenType) {
289
+ matchCount--;
290
+ if (matchCount === 0) {
291
+ tokens.splice(index, 1);
292
+ i = tokens.indexOf(stack.at(-1));
293
+ Object.assign(stack.at(-1), {
294
+ typ: tokensfuncDefMap.get(stack.at(-1).typ),
295
+ chi: trimArray(tokens.splice(i + 1, index++ - i - 1)),
296
+ });
297
+ stack.pop();
298
+ break;
299
+ }
300
+ }
301
+ tokens.push(stream[index++]);
302
+ }
303
+ }
304
+ if (matchCount > 0) {
305
+ return {
306
+ success: false,
307
+ errors: [
308
+ {
309
+ action: "drop",
310
+ message: "unmatched ')'",
311
+ syntax: "@import",
312
+ node: stack.at(-1),
313
+ location: stack.at(-1)?.loc,
314
+ },
315
+ ],
316
+ };
317
+ }
318
+ // support() is the last item in tokens array
319
+ if (isDecl) {
320
+ const supports = tokens.at(-1);
321
+ for (i = 0; i < supports.chi.length; i++) {
322
+ if (supports.chi[i].typ === EnumToken.IdenTokenType) {
323
+ break;
324
+ }
325
+ }
326
+ let j = i + 1;
327
+ while (j < supports.chi.length && supports.chi[j].typ !== EnumToken.ColonTokenType) {
328
+ j++;
329
+ }
330
+ const val = trimArray(supports.chi.slice(j + 1));
331
+ supports.chi[i] = Object.defineProperties({
332
+ typ: EnumToken.DeclarationNodeType,
333
+ nam: supports.chi[i].val,
334
+ val,
335
+ }, {
336
+ loc: {
337
+ ...definedPropertySettings,
338
+ value: {
339
+ ...supports.chi[i].loc,
340
+ end: { ...(val.at(-1) ?? supports.chi.at(-1)).loc?.end },
341
+ },
342
+ },
343
+ });
344
+ supports.chi.splice(i + 1, j - i + 1 + val.length);
345
+ const stack = [];
346
+ for (i = 0; i < val.length; i++) {
347
+ if (val[i].typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(val[i].typ)) {
348
+ stack.push(val[i]);
349
+ }
350
+ else if (val[i].typ === EnumToken.EndParensTokenType) {
351
+ const index = val.indexOf(stack.at(-1));
352
+ val.splice(i, 1);
353
+ Object.assign(stack.at(-1), {
354
+ typ: tokensfuncDefMap.get(stack.at(-1).typ),
355
+ chi: trimArray(val.splice(index + 1, i - index - 1)),
356
+ });
357
+ i = index;
358
+ stack.pop();
359
+ }
360
+ if (isColor(val[i])) {
361
+ val[i] = parseColor(val[i]);
362
+ }
363
+ }
364
+ }
365
+ else {
366
+ const result = parseAtRuleSupportSyntax(tokens[tokens.length - 1].chi, context, options);
367
+ if (!result.success && result.errors.length > 0) {
368
+ errors.push(...result.errors);
369
+ return {
370
+ success: false,
371
+ errors,
372
+ };
373
+ }
374
+ }
375
+ }
376
+ const splice = stream.splice(index, stream.length - index);
377
+ const sliced = parseMediaqueryList(splice, options);
378
+ tokens.push(...splice);
379
+ if (sliced.errors.length > 0) {
380
+ errors.push(...sliced.errors);
381
+ }
382
+ if (!sliced.success) {
383
+ success = false;
384
+ }
385
+ stream.length = 0;
386
+ stream.push(...trimArray(tokens));
387
+ return {
388
+ success,
389
+ errors,
390
+ };
391
+ }
392
+
393
+ export { matchAtRuleImportSyntax };