@tbela99/css-parser 1.5.0 → 1.6.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 (73) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +7 -7
  3. package/dist/index-umd-web.js +5562 -4731
  4. package/dist/index.cjs +5565 -4736
  5. package/dist/index.d.ts +51 -37
  6. package/dist/lib/ast/expand.js +21 -7
  7. package/dist/lib/ast/features/calc.js +17 -55
  8. package/dist/lib/ast/features/if.js +7 -3
  9. package/dist/lib/ast/features/inlinecssvariables.js +6 -5
  10. package/dist/lib/ast/features/prefix.js +41 -44
  11. package/dist/lib/ast/features/shorthand.js +10 -5
  12. package/dist/lib/ast/features/transform.js +1 -1
  13. package/dist/lib/ast/math/expression.js +190 -32
  14. package/dist/lib/ast/minify.js +37 -14
  15. package/dist/lib/ast/node.js +5 -3
  16. package/dist/lib/ast/transform/compute.js +63 -4
  17. package/dist/lib/ast/transform/minify.js +6 -6
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +1 -1
  20. package/dist/lib/ast/transform/scale.js +1 -1
  21. package/dist/lib/ast/transform/skew.js +1 -1
  22. package/dist/lib/ast/transform/translate.js +1 -1
  23. package/dist/lib/ast/transform/utils.js +46 -18
  24. package/dist/lib/ast/walk.js +28 -4
  25. package/dist/lib/fs/resolve.js +11 -2
  26. package/dist/lib/parser/declaration/list.js +22 -7
  27. package/dist/lib/parser/declaration/map.js +26 -8
  28. package/dist/lib/parser/declaration/set.js +3 -1
  29. package/dist/lib/parser/linesmap.js +2 -4
  30. package/dist/lib/parser/parse.js +388 -227
  31. package/dist/lib/parser/tokenize.js +1478 -818
  32. package/dist/lib/parser/utils/at-rule-container.js +28 -196
  33. package/dist/lib/parser/utils/at-rule-generic.js +6 -7
  34. package/dist/lib/parser/utils/at-rule-import.js +17 -13
  35. package/dist/lib/parser/utils/at-rule-media.js +36 -19
  36. package/dist/lib/parser/utils/at-rule-support.js +19 -9
  37. package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
  38. package/dist/lib/parser/utils/at-rule.js +0 -18
  39. package/dist/lib/parser/utils/declaration.js +30 -95
  40. package/dist/lib/parser/utils/hash.js +4 -4
  41. package/dist/lib/parser/utils/selector.js +89 -144
  42. package/dist/lib/parser/utils/text.js +4 -2
  43. package/dist/lib/renderer/render.js +102 -72
  44. package/dist/lib/renderer/sourcemap/sourcemap.js +2 -5
  45. package/dist/lib/syntax/color/a98rgb.js +14 -4
  46. package/dist/lib/syntax/color/cmyk.js +10 -18
  47. package/dist/lib/syntax/color/color-mix.js +21 -34
  48. package/dist/lib/syntax/color/color.js +32 -49
  49. package/dist/lib/syntax/color/hsl.js +15 -15
  50. package/dist/lib/syntax/color/hwb.js +18 -17
  51. package/dist/lib/syntax/color/lab.js +17 -17
  52. package/dist/lib/syntax/color/lch.js +13 -13
  53. package/dist/lib/syntax/color/oklab.js +10 -11
  54. package/dist/lib/syntax/color/oklch.js +15 -18
  55. package/dist/lib/syntax/color/p3.js +18 -12
  56. package/dist/lib/syntax/color/prophotorgb.js +26 -28
  57. package/dist/lib/syntax/color/rec2020.js +8 -4
  58. package/dist/lib/syntax/color/relative-color.js +26 -10
  59. package/dist/lib/syntax/color/srgb.js +5 -4
  60. package/dist/lib/syntax/color/utils/distance.js +1 -1
  61. package/dist/lib/syntax/color/xyz.js +2 -2
  62. package/dist/lib/syntax/color/xyzd50.js +2 -2
  63. package/dist/lib/syntax/constants.js +12 -2
  64. package/dist/lib/syntax/syntax.js +32 -108
  65. package/dist/lib/validation/config.json.js +3 -0
  66. package/dist/lib/validation/match.js +29 -26
  67. package/dist/node.js +16 -9
  68. package/dist/types.d.ts +5 -1
  69. package/dist/types.js +4 -0
  70. package/dist/utils/sync.js +22 -1
  71. package/dist/web.js +13 -4
  72. package/package.json +1 -1
  73. package/llms.txt +0 -80
@@ -1,6 +1,6 @@
1
1
  import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
2
2
  import { renderValue } from '../../renderer/render.js';
3
- import { LOC, ERRORS, STATE, TOKENS, pseudoElements, combinators, tokensfuncDefMap, PARENT } from '../../syntax/constants.js';
3
+ import { LOCEND, LOCSTA, LOCSRCID, ERRORS, STATE, TOKENS, pseudoElements, combinators, tokensfuncDefMap, PARENT } from '../../syntax/constants.js';
4
4
  import { isHash } from '../../syntax/syntax.js';
5
5
  import { getParsedSyntax, getSyntaxRule, getSyntaxConfig } from '../../validation/config.js';
6
6
  import { matchAllSyntaxes, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
@@ -26,7 +26,9 @@ function parseSelector(tokens, context, options, errors) {
26
26
  filtered[0] = {
27
27
  typ: EnumToken.PercentageTokenType,
28
28
  val: 0,
29
- [LOC]: filtered[0][LOC],
29
+ [LOCSRCID]: filtered[0][LOCSRCID],
30
+ [LOCSTA]: filtered[0][LOCSTA],
31
+ [LOCEND]: filtered[0][LOCEND],
30
32
  };
31
33
  }
32
34
  else if (filtered[0].typ === EnumToken.PercentageTokenType &&
@@ -34,7 +36,9 @@ function parseSelector(tokens, context, options, errors) {
34
36
  filtered[0] = {
35
37
  typ: EnumToken.IdenTokenType,
36
38
  val: "to",
37
- [LOC]: filtered[0][LOC],
39
+ [LOCSRCID]: filtered[0][LOCSRCID],
40
+ [LOCSTA]: filtered[0][LOCSTA],
41
+ [LOCEND]: filtered[0][LOCEND],
38
42
  };
39
43
  }
40
44
  part.splice(0, part.length, ...filtered);
@@ -45,7 +49,9 @@ function parseSelector(tokens, context, options, errors) {
45
49
  if (acc.length > 0) {
46
50
  acc.push({ typ: EnumToken.CommaTokenType });
47
51
  }
48
- acc.push(...curr);
52
+ for (const c of curr) {
53
+ acc.push(c);
54
+ }
49
55
  return acc;
50
56
  }, []));
51
57
  return {
@@ -57,10 +63,9 @@ function parseSelector(tokens, context, options, errors) {
57
63
  }, new Set()),
58
64
  ].join(),
59
65
  chi: [],
60
- [LOC]: {
61
- ...tokens[0][LOC],
62
- end: tokens[tokens.length - 1]?.[LOC]?.end ?? tokens[0]?.[LOC]?.end,
63
- },
66
+ [LOCSRCID]: tokens[0]?.[LOCSRCID],
67
+ [LOCSTA]: tokens[0]?.[LOCSTA],
68
+ [LOCEND]: tokens[tokens.length - 1]?.[LOCEND],
64
69
  [TOKENS]: tokens.length === 0 ? null : tokens,
65
70
  [STATE]: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
66
71
  [ERRORS]: result.errors,
@@ -110,7 +115,7 @@ function parseSelector(tokens, context, options, errors) {
110
115
  typ: EnumToken.PseudoElementTokenType,
111
116
  val: ":" + tokens[i + 1].val,
112
117
  });
113
- tokens[i][LOC].end = tokens[i + 1][LOC].end;
118
+ tokens[i][LOCEND] = tokens[i + 1][LOCEND];
114
119
  tokens.splice(i + 1, 1);
115
120
  continue;
116
121
  }
@@ -122,7 +127,7 @@ function parseSelector(tokens, context, options, errors) {
122
127
  : tokens[i + 1].typ,
123
128
  val,
124
129
  });
125
- tokens[i][LOC].end = tokens[i + 1][LOC].end;
130
+ tokens[i][LOCEND] = tokens[i + 1][LOCEND];
126
131
  tokens.splice(i + 1, 1);
127
132
  continue;
128
133
  }
@@ -134,7 +139,7 @@ function parseSelector(tokens, context, options, errors) {
134
139
  typ: EnumToken.PseudoClassTokenType,
135
140
  val: (pseudoElements.includes(val) ? "" : ":") + val,
136
141
  });
137
- tokens[i][LOC].end = tokens[i + 1][LOC].end;
142
+ tokens[i][LOCEND] = tokens[i + 1][LOCEND];
138
143
  tokens.splice(i + 1, 1);
139
144
  continue;
140
145
  }
@@ -146,7 +151,7 @@ function parseSelector(tokens, context, options, errors) {
146
151
  : EnumToken.FunctionTokenDefType,
147
152
  val,
148
153
  });
149
- tokens[i][LOC].end = tokens[i + 1][LOC].end;
154
+ tokens[i][LOCEND] = tokens[i + 1][LOCEND];
150
155
  tokens.splice(i + 1, 1);
151
156
  continue;
152
157
  }
@@ -201,10 +206,9 @@ function parseSelector(tokens, context, options, errors) {
201
206
  .keys(),
202
207
  ].join(","),
203
208
  chi: [],
204
- [LOC]: {
205
- ...tokens[0][LOC],
206
- end: tokens[tokens.length - 1][LOC].end,
207
- },
209
+ [LOCSRCID]: tokens[0][LOCSRCID],
210
+ [LOCSTA]: tokens[0][LOCSTA],
211
+ [LOCEND]: tokens[tokens.length - 1][LOCEND],
208
212
  [TOKENS]: tokens,
209
213
  [STATE]: EnumAstNodeStatus.Invalid,
210
214
  [ERRORS]: [
@@ -232,10 +236,9 @@ function parseSelector(tokens, context, options, errors) {
232
236
  index = tokens.indexOf(stack.at(-1));
233
237
  // @ts-expect-error
234
238
  const { val, ...attr } = stack.at(-1);
235
- attr[LOC] = {
236
- ...stack.at(-1)[LOC],
237
- end: token[LOC].end,
238
- };
239
+ attr[LOCSRCID] = stack.at(-1)[LOCSRCID];
240
+ attr[LOCSTA] = stack.at(-1)[LOCSTA];
241
+ attr[LOCEND] = token[LOCEND];
239
242
  tokens.splice(i, 1);
240
243
  Object.assign(attr, {
241
244
  typ: EnumToken.AttrTokenType,
@@ -253,7 +256,7 @@ function parseSelector(tokens, context, options, errors) {
253
256
  if (stack.at(-1)?.typ == EnumToken.PseudoClassFunctionTokenDefType) {
254
257
  const func = stack.at(-1);
255
258
  index = tokens.indexOf(func);
256
- stack.at(-1)[LOC].end = token[LOC].end;
259
+ stack.at(-1)[LOCEND] = token[LOCEND];
257
260
  tokens.splice(i, 1);
258
261
  if (tokensfuncDefMap.has(func.typ)) {
259
262
  // @ts-expect-error
@@ -270,20 +273,77 @@ function parseSelector(tokens, context, options, errors) {
270
273
  const list = [];
271
274
  let index;
272
275
  for (index = 0; index < func.chi.length; index++) {
273
- if (func.chi[index].typ == EnumToken.CommentTokenType || func.chi[index].typ == EnumToken.WhitespaceTokenType) {
276
+ if (func.chi[index].typ == EnumToken.CommentTokenType ||
277
+ func.chi[index].typ == EnumToken.WhitespaceTokenType) {
274
278
  continue;
275
279
  }
276
- if (func.chi[index].typ == EnumToken.IdenTokenType && equalsIgnoreCase('of', func.chi[index].val)) {
280
+ if (func.chi[index].typ == EnumToken.IdenTokenType &&
281
+ equalsIgnoreCase("of", func.chi[index].val)) {
277
282
  index--;
278
283
  break;
279
284
  }
280
285
  list.push(func.chi[index]);
281
286
  }
287
+ if (list.length == 2) {
288
+ if (list[1].typ == EnumToken.NumberTokenType) {
289
+ if (list[1].val == 0) {
290
+ list.length = 1;
291
+ if (list[0].typ == EnumToken.DimensionTokenType &&
292
+ list[0].val == -2) {
293
+ list[0].val = 2;
294
+ }
295
+ }
296
+ else {
297
+ const sign = Math.sign(list[1].val);
298
+ // @ts-ignore
299
+ list[1].val *= sign;
300
+ list.splice(1, 0, {
301
+ typ: EnumToken.LiteralTokenType,
302
+ val: sign > 0 ? "+" : "-",
303
+ });
304
+ }
305
+ }
306
+ if (list.length == 3 &&
307
+ list[2].typ == EnumToken.NumberTokenType &&
308
+ list[0].typ == EnumToken.DimensionTokenType &&
309
+ (list[0].val == 2 ||
310
+ list[0].val == -2)) {
311
+ if (1 == list[2].val) {
312
+ list.splice(0, 3, {
313
+ typ: EnumToken.IdenTokenType,
314
+ val: "odd",
315
+ [LOCSRCID]: list[0][LOCSRCID],
316
+ [LOCSTA]: list[0][LOCSTA],
317
+ [LOCEND]: list[0][LOCEND],
318
+ });
319
+ }
320
+ else if (0 == list[2].val) {
321
+ list.splice(0, 3, {
322
+ typ: EnumToken.IdenTokenType,
323
+ val: "even",
324
+ [LOCSRCID]: list[0][LOCSRCID],
325
+ [LOCSTA]: list[0][LOCSTA],
326
+ [LOCEND]: list[0][LOCEND],
327
+ });
328
+ }
329
+ }
330
+ func.chi.splice(0, index, ...list);
331
+ }
332
+ if (list.length == 1) {
333
+ if (list[0].typ == EnumToken.IdenTokenType &&
334
+ equalsIgnoreCase("-n", list[0].val)) {
335
+ list[0].val = "n";
336
+ }
337
+ }
282
338
  if (list.length == 3) {
283
- if (list[0].typ == EnumToken.IdenTokenType && ('n' == list[0].val || '-n' == list[0].val || '+n' == list[0].val)) {
339
+ if (list[0].typ == EnumToken.IdenTokenType &&
340
+ ("n" == list[0].val ||
341
+ "-n" == list[0].val ||
342
+ "+n" == list[0].val)) {
284
343
  if (list[1].typ == EnumToken.NextSiblingCombinatorTokenType) {
285
- if (list[2].typ == EnumToken.NumberTokenType && (0 == list[2].val)) {
286
- list[0].val = 'n';
344
+ if (list[2].typ == EnumToken.NumberTokenType &&
345
+ 0 == list[2].val) {
346
+ list[0].val = "n";
287
347
  func.chi.splice(0, index, list[0]);
288
348
  break;
289
349
  }
@@ -303,83 +363,10 @@ function parseSelector(tokens, context, options, errors) {
303
363
  }
304
364
  }
305
365
  else {
306
- // if (!/\d+$/.test((token as IdentToken | LiteralToken).val)) {
307
- // let index = func.chi.indexOf(token);
308
- // let i: number = index + 1;
309
- // let sign: Token | null = null;
310
- // let num: NumberToken | null = null;
311
- // for (; i < func.chi.length; i++) {
312
- // if (
313
- // func.chi[i].typ == EnumToken.WhitespaceTokenType ||
314
- // func.chi[i].typ == EnumToken.CommentTokenType
315
- // ) {
316
- // continue;
317
- // }
318
- // if (func.chi[i].typ == EnumToken.NumberTokenType) {
319
- // num = func.chi[i] as NumberToken;
320
- // break;
321
- // } else {
322
- // sign = func.chi[i] as Token;
323
- // }
324
- // }
325
- // if (num != null) {
326
- // if (num.val === 0) {
327
- // func.chi.splice(index + 1, i - index);
328
- // if ((token as IdentToken | LiteralToken).val == "-n") {
329
- // (token as IdentToken).val = "n";
330
- // }
331
- // break;
332
- // }
333
- // if (sign == null) {
334
- // func.chi.splice(index + 1, i - index - 1);
335
- // if (Math.sign(num.val as number) === 1) {
336
- // func.chi.splice(index + 1, 0, {
337
- // typ: EnumToken.LiteralTokenType,
338
- // val: "+",
339
- // });
340
- // }
341
- // }
342
- // } else if ((token as IdentToken | LiteralToken).val == "-n") {
343
- // (token as IdentToken).val = "n";
344
- // }
345
- // break;
346
- // }
347
366
  const matches = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(token.val);
348
367
  if (matches != null) {
349
368
  const a1 = matches[2] === "" ? 1 : matches[2] === "-" ? -1 : +matches[2];
350
369
  const b1 = +matches[3];
351
- // if (a1 === 0) {
352
- // if (b1 === 1) {
353
- // let hasSelector: boolean = false;
354
- // let i: number = func.chi.indexOf(token);
355
- // let j: number = i + 1;
356
- // for (; j < func.chi.length; j++) {
357
- // if (
358
- // func.chi[j].typ == EnumToken.IdenTokenType &&
359
- // (func.chi[j] as IdentToken).val == "of"
360
- // ) {
361
- // hasSelector = true;
362
- // break;
363
- // }
364
- // }
365
- // if (hasSelector) {
366
- // Object.assign(token, {
367
- // typ: EnumToken.NumberTokenType,
368
- // val: b1,
369
- // });
370
- // } else {
371
- // // :first-child
372
- // tokens[tokens.indexOf(func)] = {
373
- // typ: EnumToken.PseudoClassTokenType,
374
- // val: ":first-child",
375
- // [LOC]: func[LOC],
376
- // };
377
- // }
378
- // break;
379
- // } else {
380
- // Object.assign(token, { typ: EnumToken.NumberTokenType, val: b1 });
381
- // }
382
- // } else
383
370
  if (b1 === 0) {
384
371
  Object.assign(token, Math.abs(a1) === 1
385
372
  ? {
@@ -392,17 +379,6 @@ function parseSelector(tokens, context, options, errors) {
392
379
  unit: "n",
393
380
  });
394
381
  }
395
- // else if (Math.abs(a1) === 2) {
396
- // if (b1 === 0) {
397
- // Object.assign(token, {
398
- // typ: EnumToken.DimensionTokenType,
399
- // val: a1,
400
- // unit: "n",
401
- // });
402
- // } else if (Math.abs(b1) === 1) {
403
- // Object.assign(token, { typ: EnumToken.IdenTokenType, val: "odd" });
404
- // }
405
- // }
406
382
  }
407
383
  }
408
384
  }
@@ -421,36 +397,6 @@ function parseSelector(tokens, context, options, errors) {
421
397
  }
422
398
  }
423
399
  if (num != null) {
424
- // if ((token as DimensionToken).val === 0) {
425
- // if (num.val === 0) {
426
- // func.chi.splice(0, i);
427
- // } else if (num.val === 1) {
428
- // let hasSelector: boolean = false;
429
- // let j: number = i + 1;
430
- // for (; j < func.chi.length; j++) {
431
- // if (
432
- // func.chi[j].typ == EnumToken.IdenTokenType &&
433
- // (func.chi[j] as IdentToken).val == "of"
434
- // ) {
435
- // hasSelector = true;
436
- // break;
437
- // }
438
- // }
439
- // if (hasSelector) {
440
- // func.chi.splice(0, i);
441
- // } else {
442
- // tokens[tokens.indexOf(func)] = {
443
- // typ: EnumToken.PseudoClassTokenType,
444
- // val: ":first-child",
445
- // [LOC]: func[LOC],
446
- // };
447
- // }
448
- // break;
449
- // } else {
450
- // func.chi.splice(0, i);
451
- // }
452
- // break;
453
- // } else
454
400
  if (num.val === 0) {
455
401
  func.chi.splice(index + 1, i - index);
456
402
  if (token.val < 0) {
@@ -539,10 +485,9 @@ function parseSelector(tokens, context, options, errors) {
539
485
  .keys(),
540
486
  ].join(","),
541
487
  chi: [],
542
- [LOC]: {
543
- ...tokens[0][LOC],
544
- end: tokens[tokens.length - 1][LOC].end,
545
- },
488
+ [LOCSRCID]: tokens[0][LOCSRCID],
489
+ [LOCSTA]: tokens[0][LOCSTA],
490
+ [LOCEND]: tokens[tokens.length - 1][LOCEND],
546
491
  [TOKENS]: tokens,
547
492
  [STATE]: result.success && allowed
548
493
  ? EnumAstNodeStatus.Validated
@@ -7,9 +7,11 @@ function camelize(value) {
7
7
  function equalsIgnoreCase(a, b) {
8
8
  if (a.length !== b.length)
9
9
  return false;
10
+ let ca;
11
+ let cb;
10
12
  for (let i = 0; i < a.length; i++) {
11
- let ca = a.charCodeAt(i);
12
- let cb = b.charCodeAt(i);
13
+ ca = a.charCodeAt(i);
14
+ cb = b.charCodeAt(i);
13
15
  // Normalize A-Z to a-z
14
16
  if (ca >= 65 && ca <= 90)
15
17
  ca += 32;