@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,13 +1,14 @@
1
- import { EnumToken, ColorType, ValidationLevel } from '../../ast/types.js';
1
+ import { EnumToken, EnumAstNodeStatus, ColorType, ValidationLevel } from '../../ast/types.js';
2
2
  import { definedPropertySettings, tokensfuncDefMap, COLORS_NAMES, nonStandardColors, systemColors, deprecatedSystemColors, tokensMap, trimTokenSpace } from '../../syntax/constants.js';
3
3
  import { renamedStandardProperties, isColor, parseColor, isWhiteSpace, isValue } from '../../syntax/syntax.js';
4
4
  import { getSyntaxRule, getParsedSyntax } from '../../validation/config.js';
5
- import { trimArray, matchAllSyntax, createValidationContext } from '../../validation/match.js';
5
+ import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
6
6
  import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
7
7
  import { walkValues } from '../../ast/walk.js';
8
8
  import { equalsIgnoreCase } from './text.js';
9
9
  import { buildExpression } from '../../ast/math/expression.js';
10
10
  import { splitTokenList } from '../../validation/utils/list.js';
11
+ import { parseTokens } from '../parse.js';
11
12
 
12
13
  /**
13
14
  *
@@ -86,7 +87,7 @@ function isDeclarationValue(tokens) {
86
87
  };
87
88
  }
88
89
  /**
89
- * parse declaration
90
+ * Parse declaration
90
91
  * @param tokens
91
92
  * @param parent
92
93
  * @param options
@@ -100,7 +101,7 @@ function parseDeclaration(tokens, parent, options, errors) {
100
101
  let success = true;
101
102
  let validate = typeof options.validation === "boolean"
102
103
  ? options.validation
103
- : options.validation & ValidationLevel.Declaration;
104
+ : !!options.validation;
104
105
  if (renamedStandardProperties.has(name.val)) {
105
106
  name.val = renamedStandardProperties.get(name.val);
106
107
  }
@@ -115,14 +116,31 @@ function parseDeclaration(tokens, parent, options, errors) {
115
116
  }
116
117
  if ((name.typ !== EnumToken.IdenTokenType && name.typ !== EnumToken.DashedIdenTokenType) ||
117
118
  tokens[i]?.typ !== EnumToken.ColonTokenType) {
118
- return Object.defineProperty(Object.assign({
119
+ return Object.defineProperties(Object.assign({
119
120
  typ: EnumToken.RawNodeTokenType,
120
- chi: tokens,
121
- }), "loc", {
122
- ...definedPropertySettings,
123
- value: {
124
- ...name.loc,
125
- end: tokens[tokens.length - 1]?.loc?.end ?? name.loc.end,
121
+ val: tokens,
122
+ }), {
123
+ state: {
124
+ ...definedPropertySettings,
125
+ value: EnumAstNodeStatus.Unparsed,
126
+ },
127
+ errors: {
128
+ ...definedPropertySettings,
129
+ value: [
130
+ {
131
+ action: "drop",
132
+ node: name,
133
+ location: name.loc,
134
+ message: "invalid declaration",
135
+ },
136
+ ],
137
+ },
138
+ loc: {
139
+ ...definedPropertySettings,
140
+ value: {
141
+ ...name.loc,
142
+ end: tokens[tokens.length - 1]?.loc?.end ?? name.loc.end,
143
+ },
126
144
  },
127
145
  });
128
146
  }
@@ -173,7 +191,7 @@ function parseDeclaration(tokens, parent, options, errors) {
173
191
  }
174
192
  if (validate && name.typ === EnumToken.IdenTokenType) {
175
193
  if (parent != null &&
176
- (parent.typ == EnumToken.AtRuleNodeType || parent.typ === EnumToken.InvalidAtRuleNodeType) &&
194
+ parent.typ === EnumToken.AtRuleNodeType &&
177
195
  parent.nam != "import" &&
178
196
  parent.nam != "supports" &&
179
197
  parent.nam != "media") {
@@ -188,31 +206,54 @@ function parseDeclaration(tokens, parent, options, errors) {
188
206
  }
189
207
  else {
190
208
  syntaxRules =
191
- rules.acceptAnyDeclarations && rules.acceptAnyRules
209
+ rules.acceptAnyDeclaration && rules.acceptAnyRule
192
210
  ? getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, name.val.toLowerCase())
193
211
  : rules.getBlockRules();
194
212
  if (syntaxRules == null) {
195
- errors.push({
196
- action: "drop",
197
- message: "declaration not allowed in context",
198
- node: name,
199
- location: name.loc,
200
- });
201
- return Object.defineProperty(Object.assign(name, {
202
- typ: EnumToken.InvalidDeclarationNodeType,
203
- nam: name.val,
204
- val: tokens,
205
- }), "loc", {
206
- ...definedPropertySettings,
207
- value: {
208
- ...name.loc,
209
- end: tokens[tokens.length - 1].loc.end,
210
- },
211
- });
213
+ // check rule in nested context
214
+ let pr = parent.parent;
215
+ while (pr != null && pr.typ !== EnumToken.RuleNodeType) {
216
+ pr = pr.parent;
217
+ }
218
+ if (pr != null) {
219
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, name.val.toLowerCase());
220
+ }
221
+ if (syntaxRules == null) {
222
+ errors.push({
223
+ action: "drop",
224
+ message: "declaration not allowed in context",
225
+ node: name,
226
+ location: name.loc,
227
+ });
228
+ return Object.defineProperties(Object.assign(name, {
229
+ typ: EnumToken.DeclarationNodeType,
230
+ nam: name.val,
231
+ val: tokens,
232
+ }), {
233
+ state: {
234
+ ...definedPropertySettings,
235
+ value: EnumAstNodeStatus.Disallowed,
236
+ },
237
+ errors: {
238
+ ...definedPropertySettings,
239
+ value: [errors[errors.length - 1]],
240
+ },
241
+ loc: {
242
+ ...definedPropertySettings,
243
+ value: {
244
+ ...name.loc,
245
+ end: tokens[tokens.length - 1].loc.end,
246
+ },
247
+ },
248
+ });
249
+ }
212
250
  }
213
251
  }
214
252
  }
215
253
  }
254
+ else {
255
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, name.val.toLowerCase());
256
+ }
216
257
  // <declaration-list> or <declaration-rule-list>
217
258
  // else
218
259
  if (syntaxRules == null ||
@@ -246,15 +287,25 @@ function parseDeclaration(tokens, parent, options, errors) {
246
287
  node: name,
247
288
  location: name.loc,
248
289
  });
249
- return Object.defineProperty(Object.assign(name, {
250
- typ: EnumToken.InvalidDeclarationNodeType,
290
+ return Object.defineProperties(Object.assign(name, {
291
+ typ: EnumToken.DeclarationNodeType,
251
292
  nam: name.val,
252
293
  val: tokens,
253
- }), "loc", {
254
- ...definedPropertySettings,
255
- value: {
256
- ...name.loc,
257
- end: tokens[tokens.length - 1]?.loc.end ?? name.loc.end,
294
+ }), {
295
+ state: {
296
+ ...definedPropertySettings,
297
+ value: EnumAstNodeStatus.Invalid,
298
+ },
299
+ errors: {
300
+ ...definedPropertySettings,
301
+ value: [errors[errors.length - 1]],
302
+ },
303
+ loc: {
304
+ ...definedPropertySettings,
305
+ value: {
306
+ ...name.loc,
307
+ end: tokens[tokens.length - 1]?.loc.end ?? name.loc.end,
308
+ },
258
309
  },
259
310
  });
260
311
  }
@@ -264,7 +315,7 @@ function parseDeclaration(tokens, parent, options, errors) {
264
315
  let index;
265
316
  if (syntaxRules != null) {
266
317
  const doNotValidate = options.validation === false || options.validation === ValidationLevel.None;
267
- result = doNotValidate ? null : matchAllSyntax(syntaxRules, createValidationContext(tokens), options);
318
+ result = doNotValidate ? null : matchAllSyntaxes(syntaxRules, createValidationContext(tokens), options);
268
319
  if (doNotValidate || result != null) {
269
320
  success = doNotValidate || result?.success;
270
321
  if (success) {
@@ -283,15 +334,25 @@ function parseDeclaration(tokens, parent, options, errors) {
283
334
  errors.push(...result.errors);
284
335
  }
285
336
  if (!doNotValidate && !(success || result.success)) {
286
- return Object.defineProperty(Object.assign(name, {
287
- typ: EnumToken.InvalidDeclarationNodeType,
337
+ return Object.defineProperties(Object.assign(name, {
338
+ typ: EnumToken.DeclarationNodeType,
288
339
  nam: name.val,
289
- val: tokens,
290
- }), "loc", {
291
- ...definedPropertySettings,
292
- value: {
293
- ...name.loc,
294
- end: tokens[tokens.length - 1]?.loc.end ?? name.loc.end,
340
+ val: parseTokens(tokens),
341
+ }), {
342
+ state: {
343
+ ...definedPropertySettings,
344
+ value: EnumAstNodeStatus.ValidationFailed,
345
+ },
346
+ errors: {
347
+ ...definedPropertySettings,
348
+ value: result?.errors ?? [],
349
+ },
350
+ loc: {
351
+ ...definedPropertySettings,
352
+ value: {
353
+ ...name.loc,
354
+ end: tokens[tokens.length - 1]?.loc.end ?? name.loc.end,
355
+ },
295
356
  },
296
357
  });
297
358
  }
@@ -341,15 +402,25 @@ function parseDeclaration(tokens, parent, options, errors) {
341
402
  node: token,
342
403
  location: token.loc,
343
404
  });
344
- return Object.defineProperty(Object.assign(name, {
345
- typ: EnumToken.InvalidDeclarationNodeType,
405
+ return Object.defineProperties(Object.assign(name, {
406
+ typ: EnumToken.DeclarationNodeType,
346
407
  nam: name.val,
347
408
  val: tokens,
348
- }), "loc", {
349
- ...definedPropertySettings,
350
- value: {
351
- ...name.loc,
352
- end: tokens[tokens.length - 1]?.loc.end ?? name.loc.end,
409
+ }), {
410
+ state: {
411
+ ...definedPropertySettings,
412
+ value: EnumAstNodeStatus.Invalid,
413
+ },
414
+ errors: {
415
+ ...definedPropertySettings,
416
+ value: errors[errors.length - 1],
417
+ },
418
+ loc: {
419
+ ...definedPropertySettings,
420
+ value: {
421
+ ...name.loc,
422
+ end: tokens[tokens.length - 1]?.loc.end ?? name.loc.end,
423
+ },
353
424
  },
354
425
  });
355
426
  }
@@ -502,15 +573,25 @@ function parseDeclaration(tokens, parent, options, errors) {
502
573
  node: stack[stack.length - 1],
503
574
  location: stack[stack.length - 1].loc,
504
575
  });
505
- return Object.defineProperty(Object.assign(name, {
506
- typ: EnumToken.InvalidDeclarationNodeType,
576
+ return Object.defineProperties(Object.assign(name, {
577
+ typ: EnumToken.DeclarationNodeType,
507
578
  nam: name.val,
508
579
  val: tokens,
509
- }), "loc", {
510
- ...definedPropertySettings,
511
- value: {
512
- ...name.loc,
513
- end: tokens[tokens.length - 1].loc.end,
580
+ }), {
581
+ state: {
582
+ ...definedPropertySettings,
583
+ value: EnumAstNodeStatus.Invalid,
584
+ },
585
+ errors: {
586
+ ...definedPropertySettings,
587
+ value: result?.errors ?? [],
588
+ },
589
+ loc: {
590
+ ...definedPropertySettings,
591
+ value: {
592
+ ...name.loc,
593
+ end: tokens[tokens.length - 1].loc.end,
594
+ },
514
595
  },
515
596
  });
516
597
  }
@@ -537,17 +618,25 @@ function parseDeclaration(tokens, parent, options, errors) {
537
618
  }
538
619
  }
539
620
  if (validate && syntaxRules == null && name.typ === EnumToken.IdenTokenType) {
540
- const node = Object.defineProperty(Object.assign(name, {
541
- typ: options.validation & ValidationLevel.Declaration
542
- ? EnumToken.InvalidDeclarationNodeType
543
- : EnumToken.DeclarationNodeType,
621
+ const node = Object.defineProperties(Object.assign(name, {
622
+ typ: EnumToken.DeclarationNodeType,
544
623
  nam: name.val,
545
624
  val: tokens,
546
- }), "loc", {
547
- ...definedPropertySettings,
548
- value: {
549
- ...name.loc,
550
- end: tokens[tokens.length - 1]?.loc?.end ?? name.loc.end,
625
+ }), {
626
+ state: {
627
+ ...definedPropertySettings,
628
+ value: EnumAstNodeStatus.Unknown,
629
+ },
630
+ errors: {
631
+ ...definedPropertySettings,
632
+ value: result?.errors ?? [],
633
+ },
634
+ loc: {
635
+ ...definedPropertySettings,
636
+ value: {
637
+ ...name.loc,
638
+ end: tokens[tokens.length - 1]?.loc?.end ?? name.loc.end,
639
+ },
551
640
  },
552
641
  });
553
642
  if (options.validation & ValidationLevel.Declaration) {
@@ -585,15 +674,29 @@ function parseDeclaration(tokens, parent, options, errors) {
585
674
  }),
586
675
  ];
587
676
  }
588
- return Object.defineProperty(Object.assign(name, {
589
- typ: success ? EnumToken.DeclarationNodeType : EnumToken.InvalidDeclarationNodeType,
677
+ return Object.defineProperties(Object.assign(name, {
678
+ typ: success ? EnumToken.DeclarationNodeType : EnumToken.DeclarationNodeType,
590
679
  nam: name.val,
591
680
  val: tokens,
592
- }), "loc", {
593
- ...definedPropertySettings,
594
- value: {
595
- ...name.loc,
596
- end: (tokens[tokens.length - 1] ?? name).loc.end,
681
+ }), {
682
+ state: {
683
+ ...definedPropertySettings,
684
+ value: success
685
+ ? result == null
686
+ ? EnumAstNodeStatus.Unvalidated
687
+ : EnumAstNodeStatus.Validated
688
+ : EnumAstNodeStatus.ValidationFailed,
689
+ },
690
+ errors: {
691
+ ...definedPropertySettings,
692
+ value: result?.errors ?? [],
693
+ },
694
+ loc: {
695
+ ...definedPropertySettings,
696
+ value: {
697
+ ...name.loc,
698
+ end: (tokens[tokens.length - 1] ?? name).loc.end,
699
+ },
597
700
  },
598
701
  });
599
702
  }
@@ -1,10 +1,10 @@
1
- import { EnumToken } from '../../ast/types.js';
2
- import { renderToken } from '../../renderer/render.js';
1
+ import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
2
+ import { renderValue } from '../../renderer/render.js';
3
3
  import { definedPropertySettings, tokensfuncDefMap, combinators } from '../../syntax/constants.js';
4
4
  import { pseudoElements, isIdent, isHash } from '../../syntax/syntax.js';
5
- import { getParsedSyntax, getSyntaxConfig } from '../../validation/config.js';
6
- import { matchAllSyntax, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
7
- import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
5
+ import { getParsedSyntax, getSyntaxRule, getSyntaxConfig } from '../../validation/config.js';
6
+ import { matchAllSyntaxes, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
7
+ import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
8
8
  import { splitTokenList } from '../../validation/utils/list.js';
9
9
  import { trimWhiteSpace } from '../parse.js';
10
10
 
@@ -13,7 +13,7 @@ import { trimWhiteSpace } from '../parse.js';
13
13
  */
14
14
  function parseSelector(tokens, context, options, errors) {
15
15
  if (context?.typ === EnumToken.KeyframesAtRuleNodeType) {
16
- const result = matchAllSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(tokens), options);
16
+ const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(tokens), options);
17
17
  const parts = splitTokenList(tokens);
18
18
  for (const part of parts) {
19
19
  trimArray(part);
@@ -52,15 +52,23 @@ function parseSelector(tokens, context, options, errors) {
52
52
  return acc;
53
53
  }, []));
54
54
  return Object.defineProperties({
55
- typ: result.success ? EnumToken.KeyFramesRuleNodeType : EnumToken.InvalidRuleNodeType,
55
+ typ: EnumToken.KeyFramesRuleNodeType,
56
56
  sel: [
57
57
  ...splitTokenList(trimArray(tokens)).reduce((acc, curr) => {
58
- acc.add(curr.reduce((acc, curr) => acc + renderToken(curr, { minify: false }), ""));
58
+ acc.add(curr.reduce((acc, curr) => acc + renderValue(curr, { minify: false }), ""));
59
59
  return acc;
60
60
  }, new Set()),
61
61
  ].join(),
62
62
  chi: [],
63
63
  }, {
64
+ state: {
65
+ ...definedPropertySettings,
66
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
67
+ },
68
+ errors: {
69
+ ...definedPropertySettings,
70
+ value: result.errors,
71
+ },
64
72
  tokens: { ...definedPropertySettings, value: tokens.length === 0 ? null : tokens },
65
73
  loc: {
66
74
  ...definedPropertySettings,
@@ -73,11 +81,33 @@ function parseSelector(tokens, context, options, errors) {
73
81
  }
74
82
  const stack = [];
75
83
  const uniq = new Map();
84
+ let allowed = true;
76
85
  let i = 0;
77
86
  let index;
78
87
  let parent = context;
79
88
  let nested = false;
80
89
  let val;
90
+ if (context?.typ !== EnumToken.StyleSheetNodeType && context?.typ !== EnumToken.RuleNodeType) {
91
+ allowed = false;
92
+ if (context?.typ === EnumToken.AtRuleNodeType) {
93
+ const syntaxRule = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + context.nam);
94
+ allowed = syntaxRule?.acceptAnyRule ?? false;
95
+ if (!allowed) {
96
+ const rules = syntaxRule?.getBlockRules?.();
97
+ if (rules != null) {
98
+ for (const rule of rules) {
99
+ if (ValidationTokenEnum.PropertyType === rule.typ) {
100
+ if ("block-contents" === rule.val ||
101
+ "rule-list" === rule.val) {
102
+ allowed = true;
103
+ break;
104
+ }
105
+ }
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
81
111
  do {
82
112
  if (parent?.typ === EnumToken.AtRuleNodeType && "media" === parent.nam) {
83
113
  parent = parent.parent;
@@ -190,7 +220,7 @@ function parseSelector(tokens, context, options, errors) {
190
220
  if (tokensfuncDefMap.has(func.typ)) {
191
221
  // @ts-expect-error
192
222
  func.typ = tokensfuncDefMap.get(func.typ);
193
- func.chi = tokens.splice(index + 1, i - index - 1);
223
+ func.chi = trimArray(tokens.splice(index + 1, i - index - 1));
194
224
  }
195
225
  if (result.success && options.minify) {
196
226
  // parse an+b
@@ -419,7 +449,7 @@ function parseSelector(tokens, context, options, errors) {
419
449
  }
420
450
  }
421
451
  return Object.defineProperties({
422
- typ: result.success ? EnumToken.RuleNodeType : EnumToken.InvalidRuleNodeType,
452
+ typ: EnumToken.RuleNodeType,
423
453
  sel: [
424
454
  ...tokens
425
455
  .reduce((acc, curr, index, array) => {
@@ -434,7 +464,7 @@ function parseSelector(tokens, context, options, errors) {
434
464
  return acc;
435
465
  }
436
466
  }
437
- let t = renderToken(curr, { minify: false });
467
+ let t = renderValue(curr, { minify: false });
438
468
  if (t == ",") {
439
469
  acc.push([]);
440
470
  }
@@ -462,6 +492,18 @@ function parseSelector(tokens, context, options, errors) {
462
492
  ].join(","),
463
493
  chi: [],
464
494
  }, {
495
+ state: {
496
+ ...definedPropertySettings,
497
+ value: result.success && allowed
498
+ ? EnumAstNodeStatus.Validated
499
+ : allowed
500
+ ? EnumAstNodeStatus.Invalid
501
+ : EnumAstNodeStatus.Disallowed,
502
+ },
503
+ errors: {
504
+ ...definedPropertySettings,
505
+ value: result.success ? [] : result.errors,
506
+ },
465
507
  tokens: { ...definedPropertySettings, value: tokens },
466
508
  loc: {
467
509
  ...definedPropertySettings,
@@ -17,28 +17,30 @@ const trimSpaceAfter = new Set([
17
17
  EnumToken.SupportsQueryUnaryConditionTokenType,
18
18
  ]);
19
19
  /**
20
- * replace token in its parent node
20
+ * Replace token in its parent node
21
21
  * @param parent
22
- * @param value
22
+ * @param node
23
23
  * @param replacement
24
+ * @throws TypeError replacement is null
25
+ * @throws ReferenceError node not found in parent
24
26
  */
25
- function replaceToken(parent, value, replacement) {
27
+ function replaceNodeOrValue(parent, node, replacement) {
26
28
  if (replacement == null || (Array.isArray(replacement) && replacement.length === 0)) {
27
29
  throw new TypeError(`replacement is null`);
28
30
  }
29
31
  for (const node of Array.isArray(replacement) ? replacement : [replacement]) {
30
- if ("parent" in value && value.parent != node.parent) {
32
+ if ("parent" in node && node.parent != node.parent) {
31
33
  Object.defineProperty(node, "parent", {
32
34
  ...definedPropertySettings,
33
- value: value.parent,
35
+ value: node.parent,
34
36
  });
35
37
  }
36
38
  }
37
39
  if (parent.typ == EnumToken.BinaryExpressionTokenType) {
38
- if (parent.l == value) {
40
+ if (parent.l == node) {
39
41
  parent.l = replacement;
40
42
  }
41
- else if (parent.r == value) {
43
+ else if (parent.r == node) {
42
44
  parent.r = replacement;
43
45
  }
44
46
  else {
@@ -51,16 +53,16 @@ function replaceToken(parent, value, replacement) {
51
53
  : (parent.chi ?? parent);
52
54
  if (Array.isArray(target)) {
53
55
  // @ts-ignore
54
- const index = target.indexOf(value);
56
+ const index = target.indexOf(node);
55
57
  if (index == -1) {
56
58
  throw new ReferenceError("Node not found");
57
59
  }
58
60
  target.splice(index, 1, ...(Array.isArray(replacement) ? replacement : [replacement]));
59
61
  }
60
- else if ("l" in target && target.l == value) {
62
+ else if ("l" in target && target.l == node) {
61
63
  target.l = replacement;
62
64
  }
63
- else if ("r" in target && target.r == value) {
65
+ else if ("r" in target && target.r == node) {
64
66
  target.r = replacement;
65
67
  }
66
68
  else {
@@ -99,4 +101,4 @@ function trimWhiteSpaceTokens(tokens) {
99
101
  return tokens;
100
102
  }
101
103
 
102
- export { replaceToken, trimWhiteSpaceTokens };
104
+ export { replaceNodeOrValue, trimWhiteSpaceTokens };