@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
@@ -51,65 +51,6 @@ function parseMediaqueryList(stream, options) {
51
51
  if (i < stream.length) {
52
52
  if (stream[i].typ === EnumToken.IdenTokenType) {
53
53
  const val = stream[i].val.toLowerCase();
54
- // if ("not" === val || "only" === val) {
55
- // tokens.push(stream[i]);
56
- // stack.push(stream[i]);
57
- // i++;
58
- // while (
59
- // i < stream.length &&
60
- // (stream[i]?.typ === EnumToken.WhitespaceTokenType ||
61
- // stream[i]?.typ === EnumToken.CommentTokenType)
62
- // ) {
63
- // tokens.push(stream[i]);
64
- // i++;
65
- // }
66
- // if (
67
- // stream[i]?.typ !== EnumToken.IdenTokenType ||
68
- // !mediaTypes.includes((stream[i] as IdentToken).val.toLowerCase())
69
- // ) {
70
- // errors.push({
71
- // action: "drop",
72
- // node: stream[i],
73
- // message: `expecting '<media-type>' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
74
- // });
75
- // continue;
76
- // }
77
- // const index: number = tokens.indexOf(stack[stack.length - 1]);
78
- // tokens[index][LOC] = { ...tokens[index][LOC]!, end: stream[i][LOC]!.end };
79
- // tokens[index] = Object.assign(tokens[index], {
80
- // typ: val === "not" ? EnumToken.NotTokenType : EnumToken.OnlyTokenType,
81
- // val: stream[i],
82
- // });
83
- // tokens.length = index + 1;
84
- // i++;
85
- // // expect end of stream | and | or | not
86
- // while (
87
- // i < stream.length &&
88
- // (stream[i]?.typ === EnumToken.WhitespaceTokenType ||
89
- // stream[i]?.typ === EnumToken.CommentTokenType)
90
- // ) {
91
- // tokens.push(stream[i]);
92
- // i++;
93
- // }
94
- // if (i < stream.length) {
95
- // if (stream[i].typ === EnumToken.AndTokenType) {
96
- // tokens.push(Object.assign(stream[i], { typ: EnumToken.AndTokenType }));
97
- // stack.push(stream[i]);
98
- // i++;
99
- // } else if (stream[i].typ === EnumToken.OrTokenType) {
100
- // tokens.push(Object.assign(stream[i], { typ: EnumToken.OrTokenType }));
101
- // stack.push(stream[i]);
102
- // i++;
103
- // } else {
104
- // success = false;
105
- // errors.push({
106
- // action: "drop",
107
- // node: stream[i],
108
- // message: `expecting 'and' or 'or' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
109
- // });
110
- // }
111
- // }
112
- // } else
113
54
  if (mediaTypes.includes(val)) {
114
55
  tokens.push(stream[i]);
115
56
  i++;
@@ -118,9 +59,9 @@ function parseMediaqueryList(stream, options) {
118
59
  success = false;
119
60
  errors.push({
120
61
  action: "drop",
121
- message: `expecting '<media-type>' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
62
+ message: `expecting '<media-type>'`,
122
63
  node: stream[i],
123
- location: stream[i][LOC],
64
+ location: options.source.getSourceLocation(stream[i][LOC].sta),
124
65
  });
125
66
  }
126
67
  }
@@ -128,9 +69,9 @@ function parseMediaqueryList(stream, options) {
128
69
  success = false;
129
70
  errors.push({
130
71
  action: "drop",
131
- message: `expecting '(' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
72
+ message: `expecting '('`,
132
73
  node: stream[i],
133
- location: stream[i][LOC],
74
+ location: options.source.getSourceLocation(stream[i][LOC].sta),
134
75
  });
135
76
  }
136
77
  }
@@ -143,15 +84,6 @@ function parseMediaqueryList(stream, options) {
143
84
  if (stream[i].typ === EnumToken.IdenTokenType) {
144
85
  stream[i].val.toLowerCase();
145
86
  }
146
- // if (!valid) {
147
- // success = false;
148
- // errors.push({
149
- // action: "drop",
150
- // node: stream[i],
151
- // message: `expecting <and>, <or> or comma at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
152
- // });
153
- // break;
154
- // }
155
87
  expectAndOrComma = false;
156
88
  }
157
89
  if (stream[i].typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stream[i].typ)) {
@@ -160,14 +92,6 @@ function parseMediaqueryList(stream, options) {
160
92
  continue;
161
93
  }
162
94
  switch (stream[i].typ) {
163
- // case EnumToken.LiteralTokenType:
164
- // if ((stream[i] as LiteralToken).val === "<") {
165
- // stack.push(Object.assign(stream[i], { typ: EnumToken.LtTokenType }));
166
- // }
167
- // if ((stream[i] as LiteralToken).val === ">") {
168
- // stack.push(Object.assign(stream[i], { typ: EnumToken.GtTokenType }));
169
- // }
170
- // break;
171
95
  case EnumToken.ColonTokenType:
172
96
  case EnumToken.LtTokenType:
173
97
  case EnumToken.LteTokenType:
@@ -179,12 +103,6 @@ function parseMediaqueryList(stream, options) {
179
103
  case EnumToken.IdenTokenType:
180
104
  {
181
105
  const val = stream[i].val.toLowerCase();
182
- // if (val === "not" || val === "only") {
183
- // Object.assign(stream[i], {
184
- // typ: val === "not" ? EnumToken.NotTokenType : EnumToken.OnlyTokenType,
185
- // });
186
- // stack.push(stream[i]);
187
- // } else
188
106
  if (val === "and" || val === "or") {
189
107
  Object.assign(stream[i], {
190
108
  typ: val === "and" ? EnumToken.AndTokenType : EnumToken.OrTokenType,
@@ -194,7 +112,8 @@ function parseMediaqueryList(stream, options) {
194
112
  errors.push({
195
113
  action: "drop",
196
114
  node: stream[i],
197
- message: `<or> is not allowed outside of parentheses ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
115
+ message: `<or> is not allowed outside of parentheses`,
116
+ location: options.source.getSourceLocation(stream[i][LOC].sta),
198
117
  });
199
118
  break;
200
119
  }
@@ -203,7 +122,8 @@ function parseMediaqueryList(stream, options) {
203
122
  errors.push({
204
123
  action: "drop",
205
124
  node: stream[i],
206
- message: `cannot mix <and> and <or> at the same level at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
125
+ message: `cannot mix <and> and <or> at the same level`,
126
+ location: options.source.getSourceLocation(stream[i][LOC].sta),
207
127
  });
208
128
  }
209
129
  currentScope.add(stream[i].typ);
@@ -236,32 +156,6 @@ function parseMediaqueryList(stream, options) {
236
156
  const index = tokens.indexOf(stack.at(-1));
237
157
  const prevToken = stack[stack.length - 2];
238
158
  if (mFLT.has(prevToken?.typ) || mFGT.has(prevToken?.typ)) {
239
- // if (stack[stack.length - 3]?.typ !== EnumToken.StartParensTokenType) {
240
- // success = false;
241
- // errors.push({
242
- // action: "drop",
243
- // node: stream[i],
244
- // message: `unmatched '(' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
245
- // });
246
- // break;
247
- // }
248
- // if (!mFLT.has(stack.at(-1)?.typ) && mFLT.has(prevToken?.typ)) {
249
- // success = false;
250
- // errors.push({
251
- // action: "drop",
252
- // node: stack.at(-1),
253
- // message: `expected <mf-lt> at ${stack.at(-1)?.[LOC]?.src}:${stack.at(-1)?.[LOC]?.sta.lin}:${stack.at(-1)?.[LOC]?.sta.col}`,
254
- // });
255
- // break;
256
- // } else if (!mFGT.has(stack.at(-1)?.typ) && mFGT.has(prevToken?.typ)) {
257
- // success = false;
258
- // errors.push({
259
- // action: "drop",
260
- // node: stream[i],
261
- // message: `expected <mf-gt> at ${stack.at(-1)?.[LOC]?.src}:${stack.at(-1)?.[LOC]?.sta.lin}:${stack.at(-1)?.[LOC]?.sta.col}`,
262
- // });
263
- // break;
264
- // }
265
159
  // const index: number = tokens.indexOf(stack.at(-1)!);
266
160
  // <mf-lt> | <mf-name>
267
161
  const index2 = tokens.indexOf(prevToken);
@@ -271,15 +165,6 @@ function parseMediaqueryList(stream, options) {
271
165
  const right = trimArray(tokens.slice(index + 1, tokens.length - 1));
272
166
  const names = trimArray(tokens.slice(index2 + 1, index));
273
167
  const filteredNames = names.filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
274
- // if (filteredNames.length !== 1 || filteredNames[0].typ !== EnumToken.IdenTokenType) {
275
- // success = false;
276
- // errors.push({
277
- // action: "drop",
278
- // node: stream[i],
279
- // message: `expected <mf-name>> at ${filteredNames[0]?.[LOC]?.src}:${filteredNames[0]?.[LOC]?.sta.lin}:${filteredNames[0]?.[LOC]?.sta.col}`,
280
- // });
281
- // break;
282
- // }
283
168
  const name = filteredNames[0].val;
284
169
  const mfInfo = getMFInfo(name);
285
170
  for (const val of [left, right]) {
@@ -292,17 +177,6 @@ function parseMediaqueryList(stream, options) {
292
177
  val[l].val === "calc") {
293
178
  const value = evaluate([val[l]]);
294
179
  if (value.length == 1) {
295
- // if (mfInfo?.type === MediaFeatureType.RatioType) {
296
- // if (
297
- // value[0].typ !== EnumToken.NumberTokenType ||
298
- // !(
299
- // ((value[0] as NumberToken).val as FractionToken)
300
- // ?.typ === EnumToken.FractionTokenType
301
- // )
302
- // ) {
303
- // continue;
304
- // }
305
- // }
306
180
  value[0][LOC] = val[l][LOC];
307
181
  val[l] = value[0];
308
182
  }
@@ -311,27 +185,8 @@ function parseMediaqueryList(stream, options) {
311
185
  }
312
186
  }
313
187
  }
314
- isMFValue(name, left, true);
315
- // if (isValidMFValue.valid && !isValidMFValue.success) {
316
- // success = false;
317
- // errors.push({
318
- // action: "drop",
319
- // node: left[0] ?? prevToken,
320
- // message: `${isValidMFValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"} at ${(left[0] ?? prevToken)?.[LOC]?.src}:${(left[0] ?? prevToken)?.[LOC]?.sta.lin}:${(left[0] ?? prevToken)?.[LOC]?.sta.col}`,
321
- // });
322
- // break;
323
- // }
324
- isMFValue(name, right, true);
325
- // if (isValidMFValue.valid && !isValidMFValue.success) {
326
- // success = false;
327
- // errors.push({
328
- // action: "drop",
329
- // node: right[0] ?? stream[i],
330
- // location: (right[0] ?? stream[i])?.[LOC],
331
- // message: `${isValidMFValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"} at ${(right[0] ?? stream[i])?.[LOC]?.src}:${(right[0] ?? stream[i])?.[LOC]?.sta.lin}:${(left[0] ?? stream[i])?.[LOC]?.sta.col}`,
332
- // });
333
- // break;
334
- // }
188
+ // let isValidMFValue = isMFValue(name, left, true);
189
+ // isValidMFValue = isMFValue(name, right, true);
335
190
  for (const val of [left, right]) {
336
191
  if (mfInfo?.type === MediaFeatureType.RatioType) {
337
192
  const filteredValues = val.filter((n) => n.typ !== EnumToken.WhitespaceTokenType &&
@@ -362,75 +217,18 @@ function parseMediaqueryList(stream, options) {
362
217
  stack.pop();
363
218
  stack.pop();
364
219
  }
365
- // else if (stack[stack.length - 2]?.typ !== EnumToken.StartParensTokenType) {
366
- // success = false;
367
- // errors.push({
368
- // action: "drop",
369
- // node: stream[i],
370
- // location: stream[i]?.[LOC],
371
- // message: `expected '(' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
372
- // });
373
- // break;
374
- // }
375
220
  }
376
221
  if (stack.length > 0 &&
377
222
  (mFGT.has(stack.at(-1)?.typ) ||
378
223
  mFLT.has(stack.at(-1)?.typ) ||
379
224
  stack.at(-1)?.typ === EnumToken.DelimTokenType ||
380
225
  stack.at(-1)?.typ === EnumToken.ColonTokenType)) {
381
- // if (stack[stack.length - 2]?.typ !== EnumToken.StartParensTokenType) {
382
- // success = false;
383
- // errors.push({
384
- // action: "drop",
385
- // node: stream[i],
386
- // location: stream[i]?.[LOC],
387
- // message: `expected '(' at ${(stack[stack.length - 2] ?? tokens[0])?.[LOC]?.src}:${(stack[stack.length - 2] ?? tokens[0])?.[LOC]?.sta.lin}:${(stack[stack.length - 2] ?? tokens[0])?.[LOC]?.sta.col}`,
388
- // });
389
- // break;
390
- // }
391
226
  const index2 = tokens.indexOf(stack.at(-1));
392
227
  const index3 = tokens.indexOf(stack.at(-2));
393
228
  let names = trimArray(tokens.slice(index3 + 1, index2));
394
229
  let values = trimArray(tokens.slice(index2 + 1, tokens.length - 1));
395
- // if (stack.at(-1)?.typ !== EnumToken.ColonTokenType) {
396
- // const filteredNames = names.filter(
397
- // (n) =>
398
- // n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType,
399
- // );
400
- // if (
401
- // filteredNames.length !== 1 ||
402
- // (filteredNames[0].typ !== EnumToken.IdenTokenType &&
403
- // filteredNames[0].typ !== EnumToken.DashedIdenTokenType)
404
- // ) {
405
- // swapped = true;
406
- // }
407
- // }
408
230
  const filteredNames = (names).filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
409
- // if (
410
- // filteredNames.length !== 1 ||
411
- // (filteredNames[0].typ !== EnumToken.IdenTokenType &&
412
- // filteredNames[0].typ !== EnumToken.DashedIdenTokenType)
413
- // ) {
414
- // success = false;
415
- // errors.push({
416
- // action: "drop",
417
- // node: names[0] ?? stack.at(-1),
418
- // location: names[0]?.[LOC],
419
- // message: `expected <mf-name> at ${(names[0] ?? stack.at(-1))?.[LOC]?.src}:${(names[0] ?? stack.at(-1))?.[LOC]?.sta.lin}:${(names[0] ?? stack.at(-1))?.[LOC]?.sta.col}`,
420
- // });
421
- // break;
422
- // }
423
231
  const name = filteredNames[0].val;
424
- // if (!isMFName(name)) {
425
- // success = false;
426
- // errors.push({
427
- // action: "drop",
428
- // node: names[0] ?? stack.at(-1),
429
- // location: names[0]?.[LOC] ?? stack.at(-1)?.[LOC],
430
- // message: `expected <mf-name> at ${names[0]?.[LOC]?.src}:${names[0]?.[LOC]?.sta.lin}:${names[0]?.[LOC]?.sta.col}`,
431
- // });
432
- // break;
433
- // }
434
232
  const mfInfo = getMFInfo(name);
435
233
  if (options.computeCalcExpression) {
436
234
  if (mfInfo == null ||
@@ -442,17 +240,6 @@ function parseMediaqueryList(stream, options) {
442
240
  val[l].val === "calc") {
443
241
  const value = evaluate([val[l]]);
444
242
  if (value.length == 1) {
445
- // if (mfInfo?.type === MediaFeatureType.RatioType) {
446
- // if (
447
- // value[0].typ !== EnumToken.NumberTokenType ||
448
- // !(
449
- // ((value[0] as NumberToken).val as FractionToken)?.typ ===
450
- // EnumToken.FractionTokenType
451
- // )
452
- // ) {
453
- // continue;
454
- // }
455
- // }
456
243
  value[0][LOC] = val[l][LOC];
457
244
  val[l] = value[0];
458
245
  }
@@ -469,8 +256,8 @@ function parseMediaqueryList(stream, options) {
469
256
  errors.push({
470
257
  action: "drop",
471
258
  node: arr[0],
472
- location: arr[0]?.[LOC],
473
- message: `${mfValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"} at ${arr[0]?.[LOC]?.src}:${arr[0]?.[LOC]?.sta.lin}:${arr[0]?.[LOC]?.sta.col}`,
259
+ location: options.source.getSourceLocation(arr[0]?.[LOC].sta),
260
+ message: `${mfValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"}`,
474
261
  });
475
262
  break;
476
263
  }
@@ -504,8 +291,8 @@ function parseMediaqueryList(stream, options) {
504
291
  errors.push({
505
292
  action: "drop",
506
293
  node: stream[i],
507
- location: stream[i]?.[LOC],
508
- message: `unmatched ')' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
294
+ location: options.source.getSourceLocation(stream[i]?.[LOC].sta),
295
+ message: `unmatched ')'`,
509
296
  });
510
297
  break;
511
298
  }
@@ -521,21 +308,6 @@ function parseMediaqueryList(stream, options) {
521
308
  scopes.pop();
522
309
  currentScope = scopes.at(-1);
523
310
  stack.pop();
524
- // if (
525
- // stack.at(-1)?.typ === EnumToken.NotTokenType ||
526
- // stack.at(-1)?.typ === EnumToken.OnlyTokenType
527
- // ) {
528
- // const index = tokens.indexOf(stack.at(-1)!);
529
- // const slice = trimArray(tokens.slice(index + 1));
530
- // tokens[index] = {
531
- // typ: EnumToken.MediaQueryUnaryFeatureTokenType,
532
- // l: stack.pop()!,
533
- // r: slice,
534
- // // @ts-expect-error
535
- // [LOC]: { ...tokens[index][LOC]!, end: slice.at(-1)![LOC]!.end } as Location,
536
- // };
537
- // tokens.length = index + 1;
538
- // }
539
311
  if (stack.at(-1)?.typ === EnumToken.AndTokenType ||
540
312
  stack.at(-1)?.typ === EnumToken.OrTokenType) {
541
313
  const index = tokens.indexOf(stack.at(-1));
@@ -564,15 +336,6 @@ function parseMediaqueryList(stream, options) {
564
336
  break;
565
337
  }
566
338
  }
567
- // if (success && stack.length > 0) {
568
- // success = false;
569
- // errors.push({
570
- // action: "drop",
571
- // node: stack.at(-1),
572
- // location: stack.at(-1)?.[LOC],
573
- // message: `unmatched token '${EnumToken[stack.at(-1)?.typ]}' at ${stack.at(-1)?.[LOC]?.src}:${stack.at(-1)?.[LOC]?.sta.lin}:${stack.at(-1)?.[LOC]?.sta.col}`,
574
- // });
575
- // }
576
339
  if (!success) {
577
340
  hasErrors = true;
578
341
  parts.splice(parts.indexOf(stream), 1);