@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
@@ -0,0 +1,46 @@
1
+ import { EnumToken } from './types.js';
2
+
3
+ /**
4
+ *
5
+ * Clone an ast node or value
6
+ * @param node
7
+ * @param cloneChildren
8
+ * @param cloneMap
9
+ * @returns
10
+ */
11
+ function cloneNode(node, cloneChildren = false, cloneMap = null) {
12
+ const checkNode = node.typ == EnumToken.DeclarationNodeType ? "val" : "chi";
13
+ const clone = {};
14
+ const properties = {};
15
+ cloneMap?.set?.(node, clone);
16
+ for (const [name, value] of Object.entries(Object.getOwnPropertyDescriptors(node))) {
17
+ // @ts-expect-error
18
+ if (value.value == null || typeof value.value != "object" || name == "parent") {
19
+ // @ts-expect-error
20
+ properties[name] = { ...value };
21
+ // @ts-expect-error
22
+ }
23
+ else if (Array.isArray(value.value)) {
24
+ // @ts-expect-error
25
+ properties[name] = {
26
+ // @ts-expect-error
27
+ ...value,
28
+ value: !cloneChildren && name == checkNode
29
+ ? []
30
+ : // @ts-expect-error
31
+ value.value.map((c) => {
32
+ const newObj = cloneNode(c, cloneChildren, cloneMap);
33
+ cloneMap?.set?.(c, newObj);
34
+ return newObj;
35
+ }),
36
+ };
37
+ }
38
+ else {
39
+ // @ts-expect-error
40
+ properties[name] = { ...value, value: cloneNode(value.value, clone) };
41
+ }
42
+ }
43
+ return Object.defineProperties(clone, properties);
44
+ }
45
+
46
+ export { cloneNode };
@@ -1,12 +1,9 @@
1
- import { splitRule, combinators } from './minify.js';
1
+ import { splitRule } from './minify.js';
2
+ import { combinators } from '../syntax/constants.js';
2
3
  import { parseString } from '../parser/parse.js';
3
- import '../parser/tokenize.js';
4
- import '../parser/utils/config.js';
5
- import { EnumToken } from './types.js';
6
4
  import { walkValues } from './walk.js';
7
- import { renderToken } from '../renderer/render.js';
8
- import '../renderer/sourcemap/lib/encode.js';
9
- import '../syntax/color/utils/constants.js';
5
+ import { renderValue } from '../renderer/render.js';
6
+ import { EnumToken } from './types.js';
10
7
 
11
8
  /**
12
9
  * expand css nesting ast nodes
@@ -18,11 +15,11 @@ function expand(ast) {
18
15
  const result = { ...ast, chi: [] };
19
16
  for (let i = 0; i < ast.chi.length; i++) {
20
17
  const node = ast.chi[i];
21
- if (node.typ == EnumToken.RuleNodeType) {
18
+ if (node.typ === EnumToken.RuleNodeType) {
22
19
  // @ts-ignore
23
20
  result.chi.push(...expandRule(node));
24
21
  }
25
- else if (node.typ == EnumToken.AtRuleNodeType && 'chi' in node) {
22
+ else if (node.typ == EnumToken.AtRuleNodeType && "chi" in node) {
26
23
  let hasRule = false;
27
24
  let j = node.chi.length;
28
25
  while (j--) {
@@ -50,20 +47,23 @@ function expandRule(node) {
50
47
  for (; i < ast.chi.length; i++) {
51
48
  if (ast.chi[i].typ == EnumToken.RuleNodeType) {
52
49
  const rule = ast.chi[i];
53
- if (!rule.sel.includes('&')) {
50
+ if (!rule.sel.includes("&")) {
54
51
  const selRule = splitRule(rule.sel);
55
- const arSelf = splitRule(ast.sel).filter((r) => r.every((t) => t != ':before' && t != ':after' && !t.startsWith('::'))).reduce((acc, curr) => acc.concat([curr.join('')]), []).join(',');
52
+ const arSelf = splitRule(ast.sel)
53
+ .filter((r) => r.every((t) => t != ":before" && t != ":after" && !t.startsWith("::")))
54
+ .reduce((acc, curr) => acc.concat([curr.join("")]), [])
55
+ .join(",");
56
56
  if (arSelf.length == 0) {
57
57
  ast.chi.splice(i--, 1);
58
58
  continue;
59
59
  }
60
- //
61
- selRule.forEach(arr => combinators.includes(arr[0].charAt(0)) ? arr.unshift(arSelf) : arr.unshift(arSelf, ' '));
62
- rule.sel = selRule.reduce((acc, curr) => {
63
- acc.push(curr.join(''));
60
+ selRule.forEach((arr) => combinators.includes(arr[0].charAt(0)) ? arr.unshift(arSelf) : arr.unshift(arSelf, " "));
61
+ rule.sel = selRule
62
+ .reduce((acc, curr) => {
63
+ acc.push(curr.join(""));
64
64
  return acc;
65
- }, []).join(',');
66
- // }
65
+ }, [])
66
+ .join(",");
67
67
  }
68
68
  else {
69
69
  let childSelectorCompound = [];
@@ -71,22 +71,22 @@ function expandRule(node) {
71
71
  let withoutCompound = [];
72
72
  // pseudo elements cannot be used with '&'
73
73
  // https://www.w3.org/TR/css-nesting-1/#example-7145ff1e
74
- const rules = splitRule(ast.sel).filter((r) => r.every((t) => t != ':before' && t != ':after' && !t.startsWith('::')));
75
- const parentSelector = !node.sel.includes('&');
74
+ const rules = splitRule(ast.sel).filter((r) => r.every((t) => t != ":before" && t != ":after" && !t.startsWith("::")));
75
+ const parentSelector = !node.sel.includes("&");
76
76
  if (rules.length == 0) {
77
77
  ast.chi.splice(i--, 1);
78
78
  continue;
79
79
  }
80
- for (const sel of (rule.raw ?? splitRule(rule.sel))) {
81
- const s = sel.join('');
82
- if (s.includes('&') || parentSelector) {
83
- if (s.indexOf('&', 1) == -1) {
84
- if (s.at(0) == '&') {
85
- if (s.at(1) == ' ') {
80
+ for (const sel of rule.raw ?? splitRule(rule.sel)) {
81
+ const s = sel.join("");
82
+ if (s.includes("&") || parentSelector) {
83
+ if (s.indexOf("&", 1) == -1) {
84
+ if (s.at(0) == "&") {
85
+ if (s.at(1) == " ") {
86
86
  childSelectorCompound.push(s.slice(2));
87
87
  }
88
88
  else {
89
- if (s == '&' || parentSelector) {
89
+ if (s == "&" || parentSelector) {
90
90
  withCompound.push(s);
91
91
  }
92
92
  }
@@ -101,29 +101,48 @@ function expandRule(node) {
101
101
  }
102
102
  }
103
103
  const selectors = [];
104
- const selector = rules.length > 1 ? ':is(' + rules.map(a => a.join('')).join(',') + ')' : rules[0].join('');
104
+ const selector = rules.length > 1 ? ":is(" + rules.map((a) => a.join("")).join(",") + ")" : rules[0].join("");
105
105
  if (childSelectorCompound.length > 0) {
106
106
  if (childSelectorCompound.length == 1) {
107
- selectors.push(replaceCompound('& ' + childSelectorCompound[0].trim(), selector));
107
+ selectors.push(replaceCompound("& " + childSelectorCompound[0].trim(), selector));
108
108
  }
109
109
  else {
110
- selectors.push(replaceCompound('& :is(' + childSelectorCompound.reduce((acc, curr) => acc + (acc.length > 0 ? ',' : '') + curr.trim(), '') + ')', selector));
110
+ selectors.push(replaceCompound("& :is(" +
111
+ childSelectorCompound.reduce((acc, curr) => acc + (acc.length > 0 ? "," : "") + curr.trim(), "") +
112
+ ")", selector));
111
113
  }
112
114
  }
113
115
  if (withCompound.length > 0) {
114
- if (withCompound.every((t) => t[0] == '&' && t.indexOf('&', 1) == -1)) {
115
- withoutCompound.push(...withCompound.map(t => t.slice(1)));
116
+ if (withCompound.every((t) => t[0] == "&" && t.indexOf("&", 1) == -1)) {
117
+ withoutCompound.push(...withCompound.map((t) => t.slice(1)));
116
118
  withCompound.length = 0;
117
119
  }
118
120
  }
119
121
  if (withoutCompound.length > 0) {
120
122
  if (withoutCompound.length == 1) {
121
- const useIs = rules.length == 1 && selector.match(/^[a-zA-Z.:]/) != null && selector.includes(' ') && withoutCompound.length == 1 && withoutCompound[0].match(/^[a-zA-Z]+$/) != null;
122
- const compound = useIs ? ':is(&)' : '&';
123
- selectors.push(replaceCompound(rules.length == 1 ? (useIs ? withoutCompound[0] + ':is(&)' : (selector.match(/^[.:]/) && withoutCompound[0].match(/^[a-zA-Z]+$/) ? withoutCompound[0] + compound : compound + withoutCompound[0])) : (withoutCompound[0].match(/^[a-zA-Z:]+$/) ? withoutCompound[0].trim() + compound : '&' + (withoutCompound[0].match(/^\S+$/) ? withoutCompound[0].trim() : ':is(' + withoutCompound[0].trim() + ')')), selector));
123
+ const useIs = rules.length == 1 &&
124
+ selector.match(/^[a-zA-Z.:]/) != null &&
125
+ selector.includes(" ") &&
126
+ withoutCompound.length == 1 &&
127
+ withoutCompound[0].match(/^[a-zA-Z]+$/) != null;
128
+ const compound = useIs ? ":is(&)" : "&";
129
+ selectors.push(replaceCompound(rules.length == 1
130
+ ? useIs
131
+ ? withoutCompound[0] + ":is(&)"
132
+ : selector.match(/^[.:]/) && withoutCompound[0].match(/^[a-zA-Z]+$/)
133
+ ? withoutCompound[0] + compound
134
+ : compound + withoutCompound[0]
135
+ : withoutCompound[0].match(/^[a-zA-Z:]+$/)
136
+ ? withoutCompound[0].trim() + compound
137
+ : "&" +
138
+ (withoutCompound[0].match(/^\S+$/)
139
+ ? withoutCompound[0].trim()
140
+ : ":is(" + withoutCompound[0].trim() + ")"), selector));
124
141
  }
125
142
  else {
126
- selectors.push(replaceCompound('&:is(' + withoutCompound.reduce((acc, curr) => acc + (acc.length > 0 ? ',' : '') + curr.trim(), '') + ')', selector));
143
+ selectors.push(replaceCompound("&:is(" +
144
+ withoutCompound.reduce((acc, curr) => acc + (acc.length > 0 ? "," : "") + curr.trim(), "") +
145
+ ")", selector));
127
146
  }
128
147
  }
129
148
  if (withCompound.length > 0) {
@@ -131,7 +150,7 @@ function expandRule(node) {
131
150
  selectors.push(replaceCompound(withCompound[0], selector));
132
151
  }
133
152
  }
134
- rule.sel = selectors.reduce((acc, curr) => curr.length == 0 ? acc : acc + (acc.length > 0 ? ',' : '') + curr, '');
153
+ rule.sel = selectors.reduce((acc, curr) => (curr.length == 0 ? acc : acc + (acc.length > 0 ? "," : "") + curr), "");
135
154
  }
136
155
  ast.chi.splice(i--, 1);
137
156
  result.push(...expandRule(rule));
@@ -139,11 +158,13 @@ function expandRule(node) {
139
158
  else if (ast.chi[i].typ == EnumToken.AtRuleNodeType) {
140
159
  let astAtRule = ast.chi[i];
141
160
  const values = [];
142
- if (astAtRule.nam == 'scope') {
143
- if (astAtRule.val.includes('&')) {
161
+ if (astAtRule.nam === "scope") {
162
+ if (astAtRule.val.includes("&")) {
144
163
  astAtRule.val = replaceCompound(astAtRule.val, ast.sel);
145
164
  }
146
- const slice = astAtRule.chi.slice().filter(t => t.typ == EnumToken.RuleNodeType && t.sel.includes('&'));
165
+ const slice = astAtRule.chi
166
+ .slice()
167
+ .filter((t) => t.typ == EnumToken.RuleNodeType && t.sel.includes("&"));
147
168
  if (slice.length > 0) {
148
169
  expandRule({ ...node, chi: astAtRule.chi.slice() });
149
170
  }
@@ -154,13 +175,13 @@ function expandRule(node) {
154
175
  // @ts-ignore
155
176
  astAtRule.chi.length = 0;
156
177
  for (const r of expandRule(clone)) {
157
- if (r.typ == EnumToken.AtRuleNodeType && 'chi' in r) {
158
- if (astAtRule.val !== '' && r.val !== '') {
159
- if (astAtRule.nam == 'media' && r.nam == 'media') {
160
- r.val = astAtRule.val + ' and ' + r.val;
178
+ if (r.typ == EnumToken.AtRuleNodeType && "chi" in r) {
179
+ if (astAtRule.val !== "" && r.val !== "") {
180
+ if (astAtRule.nam === "media" && r.nam === "media") {
181
+ r.val = astAtRule.val + " and " + r.val;
161
182
  }
162
- else if (astAtRule.nam == 'layer' && r.nam == 'layer') {
163
- r.val = astAtRule.val + '.' + r.val;
183
+ else if (astAtRule.nam == "layer" && r.nam == "layer") {
184
+ r.val = astAtRule.val + "." + r.val;
164
185
  }
165
186
  }
166
187
  // @ts-ignore
@@ -190,38 +211,42 @@ function replaceCompound(input, replace) {
190
211
  const tokens = parseString(input);
191
212
  let replacement = null;
192
213
  for (const t of walkValues(tokens)) {
193
- if (t.value.typ == EnumToken.LiteralTokenType) {
194
- if (t.value.val == '&') {
195
- if (tokens.length == 2) {
196
- if (replacement == null) {
197
- replacement = parseString(replace);
198
- }
199
- t.value.val = replaceCompoundLiteral(t.value.val, replace);
200
- // }
201
- continue;
214
+ if (t.value.typ == EnumToken.NestingSelectorTokenType) {
215
+ if (tokens.length == 2) {
216
+ if (replacement == null) {
217
+ replacement = parseString(replace);
202
218
  }
203
- const rule = splitRule(replace);
204
- t.value.val = rule.length > 1 ? ':is(' + replace + ')' : replace;
219
+ Object.assign(t.value, {
220
+ typ: EnumToken.LiteralTokenType,
221
+ val: replaceCompoundLiteral(t.value.val, replace),
222
+ });
223
+ continue;
205
224
  }
225
+ const rule = splitRule(replace);
226
+ Object.assign(t.value, {
227
+ typ: EnumToken.LiteralTokenType,
228
+ val: rule.length > 1 ? ":is(" + replace + ")" : replace,
229
+ });
206
230
  }
207
231
  }
208
- return tokens.reduce((acc, curr) => acc + renderToken(curr), '');
232
+ return tokens.reduce((acc, curr) => acc + renderValue(curr), "");
209
233
  }
210
234
  function replaceCompoundLiteral(selector, replace) {
211
- const tokens = [''];
235
+ const tokens = [""];
212
236
  let i = 0;
213
237
  for (; i < selector.length; i++) {
214
- if (selector.charAt(i) == '&') {
215
- tokens.push('&');
216
- tokens.push('');
238
+ if (selector.charAt(i) == "&") {
239
+ tokens.push("&", "");
217
240
  }
218
241
  }
219
- return tokens.sort((a, b) => {
220
- if (a == '&') {
242
+ return tokens
243
+ .sort((a, b) => {
244
+ if (a == "&") {
221
245
  return 1;
222
246
  }
223
- return b == '&' ? -1 : 0;
224
- }).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
247
+ return b == "&" ? -1 : 0;
248
+ })
249
+ .reduce((acc, curr) => acc + (curr == "&" ? replace : curr), "");
225
250
  }
226
251
 
227
252
  export { expand, replaceCompound };
@@ -1,14 +1,9 @@
1
1
  import { EnumToken } from '../types.js';
2
2
  import { walkValues, WalkerEvent, WalkerOptionEnum } from '../walk.js';
3
3
  import { evaluate } from '../math/expression.js';
4
- import { renderToken } from '../../renderer/render.js';
5
- import '../../renderer/sourcemap/lib/encode.js';
6
- import { mathFuncs } from '../../syntax/syntax.js';
7
- import '../minify.js';
8
- import '../../parser/parse.js';
9
- import '../../parser/tokenize.js';
10
- import '../../parser/utils/config.js';
4
+ import { renderValue } from '../../renderer/render.js';
11
5
  import { FeatureWalkMode } from './type.js';
6
+ import { mathFuncs, tokensfuncSet } from '../../syntax/constants.js';
12
7
 
13
8
  class ComputeCalcExpressionFeature {
14
9
  accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType]);
@@ -25,14 +20,14 @@ class ComputeCalcExpressionFeature {
25
20
  }
26
21
  }
27
22
  run(ast) {
28
- if (!('chi' in ast)) {
23
+ if (!("chi" in ast)) {
29
24
  return null;
30
25
  }
31
26
  for (const node of ast.chi) {
32
27
  if (node.typ != EnumToken.DeclarationNodeType) {
33
28
  continue;
34
29
  }
35
- const set = new Set;
30
+ const set = new Set();
36
31
  for (const { value, parent } of walkValues(node.val, node, {
37
32
  event: WalkerEvent.Enter,
38
33
  // @ts-ignore
@@ -42,61 +37,109 @@ class ComputeCalcExpressionFeature {
42
37
  parent.typ == EnumToken.DeclarationNodeType &&
43
38
  // @ts-ignore
44
39
  parent.val.length == 1 &&
45
- node.typ == EnumToken.FunctionTokenType &&
40
+ (node.typ === EnumToken.MathFunctionTokenType || node.typ === EnumToken.FunctionTokenType) &&
46
41
  mathFuncs.includes(node.val) &&
47
42
  node.chi.length == 1 &&
48
43
  node.chi[0].typ == EnumToken.IdenTokenType) {
49
44
  return WalkerOptionEnum.Ignore;
50
45
  }
51
- if ((node.typ == EnumToken.FunctionTokenType && node.val == 'var') || (!mathFuncs.includes(parent.val) && [EnumToken.ColorTokenType, EnumToken.DeclarationNodeType, EnumToken.RuleNodeType, EnumToken.AtRuleNodeType, EnumToken.StyleSheetNodeType].includes(parent?.typ))) {
46
+ if ((node.typ === EnumToken.WildCardFunctionTokenType && node.val == "var") ||
47
+ (!mathFuncs.includes(parent.val) &&
48
+ [
49
+ EnumToken.MathFunctionTokenType,
50
+ EnumToken.ColorTokenType,
51
+ EnumToken.DeclarationNodeType,
52
+ EnumToken.ImageFunc,
53
+ EnumToken.RuleNodeType,
54
+ EnumToken.AtRuleNodeType,
55
+ EnumToken.StyleSheetNodeType,
56
+ ].includes(parent?.typ))) {
52
57
  return null;
53
58
  }
54
59
  // @ts-ignore
55
- const slice = (node.typ == EnumToken.FunctionTokenType ? node.chi : (node.typ == EnumToken.DeclarationNodeType ? node.val : node.chi))?.slice();
56
- if (slice != null && node.typ == EnumToken.FunctionTokenType && mathFuncs.includes(node.val)) {
60
+ const slice = (node.typ == EnumToken.FunctionTokenType || node.typ == EnumToken.MathFunctionTokenType
61
+ ? node.chi
62
+ : node.typ == EnumToken.DeclarationNodeType
63
+ ? node.val
64
+ : node.chi)?.slice();
65
+ if (slice != null &&
66
+ (node.typ === EnumToken.MathFunctionTokenType ||
67
+ (node.typ == EnumToken.FunctionTokenType &&
68
+ mathFuncs.includes(node.val)))) {
57
69
  // @ts-ignore
58
- const key = 'chi' in node ? 'chi' : 'val';
59
- const str1 = renderToken({ ...node, [key]: slice });
60
- const str2 = renderToken(node); // values.reduce((acc: string, curr: Token): string => acc + renderToken(curr), '');
61
- if (str1.length <= str2.length) {
70
+ const key = "chi" in node ? "chi" : "val";
71
+ const str1 = renderValue({ ...node, [key]: slice });
72
+ const str2 = renderValue(node); // values.reduce((acc: string, curr: Token): string => acc + renderValue(curr), '');
73
+ if (str1.length < str2.length) {
62
74
  // @ts-ignore
63
75
  node[key] = slice;
64
76
  }
65
77
  return WalkerOptionEnum.Ignore;
66
78
  }
67
79
  return null;
68
- }
80
+ },
69
81
  })) {
70
- if (value != null && value.typ == EnumToken.FunctionTokenType && mathFuncs.includes(value.val)) {
82
+ if (value != null && tokensfuncSet.has(value.typ)) {
71
83
  if (!set.has(value)) {
72
84
  set.add(value);
73
85
  if (parent != null) {
74
- // @ts-ignore
75
- const cp = value.typ == EnumToken.FunctionTokenType && mathFuncs.includes(value.val) && value.val != 'calc' ? [value] : (value.typ == EnumToken.DeclarationNodeType ? value.val : value.chi);
76
- const values = evaluate(cp);
77
- // @ts-ignore
78
- const children = parent.typ == EnumToken.DeclarationNodeType ? parent.val : parent.chi;
79
- if (values.length == 1 && values[0].typ != EnumToken.BinaryExpressionTokenType) {
80
- for (let i = 0; i < children.length; i++) {
81
- if (children[i] == value) {
82
- children.splice(i, 1, !(parent.typ == EnumToken.FunctionTokenType && parent.val == 'calc') && (typeof values[0].val != 'number' && !(values[0].typ == EnumToken.FunctionTokenType && mathFuncs.includes(values[0].val))) ? {
83
- typ: EnumToken.FunctionTokenType,
84
- val: 'calc',
85
- chi: values
86
- } : values[0]);
87
- break;
86
+ const shouldEvaluate = value.typ === EnumToken.MathFunctionTokenType ||
87
+ (value.typ == EnumToken.FunctionTokenType &&
88
+ mathFuncs.includes(value.val));
89
+ if (shouldEvaluate) {
90
+ // @ts-ignore
91
+ const cp = value.val != "calc"
92
+ ? [value]
93
+ : value.typ == EnumToken.DeclarationNodeType
94
+ ? value.val
95
+ : value.chi;
96
+ const values = evaluate(cp);
97
+ // fix a + -b to a - b
98
+ for (const { value, parent: p } of walkValues(values)) {
99
+ if (value.typ === EnumToken.BinaryExpressionTokenType) {
100
+ if (value.op === EnumToken.Add &&
101
+ Math.sign(value.r.val) == -1) {
102
+ value.op = EnumToken.Sub;
103
+ // @ts-expect-error
104
+ value.r.val *=
105
+ -1;
106
+ }
88
107
  }
89
108
  }
90
- }
91
- else {
92
- for (let i = 0; i < children.length; i++) {
93
- if (children[i] == value) {
94
- children.splice(i, 1, {
95
- typ: EnumToken.FunctionTokenType,
96
- val: 'calc',
97
- chi: values
98
- });
99
- break;
109
+ // @ts-ignore
110
+ const children = parent.typ == EnumToken.DeclarationNodeType
111
+ ? parent.val
112
+ : parent.chi;
113
+ if (values.length == 1 && values[0].typ != EnumToken.BinaryExpressionTokenType) {
114
+ for (let i = 0; i < children.length; i++) {
115
+ if (children[i] == value) {
116
+ children.splice(i, 1, !(parent.typ === EnumToken.MathFunctionTokenType ||
117
+ (parent.typ == EnumToken.FunctionTokenType &&
118
+ parent.val == "calc")) &&
119
+ typeof values[0].val != "number" &&
120
+ !(values[0].typ == EnumToken.MathFunctionTokenType ||
121
+ (values[0].typ == EnumToken.FunctionTokenType &&
122
+ mathFuncs.includes(values[0].val)))
123
+ ? {
124
+ typ: EnumToken.MathFunctionTokenType,
125
+ val: "calc",
126
+ chi: values,
127
+ }
128
+ : values[0]);
129
+ break;
130
+ }
131
+ }
132
+ }
133
+ else {
134
+ for (let i = 0; i < children.length; i++) {
135
+ if (children[i] == value) {
136
+ children.splice(i, 1, {
137
+ typ: EnumToken.MathFunctionTokenType,
138
+ val: "calc",
139
+ chi: values,
140
+ });
141
+ break;
142
+ }
100
143
  }
101
144
  }
102
145
  }