@tbela99/css-parser 1.4.3 → 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 (166) hide show
  1. package/CHANGELOG.md +192 -2
  2. package/README.md +69 -1120
  3. package/dist/config.json.js +43 -2
  4. package/dist/index-umd-web.js +17226 -15037
  5. package/dist/index.cjs +16889 -14705
  6. package/dist/index.d.ts +1804 -1684
  7. package/dist/lib/ast/clone.js +4 -3
  8. package/dist/lib/ast/expand.js +2 -2
  9. package/dist/lib/ast/features/calc.js +59 -54
  10. package/dist/lib/ast/features/if.js +22 -16
  11. package/dist/lib/ast/features/inlinecssvariables.js +2 -2
  12. package/dist/lib/ast/features/prefix.js +343 -6
  13. package/dist/lib/ast/features/transform.js +7 -4
  14. package/dist/lib/ast/find.js +5 -5
  15. package/dist/lib/ast/math/expression.js +12 -8
  16. package/dist/lib/ast/minify.js +28 -33
  17. package/dist/lib/ast/transform/minify.js +135 -131
  18. package/dist/lib/ast/transform/utils.js +3 -2
  19. package/dist/lib/ast/types.js +69 -8
  20. package/dist/lib/ast/walk.js +4 -5
  21. package/dist/lib/fs/resolve.js +71 -57
  22. package/dist/lib/parser/declaration/list.js +92 -3
  23. package/dist/lib/parser/declaration/map.js +5 -5
  24. package/dist/lib/parser/parse.js +528 -466
  25. package/dist/lib/parser/tokenize.js +110 -4
  26. package/dist/lib/parser/utils/angle.js +29 -0
  27. package/dist/lib/parser/utils/at-rule-container.js +2 -2
  28. package/dist/lib/parser/utils/at-rule-font-feature-values.js +2 -2
  29. package/dist/lib/parser/utils/at-rule-generic.js +1 -1
  30. package/dist/lib/parser/utils/at-rule-media.js +2 -2
  31. package/dist/lib/parser/utils/at-rule-support.js +73 -8
  32. package/dist/lib/parser/utils/at-rule-when-else.js +2 -2
  33. package/dist/lib/parser/utils/at-rule.js +2 -8
  34. package/dist/lib/parser/utils/declaration.js +179 -76
  35. package/dist/lib/parser/utils/selector.js +53 -11
  36. package/dist/lib/parser/utils/token.js +13 -11
  37. package/dist/lib/renderer/render.js +501 -59
  38. package/dist/lib/syntax/color/alpha.js +64 -0
  39. package/dist/lib/syntax/color/cmyk.js +2 -2
  40. package/dist/lib/syntax/color/color-mix.js +283 -220
  41. package/dist/lib/syntax/color/color.js +37 -28
  42. package/dist/lib/syntax/color/hex.js +2 -2
  43. package/dist/lib/syntax/color/hsl.js +3 -3
  44. package/dist/lib/syntax/color/hwb.js +7 -7
  45. package/dist/lib/syntax/color/lab.js +5 -5
  46. package/dist/lib/syntax/color/lch.js +4 -4
  47. package/dist/lib/syntax/color/oklab.js +3 -3
  48. package/dist/lib/syntax/color/oklch.js +3 -3
  49. package/dist/lib/syntax/color/relativecolor.js +97 -12
  50. package/dist/lib/syntax/color/srgb.js +5 -5
  51. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  52. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  53. package/dist/lib/syntax/color/utils/components.js +7 -3
  54. package/dist/lib/syntax/color/utils/distance.js +21 -4
  55. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  56. package/dist/lib/syntax/color/xyz.js +2 -2
  57. package/dist/lib/syntax/constants.js +29 -1
  58. package/dist/lib/syntax/syntax.js +307 -39
  59. package/dist/lib/validation/config.js +2 -2
  60. package/dist/lib/validation/config.json.js +32 -17
  61. package/dist/lib/validation/match.js +239 -104
  62. package/dist/lib/validation/parser/parse.js +0 -1
  63. package/dist/lib/validation/parser/typedef.js +6 -0
  64. package/dist/node.js +64 -22
  65. package/dist/web.js +65 -18
  66. package/package.json +26 -15
  67. package/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
  68. package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
  69. package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
  70. package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
  71. package/.repl_history +0 -4
  72. package/badges/coverage.svg +0 -20
  73. package/deno.lock +0 -2861
  74. package/dist/lib/ast/clone.d.ts +0 -10
  75. package/dist/lib/ast/expand.d.ts +0 -14
  76. package/dist/lib/ast/features/calc.d.ts +0 -10
  77. package/dist/lib/ast/features/if.d.ts +0 -10
  78. package/dist/lib/ast/features/index.d.ts +0 -6
  79. package/dist/lib/ast/features/inlinecssvariables.d.ts +0 -15
  80. package/dist/lib/ast/features/prefix.d.ts +0 -8
  81. package/dist/lib/ast/features/shorthand.d.ts +0 -12
  82. package/dist/lib/ast/features/transform.d.ts +0 -10
  83. package/dist/lib/ast/features/type.d.ts +0 -15
  84. package/dist/lib/ast/find.d.ts +0 -165
  85. package/dist/lib/ast/math/expression.d.ts +0 -18
  86. package/dist/lib/ast/math/math.d.ts +0 -6
  87. package/dist/lib/ast/minify.d.ts +0 -19
  88. package/dist/lib/ast/transform/compute.d.ts +0 -8
  89. package/dist/lib/ast/transform/matrix.d.ts +0 -22
  90. package/dist/lib/ast/transform/minify.d.ts +0 -5
  91. package/dist/lib/ast/transform/perspective.d.ts +0 -3
  92. package/dist/lib/ast/transform/rotate.d.ts +0 -12
  93. package/dist/lib/ast/transform/scale.d.ts +0 -6
  94. package/dist/lib/ast/transform/skew.d.ts +0 -4
  95. package/dist/lib/ast/transform/translate.d.ts +0 -6
  96. package/dist/lib/ast/transform/utils.d.ts +0 -9
  97. package/dist/lib/ast/types.d.ts +0 -903
  98. package/dist/lib/ast/walk.d.ts +0 -162
  99. package/dist/lib/fs/resolve.d.ts +0 -20
  100. package/dist/lib/parser/declaration/list.d.ts +0 -16
  101. package/dist/lib/parser/declaration/map.d.ts +0 -15
  102. package/dist/lib/parser/declaration/set.d.ts +0 -9
  103. package/dist/lib/parser/node.d.ts +0 -7
  104. package/dist/lib/parser/parse.d.ts +0 -107
  105. package/dist/lib/parser/tokenize.d.ts +0 -57
  106. package/dist/lib/parser/utils/at-rule-container.d.ts +0 -5
  107. package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +0 -5
  108. package/dist/lib/parser/utils/at-rule-generic.d.ts +0 -5
  109. package/dist/lib/parser/utils/at-rule-import.d.ts +0 -5
  110. package/dist/lib/parser/utils/at-rule-media.d.ts +0 -5
  111. package/dist/lib/parser/utils/at-rule-page.d.ts +0 -5
  112. package/dist/lib/parser/utils/at-rule-page.js +0 -28
  113. package/dist/lib/parser/utils/at-rule-support.d.ts +0 -5
  114. package/dist/lib/parser/utils/at-rule-token.d.ts +0 -1
  115. package/dist/lib/parser/utils/at-rule-when-else.d.ts +0 -5
  116. package/dist/lib/parser/utils/at-rule.d.ts +0 -13
  117. package/dist/lib/parser/utils/cache.d.ts +0 -6
  118. package/dist/lib/parser/utils/config.d.ts +0 -2
  119. package/dist/lib/parser/utils/declaration-list.d.ts +0 -5
  120. package/dist/lib/parser/utils/declaration.d.ts +0 -18
  121. package/dist/lib/parser/utils/eq.d.ts +0 -1
  122. package/dist/lib/parser/utils/hash.d.ts +0 -21
  123. package/dist/lib/parser/utils/selector.d.ts +0 -5
  124. package/dist/lib/parser/utils/text.d.ts +0 -3
  125. package/dist/lib/parser/utils/token.d.ts +0 -14
  126. package/dist/lib/parser/utils/type.d.ts +0 -2
  127. package/dist/lib/renderer/render.d.ts +0 -28
  128. package/dist/lib/renderer/sourcemap/lib/encode.d.ts +0 -1
  129. package/dist/lib/renderer/sourcemap/sourcemap.d.ts +0 -26
  130. package/dist/lib/syntax/color/a98rgb.d.ts +0 -2
  131. package/dist/lib/syntax/color/cmyk.d.ts +0 -10
  132. package/dist/lib/syntax/color/color-mix.d.ts +0 -2
  133. package/dist/lib/syntax/color/color.d.ts +0 -42
  134. package/dist/lib/syntax/color/hex.d.ts +0 -16
  135. package/dist/lib/syntax/color/hsl.d.ts +0 -20
  136. package/dist/lib/syntax/color/hsv.d.ts +0 -2
  137. package/dist/lib/syntax/color/hwb.d.ts +0 -21
  138. package/dist/lib/syntax/color/lab.d.ts +0 -25
  139. package/dist/lib/syntax/color/lch.d.ts +0 -23
  140. package/dist/lib/syntax/color/oklab.d.ts +0 -22
  141. package/dist/lib/syntax/color/oklch.d.ts +0 -20
  142. package/dist/lib/syntax/color/p3.d.ts +0 -6
  143. package/dist/lib/syntax/color/prophotorgb.d.ts +0 -2
  144. package/dist/lib/syntax/color/rec2020.d.ts +0 -2
  145. package/dist/lib/syntax/color/relativecolor.d.ts +0 -13
  146. package/dist/lib/syntax/color/rgb.d.ts +0 -20
  147. package/dist/lib/syntax/color/srgb.d.ts +0 -23
  148. package/dist/lib/syntax/color/utils/components.d.ts +0 -2
  149. package/dist/lib/syntax/color/utils/distance.d.ts +0 -18
  150. package/dist/lib/syntax/color/utils/matrix.d.ts +0 -6
  151. package/dist/lib/syntax/color/xyz.d.ts +0 -5
  152. package/dist/lib/syntax/color/xyzd50.d.ts +0 -4
  153. package/dist/lib/syntax/constants.d.ts +0 -67
  154. package/dist/lib/syntax/syntax.d.ts +0 -38
  155. package/dist/lib/validation/config.d.ts +0 -14
  156. package/dist/lib/validation/json.d.ts +0 -2
  157. package/dist/lib/validation/match.d.ts +0 -38
  158. package/dist/lib/validation/parser/parse.d.ts +0 -8
  159. package/dist/lib/validation/parser/typedef.d.ts +0 -95
  160. package/dist/lib/validation/utils/list.d.ts +0 -4
  161. package/dist/lib/validation/utils/whitespace.d.ts +0 -2
  162. package/dist/node.d.ts +0 -207
  163. package/dist/web.d.ts +0 -169
  164. package/playground/index.html +0 -1328
  165. package/playground/sw.js +0 -55
  166. package/playground/tree.js +0 -176
@@ -1,28 +1,25 @@
1
- import { isIdentColor, isIdentStart, isIdent, isColor, parseColor } from '../syntax/syntax.js';
1
+ import { isIdentColor, parseColor, isColor } from '../syntax/syntax.js';
2
2
  import { camelize, equalsIgnoreCase, dasherize } from './utils/text.js';
3
- import { renderToken } from '../renderer/render.js';
4
- import { EnumToken, ValidationLevel, ModuleCaseTransformEnum, ModuleScopeEnumOptions } from '../ast/types.js';
3
+ import { renderValue } from '../renderer/render.js';
4
+ import { EnumToken, EnumAstNodeStatus, ModuleCaseTransformEnum, ModuleScopeEnumOptions } from '../ast/types.js';
5
5
  import { minify } from '../ast/minify.js';
6
6
  import { expand } from '../ast/expand.js';
7
7
  import { WalkerEvent, walk, walkValues } from '../ast/walk.js';
8
- import { tokenize, tokenizeStream } from './tokenize.js';
9
- import { definedPropertySettings, tokensfuncDefMap, funcLike, mathFuncs, urlTokenMatcher, pageMarginBoxType } from '../syntax/constants.js';
10
- import { splitTokenList } from '../validation/utils/list.js';
11
- import { buildExpression } from '../ast/math/expression.js';
8
+ import { tokenizeStream, tokenize } from './tokenize.js';
9
+ import { definedPropertySettings, tokensfuncDefMap, pageMarginBoxType } from '../syntax/constants.js';
12
10
  import { hashAlgorithms, hash } from './utils/hash.js';
13
11
  import { parseSelector } from './utils/selector.js';
14
12
  import { parseDeclaration } from './utils/declaration.js';
15
13
  import { getSyntaxRule } from '../validation/config.js';
16
- import { matchSelectorSyntax, trimArray, matchAllSyntax, createValidationContext } from '../validation/match.js';
14
+ import { matchSelectorSyntax, trimArray, matchAllSyntaxes, createValidationContext } from '../validation/match.js';
17
15
  import { ValidationSyntaxGroupEnum } from '../validation/parser/typedef.js';
18
16
  import { matchAtRuleImportSyntax } from './utils/at-rule-import.js';
19
17
  import { matchAtRuleWhenElseSyntax } from './utils/at-rule-when-else.js';
20
18
  import { parseAtRuleSupportSyntax } from './utils/at-rule-support.js';
21
- import { replaceToken, trimWhiteSpaceTokens } from './utils/token.js';
19
+ import { replaceNodeOrValue, trimWhiteSpaceTokens } from './utils/token.js';
22
20
  import { parseAtRuleContainerQueryList } from './utils/at-rule-container.js';
23
21
  import { parseMediaqueryList } from './utils/at-rule-media.js';
24
22
  import { matchAtRuleSyntax } from './utils/at-rule.js';
25
- import { parseAtRulePage } from './utils/at-rule-page.js';
26
23
  import { parseAtRuleFontFeatureValues } from './utils/at-rule-font-feature-values.js';
27
24
  import { matchGenericSyntax } from './utils/at-rule-generic.js';
28
25
  import { memoize } from './utils/cache.js';
@@ -31,8 +28,8 @@ function renderTokens(tokens, options) {
31
28
  if (tokens == null || tokens.length === 0)
32
29
  return "";
33
30
  if (options != null)
34
- return tokens.map((t) => renderToken(t, options)).join("");
35
- return tokens.map((t) => renderToken(t)).join("");
31
+ return tokens.map((t) => renderValue(t, options)).join("");
32
+ return tokens.map((t) => renderValue(t)).join("");
36
33
  }
37
34
  const trimWhiteSpace = [
38
35
  EnumToken.CommentTokenType,
@@ -52,7 +49,7 @@ let keyNameCounter = 0;
52
49
  let keyNameCache = {};
53
50
  const forbiddenStartCharacters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].map((c) => c.charCodeAt(0));
54
51
  /**
55
- * short-scoped name generator.
52
+ * Short-scoped name generator.
56
53
  *
57
54
  * @param localName
58
55
  * @param filePath
@@ -78,7 +75,7 @@ function reject(reason) {
78
75
  throw new Error(reason ?? "Parsing aborted");
79
76
  }
80
77
  /**
81
- * transform case of key name
78
+ * Transform case of key name
82
79
  * @param key
83
80
  * @param how
84
81
  *
@@ -97,7 +94,7 @@ const getKeyName = memoize((key, how) => {
97
94
  return key;
98
95
  });
99
96
  /**
100
- * generate scoped name
97
+ * Generate scoped name
101
98
  * @param localName
102
99
  * @param filePath
103
100
  * @param pattern
@@ -201,7 +198,7 @@ const generateScopedName = memoize(async (localName, filePath, pattern, hashLeng
201
198
  return (/^[0-9]/.test(result) ? "_" : "") + result;
202
199
  });
203
200
  /**
204
- * parse css string
201
+ * Parse css string
205
202
  * @param iter
206
203
  * @param options
207
204
  *
@@ -231,12 +228,12 @@ async function doParse(iter, options = {}) {
231
228
  inlineCssVariables: false,
232
229
  setParent: true,
233
230
  removePrefix: false,
234
- validation: ValidationLevel.Default,
231
+ validation: false,
235
232
  lenient: true,
236
233
  ...options,
237
234
  };
238
- if (typeof options.validation == "boolean") {
239
- options.validation = options.validation ? ValidationLevel.All : ValidationLevel.None;
235
+ if (typeof options.validation !== "boolean") {
236
+ options.validation = !!options.validation;
240
237
  }
241
238
  if (options.module) {
242
239
  options.expandNestingRules = true;
@@ -263,6 +260,7 @@ async function doParse(iter, options = {}) {
263
260
  total: `0ms`,
264
261
  imports: [],
265
262
  };
263
+ const invalidNodes = [];
266
264
  let ast = {
267
265
  typ: EnumToken.StyleSheetNodeType,
268
266
  chi: [],
@@ -438,7 +436,7 @@ async function doParse(iter, options = {}) {
438
436
  (item.token.typ === EnumToken.SemiColonTokenType ||
439
437
  item.token.typ === EnumToken.BlockStartTokenType)) ||
440
438
  item.token.typ === EnumToken.EOFTokenType) {
441
- node = parseNode(tokens, context, options, errors, stats);
439
+ node = parseNode(tokens, context, options, errors, stats, invalidNodes);
442
440
  if (node != null) {
443
441
  if ("chi" in node) {
444
442
  stack.push(node);
@@ -483,7 +481,7 @@ async function doParse(iter, options = {}) {
483
481
  tokens = [];
484
482
  }
485
483
  else if (item.token.typ === EnumToken.BlockEndTokenType) {
486
- parseNode(tokens, context, options, errors, stats);
484
+ parseNode(tokens, context, options, errors, stats, invalidNodes);
487
485
  if (context.loc != null) {
488
486
  context.loc.end = item.token.loc.end;
489
487
  }
@@ -499,7 +497,7 @@ async function doParse(iter, options = {}) {
499
497
  }
500
498
  }
501
499
  if (tokens.length > 0) {
502
- node = parseNode(tokens, context, options, errors, stats);
500
+ node = parseNode(tokens, context, options, errors, stats, invalidNodes);
503
501
  if (node != null) {
504
502
  if (node.typ == EnumToken.AtRuleNodeType && "import" === node.val) {
505
503
  imports.push(node);
@@ -512,10 +510,13 @@ async function doParse(iter, options = {}) {
512
510
  }
513
511
  if (imports.length > 0 && options.resolveImport) {
514
512
  await Promise.all(imports.map(async (node) => {
513
+ if (node.state !== EnumAstNodeStatus.Validated) {
514
+ return;
515
+ }
515
516
  const token = node.tokens[0];
516
517
  const url = token.typ == EnumToken.StringTokenType ? token.val.slice(1, -1) : token.val;
517
518
  try {
518
- const result = options.load(url, options.src);
519
+ const result = options.load(url, options.src || options.cwd);
519
520
  const stream = result instanceof Promise || Object.getPrototypeOf(result).constructor.name == "AsyncFunction"
520
521
  ? await result
521
522
  : result;
@@ -546,31 +547,6 @@ async function doParse(iter, options = {}) {
546
547
  }
547
548
  }));
548
549
  }
549
- while (stack.length > 0 && context != ast) {
550
- const previousNode = stack.pop();
551
- context = (stack[stack.length - 1] ?? ast);
552
- // remove empty nodes
553
- if (options.removeEmpty &&
554
- previousNode != null &&
555
- previousNode.chi.length == 0 &&
556
- context.chi[context.chi.length - 1] == previousNode) {
557
- context.chi.pop();
558
- continue;
559
- }
560
- // remove invalid nodes
561
- if (!options.lenient &&
562
- previousNode?.parent != null &&
563
- // @ts-expect-error
564
- (previousNode.typ == EnumToken.InvalidRuleNodeType || previousNode.typ == EnumToken.InvalidAtRuleNodeType)) {
565
- for (let i = context.chi.length - 1; i >= 0; i--) {
566
- if (context.chi[i] == previousNode) {
567
- context.chi.splice(i, 1);
568
- break;
569
- }
570
- }
571
- }
572
- break;
573
- }
574
550
  const endParseTime = performance.now();
575
551
  if (options.expandNestingRules) {
576
552
  ast = expand(ast);
@@ -655,7 +631,7 @@ async function doParse(iter, options = {}) {
655
631
  }
656
632
  }
657
633
  if (node != result.node) {
658
- replaceToken(result.parent, result.node, node);
634
+ replaceNodeOrValue(result.parent, result.node, node);
659
635
  }
660
636
  }
661
637
  else if ((result.node.typ == EnumToken.RuleNodeType &&
@@ -710,7 +686,7 @@ async function doParse(iter, options = {}) {
710
686
  // @ts-ignore
711
687
  if (node != result.node) {
712
688
  // @ts-ignore
713
- replaceToken(result.parent, result.node, node);
689
+ replaceNodeOrValue(result.parent, result.node, node);
714
690
  }
715
691
  }
716
692
  else if (valuesHandlers.size > 0) {
@@ -744,7 +720,7 @@ async function doParse(iter, options = {}) {
744
720
  }
745
721
  if (node != result.node) {
746
722
  // @ts-ignore
747
- replaceToken(result.parent, value, node);
723
+ replaceNodeOrValue(result.parent, value, node);
748
724
  }
749
725
  const tokens = Array.isArray(result.node.tokens) ? result.node.tokens : [];
750
726
  if (Array.isArray(result.node.val)) {
@@ -786,13 +762,50 @@ async function doParse(iter, options = {}) {
786
762
  }
787
763
  if (node != value) {
788
764
  // @ts-ignore
789
- replaceToken(parent, value, node);
765
+ replaceNodeOrValue(parent, value, node);
790
766
  }
791
767
  }
792
768
  }
793
769
  }
794
770
  }
795
771
  }
772
+ if (invalidNodes.length > 0) {
773
+ let k = invalidNodes.length;
774
+ while (k-- > 0) {
775
+ if (invalidNodes[k].state == EnumAstNodeStatus.Validated || invalidNodes[k].state == EnumAstNodeStatus.Unvalidated || invalidNodes[k].state == EnumAstNodeStatus.ValidationFailed) {
776
+ continue;
777
+ }
778
+ if (options.lenient && invalidNodes[k].state == EnumAstNodeStatus.Unknown) {
779
+ continue;
780
+ }
781
+ invalidNodes[k].parent.chi.splice(invalidNodes[k].parent.chi.indexOf(invalidNodes[k]), 1);
782
+ }
783
+ }
784
+ while (stack.length > 0 && context != ast) {
785
+ const previousNode = stack.pop();
786
+ context = (stack[stack.length - 1] ?? ast);
787
+ // remove empty nodes
788
+ if (options.removeEmpty &&
789
+ previousNode != null &&
790
+ previousNode.chi.length == 0 &&
791
+ context.chi[context.chi.length - 1] == previousNode) {
792
+ context.chi.pop();
793
+ continue;
794
+ }
795
+ // remove invalid nodes
796
+ if (!options.lenient &&
797
+ previousNode?.parent != null &&
798
+ // @ts-expect-error
799
+ (previousNode.typ == EnumToken.InvalidRuleNodeType || previousNode.typ == EnumToken.InvalidAtRuleNodeType)) {
800
+ for (let i = context.chi.length - 1; i >= 0; i--) {
801
+ if (context.chi[i] == previousNode) {
802
+ context.chi.splice(i, 1);
803
+ break;
804
+ }
805
+ }
806
+ }
807
+ break;
808
+ }
796
809
  if (options.minify) {
797
810
  if (ast.chi.length > 0) {
798
811
  let passes = options.pass ?? 1;
@@ -885,7 +898,7 @@ async function doParse(iter, options = {}) {
885
898
  if (node.typ == EnumToken.CssVariableImportTokenType) {
886
899
  const url = node.val.find((t) => t.typ == EnumToken.StringTokenType).val.slice(1, -1);
887
900
  const src = options.resolve(url, options.dirname(options.src), options.cwd);
888
- const result = options.load(url, options.src);
901
+ const result = options.load(src, "");
889
902
  const stream = result instanceof Promise || Object.getPrototypeOf(result).constructor.name == "AsyncFunction"
890
903
  ? await result
891
904
  : result;
@@ -1027,7 +1040,7 @@ async function doParse(iter, options = {}) {
1027
1040
  else if (token.r.typ == EnumToken.String) {
1028
1041
  const url = token.r.val.slice(1, -1);
1029
1042
  const src = options.resolve(url, options.dirname(options.src), options.cwd);
1030
- const result = options.load(url, options.src);
1043
+ const result = options.load(src, "");
1031
1044
  const stream = result instanceof Promise ||
1032
1045
  Object.getPrototypeOf(result).constructor.name == "AsyncFunction"
1033
1046
  ? await result
@@ -1170,9 +1183,7 @@ async function doParse(iter, options = {}) {
1170
1183
  else if (node.nam == "grid-template-areas" || node.nam == "grid-template") {
1171
1184
  for (let i = 0; i < node.val.length; i++) {
1172
1185
  if (node.val[i].typ == EnumToken.String) {
1173
- const tokens = parseString(node.val[i].val.slice(1, -1), {
1174
- location: true,
1175
- });
1186
+ const tokens = parseString(node.val[i].val.slice(1, -1));
1176
1187
  for (const { value } of walkValues(tokens)) {
1177
1188
  if (value.typ == EnumToken.IdenTokenType ||
1178
1189
  value.typ == EnumToken.DashedIdenTokenType) {
@@ -1261,13 +1272,13 @@ async function doParse(iter, options = {}) {
1261
1272
  }
1262
1273
  else if ((value.typ == EnumToken.IdenTokenType || isIdentColor(value)) &&
1263
1274
  value.val in importedCssVariables) {
1264
- replaceToken(parent, value, importedCssVariables[value.val].val);
1275
+ replaceNodeOrValue(parent, value, importedCssVariables[value.val].val);
1265
1276
  }
1266
1277
  }
1267
1278
  }
1268
1279
  else if (node.typ == EnumToken.RuleNodeType) {
1269
1280
  if (node.tokens == null) {
1270
- const tokens = parseString(node.sel, { location: true });
1281
+ const tokens = parseString(node.sel);
1271
1282
  matchSelectorSyntax(tokens, [], options);
1272
1283
  Object.defineProperty(node, "tokens", {
1273
1284
  ...definedPropertySettings,
@@ -1377,7 +1388,7 @@ async function doParse(iter, options = {}) {
1377
1388
  }
1378
1389
  node.sel = "";
1379
1390
  for (const token of node.tokens) {
1380
- node.sel += renderToken(token);
1391
+ node.sel += renderValue(token);
1381
1392
  }
1382
1393
  }
1383
1394
  else if (node.typ == EnumToken.AtRuleNodeType || node.typ == EnumToken.KeyframesAtRuleNodeType) {
@@ -1454,7 +1465,7 @@ async function doParse(iter, options = {}) {
1454
1465
  }
1455
1466
  return result;
1456
1467
  }
1457
- function parseNode(tokens, context, options, errors, stats) {
1468
+ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
1458
1469
  let i = 0;
1459
1470
  if (tokens.at(-1)?.typ === EnumToken.EOFTokenType) {
1460
1471
  tokens.pop();
@@ -1471,6 +1482,13 @@ function parseNode(tokens, context, options, errors, stats) {
1471
1482
  }
1472
1483
  }
1473
1484
  if (matchCount > 0) {
1485
+ let k = tokens.length;
1486
+ while (k-- > 0 &&
1487
+ (tokens[k].typ === EnumToken.WhitespaceTokenType || tokens[k].typ === EnumToken.CommentTokenType))
1488
+ ;
1489
+ if (tokens[k]?.typ == EnumToken.SemiColonTokenType) {
1490
+ matchCount = 0;
1491
+ }
1474
1492
  while (matchCount > 0) {
1475
1493
  position = {
1476
1494
  ...position,
@@ -1508,6 +1526,7 @@ function parseNode(tokens, context, options, errors, stats) {
1508
1526
  }
1509
1527
  if (i > 0) {
1510
1528
  context.chi.push(...tokens.splice(0, i).filter((n) => n.typ !== EnumToken.WhitespaceTokenType));
1529
+ i = 0;
1511
1530
  }
1512
1531
  for (; i < tokens.length; i++) {
1513
1532
  if (tokens[i].typ == EnumToken.CommentTokenType || tokens[i].typ == EnumToken.CDOCOMMTokenType) {
@@ -1556,6 +1575,13 @@ function parseNode(tokens, context, options, errors, stats) {
1556
1575
  if (node == null) {
1557
1576
  return null;
1558
1577
  }
1578
+ if (node.state == EnumAstNodeStatus.Invalid ||
1579
+ node.state == EnumAstNodeStatus.Disallowed ||
1580
+ node.state == EnumAstNodeStatus.Unknown ||
1581
+ node.state == EnumAstNodeStatus.Unparsed ||
1582
+ node.state == EnumAstNodeStatus.Malformed) {
1583
+ invalidNodes.push(node);
1584
+ }
1559
1585
  stats.nodesCount++;
1560
1586
  context.chi.push(node);
1561
1587
  // @ts-expect-error
@@ -1568,14 +1594,20 @@ function parseNode(tokens, context, options, errors, stats) {
1568
1594
  const node = parseSelector(tokens, context, options, errors);
1569
1595
  context.chi.push(node);
1570
1596
  Object.defineProperty(node, "parent", { ...definedPropertySettings, value: context });
1597
+ if (node.state == EnumAstNodeStatus.Invalid ||
1598
+ node.state == EnumAstNodeStatus.Disallowed ||
1599
+ node.state == EnumAstNodeStatus.Unknown ||
1600
+ node.state == EnumAstNodeStatus.Unparsed ||
1601
+ node.state == EnumAstNodeStatus.Malformed) {
1602
+ invalidNodes.push(node);
1603
+ }
1571
1604
  return node;
1572
1605
  }
1573
1606
  else {
1574
1607
  const node = parseDeclaration(tokens, context, options, errors);
1575
1608
  Object.defineProperty(node, "parent", { ...definedPropertySettings, value: context });
1576
1609
  if (context.typ === EnumToken.StyleSheetNodeType && node.typ === EnumToken.DeclarationNodeType) {
1577
- // @ts-expect-error
1578
- node.typ = EnumToken.InvalidDeclarationNodeType;
1610
+ node.state = EnumAstNodeStatus.Invalid;
1579
1611
  errors.push({
1580
1612
  message: "<declaration> not allowed in <stylesheet>",
1581
1613
  action: "drop",
@@ -1586,17 +1618,23 @@ function parseNode(tokens, context, options, errors, stats) {
1586
1618
  else if (options.lenient || node.typ === EnumToken.DeclarationNodeType) {
1587
1619
  context.chi.push(node);
1588
1620
  }
1621
+ if (node.state == EnumAstNodeStatus.Invalid ||
1622
+ node.state == EnumAstNodeStatus.Disallowed ||
1623
+ node.state == EnumAstNodeStatus.Unknown ||
1624
+ node.state == EnumAstNodeStatus.Unparsed ||
1625
+ node.state == EnumAstNodeStatus.Malformed) {
1626
+ invalidNodes.push(node);
1627
+ }
1589
1628
  }
1590
1629
  }
1591
1630
  return null;
1592
1631
  }
1593
- /**mjgvgyikjkml,kmbm b8790u89y70
1594
- vbbnkit;;;jmjhyg77 * @param options
1632
+ /**
1633
+ * @param options
1595
1634
  * @param errors
1596
1635
  * @param parseAsBlock
1597
1636
  */
1598
1637
  function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1599
- // const rules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + (stream[0] as AtRuleToken).nam);
1600
1638
  let success = true;
1601
1639
  let atRuleName = stream[0].nam;
1602
1640
  if (atRuleName.startsWith("-")) {
@@ -1607,21 +1645,30 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1607
1645
  const syntax = syntaxRules?.getPreludeRules()?.slice?.(1);
1608
1646
  const blockAllowed = syntaxRules?.getBlockRules() != null;
1609
1647
  if (syntaxRules == null) {
1610
- if (!options.lenient) {
1611
- errors.push({
1612
- action: "drop",
1613
- node: atRule,
1614
- location: atRule.loc,
1615
- message: "unknown at-rule",
1616
- });
1617
- // @ts-expect-error
1618
- return {
1619
- ...atRule,
1620
- typ: EnumToken.InvalidRuleNodeType,
1621
- val: renderTokens(trimArray(stream), options),
1622
- ...(parseAsBlock ? { chi: [] } : {}),
1623
- };
1624
- }
1648
+ // if (!options.lenient) {
1649
+ errors.push({
1650
+ action: "drop",
1651
+ node: atRule,
1652
+ location: atRule.loc,
1653
+ message: "unknown at-rule",
1654
+ });
1655
+ const result = matchGenericSyntax(stream);
1656
+ // @ts-expect-error
1657
+ return Object.defineProperties(Object.assign(atRule, {
1658
+ typ: EnumToken.AtRuleNodeType,
1659
+ val: renderTokens(trimArray(stream), options),
1660
+ ...(parseAsBlock ? { chi: [] } : {}),
1661
+ }), {
1662
+ state: {
1663
+ ...definedPropertySettings,
1664
+ value: result.success ? EnumAstNodeStatus.Unknown : EnumAstNodeStatus.Invalid,
1665
+ },
1666
+ errors: {
1667
+ ...definedPropertySettings,
1668
+ value: result.success ? [errors[errors.length - 1]] : [errors[errors.length - 1], ...result.errors],
1669
+ },
1670
+ });
1671
+ // }
1625
1672
  }
1626
1673
  else if (context.typ === EnumToken.AtRuleNodeType &&
1627
1674
  "page" === context.nam &&
@@ -1634,12 +1681,20 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1634
1681
  message: parseAsBlock ? "at-rule block not supported" : "at-rule block is required",
1635
1682
  });
1636
1683
  // @ts-expect-error
1637
- return {
1638
- ...atRule,
1639
- typ: EnumToken.InvalidRuleNodeType,
1684
+ return Object.defineProperties(Object.assign(atRule, {
1685
+ typ: EnumToken.AtRuleNodeType,
1640
1686
  val: renderTokens(trimArray(stream), options),
1641
1687
  ...(parseAsBlock ? { chi: [] } : {}),
1642
- };
1688
+ }), {
1689
+ state: {
1690
+ ...definedPropertySettings,
1691
+ value: EnumAstNodeStatus.Invalid,
1692
+ },
1693
+ errors: {
1694
+ ...definedPropertySettings,
1695
+ value: [errors[errors.length - 1]],
1696
+ },
1697
+ });
1643
1698
  }
1644
1699
  const token = stream.find((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ === EnumToken.CommentTokenType) ?? null;
1645
1700
  if (token != null) {
@@ -1650,12 +1705,20 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1650
1705
  message: `unexpected token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
1651
1706
  });
1652
1707
  // @ts-expect-error
1653
- return {
1654
- ...atRule,
1655
- typ: EnumToken.InvalidRuleNodeType,
1708
+ return Object.defineProperties(Object.assign(atRule, {
1709
+ typ: EnumToken.AtRuleNodeType,
1656
1710
  val: renderTokens(trimArray(stream), options),
1657
1711
  ...(parseAsBlock ? { chi: [] } : {}),
1658
- };
1712
+ }), {
1713
+ state: {
1714
+ ...definedPropertySettings,
1715
+ value: EnumAstNodeStatus.Invalid,
1716
+ },
1717
+ errors: {
1718
+ ...definedPropertySettings,
1719
+ value: [errors[errors.length - 1]],
1720
+ },
1721
+ });
1659
1722
  }
1660
1723
  }
1661
1724
  if (parseAsBlock === null) {
@@ -1670,12 +1733,20 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1670
1733
  message: parseAsBlock ? "at-rule block not supported" : "at-rule block is required",
1671
1734
  });
1672
1735
  // @ts-expect-error
1673
- return {
1674
- ...atRule,
1675
- typ: EnumToken.InvalidRuleNodeType,
1736
+ return Object.defineProperties(Object.assign(atRule, {
1737
+ typ: EnumToken.AtRuleNodeType,
1676
1738
  val: renderTokens(trimArray(stream), options),
1677
1739
  ...(parseAsBlock ? { chi: [] } : {}),
1678
- };
1740
+ }), {
1741
+ state: {
1742
+ ...definedPropertySettings,
1743
+ value: EnumAstNodeStatus.Invalid,
1744
+ },
1745
+ errors: {
1746
+ ...definedPropertySettings,
1747
+ value: [errors[errors.length - 1]],
1748
+ },
1749
+ });
1679
1750
  }
1680
1751
  switch (atRuleName) {
1681
1752
  case "charset": {
@@ -1709,7 +1780,28 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1709
1780
  message: "expecting double-quoted string",
1710
1781
  });
1711
1782
  }
1712
- if (success && options.removeCharset) {
1783
+ if (!success) {
1784
+ // @ts-expect-error
1785
+ return Object.defineProperties(Object.assign(atRule, {
1786
+ typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1787
+ val: renderTokens(trimArray(stream), options),
1788
+ }), {
1789
+ state: {
1790
+ ...definedPropertySettings,
1791
+ value: EnumAstNodeStatus.Invalid,
1792
+ },
1793
+ errors: {
1794
+ ...definedPropertySettings,
1795
+ value: [errors[errors.length - 1]],
1796
+ },
1797
+ loc: {
1798
+ ...definedPropertySettings,
1799
+ value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
1800
+ },
1801
+ tokens: { ...definedPropertySettings, value: stream },
1802
+ });
1803
+ }
1804
+ if (options.removeCharset) {
1713
1805
  return null;
1714
1806
  }
1715
1807
  // @ts-expect-error
@@ -1717,6 +1809,14 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1717
1809
  typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1718
1810
  val: renderTokens(trimArray(stream), options),
1719
1811
  }), {
1812
+ state: {
1813
+ ...definedPropertySettings,
1814
+ value: EnumAstNodeStatus.Validated,
1815
+ },
1816
+ errors: {
1817
+ ...definedPropertySettings,
1818
+ value: [],
1819
+ },
1720
1820
  loc: {
1721
1821
  ...definedPropertySettings,
1722
1822
  value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
@@ -1731,10 +1831,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1731
1831
  }
1732
1832
  // @ts-expect-error
1733
1833
  return Object.defineProperties(Object.assign(atRule, {
1734
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1834
+ typ: EnumToken.AtRuleNodeType,
1735
1835
  val: renderTokens(trimWhiteSpaceTokens(stream), options),
1736
1836
  chi: [],
1737
1837
  }), {
1838
+ state: {
1839
+ ...definedPropertySettings,
1840
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
1841
+ },
1842
+ errors: {
1843
+ ...definedPropertySettings,
1844
+ value: result.success ? [] : result.errors,
1845
+ },
1738
1846
  loc: {
1739
1847
  ...definedPropertySettings,
1740
1848
  value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
@@ -1772,10 +1880,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1772
1880
  }
1773
1881
  // @ts-expect-error
1774
1882
  return Object.defineProperties(Object.assign(atRule, {
1775
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1883
+ typ: EnumToken.AtRuleNodeType,
1776
1884
  val: renderTokens(trimWhiteSpaceTokens(stream), options),
1777
1885
  chi: [],
1778
1886
  }), {
1887
+ state: {
1888
+ ...definedPropertySettings,
1889
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
1890
+ },
1891
+ errors: {
1892
+ ...definedPropertySettings,
1893
+ value: [errors[errors.length - 1]],
1894
+ },
1779
1895
  loc: {
1780
1896
  ...definedPropertySettings,
1781
1897
  value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
@@ -1790,10 +1906,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1790
1906
  }
1791
1907
  // @ts-expect-error
1792
1908
  return Object.defineProperties(Object.assign(atRule, {
1793
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1909
+ typ: EnumToken.AtRuleNodeType,
1794
1910
  val: renderTokens(trimWhiteSpaceTokens(stream), options),
1795
1911
  chi: [],
1796
1912
  }), {
1913
+ state: {
1914
+ ...definedPropertySettings,
1915
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
1916
+ },
1917
+ errors: {
1918
+ ...definedPropertySettings,
1919
+ value: result.success ? [] : result.errors,
1920
+ },
1797
1921
  loc: {
1798
1922
  ...definedPropertySettings,
1799
1923
  value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
@@ -1803,7 +1927,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1803
1927
  }
1804
1928
  case "custom-media": {
1805
1929
  const tokens = trimArray(stream.slice(1));
1806
- const result = matchAllSyntax(syntax, createValidationContext(tokens), options);
1930
+ const result = matchAllSyntaxes(syntax, createValidationContext(tokens), options);
1807
1931
  if (result.errors.length > 0) {
1808
1932
  errors.push(...result.errors);
1809
1933
  }
@@ -1811,9 +1935,17 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1811
1935
  options = { ...options, convertColor: false };
1812
1936
  // @ts-expect-error
1813
1937
  return Object.defineProperties(Object.assign(atRule, {
1814
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1938
+ typ: EnumToken.AtRuleNodeType,
1815
1939
  val: renderTokens(trimWhiteSpaceTokens(tokens), options),
1816
1940
  }), {
1941
+ state: {
1942
+ ...definedPropertySettings,
1943
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.ValidationFailed,
1944
+ },
1945
+ errors: {
1946
+ ...definedPropertySettings,
1947
+ value: result.success ? [] : result.errors,
1948
+ },
1817
1949
  loc: {
1818
1950
  ...definedPropertySettings,
1819
1951
  value: { ...atRule.loc, end: (tokens.at(-1) ?? atRule).loc.end },
@@ -1840,10 +1972,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1840
1972
  options = { ...options, convertColor: false };
1841
1973
  // @ts-expect-error
1842
1974
  return Object.defineProperties(Object.assign(atRule, {
1843
- typ: success ? EnumToken.KeyframesAtRuleNodeType : EnumToken.InvalidRuleNodeType,
1975
+ typ: EnumToken.KeyframesAtRuleNodeType,
1844
1976
  val: renderTokens(tokens, options),
1845
1977
  chi: [],
1846
1978
  }), {
1979
+ state: {
1980
+ ...definedPropertySettings,
1981
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
1982
+ },
1983
+ errors: {
1984
+ ...definedPropertySettings,
1985
+ value: success ? [] : errors[errors.length - 1],
1986
+ },
1847
1987
  loc: {
1848
1988
  ...definedPropertySettings,
1849
1989
  value: { ...atRule.loc, end: (tokens.at(-1) ?? atRule).loc.end },
@@ -1852,10 +1992,13 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1852
1992
  });
1853
1993
  }
1854
1994
  case "namespace": {
1855
- const result = matchAllSyntax(syntax, createValidationContext(stream), options);
1995
+ const result = matchAllSyntaxes(syntax, createValidationContext(stream), options);
1856
1996
  if (!result.success) {
1857
1997
  errors.push(...result.errors);
1858
1998
  }
1999
+ // else {
2000
+ // parseUrlToken(stream);
2001
+ // }
1859
2002
  const valid = blockAllowed === parseAsBlock && result.success;
1860
2003
  if (valid) {
1861
2004
  let start = 0;
@@ -1884,16 +2027,24 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1884
2027
  options = { ...options, convertColor: false };
1885
2028
  // @ts-expect-error
1886
2029
  return Object.defineProperties(Object.assign(atRule, {
1887
- typ: valid ? EnumToken.AtRuleNodeType : EnumToken.InvalidAtRuleNodeType,
2030
+ typ: EnumToken.AtRuleNodeType,
1888
2031
  val: trimArray(stream).reduce((acc, t, index) => acc +
1889
2032
  (t.typ === EnumToken.CommentTokenType ||
1890
2033
  (t.typ === EnumToken.WhitespaceTokenType &&
1891
2034
  stream[index + 1]?.typ === EnumToken.CommentTokenType &&
1892
2035
  (stream.length < index + 3 || stream[index + 2]?.typ === EnumToken.WhitespaceTokenType))
1893
2036
  ? ""
1894
- : renderToken(t, options)), ""),
2037
+ : renderValue(t, options)), ""),
1895
2038
  ...(parseAsBlock ? { chi: [] } : {}),
1896
2039
  }), {
2040
+ state: {
2041
+ ...definedPropertySettings,
2042
+ value: valid ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2043
+ },
2044
+ errors: {
2045
+ ...definedPropertySettings,
2046
+ value: valid ? [] : result.errors,
2047
+ },
1897
2048
  tokens: { ...definedPropertySettings, value: stream.slice() },
1898
2049
  loc: {
1899
2050
  ...definedPropertySettings,
@@ -1908,21 +2059,29 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1908
2059
  }
1909
2060
  else {
1910
2061
  if (stream[0]?.typ == EnumToken.UrlFunctionTokenType &&
1911
- stream[0].chi.some((t) => t.typ == EnumToken.StringTokenType)) {
2062
+ stream[0].chi.some((t) => t.typ == EnumToken.StringTokenType || t.typ == EnumToken.UrlTokenTokenType)) {
1912
2063
  stream.splice(0, 1, ...stream[0].chi);
1913
2064
  }
1914
2065
  }
1915
2066
  // @ts-expect-error
1916
2067
  return Object.defineProperties(Object.assign(atRule, {
1917
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2068
+ typ: EnumToken.AtRuleNodeType,
1918
2069
  val: stream.reduce((acc, t, index) => acc +
1919
2070
  (t.typ === EnumToken.CommentTokenType ||
1920
2071
  (t.typ === EnumToken.WhitespaceTokenType &&
1921
2072
  stream[index + 1]?.typ === EnumToken.CommentTokenType &&
1922
2073
  (stream.length < index + 3 || stream[index + 2]?.typ === EnumToken.WhitespaceTokenType))
1923
2074
  ? ""
1924
- : renderToken(t, options)), ""),
2075
+ : renderValue(t, options)), ""),
1925
2076
  }), {
2077
+ state: {
2078
+ ...definedPropertySettings,
2079
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2080
+ },
2081
+ errors: {
2082
+ ...definedPropertySettings,
2083
+ value: result.success ? [] : result.errors,
2084
+ },
1926
2085
  tokens: { ...definedPropertySettings, value: stream.slice() },
1927
2086
  loc: {
1928
2087
  ...definedPropertySettings,
@@ -1940,7 +2099,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1940
2099
  if (result.errors.length > 0) {
1941
2100
  errors.push(...result.errors);
1942
2101
  }
1943
- let success = true;
2102
+ let success = result.success;
1944
2103
  if (atRule.nam === "else") {
1945
2104
  const siblings = context.chi;
1946
2105
  let sibling = null;
@@ -1990,10 +2149,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1990
2149
  options = { ...options, minify: false, convertColor: false };
1991
2150
  // @ts-expect-error
1992
2151
  return Object.defineProperties(Object.assign(atRule, {
1993
- typ: success && result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2152
+ typ: EnumToken.AtRuleNodeType,
1994
2153
  val: renderTokens(stream, options),
1995
2154
  chi: [],
1996
2155
  }), {
2156
+ state: {
2157
+ ...definedPropertySettings,
2158
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2159
+ },
2160
+ errors: {
2161
+ ...definedPropertySettings,
2162
+ value: result.success ? [] : [errors[errors.length - 1]].concat(result.errors),
2163
+ },
1997
2164
  tokens: { ...definedPropertySettings, value: stream.slice() },
1998
2165
  loc: {
1999
2166
  ...definedPropertySettings,
@@ -2009,10 +2176,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2009
2176
  }
2010
2177
  // @ts-expect-error
2011
2178
  return Object.defineProperties(Object.assign(atRule, {
2012
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2179
+ typ: EnumToken.AtRuleNodeType,
2013
2180
  val: renderTokens(stream, options),
2014
2181
  chi: [],
2015
2182
  }), {
2183
+ state: {
2184
+ ...definedPropertySettings,
2185
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2186
+ },
2187
+ errors: {
2188
+ ...definedPropertySettings,
2189
+ value: result.success ? [] : result.errors,
2190
+ },
2016
2191
  tokens: { ...definedPropertySettings, value: stream.slice() },
2017
2192
  loc: {
2018
2193
  ...definedPropertySettings,
@@ -2111,10 +2286,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2111
2286
  }
2112
2287
  // @ts-expect-error
2113
2288
  return Object.defineProperties(Object.assign(atRule, {
2114
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2289
+ typ: EnumToken.AtRuleNodeType,
2115
2290
  val: renderTokens(stream, options),
2116
2291
  chi: [],
2117
2292
  }), {
2293
+ state: {
2294
+ ...definedPropertySettings,
2295
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2296
+ },
2297
+ errors: {
2298
+ ...definedPropertySettings,
2299
+ value: success ? [] : [errors[errors.length - 1]],
2300
+ },
2118
2301
  tokens: { ...definedPropertySettings, value: stream.slice() },
2119
2302
  loc: {
2120
2303
  ...definedPropertySettings,
@@ -2124,13 +2307,20 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2124
2307
  }
2125
2308
  case "page": {
2126
2309
  trimArray(stream);
2127
- parseAtRulePage(atRule, stream, options, errors);
2128
2310
  // @ts-expect-error
2129
2311
  return Object.defineProperties(Object.assign(atRule, {
2130
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2312
+ typ: EnumToken.AtRuleNodeType,
2131
2313
  val: renderTokens(stream, options),
2132
2314
  chi: [],
2133
2315
  }), {
2316
+ state: {
2317
+ ...definedPropertySettings,
2318
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2319
+ },
2320
+ errors: {
2321
+ ...definedPropertySettings,
2322
+ value: success ? [] : [errors[errors.length - 1]],
2323
+ },
2134
2324
  tokens: { ...definedPropertySettings, value: stream.slice() },
2135
2325
  loc: {
2136
2326
  ...definedPropertySettings,
@@ -2180,10 +2370,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2180
2370
  }
2181
2371
  // @ts-expect-error
2182
2372
  return Object.defineProperties(Object.assign(atRule, {
2183
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2373
+ typ: EnumToken.AtRuleNodeType,
2184
2374
  val: renderTokens(stream, options),
2185
2375
  chi: [],
2186
2376
  }), {
2377
+ state: {
2378
+ ...definedPropertySettings,
2379
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2380
+ },
2381
+ errors: {
2382
+ ...definedPropertySettings,
2383
+ value: success ? [] : [errors[errors.length - 1]],
2384
+ },
2187
2385
  tokens: { ...definedPropertySettings, value: stream.slice() },
2188
2386
  loc: {
2189
2387
  ...definedPropertySettings,
@@ -2222,15 +2420,25 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2222
2420
  // @value <ident>: <string>; // import from file as alias
2223
2421
  // @value id : <declaration-value>; // variable declaration
2224
2422
  // @value <ident># from <ident>; // import variables from alias
2225
- let result = matchAllSyntax(syntaxRules?.getPreludeRules()?.slice?.(1), createValidationContext(stream), options);
2423
+ let result = matchAllSyntaxes(syntaxRules?.getPreludeRules()?.slice?.(1), createValidationContext(stream), options);
2226
2424
  if (!result.success) {
2227
2425
  errors.push(...result.errors);
2228
- return Object.defineProperty({
2229
- typ: EnumToken.InvalidAtRuleNodeType,
2426
+ return Object.defineProperties({
2427
+ typ: EnumToken.AtRuleNodeType,
2230
2428
  val: renderTokens(stream, options),
2231
- }, "loc", {
2232
- ...definedPropertySettings,
2233
- value: { ...atRule.loc, end: { ...(stream.at(-1)?.loc?.end ?? atRule.loc.end) } },
2429
+ }, {
2430
+ state: {
2431
+ ...definedPropertySettings,
2432
+ value: EnumAstNodeStatus.Invalid,
2433
+ },
2434
+ errors: {
2435
+ ...definedPropertySettings,
2436
+ value: result.errors,
2437
+ },
2438
+ loc: {
2439
+ ...definedPropertySettings,
2440
+ value: { ...atRule.loc, end: { ...(stream.at(-1)?.loc?.end ?? atRule.loc.end) } },
2441
+ },
2234
2442
  });
2235
2443
  }
2236
2444
  if (isVarDeclaration) {
@@ -2238,18 +2446,28 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2238
2446
  const value = trimArray(stream.slice(index + 1).filter((t) => t.typ != EnumToken.CommentTokenType));
2239
2447
  if (value.length == 1 && value[0].typ == EnumToken.StringTokenType) {
2240
2448
  // import from file as alias
2241
- return {
2449
+ return Object.defineProperties({
2242
2450
  typ: EnumToken.CssVariableImportTokenType,
2243
2451
  nam: nam.val,
2244
2452
  val: value,
2245
- };
2453
+ }, {
2454
+ loc: {
2455
+ ...definedPropertySettings,
2456
+ value: { ...atRule.loc, end: { ...(stream.at(-1)?.loc?.end ?? atRule.loc.end) } },
2457
+ },
2458
+ });
2246
2459
  }
2247
2460
  // import variables from alias
2248
- return {
2461
+ return Object.defineProperties({
2249
2462
  typ: EnumToken.CssVariableTokenType,
2250
2463
  nam: nam.val,
2251
2464
  val: value,
2252
- };
2465
+ }, {
2466
+ loc: {
2467
+ ...definedPropertySettings,
2468
+ value: { ...atRule.loc, end: { ...(stream.at(-1)?.loc?.end ?? atRule.loc.end) } },
2469
+ },
2470
+ });
2253
2471
  }
2254
2472
  // @ts-expect-error
2255
2473
  return Object.defineProperties(Object.assign(atRule, {
@@ -2272,13 +2490,19 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2272
2490
  // check commas , or ,,
2273
2491
  // check colon :
2274
2492
  // check or and and
2275
- result = matchGenericSyntax(atRule, stream);
2493
+ result = matchGenericSyntax(stream);
2276
2494
  if (result.errors.length > 0) {
2277
2495
  errors.push(...result.errors);
2278
2496
  }
2279
2497
  }
2280
2498
  else {
2281
2499
  result = matchAtRuleSyntax(atRule, stream, options);
2500
+ if (result.errors.length > 0) {
2501
+ errors.push(...result.errors);
2502
+ }
2503
+ // else if (atRuleName === "document") {
2504
+ // parseUrlToken(stream);
2505
+ // }
2282
2506
  if (result.success) {
2283
2507
  let i = 0;
2284
2508
  const stack = [];
@@ -2304,10 +2528,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2304
2528
  }
2305
2529
  // @ts-expect-error
2306
2530
  return Object.defineProperties(Object.assign(atRule, {
2307
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2531
+ typ: EnumToken.AtRuleNodeType,
2308
2532
  val: renderTokens(trimWhiteSpaceTokens(stream), options),
2309
2533
  ...(parseAsBlock ? { chi: [] } : {}),
2310
2534
  }), {
2535
+ state: {
2536
+ ...definedPropertySettings,
2537
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2538
+ },
2539
+ errors: {
2540
+ ...definedPropertySettings,
2541
+ value: result.errors,
2542
+ },
2311
2543
  tokens: { ...definedPropertySettings, value: stream.slice() },
2312
2544
  loc: {
2313
2545
  ...definedPropertySettings,
@@ -2318,7 +2550,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2318
2550
  }
2319
2551
  }
2320
2552
  /**
2321
- * parse a string as an array of declaration nodes
2553
+ * Parse a string as an array of declaration nodes
2322
2554
  * @param declaration
2323
2555
  *
2324
2556
  * Example:
@@ -2337,16 +2569,17 @@ async function parseDeclarations(declaration) {
2337
2569
  currentPosition: { ind: -1, lin: 1, col: 0 },
2338
2570
  }), { setParent: false, minify: false, validation: false }).then((result) => {
2339
2571
  return result.ast.chi[0].chi.filter((t) => t.typ == EnumToken.DeclarationNodeType ||
2340
- t.typ == EnumToken.CommentNodeType ||
2341
- t.typ == EnumToken.InvalidDeclarationNodeType);
2572
+ t.typ == EnumToken.CommentNodeType);
2342
2573
  });
2343
2574
  }
2344
2575
  /**
2345
- * parse css string and return an array of tokens
2576
+ * Parse css string and return an array of tokens
2346
2577
  * @param src
2347
2578
  * @param options
2348
- *
2349
- * @private
2579
+ * - parseColor: parse identifiers as colors
2580
+ * - src: source url used for source map
2581
+ * @param errors capture parse errors in the provided array
2582
+
2350
2583
  *
2351
2584
  * Example:
2352
2585
  *
@@ -2357,26 +2590,35 @@ async function parseDeclarations(declaration) {
2357
2590
  * let tokens = parseString('body { color: red; }');
2358
2591
  * console.log(tokens);
2359
2592
  *
2360
- * tokens = parseString('#c322c980');
2593
+ * tokens = parseString('#c322c980');
2361
2594
  * console.log(tokens);
2362
2595
  * ```
2363
2596
  */
2364
- function parseString(src, options = { location: false }) {
2597
+ function parseString(src, options = { parseColor: true }, errors) {
2365
2598
  const parseInfo = {
2366
2599
  stream: src,
2367
- src: options.src ?? "",
2600
+ src: options?.src ?? "",
2368
2601
  buffer: "",
2369
2602
  offset: 0,
2370
2603
  time: 0,
2371
2604
  position: { ind: 0, lin: 1, col: 1 },
2372
2605
  currentPosition: { ind: -1, lin: 1, col: 0 },
2373
2606
  };
2374
- return parseTokens([...tokenize(parseInfo)].map((t) => t.token), { sourcemap: options.location }).slice(0, -1);
2607
+ const result = parseTokens([...tokenize(parseInfo)].map((t) => t.token), options, errors);
2608
+ // remove EOF token
2609
+ result.pop();
2610
+ if (result.at(-1)?.typ === EnumToken.WhitespaceTokenType) {
2611
+ result.pop();
2612
+ }
2613
+ return result;
2375
2614
  }
2376
2615
  /**
2377
- * parse function tokens in a token array
2616
+ * Parse function tokens in a token array
2378
2617
  * @param tokens
2379
2618
  * @param options
2619
+ * - parseColor: parse identifiers as colors
2620
+ * @param errors parse errors
2621
+ *
2380
2622
  *
2381
2623
  * Example:
2382
2624
  *
@@ -2393,62 +2635,145 @@ function parseString(src, options = { location: false }) {
2393
2635
  *
2394
2636
  * @private
2395
2637
  */
2396
- function parseTokens(tokens, options = {}) {
2397
- for (let i = 0; i < tokens.length; i++) {
2398
- const t = tokens[i];
2399
- if (t.typ == EnumToken.IdenTokenType && t.val == "from" && i > 0) {
2400
- const left = [];
2401
- const right = [];
2402
- let foundLeft = 0;
2403
- let foundRight = 0;
2404
- let k = i;
2405
- let l = i;
2406
- while (k > 0) {
2407
- if (tokens[k - 1].typ == EnumToken.CommentTokenType ||
2408
- tokens[k - 1].typ == EnumToken.WhitespaceTokenType) {
2409
- left.push(tokens[--k]);
2410
- continue;
2411
- }
2412
- if (tokens[k - 1].typ == EnumToken.IdenTokenType ||
2413
- tokens[k - 1].typ == EnumToken.DashedIdenTokenType) {
2414
- foundLeft++;
2415
- left.push(tokens[--k]);
2416
- continue;
2417
- }
2418
- break;
2638
+ function parseTokens(tokens, options, errors) {
2639
+ const stack = [];
2640
+ let i = 0;
2641
+ let index;
2642
+ let t;
2643
+ options ??= { parseColor: true };
2644
+ for (; i < tokens.length; i++) {
2645
+ t = tokens[i];
2646
+ if (t.typ === EnumToken.FunctionTokenDefType) {
2647
+ if (tokens[i - 1]?.typ === EnumToken.ColonTokenType ||
2648
+ tokens[i - 1]?.typ === EnumToken.DoubleColonTokenType) {
2649
+ t = Object.assign(tokens[i - 1], {
2650
+ typ: EnumToken.PseudoClassFunctionTokenDefType,
2651
+ val: (tokens[i - 1].typ === EnumToken.ColonTokenType ? ":" : "::") +
2652
+ tokens[i].val,
2653
+ });
2654
+ t.loc.end = tokens[i].loc.end;
2655
+ tokens.splice(i--, 1);
2419
2656
  }
2420
- while (++l < tokens.length) {
2421
- if (tokens[l].typ == EnumToken.CommentTokenType || tokens[l].typ == EnumToken.WhitespaceTokenType) {
2422
- right.push(tokens[l]);
2423
- continue;
2424
- }
2425
- if (tokens[l].typ == EnumToken.IdenTokenType || tokens[l].typ == EnumToken.StringTokenType) {
2426
- foundRight++;
2427
- right.push(tokens[l]);
2428
- continue;
2429
- }
2430
- break;
2657
+ }
2658
+ if (t.typ === EnumToken.AttrStartTokenType ||
2659
+ t.typ === EnumToken.StartParensTokenType ||
2660
+ tokensfuncDefMap.has(t.typ)) {
2661
+ stack.push(t);
2662
+ continue;
2663
+ }
2664
+ if (t.typ === EnumToken.EndParensTokenType) {
2665
+ if (stack.length === 0 ||
2666
+ (stack.at(-1)?.typ !== EnumToken.StartParensTokenType && !tokensfuncDefMap.has(stack.at(-1)?.typ))) {
2667
+ // unbalanced parens
2668
+ const node = stack.at(-1);
2669
+ errors?.push?.({
2670
+ action: "drop",
2671
+ message: `Unbalanced token ')' at ${node.loc.src}:${node.loc.sta.lin}:${node.loc.sta.col}`,
2672
+ node,
2673
+ location: node.loc,
2674
+ });
2675
+ // return [];
2676
+ continue;
2431
2677
  }
2432
- if (foundLeft > 0 && foundRight == 1) {
2433
- while (left?.[0].typ == EnumToken.WhitespaceTokenType) {
2434
- left.shift();
2678
+ tokens.splice(i, 1);
2679
+ index = tokens.indexOf(stack.at(-1));
2680
+ Object.assign(tokens[index], {
2681
+ typ: tokens[index].typ === EnumToken.StartParensTokenType
2682
+ ? EnumToken.ParensTokenType
2683
+ : tokensfuncDefMap.get(tokens[index].typ),
2684
+ chi: trimArray(tokens.splice(index + 1, i - index - 1)),
2685
+ });
2686
+ i = index;
2687
+ if (tokens[index].typ === EnumToken.ColorTokenType && options?.parseColor) {
2688
+ parseColor(tokens[index]);
2689
+ }
2690
+ stack.pop();
2691
+ continue;
2692
+ }
2693
+ if (t.typ === EnumToken.AttrEndTokenType) {
2694
+ if (stack.at(-1)?.typ !== EnumToken.AttrStartTokenType) {
2695
+ // unbalanced
2696
+ const node = stack.at(-1);
2697
+ errors?.push?.({
2698
+ action: "drop",
2699
+ message: `Unbalanced token ']' at ${node.loc.src}:${node.loc.sta.lin}:${node.loc.sta.col}`,
2700
+ node,
2701
+ location: node.loc,
2702
+ });
2703
+ // return [];
2704
+ continue;
2705
+ }
2706
+ index = tokens.indexOf(stack.at(-1));
2707
+ const attr = stack.at(-1);
2708
+ attr.loc.end = t.loc.end;
2709
+ tokens.splice(i, 1);
2710
+ Object.assign(attr, {
2711
+ typ: EnumToken.AttrTokenType,
2712
+ chi: tokens.splice(index + 1, i - index - 1),
2713
+ });
2714
+ tokens[index] = attr;
2715
+ i = index;
2716
+ stack.pop();
2717
+ continue;
2718
+ }
2719
+ if (t.typ == EnumToken.IdenTokenType) {
2720
+ if (t.val == "from" && i > 0) {
2721
+ const left = [];
2722
+ const right = [];
2723
+ let foundLeft = 0;
2724
+ let foundRight = 0;
2725
+ let k = i;
2726
+ let l = i;
2727
+ while (k > 0) {
2728
+ if (tokens[k - 1].typ == EnumToken.CommentTokenType ||
2729
+ tokens[k - 1].typ == EnumToken.WhitespaceTokenType) {
2730
+ left.push(tokens[--k]);
2731
+ continue;
2732
+ }
2733
+ if (tokens[k - 1].typ == EnumToken.IdenTokenType ||
2734
+ tokens[k - 1].typ == EnumToken.DashedIdenTokenType) {
2735
+ foundLeft++;
2736
+ left.push(tokens[--k]);
2737
+ continue;
2738
+ }
2739
+ break;
2435
2740
  }
2436
- while (left.at(-1)?.typ == EnumToken.WhitespaceTokenType) {
2437
- left.pop();
2741
+ while (++l < tokens.length) {
2742
+ if (tokens[l].typ == EnumToken.CommentTokenType || tokens[l].typ == EnumToken.WhitespaceTokenType) {
2743
+ right.push(tokens[l]);
2744
+ continue;
2745
+ }
2746
+ if (tokens[l].typ == EnumToken.IdenTokenType || tokens[l].typ == EnumToken.StringTokenType) {
2747
+ foundRight++;
2748
+ right.push(tokens[l]);
2749
+ continue;
2750
+ }
2751
+ break;
2438
2752
  }
2439
- tokens.splice(k, l - k + 1, {
2440
- typ: EnumToken.ComposesSelectorNodeType,
2441
- l: left,
2442
- r: right.reduce((a, b) => {
2443
- return a == null
2444
- ? b
2445
- : b.typ == EnumToken.IdenTokenType || b.typ == EnumToken.StringTokenType
2753
+ if (foundLeft > 0 && foundRight == 1) {
2754
+ while (left?.[0].typ == EnumToken.WhitespaceTokenType) {
2755
+ left.shift();
2756
+ }
2757
+ while (left.at(-1)?.typ == EnumToken.WhitespaceTokenType) {
2758
+ left.pop();
2759
+ }
2760
+ tokens.splice(k, l - k + 1, {
2761
+ typ: EnumToken.ComposesSelectorNodeType,
2762
+ l: left,
2763
+ r: right.reduce((a, b) => {
2764
+ return a == null
2446
2765
  ? b
2447
- : a;
2448
- }, null),
2449
- });
2450
- i = k;
2451
- continue;
2766
+ : b.typ == EnumToken.IdenTokenType || b.typ == EnumToken.StringTokenType
2767
+ ? b
2768
+ : a;
2769
+ }, null),
2770
+ });
2771
+ i = k;
2772
+ continue;
2773
+ }
2774
+ }
2775
+ else if (options?.parseColor && isColor(t)) {
2776
+ parseColor(t);
2452
2777
  }
2453
2778
  }
2454
2779
  if (t.typ == EnumToken.WhitespaceTokenType &&
@@ -2479,290 +2804,27 @@ function parseTokens(tokens, options = {}) {
2479
2804
  i--;
2480
2805
  }
2481
2806
  }
2482
- continue;
2483
2807
  }
2484
- if (t.typ == EnumToken.AttrStartTokenType) {
2485
- let k = i;
2486
- let inAttr = 1;
2487
- while (++k < tokens.length) {
2488
- if (tokens[k].typ == EnumToken.AttrEndTokenType) {
2489
- inAttr--;
2490
- }
2491
- else if (tokens[k].typ == EnumToken.AttrStartTokenType) {
2492
- inAttr++;
2493
- }
2494
- if (inAttr == 0) {
2495
- break;
2496
- }
2808
+ if (t.typ === EnumToken.CommaTokenType) {
2809
+ if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
2810
+ tokens.splice(i + 1, 1);
2497
2811
  }
2498
- const attr = Object.assign(t, {
2499
- typ: inAttr == 0 ? EnumToken.AttrTokenType : EnumToken.InvalidAttrTokenType,
2500
- chi: tokens.splice(i + 1, k - i),
2501
- });
2502
- // @ts-ignore
2503
- if (attr.chi.at(-1).typ == EnumToken.AttrEndTokenType) {
2504
- // @ts-ignore
2505
- attr.chi.pop();
2506
- }
2507
- // @ts-ignore
2508
- if (attr.chi.length > 1) {
2509
- // @ts-ignore
2510
- parseTokens(attr.chi, t.typ);
2511
- }
2512
- let m = attr.chi.length;
2513
- let val;
2514
- for (m = 0; m < attr.chi.length; m++) {
2515
- val = attr.chi[m];
2516
- if (val.typ == EnumToken.StringTokenType) {
2517
- const slice = val.val.slice(1, -1);
2518
- if ((slice.charAt(0) != "-" || (slice.charAt(0) == "-" && isIdentStart(slice.charCodeAt(1)))) &&
2519
- isIdent(slice)) {
2520
- Object.assign(val, { typ: EnumToken.IdenTokenType, val: slice });
2521
- }
2522
- }
2523
- else if (val.typ == EnumToken.LiteralTokenType && val.val == "|") {
2524
- let upper = m;
2525
- let lower = m;
2526
- while (++upper < attr.chi.length) {
2527
- if (attr.chi[upper].typ == EnumToken.CommentTokenType) {
2528
- continue;
2529
- }
2530
- break;
2531
- }
2532
- while (lower-- > 0) {
2533
- if (attr.chi[lower].typ == EnumToken.CommentTokenType) {
2534
- continue;
2535
- }
2536
- break;
2537
- }
2538
- // @ts-ignore
2539
- attr.chi[m] = {
2540
- typ: EnumToken.NameSpaceAttributeTokenType,
2541
- l: attr.chi[lower],
2542
- r: attr.chi[upper],
2543
- };
2544
- attr.chi.splice(upper, 1);
2545
- if (lower >= 0) {
2546
- attr.chi.splice(lower, 1);
2547
- m--;
2548
- }
2549
- }
2550
- else if ([
2551
- EnumToken.DashMatchTokenType,
2552
- EnumToken.StartMatchTokenType,
2553
- EnumToken.ContainMatchTokenType,
2554
- EnumToken.EndMatchTokenType,
2555
- EnumToken.IncludeMatchTokenType,
2556
- EnumToken.DelimTokenType,
2557
- ].includes(attr.chi[m].typ)) {
2558
- let upper = m;
2559
- let lower = m;
2560
- while (++upper < attr.chi.length) {
2561
- if (attr.chi[upper].typ == EnumToken.CommentTokenType) {
2562
- continue;
2563
- }
2564
- break;
2565
- }
2566
- while (lower-- > 0) {
2567
- if (attr.chi[lower].typ == EnumToken.CommentTokenType) {
2568
- continue;
2569
- }
2570
- break;
2571
- }
2572
- val = attr.chi[lower];
2573
- if (val.typ == EnumToken.StringTokenType) {
2574
- const slice = val.val.slice(1, -1);
2575
- if ((slice.charAt(0) != "-" || (slice.charAt(0) == "-" && isIdentStart(slice.charCodeAt(1)))) &&
2576
- isIdent(slice)) {
2577
- Object.assign(val, { typ: EnumToken.IdenTokenType, val: slice });
2578
- }
2579
- }
2580
- val = attr.chi[upper];
2581
- if (val.typ == EnumToken.StringTokenType) {
2582
- const slice = val.val.slice(1, -1);
2583
- if ((slice.charAt(0) != "-" || (slice.charAt(0) == "-" && isIdentStart(slice.charCodeAt(1)))) &&
2584
- isIdent(slice)) {
2585
- Object.assign(val, { typ: EnumToken.IdenTokenType, val: slice });
2586
- }
2587
- }
2588
- // @ts-ignore
2589
- const typ = t.chi[m].typ;
2590
- // @ts-ignore
2591
- t.chi[m] = {
2592
- typ: EnumToken.MatchExpressionTokenType,
2593
- op: {
2594
- // @ts-ignore
2595
- typ: typ == EnumToken.DelimTokenType ? EnumToken.EqualMatchTokenType : typ,
2596
- },
2597
- l: t.chi[lower],
2598
- r: t.chi[upper],
2599
- };
2600
- if (isIdentColor(t.chi[m].l)) {
2601
- t.chi[m].l.typ =
2602
- EnumToken.IdenTokenType;
2603
- }
2604
- if (isIdentColor(t.chi[m].r)) {
2605
- t.chi[m].r.typ =
2606
- EnumToken.IdenTokenType;
2607
- }
2608
- t.chi.splice(upper, 1);
2609
- t.chi.splice(lower, 1);
2610
- upper = m;
2611
- m--;
2612
- while (upper < t.chi.length &&
2613
- t.chi[upper].typ == EnumToken.WhitespaceTokenType) {
2614
- upper++;
2615
- }
2616
- if (upper < t.chi.length &&
2617
- t.chi[upper].typ == EnumToken.IdenTokenType &&
2618
- ["i", "s"].includes(t.chi[upper].val.toLowerCase())) {
2619
- t.chi[m].attr = (t.chi[upper].val);
2620
- t.chi.splice(upper, 1);
2621
- }
2622
- }
2623
- }
2624
- m = t.chi.length;
2625
- while (t.chi.at(-1)?.typ == EnumToken.WhitespaceTokenType) {
2626
- t.chi.pop();
2627
- }
2628
- continue;
2629
- }
2630
- if (funcLike.includes(t.typ)) {
2631
- let parens = 1;
2632
- let k = i;
2633
- while (++k < tokens.length) {
2634
- if (tokens[k].typ == EnumToken.ColonTokenType) {
2635
- const typ = tokens[k + 1]?.typ;
2636
- if (typ != null) {
2637
- if (typ == EnumToken.IdenTokenType) {
2638
- tokens[k + 1].typ = EnumToken.PseudoClassTokenType;
2639
- tokens[k + 1].val = ":" + tokens[k + 1].val;
2640
- }
2641
- else if (typ == EnumToken.FunctionTokenType) {
2642
- tokens[k + 1].typ = EnumToken.PseudoClassFuncTokenType;
2643
- tokens[k + 1].val =
2644
- ":" + tokens[k + 1].val;
2645
- }
2646
- if (typ == EnumToken.FunctionTokenType || typ == EnumToken.IdenTokenType) {
2647
- tokens.splice(k, 1);
2648
- k--;
2649
- continue;
2650
- }
2651
- }
2652
- }
2653
- if (funcLike.includes(tokens[k].typ)) {
2654
- parens++;
2655
- }
2656
- else if (tokens[k].typ == EnumToken.EndParensTokenType) {
2657
- parens--;
2658
- }
2659
- if (parens == 0) {
2660
- break;
2661
- }
2662
- }
2663
- // @ts-ignore
2664
- t.chi = tokens.splice(i + 1, k - i);
2665
- // @ts-ignore
2666
- if (t.chi.at(-1)?.typ == EnumToken.EndParensTokenType) {
2667
- // @ts-ignore
2668
- t.chi.pop();
2669
- }
2670
- // @ts-ignore
2671
- if (t.chi.length > 0) {
2672
- // @ts-ignore
2673
- parseTokens(t.chi, options);
2674
- }
2675
- if (t.typ == EnumToken.FunctionTokenType && mathFuncs.includes(t.val)) {
2676
- for (const { value, parent } of walkValues(t.chi)) {
2677
- if (value.typ == EnumToken.WhitespaceTokenType) {
2678
- const p = (parent ?? t);
2679
- for (let i = 0; i < p.chi.length; i++) {
2680
- // @ts-ignore
2681
- if (p.chi[i] == value) {
2682
- // @ts-ignore
2683
- p.chi.splice(i, 1);
2684
- i--;
2685
- break;
2686
- }
2687
- }
2688
- }
2689
- else if (value.typ == EnumToken.LiteralTokenType &&
2690
- ["+", "-", "/", "*"].includes(value.val)) {
2691
- // @ts-ignore
2692
- value.typ =
2693
- value.val === "+"
2694
- ? EnumToken.Add
2695
- : value.val === "-"
2696
- ? EnumToken.Sub
2697
- : value.val === "*"
2698
- ? EnumToken.Mul
2699
- : EnumToken.Div;
2700
- // @ts-ignore
2701
- delete value.val;
2702
- }
2703
- }
2704
- t.chi = splitTokenList(t.chi).reduce((acc, t) => {
2705
- if (acc.length > 0) {
2706
- acc.push({ typ: EnumToken.CommaTokenType });
2707
- }
2708
- acc.push(buildExpression(t));
2709
- return acc;
2710
- }, []);
2711
- }
2712
- else if (t.typ == EnumToken.FunctionTokenType &&
2713
- ["minmax", "fit-content", "repeat"].includes(t.val)) {
2714
- // @ts-ignore
2715
- t.typ = EnumToken.GridTemplateFuncTokenType;
2716
- }
2717
- else if (t.typ == EnumToken.StartParensTokenType) {
2718
- // @ts-ignore
2719
- t.typ = EnumToken.ParensTokenType;
2720
- }
2721
- // @ts-ignore
2722
- if (options.parseColor && t.typ == EnumToken.FunctionTokenType && isColor(t)) {
2723
- parseColor(t);
2724
- continue;
2725
- }
2726
- if (t.typ == EnumToken.UrlFunctionTokenType) {
2727
- // @ts-ignore
2728
- if (t.chi[0]?.typ == EnumToken.StringTokenType) {
2729
- // @ts-ignore
2730
- const value = t.chi[0].val.slice(1, -1);
2731
- // @ts-ignore
2732
- if (t.chi[0].val.slice(1, 5) != "data:" && urlTokenMatcher.test(value)) {
2733
- // @ts-ignore
2734
- t.chi[0].typ = EnumToken.UrlTokenTokenType;
2735
- // @ts-ignore
2736
- t.chi[0].val =
2737
- options.src !== "" && options.resolveUrls
2738
- ? options.resolve(value, options.src)?.absolute
2739
- : value;
2740
- }
2741
- }
2742
- if (t.chi[0]?.typ == EnumToken.UrlTokenTokenType) {
2743
- if (options.src !== "" && options.resolveUrls) {
2744
- // @ts-ignore
2745
- t.chi[0].val = options.resolve(t.chi[0].val, options.src, options.cwd).relative;
2746
- }
2747
- }
2748
- }
2749
- // @ts-ignore
2750
- if (t.chi.length > 0) {
2751
- if (t.typ == EnumToken.PseudoClassFuncTokenType &&
2752
- t.val === ":is" &&
2753
- options.minify) {
2754
- const count = t.chi.filter((t) => t.typ != EnumToken.CommentTokenType).length;
2755
- if (count == 1 ||
2756
- (i == 0 && (tokens[i + 1]?.typ == EnumToken.CommaTokenType || tokens.length == i + 1)) ||
2757
- (tokens[i - 1]?.typ == EnumToken.CommaTokenType &&
2758
- (tokens[i + 1]?.typ == EnumToken.CommaTokenType || tokens.length == i + 1))) {
2759
- tokens.splice(i, 1, ...t.chi);
2760
- i = Math.max(0, i - t.chi.length);
2761
- }
2762
- }
2812
+ if (tokens[i - 1]?.typ === EnumToken.WhitespaceTokenType) {
2813
+ tokens.splice(i - 1, 1);
2814
+ i--;
2763
2815
  }
2764
2816
  }
2765
2817
  }
2818
+ if (stack.length > 0) {
2819
+ const node = stack.at(-1);
2820
+ errors?.push?.({
2821
+ action: "drop",
2822
+ message: `Unbalanced token. Expecting ${node.typ === EnumToken.AttrStartTokenType ? "']'" : ")"}'`,
2823
+ node,
2824
+ location: node.loc,
2825
+ });
2826
+ // return [];
2827
+ }
2766
2828
  return tokens;
2767
2829
  }
2768
2830