@tbela99/css-parser 1.4.2 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -1,123 +1,704 @@
1
- import { EnumToken } from '../../ast/types.js';
2
- import '../../ast/minify.js';
1
+ import { EnumToken, EnumAstNodeStatus, ColorType, ValidationLevel } from '../../ast/types.js';
2
+ import { definedPropertySettings, tokensfuncDefMap, COLORS_NAMES, nonStandardColors, systemColors, deprecatedSystemColors, tokensMap, trimTokenSpace } from '../../syntax/constants.js';
3
+ import { renamedStandardProperties, isColor, parseColor, isWhiteSpace, isValue } from '../../syntax/syntax.js';
4
+ import { getSyntaxRule, getParsedSyntax } from '../../validation/config.js';
5
+ import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
6
+ import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
3
7
  import { walkValues } from '../../ast/walk.js';
4
- import '../parse.js';
5
- import '../tokenize.js';
6
- import './config.js';
7
- import { isWhiteSpace } from '../../syntax/syntax.js';
8
- import '../../syntax/color/utils/constants.js';
9
- import '../../renderer/sourcemap/lib/encode.js';
8
+ import { equalsIgnoreCase } from './text.js';
9
+ import { buildExpression } from '../../ast/math/expression.js';
10
+ import { splitTokenList } from '../../validation/utils/list.js';
11
+ import { parseTokens } from '../parse.js';
10
12
 
11
- function parseDeclarationNode(node, errors, location) {
12
- while (node.val[0]?.typ == EnumToken.WhitespaceTokenType) {
13
- node.val.shift();
13
+ /**
14
+ *
15
+ * @param template
16
+ * @returns
17
+ */
18
+ function parseGridTemplate(template) {
19
+ let result = "";
20
+ let buffer = "";
21
+ for (let i = 0; i < template.length; i++) {
22
+ const char = template[i];
23
+ if (isWhiteSpace(char.charCodeAt(0))) {
24
+ while (i + 1 < template.length && isWhiteSpace(template[i + 1].charCodeAt(0))) {
25
+ i++;
26
+ }
27
+ result += buffer + " ";
28
+ buffer = "";
29
+ }
30
+ else if (char == ".") {
31
+ while (i + 1 < template.length && template[i + 1] == ".") {
32
+ i++;
33
+ }
34
+ if (isWhiteSpace(result.at(-1)?.charCodeAt(0))) {
35
+ result = result.slice(0, -1);
36
+ }
37
+ result += buffer + char;
38
+ buffer = "";
39
+ }
40
+ else {
41
+ buffer += char;
42
+ }
14
43
  }
15
- if (!node.nam.startsWith('--') && node.val.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)).length == 0) {
16
- errors.push({
17
- action: 'drop',
18
- message: 'doParse: invalid declaration',
19
- location
44
+ return buffer.length > 0 ? result + buffer : result;
45
+ }
46
+ /**
47
+ *
48
+ * @param tokens
49
+ * @returns
50
+ */
51
+ function isDeclarationValue(tokens) {
52
+ const stack = [];
53
+ let i = 0;
54
+ for (; i < tokens.length; i++) {
55
+ if (tokens[i].typ === EnumToken.WhitespaceTokenType || tokens[i].typ === EnumToken.CommentTokenType) {
56
+ continue;
57
+ }
58
+ else if (tokensfuncDefMap.has(tokens[i].typ) || tokens[i].typ === EnumToken.StartParensTokenType) {
59
+ stack.push(tokens[i]);
60
+ }
61
+ else if (tokens[i].typ === EnumToken.CommaTokenType || tokens[i].typ === EnumToken.LiteralTokenType) {
62
+ stack.push(tokens[i]);
63
+ }
64
+ else if (isValue(tokens[i])) {
65
+ if (stack.at(-1)?.typ === EnumToken.LiteralTokenType) {
66
+ stack.pop();
67
+ }
68
+ }
69
+ else if (tokens[i].typ === EnumToken.EndParensTokenType) {
70
+ if (stack.at(-1)?.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stack.at(-1)?.typ)) {
71
+ stack.pop();
72
+ }
73
+ }
74
+ }
75
+ return {
76
+ success: stack.length === 0,
77
+ errors: stack.length > 0
78
+ ? [
79
+ {
80
+ action: "drop",
81
+ message: `unexpected declaration value at ${stack.at(-1)?.loc?.src}:${stack.at(-1)?.loc?.sta.lin}:${stack.at(-1)?.loc?.sta.col}`,
82
+ node: stack.at(-1),
83
+ location: stack.at(-1)?.loc,
84
+ },
85
+ ]
86
+ : [],
87
+ };
88
+ }
89
+ /**
90
+ * Parse declaration
91
+ * @param tokens
92
+ * @param parent
93
+ * @param options
94
+ * @param errors
95
+ */
96
+ function parseDeclaration(tokens, parent, options, errors) {
97
+ const name = tokens.shift();
98
+ let i;
99
+ let rules = null;
100
+ let syntaxRules = null;
101
+ let success = true;
102
+ let validate = typeof options.validation === "boolean"
103
+ ? options.validation
104
+ : !!options.validation;
105
+ if (renamedStandardProperties.has(name.val)) {
106
+ name.val = renamedStandardProperties.get(name.val);
107
+ }
108
+ for (i = 0; i < tokens.length; i++) {
109
+ const token = tokens[i];
110
+ if (token.typ == EnumToken.WhitespaceTokenType ||
111
+ token.typ == EnumToken.CommentTokenType ||
112
+ token.typ == EnumToken.InvalidCommentTokenType) {
113
+ continue;
114
+ }
115
+ break;
116
+ }
117
+ if ((name.typ !== EnumToken.IdenTokenType && name.typ !== EnumToken.DashedIdenTokenType) ||
118
+ tokens[i]?.typ !== EnumToken.ColonTokenType) {
119
+ return Object.defineProperties(Object.assign({
120
+ typ: EnumToken.RawNodeTokenType,
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
+ },
144
+ },
20
145
  });
21
- return null;
22
146
  }
23
- if ('composes' == node.nam.toLowerCase()) {
24
- let left = [];
25
- let right = [];
26
- let current = 0;
27
- let hasFrom = 0;
28
- for (; current < node.val.length; current++) {
29
- if (EnumToken.WhitespaceTokenType == node.val[current].typ || EnumToken.CommentTokenType == node.val[current].typ) {
30
- if (!hasFrom) {
31
- left.push(node.val[current]);
147
+ tokens = trimArray(tokens.slice(i + 1));
148
+ for (i = 0; i < tokens.length; i++) {
149
+ const token = tokens[i];
150
+ if (token.typ == EnumToken.WhitespaceTokenType ||
151
+ token.typ == EnumToken.CommentTokenType ||
152
+ token.typ == EnumToken.InvalidCommentTokenType) {
153
+ continue;
154
+ }
155
+ if (tokens[i].typ === EnumToken.UrlFunctionTokenDefType) {
156
+ let k = i;
157
+ while (k < tokens.length) {
158
+ if (tokens[++k]?.typ === EnumToken.WhitespaceTokenType ||
159
+ tokens[k]?.typ === EnumToken.CommentTokenType) {
160
+ continue;
32
161
  }
33
- else {
34
- right.push(node.val[current]);
162
+ if (tokens[k]?.typ !== EnumToken.EndParensTokenType) {
163
+ break;
35
164
  }
36
- continue;
37
165
  }
38
- if (EnumToken.IdenTokenType == node.val[current].typ || EnumToken.DashedIdenTokenType == node.val[current].typ || EnumToken.StringTokenType == node.val[current].typ) {
39
- if (EnumToken.IdenTokenType == node.val[current].typ) {
40
- if ('from' == node.val[current].val) {
41
- if (hasFrom) {
42
- return null;
43
- }
44
- hasFrom++;
45
- continue;
166
+ if (tokens[k].typ === EnumToken.LiteralTokenType ||
167
+ tokens[k].typ === EnumToken.IdenTokenType ||
168
+ tokens[k].typ === EnumToken.DashedIdenTokenType ||
169
+ tokens[k].typ === EnumToken.HashTokenType ||
170
+ tokens[k].typ === EnumToken.ClassSelectorTokenType) {
171
+ let j = k;
172
+ let val = tokens[k].val;
173
+ while (j + 1 < tokens.length) {
174
+ if (tokens[++j].typ !== EnumToken.LiteralTokenType &&
175
+ tokens[j].typ !== EnumToken.IdenTokenType &&
176
+ tokens[j].typ !== EnumToken.DashedIdenTokenType &&
177
+ tokens[j].typ !== EnumToken.HashTokenType &&
178
+ tokens[j].typ !== EnumToken.ClassSelectorTokenType) {
179
+ break;
46
180
  }
181
+ val += tokens[j].val;
47
182
  }
48
- if (hasFrom) {
49
- right.push(node.val[current]);
183
+ Object.assign(tokens[k], {
184
+ typ: EnumToken.UrlTokenTokenType,
185
+ val,
186
+ });
187
+ tokens[k].loc.end = tokens[j].loc.end;
188
+ tokens.splice(k + 1, j - k - 1);
189
+ }
190
+ }
191
+ }
192
+ if (validate && name.typ === EnumToken.IdenTokenType) {
193
+ if (parent != null &&
194
+ parent.typ === EnumToken.AtRuleNodeType &&
195
+ parent.nam != "import" &&
196
+ parent.nam != "supports" &&
197
+ parent.nam != "media") {
198
+ // check @at-rule description
199
+ // pick up the syntax or reject immediately
200
+ // else do this
201
+ rules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + parent.nam);
202
+ if (rules != null) {
203
+ const propertyDescriptors = rules.getPropertyDescriptors();
204
+ if (propertyDescriptors != null) {
205
+ syntaxRules = propertyDescriptors[name.val.toLowerCase()];
50
206
  }
51
207
  else {
52
- left.push(node.val[current]);
208
+ syntaxRules =
209
+ rules.acceptAnyDeclaration && rules.acceptAnyRule
210
+ ? getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, name.val.toLowerCase())
211
+ : rules.getBlockRules();
212
+ if (syntaxRules == null) {
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
+ }
250
+ }
53
251
  }
54
- continue;
55
252
  }
56
- break;
57
253
  }
58
- if (hasFrom <= 1 && current > 0) {
59
- if (hasFrom == 0) {
60
- node.val.splice(0, left.length, {
61
- typ: EnumToken.ComposesSelectorNodeType,
62
- l: left,
63
- r: null
64
- });
254
+ else {
255
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, name.val.toLowerCase());
256
+ }
257
+ // <declaration-list> or <declaration-rule-list>
258
+ // else
259
+ if (syntaxRules == null ||
260
+ (syntaxRules.length === 1 &&
261
+ syntaxRules[0].typ === ValidationTokenEnum.PropertyType &&
262
+ (syntaxRules[0].val === "declaration-list" ||
263
+ syntaxRules[0].val === "declaration-rule-list"))) {
264
+ if (parent?.typ === EnumToken.AtRuleNodeType &&
265
+ (parent.nam === "swash" ||
266
+ parent.nam === "annotation" ||
267
+ parent.nam === "ornaments" ||
268
+ parent.nam === "stylistic" ||
269
+ parent.nam === "historical-forms")) {
270
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "font-feature-custom-ident");
271
+ }
272
+ else if (parent?.typ === EnumToken.AtRuleNodeType && parent.nam === "styleset") {
273
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "font-feature-custom-ident-list");
274
+ }
275
+ else if (parent?.typ === EnumToken.AtRuleNodeType && parent.nam === "character-variant") {
276
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "font-feature-custom-ident-character-variant");
65
277
  }
66
278
  else {
67
- node.val.splice(0, current, {
68
- typ: EnumToken.ComposesSelectorNodeType,
69
- l: left,
70
- r: right.reduce((a, b) => {
71
- return a == null ? b : b.typ == EnumToken.WhitespaceTokenType || b.typ == EnumToken.CommentTokenType ? a : b;
72
- }, null)
279
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, name.val.toLowerCase());
280
+ }
281
+ }
282
+ }
283
+ if (tokens.length === 0 && name.typ !== EnumToken.DashedIdenTokenType) {
284
+ errors.push({
285
+ action: "drop",
286
+ message: "declaration value missing",
287
+ node: name,
288
+ location: name.loc,
289
+ });
290
+ return Object.defineProperties(Object.assign(name, {
291
+ typ: EnumToken.DeclarationNodeType,
292
+ nam: name.val,
293
+ val: tokens,
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
+ },
309
+ },
310
+ });
311
+ }
312
+ const stack = [];
313
+ let result = null;
314
+ let token;
315
+ let index;
316
+ if (syntaxRules != null) {
317
+ const doNotValidate = options.validation === false || options.validation === ValidationLevel.None;
318
+ result = doNotValidate ? null : matchAllSyntaxes(syntaxRules, createValidationContext(tokens), options);
319
+ if (doNotValidate || result != null) {
320
+ success = doNotValidate || result?.success;
321
+ if (success) {
322
+ for (const { value } of walkValues(tokens)) {
323
+ if (value.typ === EnumToken.IdenTokenType && isColor(value)) {
324
+ parseColor(value);
325
+ }
326
+ else if (value.typ === EnumToken.ColorTokenType) {
327
+ if (isColor(value)) {
328
+ parseColor(value);
329
+ }
330
+ }
331
+ }
332
+ }
333
+ if (!doNotValidate && !result?.success && result.errors.length > 0) {
334
+ errors.push(...result.errors);
335
+ }
336
+ if (!doNotValidate && !(success || result.success)) {
337
+ return Object.defineProperties(Object.assign(name, {
338
+ typ: EnumToken.DeclarationNodeType,
339
+ nam: name.val,
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
+ },
356
+ },
73
357
  });
74
358
  }
75
359
  }
76
360
  }
77
- for (const { value: val, parent } of walkValues(node.val, node)) {
78
- if (val.typ == EnumToken.AttrTokenType && val.chi.every((t) => [EnumToken.IdenTokenType, EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ))) {
79
- // @ts-ignore
80
- val.typ = EnumToken.IdenListTokenType;
361
+ for (i = 0; i < tokens.length; i++) {
362
+ token = tokens[i];
363
+ if (tokensfuncDefMap.has(token.typ) || token.typ == EnumToken.StartParensTokenType) {
364
+ stack.push(token);
365
+ continue;
366
+ }
367
+ switch (token.typ) {
368
+ case EnumToken.IdenTokenType:
369
+ if (tokens[i + 1]?.typ == EnumToken.StartParensTokenType) {
370
+ Object.assign(token, {
371
+ typ: EnumToken.FunctionTokenDefType,
372
+ });
373
+ token.loc.end = tokens[i + 1].loc.end;
374
+ tokens.splice(i + 1, 1);
375
+ stack.push(token);
376
+ }
377
+ break;
378
+ case EnumToken.Literal:
379
+ if (token.val === "/" && stack.at(-1)?.typ == EnumToken.MathFunctionTokenDefType) {
380
+ Object.assign(token, {
381
+ typ: EnumToken.Div,
382
+ });
383
+ }
384
+ else if (token.val === "-" &&
385
+ stack.at(-1)?.typ == EnumToken.MathFunctionTokenDefType) {
386
+ Object.assign(token, {
387
+ typ: EnumToken.Sub,
388
+ });
389
+ }
390
+ break;
391
+ case EnumToken.Star:
392
+ case EnumToken.Plus:
393
+ Object.assign(token, {
394
+ typ: tokensMap.get(token.typ),
395
+ });
396
+ break;
397
+ case EnumToken.EndParensTokenType:
398
+ if (stack.length == 0) {
399
+ errors.push({
400
+ action: "drop",
401
+ message: "unbalanced parentheses",
402
+ node: token,
403
+ location: token.loc,
404
+ });
405
+ return Object.defineProperties(Object.assign(name, {
406
+ typ: EnumToken.DeclarationNodeType,
407
+ nam: name.val,
408
+ val: tokens,
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
+ },
424
+ },
425
+ });
426
+ }
427
+ if (stack.at(-1)?.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stack.at(-1)?.typ)) {
428
+ index = tokens.indexOf(stack.at(-1));
429
+ tokens.splice(i, 1);
430
+ Object.assign(tokens[index], {
431
+ typ: stack.at(-1)?.typ === EnumToken.StartParensTokenType
432
+ ? EnumToken.ParensTokenType
433
+ : tokensfuncDefMap.get(stack.at(-1)?.typ),
434
+ chi: trimArray(tokens.splice(index + 1, i - index - 1)),
435
+ });
436
+ if (tokens[index].typ === EnumToken.WildCardFunctionTokenType &&
437
+ equalsIgnoreCase(tokens[index].val, "if")) {
438
+ if (tokens[index].chi.at(-1)?.typ === EnumToken.SemiColonTokenType) {
439
+ tokens[index].chi.pop();
440
+ }
441
+ let foundElse = false;
442
+ tokens[index].chi = splitTokenList(tokens[index].chi, [EnumToken.SemiColonTokenType], true).reduce((acc, curr) => {
443
+ if (foundElse) {
444
+ // else already found
445
+ // ignore everything after else
446
+ // if(media(any-pointer: fine): 30px; else: 44px; media(any-pointer: fine): 23px); -> if(media(any-pointer: fine): 30px; else: 44px;);
447
+ return acc;
448
+ }
449
+ let index = -1;
450
+ while (++index < curr.length) {
451
+ if (curr[index].typ == EnumToken.IdenTokenType &&
452
+ equalsIgnoreCase("else", curr[index].val)) {
453
+ foundElse = true;
454
+ }
455
+ if (curr[index].typ === EnumToken.ColonTokenType) {
456
+ break;
457
+ }
458
+ }
459
+ if (foundElse && acc.length > 0) {
460
+ acc[acc.length - 1] = {
461
+ typ: EnumToken.IfElseConditionTokenType,
462
+ l: acc[acc.length - 1],
463
+ r: {
464
+ typ: EnumToken.IfConditionTokenType,
465
+ l: trimArray(curr.slice(0, index)),
466
+ r: trimArray(curr.slice(index + 1)),
467
+ },
468
+ };
469
+ }
470
+ else {
471
+ acc.push({
472
+ typ: EnumToken.IfConditionTokenType,
473
+ l: trimArray(curr.slice(0, index)),
474
+ r: trimArray(curr.slice(index + 1)),
475
+ });
476
+ }
477
+ return acc;
478
+ }, []);
479
+ }
480
+ else if (tokens[index].typ === EnumToken.UrlFunctionTokenType) {
481
+ let l = -1;
482
+ while (++l < tokens[index].chi.length) {
483
+ if (tokens[index].chi[l].typ === EnumToken.StringTokenType) {
484
+ break;
485
+ }
486
+ else if (tokens[index].chi[l].typ === EnumToken.IdenTokenType) {
487
+ let m = l + 1;
488
+ while (tokens[index].chi[m]?.typ === EnumToken.ClassSelectorTokenType) {
489
+ Object.assign(tokens[index].chi[l], {
490
+ typ: EnumToken.UrlTokenTokenType,
491
+ val: tokens[index].chi[l].val +
492
+ tokens[index].chi[m].val,
493
+ });
494
+ tokens[index].chi[l].loc.end = tokens[index].chi[m].loc.end;
495
+ tokens[index].chi.splice(m, 1);
496
+ }
497
+ break;
498
+ }
499
+ }
500
+ if (tokens[index].chi[l]?.typ === EnumToken.StringTokenType &&
501
+ /^[a-zA-Z0-0/_.-]+$/.test(tokens[index].chi[l].val.slice(1, -1))) {
502
+ Object.assign(tokens[index].chi[l], {
503
+ typ: EnumToken.UrlTokenTokenType,
504
+ val: tokens[index].chi[l].val.slice(1, -1),
505
+ });
506
+ }
507
+ }
508
+ i = index;
509
+ if (tokens[index].typ === EnumToken.MathFunctionTokenType &&
510
+ equalsIgnoreCase("calc", tokens[index].val)) {
511
+ tokens[index].chi = [buildExpression(tokens[index].chi)];
512
+ }
513
+ else if (tokens[index].typ === EnumToken.ColorTokenType) {
514
+ if (isColor(tokens[index])) {
515
+ parseColor(tokens[index]);
516
+ }
517
+ else {
518
+ success = false;
519
+ tokens[index].typ = EnumToken.FunctionTokenType;
520
+ errors.push({
521
+ action: "drop",
522
+ message: `invalid color at ${tokens[index].loc?.src}:${tokens[index].loc?.sta.lin}:${tokens[index].loc?.sta.col}`,
523
+ });
524
+ }
525
+ }
526
+ }
527
+ stack.pop();
528
+ break;
529
+ case EnumToken.IdenTokenType:
530
+ {
531
+ const val = token.val.toLowerCase();
532
+ if (val in COLORS_NAMES || val === "currentcolor") {
533
+ Object.assign(token, {
534
+ val,
535
+ typ: EnumToken.ColorTokenType,
536
+ kin: ColorType.LIT,
537
+ });
538
+ }
539
+ else if (nonStandardColors.has(val)) {
540
+ Object.assign(token, {
541
+ typ: EnumToken.ColorTokenType,
542
+ kin: ColorType.NON_STD,
543
+ });
544
+ }
545
+ else if (systemColors.has(val)) {
546
+ Object.assign(token, {
547
+ typ: EnumToken.ColorTokenType,
548
+ kin: ColorType.SYS,
549
+ });
550
+ }
551
+ else if (deprecatedSystemColors.has(val)) {
552
+ Object.assign(token, {
553
+ typ: EnumToken.ColorTokenType,
554
+ kin: ColorType.DPSYS,
555
+ });
556
+ }
557
+ }
558
+ break;
81
559
  }
82
- else if (val.typ == EnumToken.StringTokenType && (node.nam == 'grid' || node.nam == 'grid-template-areas' || node.nam == 'grid-template-rows' || node.nam == 'grid-template-columns')) {
83
- val.val = val.val.at(0) + parseGridTemplate(val.val.slice(1, -1)) + val.val.at(-1);
84
- // @ts-ignore
85
- const array = parent?.chi ?? node.val;
86
- const index = array.indexOf(val);
87
- if (index > 0 && array[index - 1].typ == EnumToken.WhitespaceTokenType) {
88
- array.splice(index - 1, 1);
560
+ if (trimTokenSpace.has(token.typ)) {
561
+ if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
562
+ tokens.splice(i + 1, 1);
563
+ }
564
+ if (tokens[i - 1]?.typ == EnumToken.WhitespaceTokenType) {
565
+ tokens.splice(--i, 1);
89
566
  }
90
567
  }
91
568
  }
92
- return node;
93
- }
94
- function parseGridTemplate(template) {
95
- let result = '';
96
- let buffer = '';
97
- for (let i = 0; i < template.length; i++) {
98
- const char = template[i];
99
- if (isWhiteSpace(char.codePointAt(0))) {
100
- while (i + 1 < template.length && isWhiteSpace(template[i + 1].codePointAt(0))) {
101
- i++;
102
- }
103
- result += buffer + ' ';
104
- buffer = '';
569
+ if (stack.length > 0) {
570
+ errors.push({
571
+ action: "drop",
572
+ message: "unbalanced parenthesis",
573
+ node: stack[stack.length - 1],
574
+ location: stack[stack.length - 1].loc,
575
+ });
576
+ return Object.defineProperties(Object.assign(name, {
577
+ typ: EnumToken.DeclarationNodeType,
578
+ nam: name.val,
579
+ val: tokens,
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
+ },
595
+ },
596
+ });
597
+ }
598
+ for (const { value } of walkValues(tokens)) {
599
+ if (value.typ === EnumToken.IdenTokenType && isColor(value)) {
600
+ parseColor(value);
105
601
  }
106
- else if (char == '.') {
107
- while (i + 1 < template.length && template[i + 1] == '.') {
108
- i++;
109
- }
110
- if (isWhiteSpace((result.at(-1)?.codePointAt(0)))) {
111
- result = result.slice(0, -1);
602
+ }
603
+ if (name.val === "grid" ||
604
+ name.val === "grid-template-areas" ||
605
+ name.val === "grid-template-rows" ||
606
+ name.val === "grid-template-columns") {
607
+ let i = 0;
608
+ for (; i < tokens.length; i++) {
609
+ if (tokens[i].typ === EnumToken.StringTokenType) {
610
+ if (/[\s.]/.test(tokens[i].val)) {
611
+ tokens[i].val = parseGridTemplate(tokens[i].val);
612
+ }
613
+ if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType &&
614
+ tokens[i + 2]?.typ === EnumToken.StringTokenType) {
615
+ tokens.splice(i + 1, 1);
616
+ }
112
617
  }
113
- result += buffer + char;
114
- buffer = '';
115
618
  }
116
- else {
117
- buffer += char;
619
+ }
620
+ if (validate && syntaxRules == null && name.typ === EnumToken.IdenTokenType) {
621
+ const node = Object.defineProperties(Object.assign(name, {
622
+ typ: EnumToken.DeclarationNodeType,
623
+ nam: name.val,
624
+ val: tokens,
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
+ },
640
+ },
641
+ });
642
+ if (options.validation & ValidationLevel.Declaration) {
643
+ errors.push({
644
+ action: "drop",
645
+ message: "unknown declaration",
646
+ node: node,
647
+ location: node.loc,
648
+ });
118
649
  }
650
+ return node;
119
651
  }
120
- return buffer.length > 0 ? result + buffer : result;
652
+ if (equalsIgnoreCase("composes", name.val)) {
653
+ let index = -1;
654
+ for (let i = 0; i < tokens.length; i++) {
655
+ if (tokens[i].typ === EnumToken.IdenTokenType && equalsIgnoreCase("from", tokens[i].val)) {
656
+ index = i;
657
+ break;
658
+ }
659
+ }
660
+ const left = trimArray(index == -1 ? tokens : tokens.slice(0, index));
661
+ const right = index == -1 ? null : trimArray(tokens.slice(index + 1));
662
+ tokens = [
663
+ Object.defineProperty({
664
+ typ: EnumToken.ComposesSelectorNodeType,
665
+ l: left,
666
+ r: right?.[0] ?? null,
667
+ }, "loc", {
668
+ ...definedPropertySettings,
669
+ value: {
670
+ ...tokens[0].loc,
671
+ sta: left[0]?.loc?.sta,
672
+ end: index != -1 ? right[right.length - 1]?.loc?.end : left[left.length - 1].loc.end,
673
+ },
674
+ }),
675
+ ];
676
+ }
677
+ return Object.defineProperties(Object.assign(name, {
678
+ typ: success ? EnumToken.DeclarationNodeType : EnumToken.DeclarationNodeType,
679
+ nam: name.val,
680
+ val: tokens,
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
+ },
700
+ },
701
+ });
121
702
  }
122
703
 
123
- export { parseDeclarationNode };
704
+ export { isDeclarationValue, parseDeclaration };