@tbela99/css-parser 1.4.8 → 1.4.9

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 (53) hide show
  1. package/CHANGELOG.md +28 -3
  2. package/dist/index-umd-web.js +12897 -13217
  3. package/dist/index.cjs +10394 -10716
  4. package/dist/index.d.ts +816 -89
  5. package/dist/lib/ast/features/calc.js +3 -1
  6. package/dist/lib/ast/features/if.js +2 -96
  7. package/dist/lib/ast/features/prefix.js +7 -2
  8. package/dist/lib/ast/features/shorthand.js +1 -1
  9. package/dist/lib/ast/features/transform.js +1 -1
  10. package/dist/lib/ast/find.js +6 -6
  11. package/dist/lib/ast/math/expression.js +30 -12
  12. package/dist/lib/ast/minify.js +34 -5
  13. package/dist/lib/ast/transform/perspective.js +1 -1
  14. package/dist/lib/ast/transform/utils.js +29 -21
  15. package/dist/lib/ast/types.js +8 -0
  16. package/dist/lib/fs/resolve.js +1 -16
  17. package/dist/lib/parser/declaration/list.js +6 -1
  18. package/dist/lib/parser/{linemap.js → linesmap.js} +25 -9
  19. package/dist/lib/parser/parse.js +1264 -151
  20. package/dist/lib/parser/source.js +48 -17
  21. package/dist/lib/parser/tokenize.js +112 -133
  22. package/dist/lib/parser/utils/at-rule-container.js +12 -44
  23. package/dist/lib/parser/utils/at-rule-generic.js +12 -24
  24. package/dist/lib/parser/utils/at-rule-import.js +4 -172
  25. package/dist/lib/parser/utils/at-rule-media.js +14 -251
  26. package/dist/lib/parser/utils/at-rule-support.js +8 -258
  27. package/dist/lib/parser/utils/at-rule-when-else.js +4 -214
  28. package/dist/lib/parser/utils/declaration.js +5 -95
  29. package/dist/lib/parser/utils/hash.js +54 -1
  30. package/dist/lib/parser/utils/selector.js +1 -1
  31. package/dist/lib/parser/utils/token.js +1 -0
  32. package/dist/lib/renderer/render.js +76 -43
  33. package/dist/lib/renderer/sourcemap/sourcemap.js +47 -43
  34. package/dist/lib/syntax/color/cmyk.js +3 -3
  35. package/dist/lib/syntax/color/color-mix.js +7 -7
  36. package/dist/lib/syntax/color/color.js +7 -7
  37. package/dist/lib/syntax/color/hsl.js +6 -6
  38. package/dist/lib/syntax/color/hwb.js +6 -6
  39. package/dist/lib/syntax/color/lab.js +5 -5
  40. package/dist/lib/syntax/color/lch.js +6 -6
  41. package/dist/lib/syntax/color/oklab.js +5 -5
  42. package/dist/lib/syntax/color/oklch.js +5 -5
  43. package/dist/lib/syntax/color/relative-color.js +8 -1
  44. package/dist/lib/syntax/color/utils/distance.js +2 -2
  45. package/dist/lib/syntax/constants.js +2 -1
  46. package/dist/lib/syntax/syntax.js +11 -8
  47. package/dist/lib/validation/config.json.js +27 -6
  48. package/dist/lib/validation/match.js +42 -845
  49. package/dist/node.js +134 -9
  50. package/dist/web.js +133 -6
  51. package/llms.txt +73 -0
  52. package/package.json +13 -16
  53. package/dist/lib/parser/arena.js +0 -206
@@ -76,34 +76,12 @@ function parseAtRuleContainerQueryList(stream, context, options = {}) {
76
76
  {
77
77
  action: "drop",
78
78
  node: stream[i],
79
- location: stream[i]?.[LOC], // ?? context[LOC],
80
- message: `expecting <container-condition> at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
79
+ location: options.source.getSourceLocation(stream[i]?.[LOC].sta), // ?? context[LOC],
80
+ message: `expecting <container-condition>`,
81
81
  },
82
82
  ],
83
83
  };
84
84
  }
85
- // if (stream[i]?.typ === EnumToken.IdenTokenType) {
86
- // const val: string = (stream[i] as IdentToken).val.toLowerCase();
87
- // if ("not" === val) {
88
- // tokens.push(stream[i]);
89
- // stack.push(Object.assign(stream[i], { typ: EnumToken.NotTokenType }));
90
- // i++;
91
- // } else if ("and" === val || "or" === val) {
92
- // return {
93
- // success: false,
94
- // errors: [
95
- // {
96
- // action: "drop",
97
- // node: stream[i],
98
- // location: stream[i]?.[LOC],
99
- // message: `unexpected token at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
100
- // },
101
- // ],
102
- // };
103
- // } else {
104
- // tokens.push(stream[i++]);
105
- // }
106
- // }
107
85
  for (; i < stream.length; i++) {
108
86
  tokens.push(stream[i]);
109
87
  if (stream[i].typ === EnumToken.WhitespaceTokenType || stream[i].typ === EnumToken.CommentTokenType) {
@@ -122,7 +100,8 @@ function parseAtRuleContainerQueryList(stream, context, options = {}) {
122
100
  errors.push({
123
101
  action: "drop",
124
102
  node: stream[i],
125
- message: `expecting <and>, <or> or comma at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
103
+ message: `expecting <and>, <or> or comma`,
104
+ location: options.source.getSourceLocation(stream[i]?.[LOC].sta),
126
105
  });
127
106
  break;
128
107
  }
@@ -134,14 +113,6 @@ function parseAtRuleContainerQueryList(stream, context, options = {}) {
134
113
  continue;
135
114
  }
136
115
  switch (stream[i].typ) {
137
- // case EnumToken.LiteralTokenType:
138
- // if ((stream[i] as LiteralToken).val === "<") {
139
- // stack.push(Object.assign(stream[i], { typ: EnumToken.LtTokenType }));
140
- // }
141
- // if ((stream[i] as LiteralToken).val === ">") {
142
- // stack.push(Object.assign(stream[i], { typ: EnumToken.GtTokenType }));
143
- // }
144
- // break;
145
116
  case EnumToken.ColonTokenType:
146
117
  case EnumToken.LtTokenType:
147
118
  case EnumToken.LteTokenType:
@@ -167,8 +138,8 @@ function parseAtRuleContainerQueryList(stream, context, options = {}) {
167
138
  success = false;
168
139
  errors.push({
169
140
  action: "drop",
170
- node: stream[i],
171
- message: `<or> is not allowed outside of parentheses ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
141
+ node: options.source.getSourceLocation(stream[i][LOC].sta),
142
+ message: `<or> is not allowed outside of parentheses`,
172
143
  });
173
144
  break;
174
145
  }
@@ -350,15 +321,12 @@ function parseAtRuleContainerQueryList(stream, context, options = {}) {
350
321
  errors.push({
351
322
  action: "drop",
352
323
  node: stream[i],
353
- location: stream[i]?.[LOC],
354
- message: `expecting '<${tokens[index].val}-query>' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
324
+ location: options.source.getSourceLocation(stream[i]?.[LOC].sta),
325
+ message: `expecting '<${tokens[index].val}-query>'`,
355
326
  });
356
327
  break;
357
328
  }
358
329
  tokens.length = index + 1;
359
- // if (EnumToken.ColorTokenType === tokens[index].typ) {
360
- // parseColor(tokens[index]);
361
- // }
362
330
  stack.pop();
363
331
  scopes.pop();
364
332
  currentScope = scopes.at(-1);
@@ -375,8 +343,8 @@ function parseAtRuleContainerQueryList(stream, context, options = {}) {
375
343
  errors.push({
376
344
  action: "drop",
377
345
  node: stream[i],
378
- location: stream[i]?.[LOC],
379
- message: `expecting '<query-in-parens>' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
346
+ location: options.source.getSourceLocation(stream[i]?.[LOC].sta),
347
+ message: `expecting '<query-in-parens>'`,
380
348
  });
381
349
  break;
382
350
  }
@@ -397,8 +365,8 @@ function parseAtRuleContainerQueryList(stream, context, options = {}) {
397
365
  errors.push({
398
366
  action: "drop",
399
367
  node: tokens[k],
400
- location: tokens[k]?.[LOC],
401
- message: `unexpected token 'not' at ${tokens[k]?.[LOC]?.src}:${tokens[k]?.[LOC]?.sta.lin}:${tokens[k]?.[LOC]?.sta.col}`,
368
+ location: options.source.getSourceLocation(tokens[k]?.[LOC].sta),
369
+ message: `unexpected token 'not'`,
402
370
  });
403
371
  break;
404
372
  }
@@ -2,7 +2,7 @@ import { EnumToken } from '../../ast/types.js';
2
2
  import { tokensfuncDefMap, LOC } from '../../syntax/constants.js';
3
3
  import { equalsIgnoreCase } from './text.js';
4
4
 
5
- function matchGenericSyntax(stream) {
5
+ function matchGenericSyntax(stream, options) {
6
6
  const stack = [];
7
7
  let i = 0;
8
8
  let success = true;
@@ -26,37 +26,24 @@ function matchGenericSyntax(stream) {
26
26
  (stack.at(-1)?.typ !== EnumToken.StartParensTokenType && !tokensfuncDefMap.has(stack.at(-1)?.typ))) {
27
27
  errors.push({
28
28
  action: "drop",
29
- message: `unexpected token ${EnumToken[token.typ]} at ${token[LOC].src}:${token[LOC].sta.lin}:${token[LOC].sta.col}`,
29
+ message: `unexpected token ${EnumToken[token.typ]}`,
30
30
  node: token,
31
- location: token[LOC],
31
+ location: options.source.getSourceLocation(token[LOC].sta),
32
32
  });
33
33
  success = false;
34
34
  break;
35
35
  }
36
36
  stack.pop();
37
37
  scopes.pop();
38
- // if (scopes.length === 0) {
39
- // return {
40
- // success: false,
41
- // errors: [
42
- // {
43
- // action: "drop",
44
- // node: token,
45
- // message: `Unexpected token ${EnumToken[token.typ]} at ${token[LOC]!.src}:${token[LOC]!.sta.lin}:${token[LOC]!.sta.col}`,
46
- // location: token[LOC]!,
47
- // },
48
- // ],
49
- // };
50
- // }
51
38
  continue;
52
39
  }
53
40
  if (token.typ === EnumToken.IdenTokenType && equalsIgnoreCase("and", token.val)) {
54
41
  if (!expectAndOr || scopes.at(-1)?.has(EnumToken.OrTokenType)) {
55
42
  errors.push({
56
43
  action: "drop",
57
- message: `unexpected token ${EnumToken[token.typ]} at ${token[LOC].src}:${token[LOC].sta.lin}:${token[LOC].sta.col}`,
44
+ message: `unexpected token ${EnumToken[token.typ]}`,
58
45
  node: token,
59
- location: token[LOC],
46
+ location: options.source.getSourceLocation(token[LOC].sta),
60
47
  });
61
48
  success = false;
62
49
  break;
@@ -70,9 +57,9 @@ function matchGenericSyntax(stream) {
70
57
  if (!expectAndOr || scopes.at(-1)?.has(EnumToken.AndTokenType)) {
71
58
  errors.push({
72
59
  action: "drop",
73
- message: `unexpected token ${EnumToken[token.typ]} at ${token[LOC].src}:${token[LOC].sta.lin}:${token[LOC].sta.col}`,
60
+ message: `unexpected token ${EnumToken[token.typ]}`,
74
61
  node: token,
75
- location: token[LOC],
62
+ location: options.source.getSourceLocation(token[LOC].sta),
76
63
  });
77
64
  success = false;
78
65
  break;
@@ -86,9 +73,9 @@ function matchGenericSyntax(stream) {
86
73
  if (!expectComma) {
87
74
  errors.push({
88
75
  action: "drop",
89
- message: `unexpected token ${EnumToken[token.typ]} at ${token[LOC].src}:${token[LOC].sta.lin}:${token[LOC].sta.col}`,
76
+ message: `unexpected token ${EnumToken[token.typ]}`,
90
77
  node: token,
91
- location: token[LOC],
78
+ location: options.source.getSourceLocation(token[LOC].sta),
92
79
  });
93
80
  success = false;
94
81
  break;
@@ -106,9 +93,10 @@ function matchGenericSyntax(stream) {
106
93
  if (stack.length > 0) {
107
94
  errors.push({
108
95
  action: "drop",
109
- message: `unexpected token ${EnumToken[stack.at(-1)?.typ]} at ${stack.at(-1)?.[LOC]?.src}:${stack.at(-1)?.[LOC]?.sta.lin}:${stack.at(-1)?.[LOC]?.sta.col}`,
96
+ message: `unexpected token ${EnumToken[stack.at(-1)?.typ]}`,
110
97
  node: stack.at(-1),
111
- location: stack.at(-1)?.[LOC],
98
+ // @ts-expect-error
99
+ location: options.source.getSourceLocation(stack.at(-1)?.[LOC].sta),
112
100
  });
113
101
  success = false;
114
102
  }
@@ -26,7 +26,6 @@ function matchAtRuleImportSyntax(atRule, stream, context, options) {
26
26
  // match ending ')'
27
27
  let matchCount = 0;
28
28
  let k = 0;
29
- // let stringCount = 0;
30
29
  for (; k < stream.length; k++) {
31
30
  if (stream[k]?.typ === EnumToken.EndParensTokenType) {
32
31
  matchCount--;
@@ -34,45 +33,10 @@ function matchAtRuleImportSyntax(atRule, stream, context, options) {
34
33
  else if (stream[k]?.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stream[k]?.typ)) {
35
34
  matchCount++;
36
35
  }
37
- // else if (stream[k]?.typ === EnumToken.StringTokenType) {
38
- // stringCount++;
39
- // }
40
- // else if (stream[k]?.typ === EnumToken.UrlFunctionTokenDefType) {
41
- // urlTokenCount++;
42
- // } else if (
43
- // !(stream[k]?.typ === EnumToken.WhitespaceTokenType || stream[k]?.typ === EnumToken.CommentTokenType)
44
- // ) {
45
- // return {
46
- // success: false,
47
- // errors: [
48
- // {
49
- // action: "drop",
50
- // message: "Expected string or <url-token>",
51
- // syntax: "@import",
52
- // node: stream[k],
53
- // location: stream[k]?.[LOC],
54
- // } as ErrorDescription,
55
- // ],
56
- // };
57
- // }
58
36
  if (matchCount === 0) {
59
37
  break;
60
38
  }
61
39
  }
62
- // if (stringCount + urlTokenCount != 1) {
63
- // return {
64
- // success: false,
65
- // errors: [
66
- // {
67
- // action: "drop",
68
- // syntax: "@import",
69
- // message: "could not match syntax <url>",
70
- // node: stream[0],
71
- // location: stream[0][LOC],
72
- // },
73
- // ],
74
- // };
75
- // }
76
40
  const slice = stream.slice(index + 1, k);
77
41
  // @ts-expect-error
78
42
  stream[0][LOC] = {
@@ -112,12 +76,6 @@ function matchAtRuleImportSyntax(atRule, stream, context, options) {
112
76
  "layer".localeCompare(stream[index].val) === 0) {
113
77
  stack.push(stream[index]);
114
78
  tokens.push(stream[index++]);
115
- // while (
116
- // stream[index]?.typ === EnumToken.WhitespaceTokenType ||
117
- // stream[index]?.typ === EnumToken.CommentTokenType
118
- // ) {
119
- // tokens.push(stream[index++] as Token);
120
- // }
121
79
  // <layer-name">
122
80
  if (stream[index]?.typ === EnumToken.EndParensTokenType) {
123
81
  i = tokens.indexOf(stack.at(-1));
@@ -127,10 +85,10 @@ function matchAtRuleImportSyntax(atRule, stream, context, options) {
127
85
  errors: [
128
86
  {
129
87
  action: "drop",
130
- message: `Expected <layer-name> at ${stream[index]?.[LOC]?.src}:${stream[index]?.[LOC]?.sta.lin}:${stream[index]?.[LOC]?.sta.col}`,
88
+ message: `Expected <layer-name>`,
131
89
  syntax: "@import",
132
90
  node: stream[index],
133
- location: stream[index]?.[LOC],
91
+ location: options.source.getSourceLocation(stream[index]?.[LOC].sta),
134
92
  },
135
93
  ],
136
94
  };
@@ -140,26 +98,6 @@ function matchAtRuleImportSyntax(atRule, stream, context, options) {
140
98
  while (stream[index]?.typ == EnumToken.ClassSelectorTokenType) {
141
99
  tokens.push(stream[index++]);
142
100
  }
143
- // while (
144
- // stream[index]?.typ == EnumToken.WhitespaceTokenType ||
145
- // stream[index]?.typ == EnumToken.CommentTokenType
146
- // ) {
147
- // tokens.push(stream[index++] as Token);
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
- // } as ErrorDescription,
160
- // ],
161
- // };
162
- // }
163
101
  i = tokens.indexOf(stack.at(-1));
164
102
  tokens.splice(index, 1);
165
103
  Object.assign(stack.at(-1), {
@@ -174,10 +112,10 @@ function matchAtRuleImportSyntax(atRule, stream, context, options) {
174
112
  errors: [
175
113
  {
176
114
  action: "drop",
177
- message: `Expected <layer-name> at ${stream[index]?.[LOC]?.src}:${stream[index]?.[LOC]?.sta.lin}:${stream[index]?.[LOC]?.sta.col}`,
115
+ message: `Expected <layer-name>`,
178
116
  syntax: "@import",
179
117
  node: stream[index],
180
- location: stream[index]?.[LOC],
118
+ location: options.source.getSourceLocation(stream[index]?.[LOC].sta),
181
119
  },
182
120
  ],
183
121
  };
@@ -193,97 +131,6 @@ function matchAtRuleImportSyntax(atRule, stream, context, options) {
193
131
  stack.push(stream[index]);
194
132
  tokens.push(stream[index++]);
195
133
  matchCount = 1;
196
- // while (
197
- // stream[index]?.typ === EnumToken.WhitespaceTokenType ||
198
- // stream[index]?.typ === EnumToken.CommentTokenType
199
- // ) {
200
- // tokens.push(stream[index++] as Token);
201
- // }
202
- // <declaration>
203
- // if (
204
- // stream[index]?.typ === EnumToken.IdenTokenType &&
205
- // !["and", "or", "not", "only"].includes((stream[index] as IdentToken).val.toLowerCase())
206
- // ) {
207
- // tokens.push(stream[index++] as Token);
208
- // while (
209
- // stream[index]?.typ === EnumToken.WhitespaceTokenType ||
210
- // stream[index]?.typ === EnumToken.CommentTokenType
211
- // ) {
212
- // tokens.push(stream[index++] as Token);
213
- // }
214
- // if (stream[index]?.typ !== EnumToken.ColonTokenType) {
215
- // return {
216
- // success: false,
217
- // errors: [
218
- // {
219
- // action: "drop",
220
- // message: "Expected ':'",
221
- // syntax: "@import",
222
- // node: stream[index],
223
- // location: stream[index]?.[LOC],
224
- // } as ErrorDescription,
225
- // ],
226
- // };
227
- // } else {
228
- // isDecl = true;
229
- // tokens.push(stream[index++] as Token);
230
- // // match the next ')'
231
- // while (index < stream.length && matchCount > 0) {
232
- // if (
233
- // stream[index]?.typ === EnumToken.StartParensTokenType ||
234
- // tokensfuncDefMap.has(stream[index]?.typ)
235
- // ) {
236
- // matchCount++;
237
- // // stack.push(stream[index] as Token);
238
- // } else if (stream[index]?.typ === EnumToken.EndParensTokenType) {
239
- // matchCount--;
240
- // if (matchCount === 0) {
241
- // i = tokens.indexOf(stack.at(-1) as Token);
242
- // tokens.splice(index, 1);
243
- // Object.assign(stack.at(-1) as Token, {
244
- // typ: tokensfuncDefMap.get((stack.at(-1) as Token).typ),
245
- // chi: trimArray(tokens.splice(i + 1, index++ - i - 1)),
246
- // });
247
- // stack.pop();
248
- // break;
249
- // }
250
- // } else if (stream[index]?.typ === EnumToken.ImportantTokenType) {
251
- // tokens.push(stream[index++] as Token);
252
- // while (
253
- // stream[index]?.typ === EnumToken.WhitespaceTokenType ||
254
- // stream[index]?.typ === EnumToken.CommentTokenType
255
- // ) {
256
- // tokens.push(stream[index++] as Token);
257
- // }
258
- // // next token is ')'
259
- // if (stream[index]?.typ !== EnumToken.EndParensTokenType) {
260
- // return {
261
- // success: false,
262
- // errors: [
263
- // {
264
- // action: "drop",
265
- // message: "Expected ')'",
266
- // syntax: "@import",
267
- // node: stream[index],
268
- // location: stream[index]?.[LOC],
269
- // } as ErrorDescription,
270
- // ],
271
- // };
272
- // }
273
- // matchCount--;
274
- // i = tokens.indexOf(stack.at(-1) as Token);
275
- // tokens.splice(index, 1);
276
- // Object.assign(stack.at(-1) as Token, {
277
- // typ: tokensfuncDefMap.get((stack.at(-1) as Token).typ),
278
- // chi: trimArray(tokens.splice(i + 1, index++ - i - 1)),
279
- // });
280
- // stack.pop();
281
- // break;
282
- // }
283
- // tokens.push(stream[index++] as Token);
284
- // }
285
- // }
286
- // }
287
134
  // <supports-condition>
288
135
  // else {
289
136
  // match the next ')'
@@ -306,21 +153,6 @@ function matchAtRuleImportSyntax(atRule, stream, context, options) {
306
153
  }
307
154
  tokens.push(stream[index++]);
308
155
  }
309
- // }
310
- // if (matchCount > 0) {
311
- // return {
312
- // success: false,
313
- // errors: [
314
- // {
315
- // action: "drop",
316
- // message: "unmatched ')'",
317
- // syntax: "@import",
318
- // node: stack.at(-1),
319
- // location: stack.at(-1)?.[LOC],
320
- // } as ErrorDescription,
321
- // ],
322
- // };
323
- // }
324
156
  // support() is the last item in tokens array
325
157
  {
326
158
  const result = parseAtRuleSupportSyntax(tokens[tokens.length - 1].chi, context, options);