@tbela99/css-parser 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/README.md +16 -11
  3. package/dist/index-umd-web.js +3613 -1829
  4. package/dist/index.cjs +3611 -1827
  5. package/dist/index.d.ts +160 -50
  6. package/dist/lib/ast/expand.js +2 -1
  7. package/dist/lib/ast/features/calc.js +12 -1
  8. package/dist/lib/ast/features/inlinecssvariables.js +47 -24
  9. package/dist/lib/ast/features/prefix.js +117 -86
  10. package/dist/lib/ast/features/shorthand.js +29 -6
  11. package/dist/lib/ast/features/transform.js +10 -3
  12. package/dist/lib/ast/features/type.js +7 -0
  13. package/dist/lib/ast/math/expression.js +7 -1
  14. package/dist/lib/ast/math/math.js +6 -0
  15. package/dist/lib/ast/minify.js +165 -77
  16. package/dist/lib/ast/transform/compute.js +1 -0
  17. package/dist/lib/ast/transform/matrix.js +1 -0
  18. package/dist/lib/ast/types.js +17 -15
  19. package/dist/lib/ast/walk.js +33 -7
  20. package/dist/lib/fs/resolve.js +10 -0
  21. package/dist/lib/parser/declaration/list.js +48 -45
  22. package/dist/lib/parser/declaration/map.js +1 -0
  23. package/dist/lib/parser/declaration/set.js +2 -1
  24. package/dist/lib/parser/parse.js +364 -276
  25. package/dist/lib/parser/tokenize.js +147 -72
  26. package/dist/lib/parser/utils/declaration.js +4 -3
  27. package/dist/lib/parser/utils/type.js +2 -1
  28. package/dist/lib/renderer/color/a98rgb.js +2 -1
  29. package/dist/lib/renderer/color/color-mix.js +10 -7
  30. package/dist/lib/renderer/color/color.js +171 -153
  31. package/dist/lib/renderer/color/hex.js +2 -1
  32. package/dist/lib/renderer/color/hsl.js +2 -1
  33. package/dist/lib/renderer/color/hwb.js +2 -1
  34. package/dist/lib/renderer/color/lab.js +2 -1
  35. package/dist/lib/renderer/color/lch.js +2 -1
  36. package/dist/lib/renderer/color/oklab.js +2 -1
  37. package/dist/lib/renderer/color/oklch.js +2 -1
  38. package/dist/lib/renderer/color/p3.js +2 -1
  39. package/dist/lib/renderer/color/rec2020.js +2 -1
  40. package/dist/lib/renderer/color/relativecolor.js +17 -11
  41. package/dist/lib/renderer/color/rgb.js +4 -3
  42. package/dist/lib/renderer/color/srgb.js +18 -17
  43. package/dist/lib/renderer/color/utils/components.js +6 -5
  44. package/dist/lib/renderer/color/utils/constants.js +47 -3
  45. package/dist/lib/renderer/color/xyz.js +2 -1
  46. package/dist/lib/renderer/color/xyzd50.js +2 -1
  47. package/dist/lib/renderer/render.js +48 -20
  48. package/dist/lib/syntax/syntax.js +253 -140
  49. package/dist/lib/validation/at-rules/container.js +75 -97
  50. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  51. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  52. package/dist/lib/validation/at-rules/document.js +22 -27
  53. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  54. package/dist/lib/validation/at-rules/import.js +30 -81
  55. package/dist/lib/validation/at-rules/keyframes.js +18 -22
  56. package/dist/lib/validation/at-rules/layer.js +5 -5
  57. package/dist/lib/validation/at-rules/media.js +42 -52
  58. package/dist/lib/validation/at-rules/namespace.js +19 -23
  59. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  60. package/dist/lib/validation/at-rules/page.js +8 -7
  61. package/dist/lib/validation/at-rules/supports.js +73 -82
  62. package/dist/lib/validation/at-rules/when.js +32 -36
  63. package/dist/lib/validation/atrule.js +15 -14
  64. package/dist/lib/validation/config.js +24 -1
  65. package/dist/lib/validation/config.json.js +563 -63
  66. package/dist/lib/validation/parser/parse.js +196 -185
  67. package/dist/lib/validation/parser/types.js +1 -1
  68. package/dist/lib/validation/selector.js +3 -3
  69. package/dist/lib/validation/syntax.js +828 -0
  70. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  71. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  72. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  73. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  74. package/dist/lib/validation/syntaxes/keyframe-block-list.js +4 -3
  75. package/dist/lib/validation/syntaxes/keyframe-selector.js +15 -18
  76. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  77. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  78. package/dist/lib/validation/syntaxes/relative-selector.js +2 -1
  79. package/dist/lib/validation/syntaxes/url.js +18 -22
  80. package/dist/lib/validation/utils/list.js +2 -1
  81. package/dist/lib/validation/utils/whitespace.js +2 -1
  82. package/dist/node/index.js +4 -2
  83. package/dist/node/load.js +5 -0
  84. package/dist/web/index.js +4 -2
  85. package/dist/web/load.js +5 -0
  86. package/package.json +12 -11
@@ -1,55 +1,52 @@
1
- import { ValidationLevel, EnumToken } from '../../ast/types.js';
1
+ import { SyntaxValidationResult, EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
3
  import '../../ast/walk.js';
4
4
  import '../../parser/parse.js';
5
+ import '../../parser/tokenize.js';
6
+ import '../../parser/utils/config.js';
5
7
  import '../../renderer/color/utils/constants.js';
6
8
  import '../../renderer/sourcemap/lib/encode.js';
7
- import '../../parser/utils/config.js';
8
9
 
9
10
  function validateAtRulePageMarginBox(atRule, options, root) {
10
11
  if (Array.isArray(atRule.tokens) && atRule.tokens.length > 0) {
11
12
  // @ts-ignore
12
13
  return {
13
- valid: ValidationLevel.Valid,
14
- matches: [],
14
+ valid: SyntaxValidationResult.Valid,
15
+ context: [],
15
16
  node: null,
16
17
  syntax: '@' + atRule.nam,
17
- error: '',
18
- tokens: []
18
+ error: ''
19
19
  };
20
20
  }
21
21
  if (!('chi' in atRule)) {
22
22
  // @ts-ignore
23
23
  return {
24
- valid: ValidationLevel.Drop,
25
- matches: [],
24
+ valid: SyntaxValidationResult.Drop,
25
+ context: [],
26
26
  node: atRule,
27
27
  syntax: '@' + atRule.nam,
28
- error: 'expected margin-box body',
29
- tokens: []
28
+ error: 'expected margin-box body'
30
29
  };
31
30
  }
32
31
  for (const token of atRule.chi) {
33
32
  if (![EnumToken.DeclarationNodeType, EnumToken.CommentNodeType, EnumToken.WhitespaceTokenType].includes(token.typ)) {
34
33
  // @ts-ignore
35
34
  return {
36
- valid: ValidationLevel.Drop,
37
- matches: [],
35
+ valid: SyntaxValidationResult.Drop,
36
+ context: [],
38
37
  node: token,
39
38
  syntax: 'declaration-list',
40
- error: 'expected margin-box body',
41
- tokens: []
39
+ error: 'expected margin-box body'
42
40
  };
43
41
  }
44
42
  }
45
43
  // @ts-ignore
46
44
  return {
47
- valid: ValidationLevel.Valid,
48
- matches: [],
45
+ valid: SyntaxValidationResult.Valid,
46
+ context: [],
49
47
  node: null,
50
48
  syntax: '@' + atRule.nam,
51
- error: '',
52
- tokens: []
49
+ error: ''
53
50
  };
54
51
  }
55
52
 
@@ -1,10 +1,11 @@
1
- import { ValidationLevel, EnumToken } from '../../ast/types.js';
1
+ import { SyntaxValidationResult, EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
3
  import '../../ast/walk.js';
4
4
  import '../../parser/parse.js';
5
+ import '../../parser/tokenize.js';
6
+ import '../../parser/utils/config.js';
5
7
  import '../../renderer/color/utils/constants.js';
6
8
  import '../../renderer/sourcemap/lib/encode.js';
7
- import '../../parser/utils/config.js';
8
9
  import { splitTokenList } from '../utils/list.js';
9
10
 
10
11
  function validateAtRulePage(atRule, options, root) {
@@ -12,7 +13,7 @@ function validateAtRulePage(atRule, options, root) {
12
13
  if (!Array.isArray(atRule.tokens) || atRule.tokens.length == 0) {
13
14
  // @ts-ignore
14
15
  return {
15
- valid: ValidationLevel.Valid,
16
+ valid: SyntaxValidationResult.Valid,
16
17
  matches: [],
17
18
  node: null,
18
19
  syntax: '@page',
@@ -25,7 +26,7 @@ function validateAtRulePage(atRule, options, root) {
25
26
  if (tokens.length == 0) {
26
27
  // @ts-ignore
27
28
  return {
28
- valid: ValidationLevel.Drop,
29
+ valid: SyntaxValidationResult.Drop,
29
30
  matches: [],
30
31
  node: tokens[0] ?? atRule,
31
32
  syntax: '@page',
@@ -44,7 +45,7 @@ function validateAtRulePage(atRule, options, root) {
44
45
  if (tokens[0].typ != EnumToken.WhitespaceTokenType) {
45
46
  // @ts-ignore
46
47
  return {
47
- valid: ValidationLevel.Drop,
48
+ valid: SyntaxValidationResult.Drop,
48
49
  matches: [],
49
50
  node: tokens[0] ?? atRule,
50
51
  syntax: '@page',
@@ -63,7 +64,7 @@ function validateAtRulePage(atRule, options, root) {
63
64
  if (tokens[0].typ != EnumToken.WhitespaceTokenType) {
64
65
  // @ts-ignore
65
66
  return {
66
- valid: ValidationLevel.Drop,
67
+ valid: SyntaxValidationResult.Drop,
67
68
  matches: [],
68
69
  node: tokens[0] ?? atRule,
69
70
  syntax: '@page',
@@ -76,7 +77,7 @@ function validateAtRulePage(atRule, options, root) {
76
77
  }
77
78
  // @ts-ignore
78
79
  return {
79
- valid: ValidationLevel.Valid,
80
+ valid: SyntaxValidationResult.Valid,
80
81
  matches: [],
81
82
  node: atRule,
82
83
  syntax: '@page',
@@ -1,21 +1,21 @@
1
- import { ValidationLevel, EnumToken } from '../../ast/types.js';
1
+ import { SyntaxValidationResult, EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
3
  import '../../ast/walk.js';
4
- import { parseSelector } from '../../parser/parse.js';
5
- import { colorFontTech, fontFeaturesTech, fontFormat } from '../../syntax/syntax.js';
4
+ import '../../parser/parse.js';
5
+ import '../../parser/tokenize.js';
6
6
  import '../../parser/utils/config.js';
7
+ import { colorFontTech, fontFeaturesTech, fontFormat } from '../../syntax/syntax.js';
7
8
  import '../../renderer/color/utils/constants.js';
8
9
  import '../../renderer/sourcemap/lib/encode.js';
9
10
  import { consumeWhitespace } from '../utils/whitespace.js';
10
11
  import { splitTokenList } from '../utils/list.js';
11
- import { validateComplexSelector } from '../syntaxes/complex-selector.js';
12
12
 
13
13
  function validateAtRuleSupports(atRule, options, root) {
14
14
  // media-query-list
15
15
  if (!Array.isArray(atRule.tokens) || atRule.tokens.length == 0) {
16
16
  // @ts-ignore
17
17
  return {
18
- valid: ValidationLevel.Drop,
18
+ valid: SyntaxValidationResult.Drop,
19
19
  matches: [],
20
20
  node: atRule,
21
21
  syntax: '@' + atRule.nam,
@@ -33,22 +33,20 @@ function validateAtRuleSupports(atRule, options, root) {
33
33
  if (!('chi' in atRule)) {
34
34
  // @ts-ignore
35
35
  return {
36
- valid: ValidationLevel.Drop,
37
- matches: [],
36
+ valid: SyntaxValidationResult.Drop,
37
+ context: [],
38
38
  node: atRule,
39
39
  syntax: '@' + atRule.nam,
40
- error: 'expected at-rule body',
41
- tokens: []
40
+ error: 'expected at-rule body'
42
41
  };
43
42
  }
44
43
  // @ts-ignore
45
44
  return {
46
- valid: ValidationLevel.Valid,
47
- matches: [],
45
+ valid: SyntaxValidationResult.Valid,
46
+ context: [],
48
47
  node: atRule,
49
48
  syntax: '@' + atRule.nam,
50
- error: '',
51
- tokens: []
49
+ error: ''
52
50
  };
53
51
  }
54
52
  function validateAtRuleSupportsConditions(atRule, tokenList) {
@@ -57,8 +55,8 @@ function validateAtRuleSupportsConditions(atRule, tokenList) {
57
55
  if (tokens.length == 0) {
58
56
  // @ts-ignore
59
57
  return {
60
- valid: ValidationLevel.Drop,
61
- matches: [],
58
+ valid: SyntaxValidationResult.Drop,
59
+ context: [],
62
60
  node: tokens[0] ?? atRule,
63
61
  syntax: '@' + atRule.nam,
64
62
  error: 'unexpected token',
@@ -69,20 +67,20 @@ function validateAtRuleSupportsConditions(atRule, tokenList) {
69
67
  while (tokens.length > 0) {
70
68
  result = validateSupportCondition(atRule, tokens[0]);
71
69
  // supports-condition
72
- if (result.valid == ValidationLevel.Valid) {
70
+ if (result.valid == SyntaxValidationResult.Valid) {
73
71
  previousToken = tokens[0];
74
72
  tokens.shift();
75
73
  }
76
74
  else {
77
75
  result = validateSupportFeature(tokens[0]);
78
- if ( /*result == null || */result.valid == ValidationLevel.Valid) {
76
+ if ( /*result == null || */result.valid == SyntaxValidationResult.Valid) {
79
77
  previousToken = tokens[0];
80
78
  tokens.shift();
81
79
  }
82
80
  else {
83
81
  if (tokens[0].typ == EnumToken.ParensTokenType) {
84
82
  result = validateAtRuleSupportsConditions(atRule, tokens[0].chi);
85
- if ( /* result == null || */result.valid == ValidationLevel.Valid) {
83
+ if ( /* result == null || */result.valid == SyntaxValidationResult.Valid) {
86
84
  previousToken = tokens[0];
87
85
  tokens.shift();
88
86
  // continue;
@@ -98,7 +96,7 @@ function validateAtRuleSupportsConditions(atRule, tokenList) {
98
96
  //
99
97
  // return {
100
98
  // valid: ValidationLevel.Drop,
101
- // matches: [],
99
+ // context: [],
102
100
  // node: tokens[0] ?? atRule,
103
101
  // syntax: '@' + atRule.nam,
104
102
  // // @ts-ignore
@@ -115,73 +113,66 @@ function validateAtRuleSupportsConditions(atRule, tokenList) {
115
113
  if (previousToken?.typ != EnumToken.ParensTokenType) {
116
114
  // @ts-ignore
117
115
  return {
118
- valid: ValidationLevel.Drop,
119
- matches: [],
116
+ valid: SyntaxValidationResult.Drop,
117
+ context: [],
120
118
  node: tokens[0] ?? previousToken ?? atRule,
121
119
  syntax: '@' + atRule.nam,
122
- error: 'expected whitespace',
123
- tokens: []
120
+ error: 'expected whitespace'
124
121
  };
125
122
  }
126
123
  }
127
124
  if (![EnumToken.MediaFeatureOrTokenType, EnumToken.MediaFeatureAndTokenType].includes(tokens[0].typ)) {
128
125
  // @ts-ignore
129
126
  return {
130
- valid: ValidationLevel.Drop,
131
- matches: [],
127
+ valid: SyntaxValidationResult.Drop,
128
+ context: [],
132
129
  node: tokens[0] ?? atRule,
133
130
  syntax: '@' + atRule.nam,
134
- error: 'expected and/or',
135
- tokens: []
131
+ error: 'expected and/or'
136
132
  };
137
133
  }
138
134
  if (tokens.length == 1) {
139
135
  // @ts-ignore
140
136
  return {
141
- valid: ValidationLevel.Drop,
142
- matches: [],
137
+ valid: SyntaxValidationResult.Drop,
138
+ context: [],
143
139
  node: tokens[0] ?? atRule,
144
140
  syntax: '@' + atRule.nam,
145
- error: 'expected supports-condition',
146
- tokens: []
141
+ error: 'expected supports-condition'
147
142
  };
148
143
  }
149
144
  tokens.shift();
150
145
  if (!consumeWhitespace(tokens)) {
151
146
  // @ts-ignore
152
147
  return {
153
- valid: ValidationLevel.Drop,
154
- matches: [],
148
+ valid: SyntaxValidationResult.Drop,
149
+ context: [],
155
150
  node: tokens[0] ?? atRule,
156
151
  syntax: '@' + atRule.nam,
157
- error: 'expected whitespace',
158
- tokens: []
152
+ error: 'expected whitespace'
159
153
  };
160
154
  }
161
155
  }
162
156
  }
163
157
  return {
164
- valid: ValidationLevel.Valid,
165
- matches: [],
158
+ valid: SyntaxValidationResult.Valid,
159
+ context: [],
166
160
  node: atRule,
167
161
  syntax: '@' + atRule.nam,
168
- error: '',
169
- tokens: []
162
+ error: ''
170
163
  };
171
164
  }
172
165
  function validateSupportCondition(atRule, token) {
173
166
  if (token.typ == EnumToken.MediaFeatureNotTokenType) {
174
167
  return validateSupportCondition(atRule, token.val);
175
168
  }
176
- if (token.typ != EnumToken.ParensTokenType && !(['when', 'else'].includes(atRule.nam) && token.typ == EnumToken.FunctionTokenType && ['supports', 'font-format', 'font-tech'].includes(token.val))) {
177
- // @ts-ignore
169
+ if (token.typ == EnumToken.FunctionTokenType && token.val.localeCompare('selector', undefined, { sensitivity: 'base' }) == 0) {
178
170
  return {
179
- valid: ValidationLevel.Drop,
180
- matches: [],
171
+ valid: SyntaxValidationResult.Valid,
172
+ context: [],
181
173
  node: token,
182
174
  syntax: '@' + atRule.nam,
183
- error: 'expected supports condition-in-parens',
184
- tokens: []
175
+ error: ''
185
176
  };
186
177
  }
187
178
  const chi = token.chi.filter((t) => t.typ != EnumToken.CommentTokenType && t.typ != EnumToken.WhitespaceTokenType);
@@ -191,12 +182,11 @@ function validateSupportCondition(atRule, token) {
191
182
  if (chi[0].typ == EnumToken.IdenTokenType) {
192
183
  // @ts-ignore
193
184
  return {
194
- valid: ValidationLevel.Valid,
195
- matches: [],
185
+ valid: SyntaxValidationResult.Valid,
186
+ context: [],
196
187
  node: null,
197
188
  syntax: '@' + atRule.nam,
198
- error: '',
199
- tokens: []
189
+ error: ''
200
190
  };
201
191
  }
202
192
  if (chi[0].typ == EnumToken.MediaFeatureNotTokenType) {
@@ -206,55 +196,59 @@ function validateSupportCondition(atRule, token) {
206
196
  // @ts-ignore
207
197
  return chi[0].l.typ == EnumToken.IdenTokenType && chi[0].op.typ == EnumToken.ColonTokenType ?
208
198
  {
209
- valid: ValidationLevel.Valid,
210
- matches: [],
199
+ valid: SyntaxValidationResult.Valid,
200
+ context: [],
211
201
  node: null,
212
202
  syntax: 'supports-condition',
213
- error: '',
214
- tokens: []
203
+ error: ''
215
204
  } : {
216
- valid: ValidationLevel.Drop,
217
- matches: [],
205
+ valid: SyntaxValidationResult.Drop,
206
+ context: [],
218
207
  node: token,
219
208
  syntax: 'supports-condition',
220
- error: 'expected supports condition-in-parens',
221
- tokens: []
209
+ error: 'expected supports condition-in-parens'
222
210
  };
223
211
  }
224
212
  // @ts-ignore
225
213
  return {
226
- valid: ValidationLevel.Drop,
227
- matches: [],
214
+ valid: SyntaxValidationResult.Drop,
215
+ context: [],
228
216
  node: token,
229
217
  syntax: 'supports-condition',
230
- error: 'expected supports condition-in-parens',
231
- tokens: []
218
+ error: 'expected supports condition-in-parens'
232
219
  };
233
220
  }
234
221
  function validateSupportFeature(token) {
222
+ if (token.typ == EnumToken.MediaFeatureNotTokenType) {
223
+ return validateSupportFeature(token.val);
224
+ }
235
225
  if (token.typ == EnumToken.FunctionTokenType) {
236
226
  if (token.val.localeCompare('selector', undefined, { sensitivity: 'base' }) == 0) {
237
- return validateComplexSelector(parseSelector(token.chi));
227
+ return {
228
+ valid: SyntaxValidationResult.Valid,
229
+ context: [],
230
+ node: token,
231
+ syntax: 'selector',
232
+ error: ''
233
+ };
238
234
  }
239
235
  if (token.val.localeCompare('font-tech', undefined, { sensitivity: 'base' }) == 0) {
240
236
  const chi = token.chi.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
241
237
  // @ts-ignore
242
238
  return chi.length == 1 && chi[0].typ == EnumToken.IdenTokenType && colorFontTech.concat(fontFeaturesTech).some((t) => t.localeCompare(chi[0].val, undefined, { sensitivity: 'base' }) == 0) ?
243
239
  {
244
- valid: ValidationLevel.Valid,
245
- matches: [],
240
+ valid: SyntaxValidationResult.Valid,
241
+ context: [],
246
242
  node: token,
247
243
  syntax: 'font-tech',
248
- error: '',
249
- tokens: []
244
+ error: ''
250
245
  } :
251
246
  {
252
- valid: ValidationLevel.Drop,
253
- matches: [],
247
+ valid: SyntaxValidationResult.Drop,
248
+ context: [],
254
249
  node: token,
255
250
  syntax: 'font-tech',
256
- error: 'expected font-tech',
257
- tokens: []
251
+ error: 'expected font-tech'
258
252
  };
259
253
  }
260
254
  if (token.val.localeCompare('font-format', undefined, { sensitivity: 'base' }) == 0) {
@@ -262,31 +256,28 @@ function validateSupportFeature(token) {
262
256
  // @ts-ignore
263
257
  return chi.length == 1 && chi[0].typ == EnumToken.IdenTokenType && fontFormat.some((t) => t.localeCompare(chi[0].val, undefined, { sensitivity: 'base' }) == 0) ?
264
258
  {
265
- valid: ValidationLevel.Valid,
266
- matches: [],
259
+ valid: SyntaxValidationResult.Valid,
260
+ context: [],
267
261
  node: token,
268
262
  syntax: 'font-format',
269
- error: '',
270
- tokens: []
263
+ error: ''
271
264
  } :
272
265
  {
273
- valid: ValidationLevel.Drop,
274
- matches: [],
266
+ valid: SyntaxValidationResult.Drop,
267
+ context: [],
275
268
  node: token,
276
269
  syntax: 'font-format',
277
- error: 'expected font-format',
278
- tokens: []
270
+ error: 'expected font-format'
279
271
  };
280
272
  }
281
273
  }
282
274
  // @ts-ignore
283
275
  return {
284
- valid: ValidationLevel.Drop,
285
- matches: [],
276
+ valid: SyntaxValidationResult.Drop,
277
+ context: [],
286
278
  node: token,
287
279
  syntax: '@supports',
288
- error: 'expected feature',
289
- tokens: []
280
+ error: 'expected feature'
290
281
  };
291
282
  }
292
283
 
@@ -1,10 +1,11 @@
1
- import { ValidationLevel, EnumToken } from '../../ast/types.js';
1
+ import { SyntaxValidationResult, EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
3
  import '../../ast/walk.js';
4
4
  import '../../parser/parse.js';
5
- import '../../renderer/color/utils/constants.js';
6
- import '../../renderer/sourcemap/lib/encode.js';
5
+ import '../../parser/tokenize.js';
7
6
  import '../../parser/utils/config.js';
7
+ import { generalEnclosedFunc } from '../../renderer/color/utils/constants.js';
8
+ import '../../renderer/sourcemap/lib/encode.js';
8
9
  import { consumeWhitespace } from '../utils/whitespace.js';
9
10
  import { splitTokenList } from '../utils/list.js';
10
11
  import { validateMediaFeature, validateMediaCondition } from './media.js';
@@ -16,8 +17,8 @@ function validateAtRuleWhen(atRule, options, root) {
16
17
  if (slice.length == 0) {
17
18
  // @ts-ignore
18
19
  return {
19
- valid: ValidationLevel.Valid,
20
- matches: [],
20
+ valid: SyntaxValidationResult.Valid,
21
+ context: [],
21
22
  node: atRule,
22
23
  syntax: '@when',
23
24
  error: '',
@@ -25,14 +26,14 @@ function validateAtRuleWhen(atRule, options, root) {
25
26
  };
26
27
  }
27
28
  const result = validateAtRuleWhenQueryList(atRule.tokens, atRule);
28
- if (result.valid == ValidationLevel.Drop) {
29
+ if (result.valid == SyntaxValidationResult.Drop) {
29
30
  return result;
30
31
  }
31
32
  if (!('chi' in atRule)) {
32
33
  // @ts-ignore
33
34
  return {
34
- valid: ValidationLevel.Drop,
35
- matches: [],
35
+ valid: SyntaxValidationResult.Drop,
36
+ context: [],
36
37
  node: atRule,
37
38
  syntax: '@when',
38
39
  error: 'expected at-rule body',
@@ -40,12 +41,11 @@ function validateAtRuleWhen(atRule, options, root) {
40
41
  };
41
42
  }
42
43
  return {
43
- valid: ValidationLevel.Valid,
44
- matches: [],
44
+ valid: SyntaxValidationResult.Valid,
45
+ context: [],
45
46
  node: atRule,
46
47
  syntax: '@when',
47
- error: '',
48
- tokens: result.tokens
48
+ error: ''
49
49
  };
50
50
  }
51
51
  // media() = media( [ <mf-plain> | <mf-boolean> | <mf-range> ] )
@@ -61,10 +61,10 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
61
61
  continue;
62
62
  }
63
63
  while (split.length > 0) {
64
- if (split[0].typ != EnumToken.FunctionTokenType || !['media', 'supports', 'font-tech', 'font-format'].includes(split[0].val)) {
64
+ if (split[0].typ != EnumToken.FunctionTokenType || !generalEnclosedFunc.includes(split[0].val)) {
65
65
  result = {
66
- valid: ValidationLevel.Drop,
67
- matches: [],
66
+ valid: SyntaxValidationResult.Drop,
67
+ context: [],
68
68
  node: split[0] ?? atRule,
69
69
  syntax: '@when',
70
70
  error: 'unexpected token',
@@ -78,26 +78,24 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
78
78
  // result = valida
79
79
  if (chi.length != 1 || !(validateMediaFeature(chi[0]) || validateMediaCondition(split[0], atRule))) {
80
80
  result = {
81
- valid: ValidationLevel.Drop,
82
- matches: [],
81
+ valid: SyntaxValidationResult.Drop,
82
+ context: [],
83
83
  node: split[0] ?? atRule,
84
84
  syntax: 'media( [ <mf-plain> | <mf-boolean> | <mf-range> ] )',
85
- error: 'unexpected token',
86
- tokens: []
85
+ error: 'unexpected token'
87
86
  };
88
87
  break;
89
88
  }
90
89
  }
91
- else if (['supports', 'font-tech', 'font-format'].includes(split[0].val)) {
90
+ else if (generalEnclosedFunc.includes(split[0].val)) {
92
91
  // result = valida
93
92
  if (!validateSupportCondition(atRule, split[0])) {
94
93
  result = {
95
- valid: ValidationLevel.Drop,
96
- matches: [],
94
+ valid: SyntaxValidationResult.Drop,
95
+ context: [],
97
96
  node: split[0] ?? atRule,
98
97
  syntax: 'media( [ <mf-plain> | <mf-boolean> | <mf-range> ] )',
99
- error: 'unexpected token',
100
- tokens: []
98
+ error: 'unexpected token'
101
99
  };
102
100
  break;
103
101
  }
@@ -112,8 +110,8 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
112
110
  }
113
111
  if (![EnumToken.MediaFeatureAndTokenType, EnumToken.MediaFeatureOrTokenType].includes(split[0].typ)) {
114
112
  result = {
115
- valid: ValidationLevel.Drop,
116
- matches: [],
113
+ valid: SyntaxValidationResult.Drop,
114
+ context: [],
117
115
  node: split[0] ?? atRule,
118
116
  syntax: '@when',
119
117
  error: 'expecting and/or media-condition',
@@ -128,8 +126,8 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
128
126
  consumeWhitespace(split);
129
127
  if (split.length == 0) {
130
128
  result = {
131
- valid: ValidationLevel.Drop,
132
- matches: [],
129
+ valid: SyntaxValidationResult.Drop,
130
+ context: [],
133
131
  node: split[0] ?? atRule,
134
132
  syntax: '@when',
135
133
  error: 'expecting media-condition',
@@ -147,13 +145,12 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
147
145
  }
148
146
  if (matched.length == 0) {
149
147
  return {
150
- valid: ValidationLevel.Drop,
151
- matches: [],
148
+ valid: SyntaxValidationResult.Drop,
149
+ context: [],
152
150
  // @ts-ignore
153
151
  node: result?.node ?? atRule,
154
152
  syntax: '@when',
155
- error: 'invalid at-rule body',
156
- tokens: []
153
+ error: 'invalid at-rule body'
157
154
  };
158
155
  }
159
156
  tokenList.length = 0;
@@ -166,12 +163,11 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
166
163
  tokenList.push(...match);
167
164
  }
168
165
  return {
169
- valid: ValidationLevel.Valid,
170
- matches: [],
166
+ valid: SyntaxValidationResult.Valid,
167
+ context: [],
171
168
  node: atRule,
172
169
  syntax: '@when',
173
- error: '',
174
- tokens: tokenList
170
+ error: ''
175
171
  };
176
172
  }
177
173