@tbela99/css-parser 1.4.3 → 1.4.5

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 +196 -2
  2. package/README.md +68 -1120
  3. package/dist/config.json.js +43 -2
  4. package/dist/index-umd-web.js +18118 -15886
  5. package/dist/index.cjs +16838 -14611
  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 +64 -17
  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 +530 -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;
@@ -247,6 +244,8 @@ async function doParse(iter, options = {}) {
247
244
  if (options.resolveImport) {
248
245
  options.resolveUrls = true;
249
246
  }
247
+ // turn off expandIfSyntax for now
248
+ options.expandIfSyntax = false;
250
249
  const startTime = performance.now();
251
250
  const errors = [];
252
251
  const src = options.src;
@@ -263,6 +262,7 @@ async function doParse(iter, options = {}) {
263
262
  total: `0ms`,
264
263
  imports: [],
265
264
  };
265
+ const invalidNodes = [];
266
266
  let ast = {
267
267
  typ: EnumToken.StyleSheetNodeType,
268
268
  chi: [],
@@ -438,7 +438,7 @@ async function doParse(iter, options = {}) {
438
438
  (item.token.typ === EnumToken.SemiColonTokenType ||
439
439
  item.token.typ === EnumToken.BlockStartTokenType)) ||
440
440
  item.token.typ === EnumToken.EOFTokenType) {
441
- node = parseNode(tokens, context, options, errors, stats);
441
+ node = parseNode(tokens, context, options, errors, stats, invalidNodes);
442
442
  if (node != null) {
443
443
  if ("chi" in node) {
444
444
  stack.push(node);
@@ -483,7 +483,7 @@ async function doParse(iter, options = {}) {
483
483
  tokens = [];
484
484
  }
485
485
  else if (item.token.typ === EnumToken.BlockEndTokenType) {
486
- parseNode(tokens, context, options, errors, stats);
486
+ parseNode(tokens, context, options, errors, stats, invalidNodes);
487
487
  if (context.loc != null) {
488
488
  context.loc.end = item.token.loc.end;
489
489
  }
@@ -499,7 +499,7 @@ async function doParse(iter, options = {}) {
499
499
  }
500
500
  }
501
501
  if (tokens.length > 0) {
502
- node = parseNode(tokens, context, options, errors, stats);
502
+ node = parseNode(tokens, context, options, errors, stats, invalidNodes);
503
503
  if (node != null) {
504
504
  if (node.typ == EnumToken.AtRuleNodeType && "import" === node.val) {
505
505
  imports.push(node);
@@ -512,10 +512,13 @@ async function doParse(iter, options = {}) {
512
512
  }
513
513
  if (imports.length > 0 && options.resolveImport) {
514
514
  await Promise.all(imports.map(async (node) => {
515
+ if (node.state !== EnumAstNodeStatus.Validated) {
516
+ return;
517
+ }
515
518
  const token = node.tokens[0];
516
519
  const url = token.typ == EnumToken.StringTokenType ? token.val.slice(1, -1) : token.val;
517
520
  try {
518
- const result = options.load(url, options.src);
521
+ const result = options.load(url, options.src || options.cwd);
519
522
  const stream = result instanceof Promise || Object.getPrototypeOf(result).constructor.name == "AsyncFunction"
520
523
  ? await result
521
524
  : result;
@@ -546,31 +549,6 @@ async function doParse(iter, options = {}) {
546
549
  }
547
550
  }));
548
551
  }
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
552
  const endParseTime = performance.now();
575
553
  if (options.expandNestingRules) {
576
554
  ast = expand(ast);
@@ -655,7 +633,7 @@ async function doParse(iter, options = {}) {
655
633
  }
656
634
  }
657
635
  if (node != result.node) {
658
- replaceToken(result.parent, result.node, node);
636
+ replaceNodeOrValue(result.parent, result.node, node);
659
637
  }
660
638
  }
661
639
  else if ((result.node.typ == EnumToken.RuleNodeType &&
@@ -710,7 +688,7 @@ async function doParse(iter, options = {}) {
710
688
  // @ts-ignore
711
689
  if (node != result.node) {
712
690
  // @ts-ignore
713
- replaceToken(result.parent, result.node, node);
691
+ replaceNodeOrValue(result.parent, result.node, node);
714
692
  }
715
693
  }
716
694
  else if (valuesHandlers.size > 0) {
@@ -744,7 +722,7 @@ async function doParse(iter, options = {}) {
744
722
  }
745
723
  if (node != result.node) {
746
724
  // @ts-ignore
747
- replaceToken(result.parent, value, node);
725
+ replaceNodeOrValue(result.parent, value, node);
748
726
  }
749
727
  const tokens = Array.isArray(result.node.tokens) ? result.node.tokens : [];
750
728
  if (Array.isArray(result.node.val)) {
@@ -786,13 +764,50 @@ async function doParse(iter, options = {}) {
786
764
  }
787
765
  if (node != value) {
788
766
  // @ts-ignore
789
- replaceToken(parent, value, node);
767
+ replaceNodeOrValue(parent, value, node);
790
768
  }
791
769
  }
792
770
  }
793
771
  }
794
772
  }
795
773
  }
774
+ if (invalidNodes.length > 0) {
775
+ let k = invalidNodes.length;
776
+ while (k-- > 0) {
777
+ if (invalidNodes[k].state == EnumAstNodeStatus.Validated || invalidNodes[k].state == EnumAstNodeStatus.Unvalidated || invalidNodes[k].state == EnumAstNodeStatus.ValidationFailed) {
778
+ continue;
779
+ }
780
+ if (options.lenient && invalidNodes[k].state == EnumAstNodeStatus.Unknown) {
781
+ continue;
782
+ }
783
+ invalidNodes[k].parent.chi.splice(invalidNodes[k].parent.chi.indexOf(invalidNodes[k]), 1);
784
+ }
785
+ }
786
+ while (stack.length > 0 && context != ast) {
787
+ const previousNode = stack.pop();
788
+ context = (stack[stack.length - 1] ?? ast);
789
+ // remove empty nodes
790
+ if (options.removeEmpty &&
791
+ previousNode != null &&
792
+ previousNode.chi.length == 0 &&
793
+ context.chi[context.chi.length - 1] == previousNode) {
794
+ context.chi.pop();
795
+ continue;
796
+ }
797
+ // remove invalid nodes
798
+ if (!options.lenient &&
799
+ previousNode?.parent != null &&
800
+ // @ts-expect-error
801
+ (previousNode.typ == EnumToken.InvalidRuleNodeType || previousNode.typ == EnumToken.InvalidAtRuleNodeType)) {
802
+ for (let i = context.chi.length - 1; i >= 0; i--) {
803
+ if (context.chi[i] == previousNode) {
804
+ context.chi.splice(i, 1);
805
+ break;
806
+ }
807
+ }
808
+ }
809
+ break;
810
+ }
796
811
  if (options.minify) {
797
812
  if (ast.chi.length > 0) {
798
813
  let passes = options.pass ?? 1;
@@ -885,7 +900,7 @@ async function doParse(iter, options = {}) {
885
900
  if (node.typ == EnumToken.CssVariableImportTokenType) {
886
901
  const url = node.val.find((t) => t.typ == EnumToken.StringTokenType).val.slice(1, -1);
887
902
  const src = options.resolve(url, options.dirname(options.src), options.cwd);
888
- const result = options.load(url, options.src);
903
+ const result = options.load(src, "");
889
904
  const stream = result instanceof Promise || Object.getPrototypeOf(result).constructor.name == "AsyncFunction"
890
905
  ? await result
891
906
  : result;
@@ -1027,7 +1042,7 @@ async function doParse(iter, options = {}) {
1027
1042
  else if (token.r.typ == EnumToken.String) {
1028
1043
  const url = token.r.val.slice(1, -1);
1029
1044
  const src = options.resolve(url, options.dirname(options.src), options.cwd);
1030
- const result = options.load(url, options.src);
1045
+ const result = options.load(src, "");
1031
1046
  const stream = result instanceof Promise ||
1032
1047
  Object.getPrototypeOf(result).constructor.name == "AsyncFunction"
1033
1048
  ? await result
@@ -1170,9 +1185,7 @@ async function doParse(iter, options = {}) {
1170
1185
  else if (node.nam == "grid-template-areas" || node.nam == "grid-template") {
1171
1186
  for (let i = 0; i < node.val.length; i++) {
1172
1187
  if (node.val[i].typ == EnumToken.String) {
1173
- const tokens = parseString(node.val[i].val.slice(1, -1), {
1174
- location: true,
1175
- });
1188
+ const tokens = parseString(node.val[i].val.slice(1, -1));
1176
1189
  for (const { value } of walkValues(tokens)) {
1177
1190
  if (value.typ == EnumToken.IdenTokenType ||
1178
1191
  value.typ == EnumToken.DashedIdenTokenType) {
@@ -1261,13 +1274,13 @@ async function doParse(iter, options = {}) {
1261
1274
  }
1262
1275
  else if ((value.typ == EnumToken.IdenTokenType || isIdentColor(value)) &&
1263
1276
  value.val in importedCssVariables) {
1264
- replaceToken(parent, value, importedCssVariables[value.val].val);
1277
+ replaceNodeOrValue(parent, value, importedCssVariables[value.val].val);
1265
1278
  }
1266
1279
  }
1267
1280
  }
1268
1281
  else if (node.typ == EnumToken.RuleNodeType) {
1269
1282
  if (node.tokens == null) {
1270
- const tokens = parseString(node.sel, { location: true });
1283
+ const tokens = parseString(node.sel);
1271
1284
  matchSelectorSyntax(tokens, [], options);
1272
1285
  Object.defineProperty(node, "tokens", {
1273
1286
  ...definedPropertySettings,
@@ -1377,7 +1390,7 @@ async function doParse(iter, options = {}) {
1377
1390
  }
1378
1391
  node.sel = "";
1379
1392
  for (const token of node.tokens) {
1380
- node.sel += renderToken(token);
1393
+ node.sel += renderValue(token);
1381
1394
  }
1382
1395
  }
1383
1396
  else if (node.typ == EnumToken.AtRuleNodeType || node.typ == EnumToken.KeyframesAtRuleNodeType) {
@@ -1454,7 +1467,7 @@ async function doParse(iter, options = {}) {
1454
1467
  }
1455
1468
  return result;
1456
1469
  }
1457
- function parseNode(tokens, context, options, errors, stats) {
1470
+ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
1458
1471
  let i = 0;
1459
1472
  if (tokens.at(-1)?.typ === EnumToken.EOFTokenType) {
1460
1473
  tokens.pop();
@@ -1471,6 +1484,13 @@ function parseNode(tokens, context, options, errors, stats) {
1471
1484
  }
1472
1485
  }
1473
1486
  if (matchCount > 0) {
1487
+ let k = tokens.length;
1488
+ while (k-- > 0 &&
1489
+ (tokens[k].typ === EnumToken.WhitespaceTokenType || tokens[k].typ === EnumToken.CommentTokenType))
1490
+ ;
1491
+ if (tokens[k]?.typ == EnumToken.SemiColonTokenType) {
1492
+ matchCount = 0;
1493
+ }
1474
1494
  while (matchCount > 0) {
1475
1495
  position = {
1476
1496
  ...position,
@@ -1508,6 +1528,7 @@ function parseNode(tokens, context, options, errors, stats) {
1508
1528
  }
1509
1529
  if (i > 0) {
1510
1530
  context.chi.push(...tokens.splice(0, i).filter((n) => n.typ !== EnumToken.WhitespaceTokenType));
1531
+ i = 0;
1511
1532
  }
1512
1533
  for (; i < tokens.length; i++) {
1513
1534
  if (tokens[i].typ == EnumToken.CommentTokenType || tokens[i].typ == EnumToken.CDOCOMMTokenType) {
@@ -1556,6 +1577,13 @@ function parseNode(tokens, context, options, errors, stats) {
1556
1577
  if (node == null) {
1557
1578
  return null;
1558
1579
  }
1580
+ if (node.state == EnumAstNodeStatus.Invalid ||
1581
+ node.state == EnumAstNodeStatus.Disallowed ||
1582
+ node.state == EnumAstNodeStatus.Unknown ||
1583
+ node.state == EnumAstNodeStatus.Unparsed ||
1584
+ node.state == EnumAstNodeStatus.Malformed) {
1585
+ invalidNodes.push(node);
1586
+ }
1559
1587
  stats.nodesCount++;
1560
1588
  context.chi.push(node);
1561
1589
  // @ts-expect-error
@@ -1568,14 +1596,20 @@ function parseNode(tokens, context, options, errors, stats) {
1568
1596
  const node = parseSelector(tokens, context, options, errors);
1569
1597
  context.chi.push(node);
1570
1598
  Object.defineProperty(node, "parent", { ...definedPropertySettings, value: context });
1599
+ if (node.state == EnumAstNodeStatus.Invalid ||
1600
+ node.state == EnumAstNodeStatus.Disallowed ||
1601
+ node.state == EnumAstNodeStatus.Unknown ||
1602
+ node.state == EnumAstNodeStatus.Unparsed ||
1603
+ node.state == EnumAstNodeStatus.Malformed) {
1604
+ invalidNodes.push(node);
1605
+ }
1571
1606
  return node;
1572
1607
  }
1573
1608
  else {
1574
1609
  const node = parseDeclaration(tokens, context, options, errors);
1575
1610
  Object.defineProperty(node, "parent", { ...definedPropertySettings, value: context });
1576
1611
  if (context.typ === EnumToken.StyleSheetNodeType && node.typ === EnumToken.DeclarationNodeType) {
1577
- // @ts-expect-error
1578
- node.typ = EnumToken.InvalidDeclarationNodeType;
1612
+ node.state = EnumAstNodeStatus.Invalid;
1579
1613
  errors.push({
1580
1614
  message: "<declaration> not allowed in <stylesheet>",
1581
1615
  action: "drop",
@@ -1586,17 +1620,23 @@ function parseNode(tokens, context, options, errors, stats) {
1586
1620
  else if (options.lenient || node.typ === EnumToken.DeclarationNodeType) {
1587
1621
  context.chi.push(node);
1588
1622
  }
1623
+ if (node.state == EnumAstNodeStatus.Invalid ||
1624
+ node.state == EnumAstNodeStatus.Disallowed ||
1625
+ node.state == EnumAstNodeStatus.Unknown ||
1626
+ node.state == EnumAstNodeStatus.Unparsed ||
1627
+ node.state == EnumAstNodeStatus.Malformed) {
1628
+ invalidNodes.push(node);
1629
+ }
1589
1630
  }
1590
1631
  }
1591
1632
  return null;
1592
1633
  }
1593
- /**mjgvgyikjkml,kmbm b8790u89y70
1594
- vbbnkit;;;jmjhyg77 * @param options
1634
+ /**
1635
+ * @param options
1595
1636
  * @param errors
1596
1637
  * @param parseAsBlock
1597
1638
  */
1598
1639
  function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1599
- // const rules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + (stream[0] as AtRuleToken).nam);
1600
1640
  let success = true;
1601
1641
  let atRuleName = stream[0].nam;
1602
1642
  if (atRuleName.startsWith("-")) {
@@ -1607,21 +1647,30 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1607
1647
  const syntax = syntaxRules?.getPreludeRules()?.slice?.(1);
1608
1648
  const blockAllowed = syntaxRules?.getBlockRules() != null;
1609
1649
  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
- }
1650
+ // if (!options.lenient) {
1651
+ errors.push({
1652
+ action: "drop",
1653
+ node: atRule,
1654
+ location: atRule.loc,
1655
+ message: "unknown at-rule",
1656
+ });
1657
+ const result = matchGenericSyntax(stream);
1658
+ // @ts-expect-error
1659
+ return Object.defineProperties(Object.assign(atRule, {
1660
+ typ: EnumToken.AtRuleNodeType,
1661
+ val: renderTokens(trimArray(stream), options),
1662
+ ...(parseAsBlock ? { chi: [] } : {}),
1663
+ }), {
1664
+ state: {
1665
+ ...definedPropertySettings,
1666
+ value: result.success ? EnumAstNodeStatus.Unknown : EnumAstNodeStatus.Invalid,
1667
+ },
1668
+ errors: {
1669
+ ...definedPropertySettings,
1670
+ value: result.success ? [errors[errors.length - 1]] : [errors[errors.length - 1], ...result.errors],
1671
+ },
1672
+ });
1673
+ // }
1625
1674
  }
1626
1675
  else if (context.typ === EnumToken.AtRuleNodeType &&
1627
1676
  "page" === context.nam &&
@@ -1634,12 +1683,20 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1634
1683
  message: parseAsBlock ? "at-rule block not supported" : "at-rule block is required",
1635
1684
  });
1636
1685
  // @ts-expect-error
1637
- return {
1638
- ...atRule,
1639
- typ: EnumToken.InvalidRuleNodeType,
1686
+ return Object.defineProperties(Object.assign(atRule, {
1687
+ typ: EnumToken.AtRuleNodeType,
1640
1688
  val: renderTokens(trimArray(stream), options),
1641
1689
  ...(parseAsBlock ? { chi: [] } : {}),
1642
- };
1690
+ }), {
1691
+ state: {
1692
+ ...definedPropertySettings,
1693
+ value: EnumAstNodeStatus.Invalid,
1694
+ },
1695
+ errors: {
1696
+ ...definedPropertySettings,
1697
+ value: [errors[errors.length - 1]],
1698
+ },
1699
+ });
1643
1700
  }
1644
1701
  const token = stream.find((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ === EnumToken.CommentTokenType) ?? null;
1645
1702
  if (token != null) {
@@ -1650,12 +1707,20 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1650
1707
  message: `unexpected token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
1651
1708
  });
1652
1709
  // @ts-expect-error
1653
- return {
1654
- ...atRule,
1655
- typ: EnumToken.InvalidRuleNodeType,
1710
+ return Object.defineProperties(Object.assign(atRule, {
1711
+ typ: EnumToken.AtRuleNodeType,
1656
1712
  val: renderTokens(trimArray(stream), options),
1657
1713
  ...(parseAsBlock ? { chi: [] } : {}),
1658
- };
1714
+ }), {
1715
+ state: {
1716
+ ...definedPropertySettings,
1717
+ value: EnumAstNodeStatus.Invalid,
1718
+ },
1719
+ errors: {
1720
+ ...definedPropertySettings,
1721
+ value: [errors[errors.length - 1]],
1722
+ },
1723
+ });
1659
1724
  }
1660
1725
  }
1661
1726
  if (parseAsBlock === null) {
@@ -1670,12 +1735,20 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1670
1735
  message: parseAsBlock ? "at-rule block not supported" : "at-rule block is required",
1671
1736
  });
1672
1737
  // @ts-expect-error
1673
- return {
1674
- ...atRule,
1675
- typ: EnumToken.InvalidRuleNodeType,
1738
+ return Object.defineProperties(Object.assign(atRule, {
1739
+ typ: EnumToken.AtRuleNodeType,
1676
1740
  val: renderTokens(trimArray(stream), options),
1677
1741
  ...(parseAsBlock ? { chi: [] } : {}),
1678
- };
1742
+ }), {
1743
+ state: {
1744
+ ...definedPropertySettings,
1745
+ value: EnumAstNodeStatus.Invalid,
1746
+ },
1747
+ errors: {
1748
+ ...definedPropertySettings,
1749
+ value: [errors[errors.length - 1]],
1750
+ },
1751
+ });
1679
1752
  }
1680
1753
  switch (atRuleName) {
1681
1754
  case "charset": {
@@ -1709,7 +1782,28 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1709
1782
  message: "expecting double-quoted string",
1710
1783
  });
1711
1784
  }
1712
- if (success && options.removeCharset) {
1785
+ if (!success) {
1786
+ // @ts-expect-error
1787
+ return Object.defineProperties(Object.assign(atRule, {
1788
+ typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1789
+ val: renderTokens(trimArray(stream), options),
1790
+ }), {
1791
+ state: {
1792
+ ...definedPropertySettings,
1793
+ value: EnumAstNodeStatus.Invalid,
1794
+ },
1795
+ errors: {
1796
+ ...definedPropertySettings,
1797
+ value: [errors[errors.length - 1]],
1798
+ },
1799
+ loc: {
1800
+ ...definedPropertySettings,
1801
+ value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
1802
+ },
1803
+ tokens: { ...definedPropertySettings, value: stream },
1804
+ });
1805
+ }
1806
+ if (options.removeCharset) {
1713
1807
  return null;
1714
1808
  }
1715
1809
  // @ts-expect-error
@@ -1717,6 +1811,14 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1717
1811
  typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1718
1812
  val: renderTokens(trimArray(stream), options),
1719
1813
  }), {
1814
+ state: {
1815
+ ...definedPropertySettings,
1816
+ value: EnumAstNodeStatus.Validated,
1817
+ },
1818
+ errors: {
1819
+ ...definedPropertySettings,
1820
+ value: [],
1821
+ },
1720
1822
  loc: {
1721
1823
  ...definedPropertySettings,
1722
1824
  value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
@@ -1731,10 +1833,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1731
1833
  }
1732
1834
  // @ts-expect-error
1733
1835
  return Object.defineProperties(Object.assign(atRule, {
1734
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1836
+ typ: EnumToken.AtRuleNodeType,
1735
1837
  val: renderTokens(trimWhiteSpaceTokens(stream), options),
1736
1838
  chi: [],
1737
1839
  }), {
1840
+ state: {
1841
+ ...definedPropertySettings,
1842
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
1843
+ },
1844
+ errors: {
1845
+ ...definedPropertySettings,
1846
+ value: result.success ? [] : result.errors,
1847
+ },
1738
1848
  loc: {
1739
1849
  ...definedPropertySettings,
1740
1850
  value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
@@ -1772,10 +1882,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1772
1882
  }
1773
1883
  // @ts-expect-error
1774
1884
  return Object.defineProperties(Object.assign(atRule, {
1775
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1885
+ typ: EnumToken.AtRuleNodeType,
1776
1886
  val: renderTokens(trimWhiteSpaceTokens(stream), options),
1777
1887
  chi: [],
1778
1888
  }), {
1889
+ state: {
1890
+ ...definedPropertySettings,
1891
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
1892
+ },
1893
+ errors: {
1894
+ ...definedPropertySettings,
1895
+ value: [errors[errors.length - 1]],
1896
+ },
1779
1897
  loc: {
1780
1898
  ...definedPropertySettings,
1781
1899
  value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
@@ -1790,10 +1908,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1790
1908
  }
1791
1909
  // @ts-expect-error
1792
1910
  return Object.defineProperties(Object.assign(atRule, {
1793
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1911
+ typ: EnumToken.AtRuleNodeType,
1794
1912
  val: renderTokens(trimWhiteSpaceTokens(stream), options),
1795
1913
  chi: [],
1796
1914
  }), {
1915
+ state: {
1916
+ ...definedPropertySettings,
1917
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
1918
+ },
1919
+ errors: {
1920
+ ...definedPropertySettings,
1921
+ value: result.success ? [] : result.errors,
1922
+ },
1797
1923
  loc: {
1798
1924
  ...definedPropertySettings,
1799
1925
  value: { ...atRule.loc, end: (stream.at(-1) ?? atRule).loc.end },
@@ -1803,7 +1929,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1803
1929
  }
1804
1930
  case "custom-media": {
1805
1931
  const tokens = trimArray(stream.slice(1));
1806
- const result = matchAllSyntax(syntax, createValidationContext(tokens), options);
1932
+ const result = matchAllSyntaxes(syntax, createValidationContext(tokens), options);
1807
1933
  if (result.errors.length > 0) {
1808
1934
  errors.push(...result.errors);
1809
1935
  }
@@ -1811,9 +1937,17 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1811
1937
  options = { ...options, convertColor: false };
1812
1938
  // @ts-expect-error
1813
1939
  return Object.defineProperties(Object.assign(atRule, {
1814
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
1940
+ typ: EnumToken.AtRuleNodeType,
1815
1941
  val: renderTokens(trimWhiteSpaceTokens(tokens), options),
1816
1942
  }), {
1943
+ state: {
1944
+ ...definedPropertySettings,
1945
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.ValidationFailed,
1946
+ },
1947
+ errors: {
1948
+ ...definedPropertySettings,
1949
+ value: result.success ? [] : result.errors,
1950
+ },
1817
1951
  loc: {
1818
1952
  ...definedPropertySettings,
1819
1953
  value: { ...atRule.loc, end: (tokens.at(-1) ?? atRule).loc.end },
@@ -1840,10 +1974,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1840
1974
  options = { ...options, convertColor: false };
1841
1975
  // @ts-expect-error
1842
1976
  return Object.defineProperties(Object.assign(atRule, {
1843
- typ: success ? EnumToken.KeyframesAtRuleNodeType : EnumToken.InvalidRuleNodeType,
1977
+ typ: EnumToken.KeyframesAtRuleNodeType,
1844
1978
  val: renderTokens(tokens, options),
1845
1979
  chi: [],
1846
1980
  }), {
1981
+ state: {
1982
+ ...definedPropertySettings,
1983
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
1984
+ },
1985
+ errors: {
1986
+ ...definedPropertySettings,
1987
+ value: success ? [] : errors[errors.length - 1],
1988
+ },
1847
1989
  loc: {
1848
1990
  ...definedPropertySettings,
1849
1991
  value: { ...atRule.loc, end: (tokens.at(-1) ?? atRule).loc.end },
@@ -1852,10 +1994,13 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1852
1994
  });
1853
1995
  }
1854
1996
  case "namespace": {
1855
- const result = matchAllSyntax(syntax, createValidationContext(stream), options);
1997
+ const result = matchAllSyntaxes(syntax, createValidationContext(stream), options);
1856
1998
  if (!result.success) {
1857
1999
  errors.push(...result.errors);
1858
2000
  }
2001
+ // else {
2002
+ // parseUrlToken(stream);
2003
+ // }
1859
2004
  const valid = blockAllowed === parseAsBlock && result.success;
1860
2005
  if (valid) {
1861
2006
  let start = 0;
@@ -1884,16 +2029,24 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1884
2029
  options = { ...options, convertColor: false };
1885
2030
  // @ts-expect-error
1886
2031
  return Object.defineProperties(Object.assign(atRule, {
1887
- typ: valid ? EnumToken.AtRuleNodeType : EnumToken.InvalidAtRuleNodeType,
2032
+ typ: EnumToken.AtRuleNodeType,
1888
2033
  val: trimArray(stream).reduce((acc, t, index) => acc +
1889
2034
  (t.typ === EnumToken.CommentTokenType ||
1890
2035
  (t.typ === EnumToken.WhitespaceTokenType &&
1891
2036
  stream[index + 1]?.typ === EnumToken.CommentTokenType &&
1892
2037
  (stream.length < index + 3 || stream[index + 2]?.typ === EnumToken.WhitespaceTokenType))
1893
2038
  ? ""
1894
- : renderToken(t, options)), ""),
2039
+ : renderValue(t, options)), ""),
1895
2040
  ...(parseAsBlock ? { chi: [] } : {}),
1896
2041
  }), {
2042
+ state: {
2043
+ ...definedPropertySettings,
2044
+ value: valid ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2045
+ },
2046
+ errors: {
2047
+ ...definedPropertySettings,
2048
+ value: valid ? [] : result.errors,
2049
+ },
1897
2050
  tokens: { ...definedPropertySettings, value: stream.slice() },
1898
2051
  loc: {
1899
2052
  ...definedPropertySettings,
@@ -1908,21 +2061,29 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1908
2061
  }
1909
2062
  else {
1910
2063
  if (stream[0]?.typ == EnumToken.UrlFunctionTokenType &&
1911
- stream[0].chi.some((t) => t.typ == EnumToken.StringTokenType)) {
2064
+ stream[0].chi.some((t) => t.typ == EnumToken.StringTokenType || t.typ == EnumToken.UrlTokenTokenType)) {
1912
2065
  stream.splice(0, 1, ...stream[0].chi);
1913
2066
  }
1914
2067
  }
1915
2068
  // @ts-expect-error
1916
2069
  return Object.defineProperties(Object.assign(atRule, {
1917
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2070
+ typ: EnumToken.AtRuleNodeType,
1918
2071
  val: stream.reduce((acc, t, index) => acc +
1919
2072
  (t.typ === EnumToken.CommentTokenType ||
1920
2073
  (t.typ === EnumToken.WhitespaceTokenType &&
1921
2074
  stream[index + 1]?.typ === EnumToken.CommentTokenType &&
1922
2075
  (stream.length < index + 3 || stream[index + 2]?.typ === EnumToken.WhitespaceTokenType))
1923
2076
  ? ""
1924
- : renderToken(t, options)), ""),
2077
+ : renderValue(t, options)), ""),
1925
2078
  }), {
2079
+ state: {
2080
+ ...definedPropertySettings,
2081
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2082
+ },
2083
+ errors: {
2084
+ ...definedPropertySettings,
2085
+ value: result.success ? [] : result.errors,
2086
+ },
1926
2087
  tokens: { ...definedPropertySettings, value: stream.slice() },
1927
2088
  loc: {
1928
2089
  ...definedPropertySettings,
@@ -1940,7 +2101,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1940
2101
  if (result.errors.length > 0) {
1941
2102
  errors.push(...result.errors);
1942
2103
  }
1943
- let success = true;
2104
+ let success = result.success;
1944
2105
  if (atRule.nam === "else") {
1945
2106
  const siblings = context.chi;
1946
2107
  let sibling = null;
@@ -1990,10 +2151,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
1990
2151
  options = { ...options, minify: false, convertColor: false };
1991
2152
  // @ts-expect-error
1992
2153
  return Object.defineProperties(Object.assign(atRule, {
1993
- typ: success && result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2154
+ typ: EnumToken.AtRuleNodeType,
1994
2155
  val: renderTokens(stream, options),
1995
2156
  chi: [],
1996
2157
  }), {
2158
+ state: {
2159
+ ...definedPropertySettings,
2160
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2161
+ },
2162
+ errors: {
2163
+ ...definedPropertySettings,
2164
+ value: result.success ? [] : [errors[errors.length - 1]].concat(result.errors),
2165
+ },
1997
2166
  tokens: { ...definedPropertySettings, value: stream.slice() },
1998
2167
  loc: {
1999
2168
  ...definedPropertySettings,
@@ -2009,10 +2178,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2009
2178
  }
2010
2179
  // @ts-expect-error
2011
2180
  return Object.defineProperties(Object.assign(atRule, {
2012
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2181
+ typ: EnumToken.AtRuleNodeType,
2013
2182
  val: renderTokens(stream, options),
2014
2183
  chi: [],
2015
2184
  }), {
2185
+ state: {
2186
+ ...definedPropertySettings,
2187
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2188
+ },
2189
+ errors: {
2190
+ ...definedPropertySettings,
2191
+ value: result.success ? [] : result.errors,
2192
+ },
2016
2193
  tokens: { ...definedPropertySettings, value: stream.slice() },
2017
2194
  loc: {
2018
2195
  ...definedPropertySettings,
@@ -2111,10 +2288,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2111
2288
  }
2112
2289
  // @ts-expect-error
2113
2290
  return Object.defineProperties(Object.assign(atRule, {
2114
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2291
+ typ: EnumToken.AtRuleNodeType,
2115
2292
  val: renderTokens(stream, options),
2116
2293
  chi: [],
2117
2294
  }), {
2295
+ state: {
2296
+ ...definedPropertySettings,
2297
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2298
+ },
2299
+ errors: {
2300
+ ...definedPropertySettings,
2301
+ value: success ? [] : [errors[errors.length - 1]],
2302
+ },
2118
2303
  tokens: { ...definedPropertySettings, value: stream.slice() },
2119
2304
  loc: {
2120
2305
  ...definedPropertySettings,
@@ -2124,13 +2309,20 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2124
2309
  }
2125
2310
  case "page": {
2126
2311
  trimArray(stream);
2127
- parseAtRulePage(atRule, stream, options, errors);
2128
2312
  // @ts-expect-error
2129
2313
  return Object.defineProperties(Object.assign(atRule, {
2130
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2314
+ typ: EnumToken.AtRuleNodeType,
2131
2315
  val: renderTokens(stream, options),
2132
2316
  chi: [],
2133
2317
  }), {
2318
+ state: {
2319
+ ...definedPropertySettings,
2320
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2321
+ },
2322
+ errors: {
2323
+ ...definedPropertySettings,
2324
+ value: success ? [] : [errors[errors.length - 1]],
2325
+ },
2134
2326
  tokens: { ...definedPropertySettings, value: stream.slice() },
2135
2327
  loc: {
2136
2328
  ...definedPropertySettings,
@@ -2180,10 +2372,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2180
2372
  }
2181
2373
  // @ts-expect-error
2182
2374
  return Object.defineProperties(Object.assign(atRule, {
2183
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2375
+ typ: EnumToken.AtRuleNodeType,
2184
2376
  val: renderTokens(stream, options),
2185
2377
  chi: [],
2186
2378
  }), {
2379
+ state: {
2380
+ ...definedPropertySettings,
2381
+ value: success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2382
+ },
2383
+ errors: {
2384
+ ...definedPropertySettings,
2385
+ value: success ? [] : [errors[errors.length - 1]],
2386
+ },
2187
2387
  tokens: { ...definedPropertySettings, value: stream.slice() },
2188
2388
  loc: {
2189
2389
  ...definedPropertySettings,
@@ -2222,15 +2422,25 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2222
2422
  // @value <ident>: <string>; // import from file as alias
2223
2423
  // @value id : <declaration-value>; // variable declaration
2224
2424
  // @value <ident># from <ident>; // import variables from alias
2225
- let result = matchAllSyntax(syntaxRules?.getPreludeRules()?.slice?.(1), createValidationContext(stream), options);
2425
+ let result = matchAllSyntaxes(syntaxRules?.getPreludeRules()?.slice?.(1), createValidationContext(stream), options);
2226
2426
  if (!result.success) {
2227
2427
  errors.push(...result.errors);
2228
- return Object.defineProperty({
2229
- typ: EnumToken.InvalidAtRuleNodeType,
2428
+ return Object.defineProperties({
2429
+ typ: EnumToken.AtRuleNodeType,
2230
2430
  val: renderTokens(stream, options),
2231
- }, "loc", {
2232
- ...definedPropertySettings,
2233
- value: { ...atRule.loc, end: { ...(stream.at(-1)?.loc?.end ?? atRule.loc.end) } },
2431
+ }, {
2432
+ state: {
2433
+ ...definedPropertySettings,
2434
+ value: EnumAstNodeStatus.Invalid,
2435
+ },
2436
+ errors: {
2437
+ ...definedPropertySettings,
2438
+ value: result.errors,
2439
+ },
2440
+ loc: {
2441
+ ...definedPropertySettings,
2442
+ value: { ...atRule.loc, end: { ...(stream.at(-1)?.loc?.end ?? atRule.loc.end) } },
2443
+ },
2234
2444
  });
2235
2445
  }
2236
2446
  if (isVarDeclaration) {
@@ -2238,18 +2448,28 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2238
2448
  const value = trimArray(stream.slice(index + 1).filter((t) => t.typ != EnumToken.CommentTokenType));
2239
2449
  if (value.length == 1 && value[0].typ == EnumToken.StringTokenType) {
2240
2450
  // import from file as alias
2241
- return {
2451
+ return Object.defineProperties({
2242
2452
  typ: EnumToken.CssVariableImportTokenType,
2243
2453
  nam: nam.val,
2244
2454
  val: value,
2245
- };
2455
+ }, {
2456
+ loc: {
2457
+ ...definedPropertySettings,
2458
+ value: { ...atRule.loc, end: { ...(stream.at(-1)?.loc?.end ?? atRule.loc.end) } },
2459
+ },
2460
+ });
2246
2461
  }
2247
2462
  // import variables from alias
2248
- return {
2463
+ return Object.defineProperties({
2249
2464
  typ: EnumToken.CssVariableTokenType,
2250
2465
  nam: nam.val,
2251
2466
  val: value,
2252
- };
2467
+ }, {
2468
+ loc: {
2469
+ ...definedPropertySettings,
2470
+ value: { ...atRule.loc, end: { ...(stream.at(-1)?.loc?.end ?? atRule.loc.end) } },
2471
+ },
2472
+ });
2253
2473
  }
2254
2474
  // @ts-expect-error
2255
2475
  return Object.defineProperties(Object.assign(atRule, {
@@ -2272,13 +2492,19 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2272
2492
  // check commas , or ,,
2273
2493
  // check colon :
2274
2494
  // check or and and
2275
- result = matchGenericSyntax(atRule, stream);
2495
+ result = matchGenericSyntax(stream);
2276
2496
  if (result.errors.length > 0) {
2277
2497
  errors.push(...result.errors);
2278
2498
  }
2279
2499
  }
2280
2500
  else {
2281
2501
  result = matchAtRuleSyntax(atRule, stream, options);
2502
+ if (result.errors.length > 0) {
2503
+ errors.push(...result.errors);
2504
+ }
2505
+ // else if (atRuleName === "document") {
2506
+ // parseUrlToken(stream);
2507
+ // }
2282
2508
  if (result.success) {
2283
2509
  let i = 0;
2284
2510
  const stack = [];
@@ -2304,10 +2530,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2304
2530
  }
2305
2531
  // @ts-expect-error
2306
2532
  return Object.defineProperties(Object.assign(atRule, {
2307
- typ: result.success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2533
+ typ: EnumToken.AtRuleNodeType,
2308
2534
  val: renderTokens(trimWhiteSpaceTokens(stream), options),
2309
2535
  ...(parseAsBlock ? { chi: [] } : {}),
2310
2536
  }), {
2537
+ state: {
2538
+ ...definedPropertySettings,
2539
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
2540
+ },
2541
+ errors: {
2542
+ ...definedPropertySettings,
2543
+ value: result.errors,
2544
+ },
2311
2545
  tokens: { ...definedPropertySettings, value: stream.slice() },
2312
2546
  loc: {
2313
2547
  ...definedPropertySettings,
@@ -2318,7 +2552,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2318
2552
  }
2319
2553
  }
2320
2554
  /**
2321
- * parse a string as an array of declaration nodes
2555
+ * Parse a string as an array of declaration nodes
2322
2556
  * @param declaration
2323
2557
  *
2324
2558
  * Example:
@@ -2337,16 +2571,17 @@ async function parseDeclarations(declaration) {
2337
2571
  currentPosition: { ind: -1, lin: 1, col: 0 },
2338
2572
  }), { setParent: false, minify: false, validation: false }).then((result) => {
2339
2573
  return result.ast.chi[0].chi.filter((t) => t.typ == EnumToken.DeclarationNodeType ||
2340
- t.typ == EnumToken.CommentNodeType ||
2341
- t.typ == EnumToken.InvalidDeclarationNodeType);
2574
+ t.typ == EnumToken.CommentNodeType);
2342
2575
  });
2343
2576
  }
2344
2577
  /**
2345
- * parse css string and return an array of tokens
2578
+ * Parse css string and return an array of tokens
2346
2579
  * @param src
2347
2580
  * @param options
2348
- *
2349
- * @private
2581
+ * - parseColor: parse identifiers as colors
2582
+ * - src: source url used for source map
2583
+ * @param errors capture parse errors in the provided array
2584
+
2350
2585
  *
2351
2586
  * Example:
2352
2587
  *
@@ -2357,26 +2592,35 @@ async function parseDeclarations(declaration) {
2357
2592
  * let tokens = parseString('body { color: red; }');
2358
2593
  * console.log(tokens);
2359
2594
  *
2360
- * tokens = parseString('#c322c980');
2595
+ * tokens = parseString('#c322c980');
2361
2596
  * console.log(tokens);
2362
2597
  * ```
2363
2598
  */
2364
- function parseString(src, options = { location: false }) {
2599
+ function parseString(src, options = { parseColor: true }, errors) {
2365
2600
  const parseInfo = {
2366
2601
  stream: src,
2367
- src: options.src ?? "",
2602
+ src: options?.src ?? "",
2368
2603
  buffer: "",
2369
2604
  offset: 0,
2370
2605
  time: 0,
2371
2606
  position: { ind: 0, lin: 1, col: 1 },
2372
2607
  currentPosition: { ind: -1, lin: 1, col: 0 },
2373
2608
  };
2374
- return parseTokens([...tokenize(parseInfo)].map((t) => t.token), { sourcemap: options.location }).slice(0, -1);
2609
+ const result = parseTokens([...tokenize(parseInfo)].map((t) => t.token), options, errors);
2610
+ // remove EOF token
2611
+ result.pop();
2612
+ if (result.at(-1)?.typ === EnumToken.WhitespaceTokenType) {
2613
+ result.pop();
2614
+ }
2615
+ return result;
2375
2616
  }
2376
2617
  /**
2377
- * parse function tokens in a token array
2618
+ * Parse function tokens in a token array
2378
2619
  * @param tokens
2379
2620
  * @param options
2621
+ * - parseColor: parse identifiers as colors
2622
+ * @param errors parse errors
2623
+ *
2380
2624
  *
2381
2625
  * Example:
2382
2626
  *
@@ -2393,62 +2637,145 @@ function parseString(src, options = { location: false }) {
2393
2637
  *
2394
2638
  * @private
2395
2639
  */
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;
2640
+ function parseTokens(tokens, options, errors) {
2641
+ const stack = [];
2642
+ let i = 0;
2643
+ let index;
2644
+ let t;
2645
+ options ??= { parseColor: true };
2646
+ for (; i < tokens.length; i++) {
2647
+ t = tokens[i];
2648
+ if (t.typ === EnumToken.FunctionTokenDefType) {
2649
+ if (tokens[i - 1]?.typ === EnumToken.ColonTokenType ||
2650
+ tokens[i - 1]?.typ === EnumToken.DoubleColonTokenType) {
2651
+ t = Object.assign(tokens[i - 1], {
2652
+ typ: EnumToken.PseudoClassFunctionTokenDefType,
2653
+ val: (tokens[i - 1].typ === EnumToken.ColonTokenType ? ":" : "::") +
2654
+ tokens[i].val,
2655
+ });
2656
+ t.loc.end = tokens[i].loc.end;
2657
+ tokens.splice(i--, 1);
2419
2658
  }
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;
2659
+ }
2660
+ if (t.typ === EnumToken.AttrStartTokenType ||
2661
+ t.typ === EnumToken.StartParensTokenType ||
2662
+ tokensfuncDefMap.has(t.typ)) {
2663
+ stack.push(t);
2664
+ continue;
2665
+ }
2666
+ if (t.typ === EnumToken.EndParensTokenType) {
2667
+ if (stack.length === 0 ||
2668
+ (stack.at(-1)?.typ !== EnumToken.StartParensTokenType && !tokensfuncDefMap.has(stack.at(-1)?.typ))) {
2669
+ // unbalanced parens
2670
+ const node = stack.at(-1);
2671
+ errors?.push?.({
2672
+ action: "drop",
2673
+ message: `Unbalanced token ')' at ${node.loc.src}:${node.loc.sta.lin}:${node.loc.sta.col}`,
2674
+ node,
2675
+ location: node.loc,
2676
+ });
2677
+ // return [];
2678
+ continue;
2431
2679
  }
2432
- if (foundLeft > 0 && foundRight == 1) {
2433
- while (left?.[0].typ == EnumToken.WhitespaceTokenType) {
2434
- left.shift();
2680
+ tokens.splice(i, 1);
2681
+ index = tokens.indexOf(stack.at(-1));
2682
+ Object.assign(tokens[index], {
2683
+ typ: tokens[index].typ === EnumToken.StartParensTokenType
2684
+ ? EnumToken.ParensTokenType
2685
+ : tokensfuncDefMap.get(tokens[index].typ),
2686
+ chi: trimArray(tokens.splice(index + 1, i - index - 1)),
2687
+ });
2688
+ i = index;
2689
+ if (tokens[index].typ === EnumToken.ColorTokenType && options?.parseColor) {
2690
+ parseColor(tokens[index]);
2691
+ }
2692
+ stack.pop();
2693
+ continue;
2694
+ }
2695
+ if (t.typ === EnumToken.AttrEndTokenType) {
2696
+ if (stack.at(-1)?.typ !== EnumToken.AttrStartTokenType) {
2697
+ // unbalanced
2698
+ const node = stack.at(-1);
2699
+ errors?.push?.({
2700
+ action: "drop",
2701
+ message: `Unbalanced token ']' at ${node.loc.src}:${node.loc.sta.lin}:${node.loc.sta.col}`,
2702
+ node,
2703
+ location: node.loc,
2704
+ });
2705
+ // return [];
2706
+ continue;
2707
+ }
2708
+ index = tokens.indexOf(stack.at(-1));
2709
+ const attr = stack.at(-1);
2710
+ attr.loc.end = t.loc.end;
2711
+ tokens.splice(i, 1);
2712
+ Object.assign(attr, {
2713
+ typ: EnumToken.AttrTokenType,
2714
+ chi: tokens.splice(index + 1, i - index - 1),
2715
+ });
2716
+ tokens[index] = attr;
2717
+ i = index;
2718
+ stack.pop();
2719
+ continue;
2720
+ }
2721
+ if (t.typ == EnumToken.IdenTokenType) {
2722
+ if (t.val == "from" && i > 0) {
2723
+ const left = [];
2724
+ const right = [];
2725
+ let foundLeft = 0;
2726
+ let foundRight = 0;
2727
+ let k = i;
2728
+ let l = i;
2729
+ while (k > 0) {
2730
+ if (tokens[k - 1].typ == EnumToken.CommentTokenType ||
2731
+ tokens[k - 1].typ == EnumToken.WhitespaceTokenType) {
2732
+ left.push(tokens[--k]);
2733
+ continue;
2734
+ }
2735
+ if (tokens[k - 1].typ == EnumToken.IdenTokenType ||
2736
+ tokens[k - 1].typ == EnumToken.DashedIdenTokenType) {
2737
+ foundLeft++;
2738
+ left.push(tokens[--k]);
2739
+ continue;
2740
+ }
2741
+ break;
2435
2742
  }
2436
- while (left.at(-1)?.typ == EnumToken.WhitespaceTokenType) {
2437
- left.pop();
2743
+ while (++l < tokens.length) {
2744
+ if (tokens[l].typ == EnumToken.CommentTokenType || tokens[l].typ == EnumToken.WhitespaceTokenType) {
2745
+ right.push(tokens[l]);
2746
+ continue;
2747
+ }
2748
+ if (tokens[l].typ == EnumToken.IdenTokenType || tokens[l].typ == EnumToken.StringTokenType) {
2749
+ foundRight++;
2750
+ right.push(tokens[l]);
2751
+ continue;
2752
+ }
2753
+ break;
2438
2754
  }
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
2755
+ if (foundLeft > 0 && foundRight == 1) {
2756
+ while (left?.[0].typ == EnumToken.WhitespaceTokenType) {
2757
+ left.shift();
2758
+ }
2759
+ while (left.at(-1)?.typ == EnumToken.WhitespaceTokenType) {
2760
+ left.pop();
2761
+ }
2762
+ tokens.splice(k, l - k + 1, {
2763
+ typ: EnumToken.ComposesSelectorNodeType,
2764
+ l: left,
2765
+ r: right.reduce((a, b) => {
2766
+ return a == null
2446
2767
  ? b
2447
- : a;
2448
- }, null),
2449
- });
2450
- i = k;
2451
- continue;
2768
+ : b.typ == EnumToken.IdenTokenType || b.typ == EnumToken.StringTokenType
2769
+ ? b
2770
+ : a;
2771
+ }, null),
2772
+ });
2773
+ i = k;
2774
+ continue;
2775
+ }
2776
+ }
2777
+ else if (options?.parseColor && isColor(t)) {
2778
+ parseColor(t);
2452
2779
  }
2453
2780
  }
2454
2781
  if (t.typ == EnumToken.WhitespaceTokenType &&
@@ -2479,290 +2806,27 @@ function parseTokens(tokens, options = {}) {
2479
2806
  i--;
2480
2807
  }
2481
2808
  }
2482
- continue;
2483
2809
  }
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
- }
2810
+ if (t.typ === EnumToken.CommaTokenType) {
2811
+ if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
2812
+ tokens.splice(i + 1, 1);
2497
2813
  }
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
- }
2814
+ if (tokens[i - 1]?.typ === EnumToken.WhitespaceTokenType) {
2815
+ tokens.splice(i - 1, 1);
2816
+ i--;
2763
2817
  }
2764
2818
  }
2765
2819
  }
2820
+ if (stack.length > 0) {
2821
+ const node = stack.at(-1);
2822
+ errors?.push?.({
2823
+ action: "drop",
2824
+ message: `Unbalanced token. Expecting ${node.typ === EnumToken.AttrStartTokenType ? "']'" : ")"}'`,
2825
+ node,
2826
+ location: node.loc,
2827
+ });
2828
+ // return [];
2829
+ }
2766
2830
  return tokens;
2767
2831
  }
2768
2832