@tbela99/css-parser 1.1.1 → 1.3.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 (107) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +53 -6
  3. package/dist/index-umd-web.js +5503 -5037
  4. package/dist/index.cjs +5510 -5044
  5. package/dist/index.d.ts +148 -41
  6. package/dist/lib/ast/expand.js +81 -65
  7. package/dist/lib/ast/features/calc.js +14 -36
  8. package/dist/lib/ast/features/inlinecssvariables.js +6 -17
  9. package/dist/lib/ast/features/prefix.js +22 -19
  10. package/dist/lib/ast/features/shorthand.js +1 -1
  11. package/dist/lib/ast/features/transform.js +17 -2
  12. package/dist/lib/ast/math/expression.js +74 -172
  13. package/dist/lib/ast/math/math.js +24 -22
  14. package/dist/lib/ast/minify.js +249 -199
  15. package/dist/lib/ast/transform/compute.js +18 -41
  16. package/dist/lib/ast/transform/matrix.js +36 -36
  17. package/dist/lib/ast/transform/minify.js +37 -59
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +13 -13
  20. package/dist/lib/ast/transform/scale.js +8 -8
  21. package/dist/lib/ast/transform/skew.js +4 -4
  22. package/dist/lib/ast/transform/translate.js +8 -8
  23. package/dist/lib/ast/transform/utils.js +80 -28
  24. package/dist/lib/ast/types.js +122 -2
  25. package/dist/lib/fs/resolve.js +1 -14
  26. package/dist/lib/parser/declaration/list.js +1 -1
  27. package/dist/lib/parser/declaration/map.js +1 -1
  28. package/dist/lib/parser/declaration/set.js +3 -3
  29. package/dist/lib/parser/parse.js +53 -107
  30. package/dist/lib/parser/tokenize.js +29 -53
  31. package/dist/lib/parser/utils/declaration.js +1 -1
  32. package/dist/lib/parser/utils/type.js +2 -2
  33. package/dist/lib/renderer/render.js +79 -194
  34. package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
  35. package/dist/lib/syntax/color/cmyk.js +104 -0
  36. package/dist/lib/{renderer → syntax}/color/color-mix.js +31 -33
  37. package/dist/lib/syntax/color/color.js +581 -0
  38. package/dist/lib/syntax/color/hex.js +179 -0
  39. package/dist/lib/syntax/color/hsl.js +201 -0
  40. package/dist/lib/syntax/color/hwb.js +204 -0
  41. package/dist/lib/syntax/color/lab.js +262 -0
  42. package/dist/lib/syntax/color/lch.js +194 -0
  43. package/dist/lib/syntax/color/oklab.js +237 -0
  44. package/dist/lib/syntax/color/oklch.js +166 -0
  45. package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
  46. package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
  47. package/dist/lib/{renderer → syntax}/color/relativecolor.js +54 -41
  48. package/dist/lib/syntax/color/rgb.js +140 -0
  49. package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
  50. package/dist/lib/syntax/color/utils/components.js +46 -0
  51. package/dist/lib/{renderer → syntax}/color/utils/constants.js +6 -33
  52. package/dist/lib/syntax/color/utils/distance.js +40 -0
  53. package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
  54. package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
  55. package/dist/lib/syntax/syntax.js +78 -77
  56. package/dist/lib/syntax/utils.js +70 -0
  57. package/dist/lib/validation/at-rules/container.js +1 -1
  58. package/dist/lib/validation/at-rules/counter-style.js +1 -1
  59. package/dist/lib/validation/at-rules/custom-media.js +1 -1
  60. package/dist/lib/validation/at-rules/document.js +2 -2
  61. package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
  62. package/dist/lib/validation/at-rules/import.js +5 -5
  63. package/dist/lib/validation/at-rules/keyframes.js +3 -13
  64. package/dist/lib/validation/at-rules/layer.js +1 -1
  65. package/dist/lib/validation/at-rules/media.js +1 -1
  66. package/dist/lib/validation/at-rules/namespace.js +1 -1
  67. package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
  68. package/dist/lib/validation/at-rules/page.js +1 -1
  69. package/dist/lib/validation/at-rules/supports.js +7 -7
  70. package/dist/lib/validation/at-rules/when.js +1 -1
  71. package/dist/lib/validation/atrule.js +2 -2
  72. package/dist/lib/validation/config.js +0 -3
  73. package/dist/lib/validation/config.json.js +1 -1
  74. package/dist/lib/validation/parser/parse.js +8 -11
  75. package/dist/lib/validation/selector.js +1 -9
  76. package/dist/lib/validation/syntax.js +67 -137
  77. package/dist/lib/validation/syntaxes/complex-selector-list.js +2 -19
  78. package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
  79. package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
  80. package/dist/lib/validation/syntaxes/family-name.js +5 -40
  81. package/dist/lib/validation/syntaxes/keyframe-selector.js +3 -22
  82. package/dist/lib/validation/syntaxes/layer-name.js +1 -1
  83. package/dist/lib/validation/syntaxes/relative-selector-list.js +1 -25
  84. package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
  85. package/dist/lib/validation/syntaxes/url.js +3 -34
  86. package/dist/lib/validation/utils/list.js +2 -9
  87. package/dist/lib/validation/utils/whitespace.js +1 -1
  88. package/dist/node/index.js +4 -2
  89. package/dist/web/index.js +4 -2
  90. package/package.json +4 -4
  91. package/.editorconfig +0 -484
  92. package/dist/lib/ast/transform/convert.js +0 -33
  93. package/dist/lib/ast/utils/utils.js +0 -104
  94. package/dist/lib/renderer/color/color.js +0 -654
  95. package/dist/lib/renderer/color/hex.js +0 -105
  96. package/dist/lib/renderer/color/hsl.js +0 -125
  97. package/dist/lib/renderer/color/hwb.js +0 -103
  98. package/dist/lib/renderer/color/lab.js +0 -148
  99. package/dist/lib/renderer/color/lch.js +0 -90
  100. package/dist/lib/renderer/color/oklab.js +0 -131
  101. package/dist/lib/renderer/color/oklch.js +0 -75
  102. package/dist/lib/renderer/color/rgb.js +0 -50
  103. package/dist/lib/renderer/color/utils/components.js +0 -34
  104. package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
  105. package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
  106. package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
  107. /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
@@ -2,14 +2,14 @@ import { EnumToken, SyntaxValidationResult } from '../types.js';
2
2
  import { getSyntaxConfig } from '../../validation/config.js';
3
3
  import '../../validation/parser/types.js';
4
4
  import '../../validation/parser/parse.js';
5
- import { splitRule, definedPropertySettings } from '../minify.js';
5
+ import { splitRule } from '../minify.js';
6
6
  import { walkValues } from '../walk.js';
7
- import { parseAtRulePrelude, parseString } from '../../parser/parse.js';
7
+ import '../../parser/parse.js';
8
8
  import '../../parser/tokenize.js';
9
9
  import '../../parser/utils/config.js';
10
10
  import { pseudoAliasMap } from '../../syntax/syntax.js';
11
11
  import { renderToken } from '../../renderer/render.js';
12
- import { funcLike } from '../../renderer/color/utils/constants.js';
12
+ import { funcLike } from '../../syntax/color/utils/constants.js';
13
13
  import '../../validation/syntaxes/complex-selector.js';
14
14
  import { evaluateSyntax } from '../../validation/syntax.js';
15
15
 
@@ -73,12 +73,15 @@ class ComputePrefixFeature {
73
73
  run(node) {
74
74
  if (node.typ == EnumToken.RuleNodeType) {
75
75
  node.sel = replacePseudo(splitRule(node.sel)).reduce((acc, curr, index) => acc + (index > 0 ? ',' : '') + curr.join(''), '');
76
- if (node.raw != null) {
77
- node.raw = replacePseudo(node.raw);
78
- }
79
- if (node.optimized != null) {
80
- node.optimized.selector = replacePseudo(node.optimized.selector);
81
- }
76
+ // if ((node as AstRule).raw != null) {
77
+ //
78
+ // (node as AstRule).raw = replacePseudo((node as AstRule).raw as string[][]);
79
+ // }
80
+ //
81
+ // if ((node as AstRule).optimized != null) {
82
+ //
83
+ // (node as AstRule).optimized!.selector = replacePseudo((node as AstRule).optimized!.selector as string[][]);
84
+ // }
82
85
  if (node.tokens != null) {
83
86
  replaceAstNodes(node.tokens);
84
87
  }
@@ -114,10 +117,9 @@ class ComputePrefixFeature {
114
117
  for (const { value } of walkValues(nodes)) {
115
118
  if ((value.typ == EnumToken.IdenTokenType || funcLike.includes(value.typ))) {
116
119
  const match = value.val.match(/^-([^-]+)-(.+)$/);
117
- if (match == null) {
118
- continue;
120
+ if (match != null) {
121
+ value.val = match[2];
119
122
  }
120
- value.val = match[2];
121
123
  }
122
124
  }
123
125
  if (SyntaxValidationResult.Valid == evaluateSyntax({ ...node, val: nodes }, {}).valid) {
@@ -133,13 +135,14 @@ class ComputePrefixFeature {
133
135
  }
134
136
  }
135
137
  if (node.typ == EnumToken.AtRuleNodeType && node.val !== '') {
136
- if (node.tokens == null) {
137
- Object.defineProperty(node, 'tokens', {
138
- // @ts-ignore
139
- ...definedPropertySettings,
140
- value: parseAtRulePrelude(parseString(node.val), node),
141
- });
142
- }
138
+ // if ((node as AstAtRule).tokens == null) {
139
+ //
140
+ // Object.defineProperty(node, 'tokens', {
141
+ // // @ts-ignore
142
+ // ...definedPropertySettings,
143
+ // value: parseAtRulePrelude(parseString((node as AstAtRule).val), node as AstAtRule),
144
+ // })
145
+ // }
143
146
  if (replaceAstNodes(node.tokens)) {
144
147
  node.val = node.tokens.reduce((acc, curr) => acc + renderToken(curr), '');
145
148
  }
@@ -5,7 +5,7 @@ import '../walk.js';
5
5
  import '../../parser/parse.js';
6
6
  import '../../parser/tokenize.js';
7
7
  import '../../parser/utils/config.js';
8
- import '../../renderer/color/utils/constants.js';
8
+ import '../../syntax/color/utils/constants.js';
9
9
  import '../../renderer/sourcemap/lib/encode.js';
10
10
 
11
11
  class ComputeShorthandFeature {
@@ -6,7 +6,7 @@ import '../../parser/parse.js';
6
6
  import '../../parser/tokenize.js';
7
7
  import '../../parser/utils/config.js';
8
8
  import { filterValues, renderToken } from '../../renderer/render.js';
9
- import '../../renderer/color/utils/constants.js';
9
+ import '../../syntax/color/utils/constants.js';
10
10
  import { compute } from '../transform/compute.js';
11
11
  import { eqMatrix } from '../transform/minify.js';
12
12
 
@@ -40,7 +40,22 @@ class TransformCssFeature {
40
40
  if (node.typ != EnumToken.DeclarationNodeType || !node.nam.match(/^(-[a-z]+-)?transform$/)) {
41
41
  continue;
42
42
  }
43
- const children = node.val.slice();
43
+ const children = node.val.reduce((acc, curr) => {
44
+ if (curr.typ == EnumToken.FunctionTokenType && 'skew' == curr.val.toLowerCase()) {
45
+ if (curr.chi.length == 3) {
46
+ if (curr.chi[2].val == 0) {
47
+ curr.chi.length = 1;
48
+ curr.val = 'skew';
49
+ }
50
+ else if (curr.chi[0].val == 0) {
51
+ curr.chi = [curr.chi[2]];
52
+ curr.val = 'skewY';
53
+ }
54
+ }
55
+ }
56
+ acc.push(curr);
57
+ return acc;
58
+ }, []);
44
59
  consumeWhitespace(children);
45
60
  let { matrix, cumulative, minified } = compute(children) ?? {};
46
61
  if (matrix == null || cumulative == null || minified == null) {
@@ -1,13 +1,13 @@
1
1
  import { EnumToken } from '../types.js';
2
2
  import { rem, compute } from './math.js';
3
- import { reduceNumber } from '../../renderer/render.js';
4
- import '../../renderer/color/utils/constants.js';
3
+ import { mathFuncs } from '../../syntax/syntax.js';
5
4
  import '../minify.js';
6
5
  import '../walk.js';
7
6
  import '../../parser/parse.js';
8
7
  import '../../parser/tokenize.js';
9
8
  import '../../parser/utils/config.js';
10
- import { mathFuncs } from '../../syntax/syntax.js';
9
+ import '../../syntax/color/utils/constants.js';
10
+ import '../../renderer/sourcemap/lib/encode.js';
11
11
 
12
12
  /**
13
13
  * evaluate an array of tokens
@@ -38,12 +38,7 @@ function evaluate(tokens) {
38
38
  });
39
39
  return evaluateFunc(tokens[0]);
40
40
  }
41
- try {
42
- nodes = inlineExpression(evaluateExpression(buildExpression(tokens)));
43
- }
44
- catch (e) {
45
- return tokens;
46
- }
41
+ nodes = inlineExpression(evaluateExpression(buildExpression(tokens)));
47
42
  if (nodes.length <= 1) {
48
43
  if (nodes.length == 1) {
49
44
  if (nodes[0].typ == EnumToken.BinaryExpressionTokenType) {
@@ -54,7 +49,7 @@ function evaluate(tokens) {
54
49
  return [{
55
50
  ...nodes[0],
56
51
  // @ts-ignore
57
- val: '' + Math[nodes[0].val.toUpperCase()],
52
+ val: Math[nodes[0].val.toUpperCase()],
58
53
  typ: EnumToken.NumberTokenType
59
54
  }];
60
55
  }
@@ -74,7 +69,7 @@ function evaluate(tokens) {
74
69
  token = { typ: EnumToken.ListToken, chi: [nodes[i], nodes[i + 1]] };
75
70
  }
76
71
  else {
77
- token = doEvaluate(nodes[i + 1], { typ: EnumToken.NumberTokenType, val: '-1' }, EnumToken.Mul);
72
+ token = doEvaluate(nodes[i + 1], { typ: EnumToken.NumberTokenType, val: -1 }, EnumToken.Mul);
78
73
  }
79
74
  i++;
80
75
  }
@@ -89,7 +84,7 @@ function evaluate(tokens) {
89
84
  const token = curr[1].reduce((acc, curr) => doEvaluate(acc, curr, EnumToken.Add));
90
85
  if (token.typ != EnumToken.BinaryExpressionTokenType) {
91
86
  if ('val' in token && +token.val < 0) {
92
- acc.push({ typ: EnumToken.Sub }, { ...token, val: String(-token.val) });
87
+ acc.push({ typ: EnumToken.Sub }, { ...token, val: -token.val });
93
88
  return acc;
94
89
  }
95
90
  }
@@ -116,29 +111,11 @@ function doEvaluate(l, r, op) {
116
111
  if (!isScalarToken(l) || !isScalarToken(r) || (l.typ == r.typ && 'unit' in l && 'unit' in r && l.unit != r.unit)) {
117
112
  return defaultReturn;
118
113
  }
119
- if (l.typ == EnumToken.FunctionTokenType) {
120
- const val = evaluateFunc(l);
121
- if (val.length == 1) {
122
- l = val[0];
123
- }
124
- else {
125
- return defaultReturn;
126
- }
127
- }
128
114
  if (r.typ == EnumToken.FunctionTokenType) {
129
115
  const val = evaluateFunc(r);
130
116
  if (val.length == 1) {
131
117
  r = val[0];
132
118
  }
133
- else {
134
- return defaultReturn;
135
- }
136
- }
137
- if (l.typ == EnumToken.FunctionTokenType) {
138
- const val = evaluateFunc(l);
139
- if (val.length == 1) {
140
- l = val[0];
141
- }
142
119
  }
143
120
  if ((op == EnumToken.Add || op == EnumToken.Sub)) {
144
121
  // @ts-ignore
@@ -146,32 +123,25 @@ function doEvaluate(l, r, op) {
146
123
  return defaultReturn;
147
124
  }
148
125
  }
149
- else if (op == EnumToken.Mul &&
150
- ![EnumToken.NumberTokenType, EnumToken.PercentageTokenType].includes(l.typ) &&
151
- ![EnumToken.NumberTokenType, EnumToken.PercentageTokenType].includes(r.typ)) {
152
- return defaultReturn;
153
- }
154
126
  let typ = l.typ == EnumToken.NumberTokenType ? r.typ : (r.typ == EnumToken.NumberTokenType ? l.typ : (l.typ == EnumToken.PercentageTokenType ? r.typ : l.typ));
155
127
  // @ts-ignore
156
- let v1 = getValue(l);
157
- let v2 = getValue(r);
158
- if (v1 == null || v2 == null) {
159
- return defaultReturn;
160
- }
128
+ let v1 = l.val?.typ == EnumToken.FractionTokenType ? l.val : getValue(l);
129
+ // @ts-ignore
130
+ let v2 = r.val?.typ == EnumToken.FractionTokenType ? r.val : getValue(r);
161
131
  if (op == EnumToken.Mul) {
162
132
  if (l.typ != EnumToken.NumberTokenType && r.typ != EnumToken.NumberTokenType) {
163
133
  if (typeof v1 == 'number' && l.typ == EnumToken.PercentageTokenType) {
164
134
  v1 = {
165
135
  typ: EnumToken.FractionTokenType,
166
- l: { typ: EnumToken.NumberTokenType, val: String(v1) },
167
- r: { typ: EnumToken.NumberTokenType, val: '100' }
136
+ l: { typ: EnumToken.NumberTokenType, val: v1 },
137
+ r: { typ: EnumToken.NumberTokenType, val: 100 }
168
138
  };
169
139
  }
170
140
  else if (typeof v2 == 'number' && r.typ == EnumToken.PercentageTokenType) {
171
141
  v2 = {
172
142
  typ: EnumToken.FractionTokenType,
173
- l: { typ: EnumToken.NumberTokenType, val: String(v2) },
174
- r: { typ: EnumToken.NumberTokenType, val: '100' }
143
+ l: { typ: EnumToken.NumberTokenType, val: v2 },
144
+ r: { typ: EnumToken.NumberTokenType, val: 100 }
175
145
  };
176
146
  }
177
147
  }
@@ -181,7 +151,7 @@ function doEvaluate(l, r, op) {
181
151
  const token = {
182
152
  ...(l.typ == EnumToken.NumberTokenType ? r : l),
183
153
  typ,
184
- val: typeof val == 'number' ? reduceNumber(val) : val
154
+ val /* : typeof val == 'number' ? minifyNumber(val) : val */
185
155
  };
186
156
  if (token.typ == EnumToken.IdenTokenType) {
187
157
  // @ts-ignore
@@ -190,22 +160,10 @@ function doEvaluate(l, r, op) {
190
160
  return token;
191
161
  }
192
162
  function getValue(t) {
193
- let v1;
194
- if (t.typ == EnumToken.FunctionTokenType) {
195
- v1 = evaluateFunc(t);
196
- if (v1.length != 1 || v1[0].typ == EnumToken.BinaryExpressionTokenType) {
197
- return null;
198
- }
199
- t = v1[0];
200
- }
201
163
  if (t.typ == EnumToken.IdenTokenType) {
202
164
  // @ts-ignore
203
165
  return Math[t.val.toUpperCase()];
204
166
  }
205
- if (t.val.typ == EnumToken.FractionTokenType) {
206
- // @ts-ignore
207
- return t.val.l.val / t.val.r.val;
208
- }
209
167
  // @ts-ignore
210
168
  return t.typ == EnumToken.FractionTokenType ? t.l.val / t.r.val : +t.val;
211
169
  }
@@ -223,14 +181,11 @@ function evaluateFunc(token) {
223
181
  case 'sqrt':
224
182
  case 'exp': {
225
183
  const value = evaluate(values);
226
- if (value.length != 1 || (value[0].typ != EnumToken.NumberTokenType && value[0].typ != EnumToken.FractionTokenType) || (value[0].typ == EnumToken.FractionTokenType && (+value[0].r.val == 0 || !Number.isFinite(+value[0].l.val) || !Number.isFinite(+value[0].r.val)))) {
227
- return value;
228
- }
229
184
  // @ts-ignore
230
185
  let val = value[0].typ == EnumToken.NumberTokenType ? +value[0].val : value[0].l.val / value[0].r.val;
231
186
  return [{
232
187
  typ: EnumToken.NumberTokenType,
233
- val: '' + Math[token.val](val)
188
+ val: Math[token.val](val)
234
189
  }];
235
190
  }
236
191
  case 'hypot': {
@@ -238,26 +193,16 @@ function evaluateFunc(token) {
238
193
  let all = [];
239
194
  let ref = chi[0];
240
195
  let value = 0;
241
- if (![EnumToken.NumberTokenType, EnumToken.PercentageTokenType].includes(ref.typ) && !('unit' in ref)) {
242
- return [token];
243
- }
244
196
  for (let i = 0; i < chi.length; i++) {
245
- // @ts-ignore
246
- if (chi[i].typ != ref.typ || ('unit' in chi[i] && 'unit' in ref && chi[i].unit != ref.unit)) {
247
- return [token];
248
- }
249
197
  // @ts-ignore
250
198
  const val = getValue(chi[i]);
251
- if (val == null) {
252
- return [token];
253
- }
254
199
  all.push(val);
255
200
  value += val * val;
256
201
  }
257
202
  return [
258
203
  {
259
204
  ...ref,
260
- val: Math.sqrt(value).toFixed(rem(...all))
205
+ val: +(Math.sqrt(value).toFixed(rem(...all)))
261
206
  }
262
207
  ];
263
208
  }
@@ -266,39 +211,18 @@ function evaluateFunc(token) {
266
211
  case 'rem':
267
212
  case 'mod': {
268
213
  const chi = values.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
269
- if (chi.length != 3 || chi[1].typ != EnumToken.CommaTokenType) {
270
- return [token];
271
- }
272
- if (token.val == 'pow' && (chi[0].typ != EnumToken.NumberTokenType || chi[2].typ != EnumToken.NumberTokenType)) {
273
- return [token];
274
- }
275
- if (['rem', 'mod'].includes(token.val) &&
276
- (chi[0].typ != chi[2].typ) || ('unit' in chi[0] && 'unit' in chi[2] &&
277
- chi[0].unit != chi[2].unit)) {
278
- return [token];
279
- }
280
214
  // https://developer.mozilla.org/en-US/docs/Web/CSS/mod
281
215
  const v1 = evaluate([chi[0]]);
282
216
  const v2 = evaluate([chi[2]]);
283
- const types = [EnumToken.PercentageTokenType, EnumToken.DimensionTokenType, EnumToken.AngleTokenType, EnumToken.NumberTokenType, EnumToken.LengthTokenType, EnumToken.TimeTokenType, EnumToken.FrequencyTokenType, EnumToken.ResolutionTokenType];
284
- if (v1.length != 1 || v2.length != 1 || !types.includes(v1[0].typ) || !types.includes(v2[0].typ) || v1[0].unit != v2[0].unit) {
285
- return [token];
286
- }
287
217
  // @ts-ignore
288
218
  const val1 = getValue(v1[0]);
289
219
  // @ts-ignore
290
220
  const val2 = getValue(v2[0]);
291
- if (val1 == null || val2 == null || (v1[0].typ != v2[0].typ && val1 != 0 && val2 != 0)) {
292
- return [token];
293
- }
294
221
  if (token.val == 'rem') {
295
- if (val2 == 0) {
296
- return [token];
297
- }
298
222
  return [
299
223
  {
300
224
  ...v1[0],
301
- val: (val1 % val2).toFixed(rem(val1, val2))
225
+ val: +((val1 % val2).toFixed(rem(val1, val2)))
302
226
  }
303
227
  ];
304
228
  }
@@ -306,7 +230,7 @@ function evaluateFunc(token) {
306
230
  return [
307
231
  {
308
232
  ...v1[0],
309
- val: String(Math.pow(val1, val2))
233
+ val: Math.pow(val1, val2)
310
234
  }
311
235
  ];
312
236
  }
@@ -314,14 +238,14 @@ function evaluateFunc(token) {
314
238
  return [
315
239
  {
316
240
  ...{}, ...v1[0],
317
- val: String(Math.atan2(val1, val2))
241
+ val: Math.atan2(val1, val2)
318
242
  }
319
243
  ];
320
244
  }
321
245
  return [
322
246
  {
323
247
  ...v1[0],
324
- val: String(val2 == 0 ? val1 : val1 - (Math.floor(val1 / val2) * val2))
248
+ val: val2 == 0 ? val1 : val1 - (Math.floor(val1 / val2) * val2)
325
249
  }
326
250
  ];
327
251
  }
@@ -331,82 +255,63 @@ function evaluateFunc(token) {
331
255
  case 'log':
332
256
  case 'round':
333
257
  case 'min':
334
- case 'max':
335
- {
336
- const strategy = token.val == 'round' && values[0]?.typ == EnumToken.IdenTokenType ? values.shift().val : null;
337
- const valuesMap = new Map;
338
- for (const curr of values) {
339
- if (curr.typ == EnumToken.CommaTokenType || curr.typ == EnumToken.WhitespaceTokenType || curr.typ == EnumToken.CommentTokenType) {
340
- continue;
341
- }
342
- const result = evaluate([curr]);
343
- if (result.length != 1 || result[0].typ == EnumToken.FunctionTokenType) {
344
- return [token];
345
- }
346
- const key = result[0].typ + ('unit' in result[0] ? result[0].unit : '');
347
- if (!valuesMap.has(key)) {
348
- valuesMap.set(key, []);
349
- }
350
- valuesMap.get(key).push(result[0]);
258
+ case 'max': {
259
+ const strategy = token.val == 'round' && values[0]?.typ == EnumToken.IdenTokenType ? values.shift().val : null;
260
+ const valuesMap = new Map;
261
+ for (const curr of values) {
262
+ if (curr.typ == EnumToken.CommaTokenType || curr.typ == EnumToken.WhitespaceTokenType || curr.typ == EnumToken.CommentTokenType) {
263
+ continue;
351
264
  }
352
- if (valuesMap.size == 1) {
353
- const values = valuesMap.values().next().value;
354
- if (token.val == 'log') {
355
- if (values[0].typ != EnumToken.NumberTokenType || values.length > 2) {
356
- return [token];
265
+ const result = evaluate([curr]);
266
+ const key = result[0].typ + ('unit' in result[0] ? result[0].unit : '');
267
+ if (!valuesMap.has(key)) {
268
+ valuesMap.set(key, []);
269
+ }
270
+ valuesMap.get(key).push(result[0]);
271
+ }
272
+ if (valuesMap.size == 1) {
273
+ const values = valuesMap.values().next().value;
274
+ if (token.val == 'log') {
275
+ const val1 = getValue(values[0]);
276
+ const val2 = values.length == 2 ? getValue(values[1]) : null;
277
+ return [
278
+ {
279
+ ...values[0],
280
+ val: Math.log(val1) / Math.log(val2)
357
281
  }
358
- const val1 = getValue(values[0]);
359
- const val2 = values.length == 2 ? getValue(values[1]) : null;
360
- if (values.length == 1) {
361
- return [
362
- {
363
- ...values[0],
364
- val: String(Math.log(val1))
365
- }
366
- ];
282
+ ];
283
+ }
284
+ if (token.val == 'min' || token.val == 'max') {
285
+ let val = getValue(values[0]);
286
+ let val2 = val;
287
+ let ret = values[0];
288
+ for (const curr of values.slice(1)) {
289
+ val2 = getValue(curr);
290
+ if (val2 < val && token.val == 'min') {
291
+ val = val2;
292
+ ret = curr;
367
293
  }
368
- return [
369
- {
370
- ...values[0],
371
- val: String(Math.log(val1) / Math.log(val2))
372
- }
373
- ];
374
- }
375
- if (token.val == 'min' || token.val == 'max') {
376
- let val = getValue(values[0]);
377
- let val2 = val;
378
- let ret = values[0];
379
- for (const curr of values.slice(1)) {
380
- val2 = getValue(curr);
381
- if (val2 < val && token.val == 'min') {
382
- val = val2;
383
- ret = curr;
384
- }
385
- else if (val2 > val && token.val == 'max') {
386
- val = val2;
387
- ret = curr;
388
- }
294
+ else if (val2 > val && token.val == 'max') {
295
+ val = val2;
296
+ ret = curr;
389
297
  }
390
- return [ret];
391
298
  }
392
- if (token.val == 'round') {
393
- let val = getValue(values[0]);
394
- let val2 = getValue(values[1]);
395
- if (Number.isNaN(val) || Number.isNaN(val2)) {
396
- return [token];
397
- }
398
- if (strategy == null || strategy == 'down') {
399
- val = val - (val % val2);
400
- }
401
- else {
402
- val = strategy == 'to-zero' ? Math.trunc(val / val2) * val2 : (strategy == 'nearest' ? Math.round(val / val2) * val2 : Math.ceil(val / val2) * val2);
403
- }
404
- // @ts-ignore
405
- return [{ ...values[0], val: String(val) }];
299
+ return [ret];
300
+ }
301
+ if (token.val == 'round') {
302
+ let val = getValue(values[0]);
303
+ let val2 = getValue(values[1]);
304
+ if (strategy == null || strategy == 'down') {
305
+ val = val - (val % val2);
306
+ }
307
+ else {
308
+ val = strategy == 'to-zero' ? Math.trunc(val / val2) * val2 : (strategy == 'nearest' ? Math.round(val / val2) * val2 : Math.ceil(val / val2) * val2);
406
309
  }
310
+ // @ts-ignore
311
+ return [{ ...values[0], val }];
407
312
  }
408
313
  }
409
- return [token];
314
+ }
410
315
  }
411
316
  return [token];
412
317
  }
@@ -416,10 +321,7 @@ function evaluateFunc(token) {
416
321
  */
417
322
  function inlineExpression(token) {
418
323
  const result = [];
419
- if (token.typ == EnumToken.ParensTokenType && token.chi.length == 1) {
420
- result.push(token.chi[0]);
421
- }
422
- else if (token.typ == EnumToken.BinaryExpressionTokenType) {
324
+ if (token.typ == EnumToken.BinaryExpressionTokenType) {
423
325
  if ([EnumToken.Mul, EnumToken.Div].includes(token.op)) {
424
326
  result.push(token);
425
327
  }
@@ -457,7 +359,7 @@ function isScalarToken(token) {
457
359
  }
458
360
  /**
459
361
  *
460
- * generate binary expression tree
362
+ * generate a binary expression tree
461
363
  * @param tokens
462
364
  */
463
365
  function buildExpression(tokens) {
@@ -477,7 +379,7 @@ function getArithmeticOperation(op) {
477
379
  }
478
380
  /**
479
381
  *
480
- * generate binary expression tree
382
+ * generate a binary expression tree
481
383
  * @param token
482
384
  */
483
385
  function factorToken(token) {
@@ -492,7 +394,7 @@ function factorToken(token) {
492
394
  return token;
493
395
  }
494
396
  /**
495
- * generate binary expression tree
397
+ * generate a binary expression tree
496
398
  * @param tokens
497
399
  * @param ops
498
400
  */
@@ -1,11 +1,12 @@
1
1
  import { EnumToken } from '../types.js';
2
- import { reduceNumber } from '../../renderer/render.js';
3
- import '../../renderer/color/utils/constants.js';
4
2
  import '../minify.js';
5
3
  import '../walk.js';
6
4
  import '../../parser/parse.js';
7
5
  import '../../parser/tokenize.js';
8
6
  import '../../parser/utils/config.js';
7
+ import '../../syntax/color/utils/constants.js';
8
+ import '../../renderer/sourcemap/lib/encode.js';
9
+ import { minifyNumber } from '../../syntax/utils.js';
9
10
 
10
11
  function gcd(x, y) {
11
12
  x = Math.abs(x);
@@ -14,16 +15,17 @@ function gcd(x, y) {
14
15
  return x;
15
16
  }
16
17
  let t;
17
- if (x == 0) {
18
- return y;
19
- }
20
- if (y == 0) {
21
- return x;
22
- }
18
+ // if (x == 0) {
19
+ //
20
+ // return y;
21
+ // }
22
+ //
23
+ // if (y == 0) {
24
+ //
25
+ // return x;
26
+ // }
23
27
  if (y > x) {
24
- t = x;
25
- x = y;
26
- y = t;
28
+ [x, y] = [y, x];
27
29
  }
28
30
  while (y) {
29
31
  t = y;
@@ -47,23 +49,23 @@ function compute(a, b, op) {
47
49
  return r[0];
48
50
  }
49
51
  const result = a / b;
50
- const r2 = reduceNumber(r[0]) + '/' + reduceNumber(r[1]);
51
- return reduceNumber(result).length <= r2.length ? result : {
52
+ const r2 = minifyNumber(r[0]) + '/' + minifyNumber(r[1]);
53
+ return minifyNumber(result).length < r2.length ? result : {
52
54
  typ: EnumToken.FractionTokenType,
53
- l: { typ: EnumToken.NumberTokenType, val: reduceNumber(r[0]) },
54
- r: { typ: EnumToken.NumberTokenType, val: reduceNumber(r[1]) }
55
+ l: { typ: EnumToken.NumberTokenType, val: r[0] },
56
+ r: { typ: EnumToken.NumberTokenType, val: r[1] }
55
57
  };
56
58
  }
57
59
  }
58
60
  let l1 = typeof a == 'number' ? {
59
61
  typ: EnumToken.FractionTokenType,
60
- l: { typ: EnumToken.NumberTokenType, val: reduceNumber(a) },
61
- r: { typ: EnumToken.NumberTokenType, val: '1' }
62
+ l: { typ: EnumToken.NumberTokenType, val: a },
63
+ r: { typ: EnumToken.NumberTokenType, val: 1 }
62
64
  } : a;
63
65
  let r1 = typeof b == 'number' ? {
64
66
  typ: EnumToken.FractionTokenType,
65
- l: { typ: EnumToken.NumberTokenType, val: reduceNumber(b) },
66
- r: { typ: EnumToken.NumberTokenType, val: '1' }
67
+ l: { typ: EnumToken.NumberTokenType, val: b },
68
+ r: { typ: EnumToken.NumberTokenType, val: 1 }
67
69
  } : b;
68
70
  let l2;
69
71
  let r2;
@@ -99,10 +101,10 @@ function compute(a, b, op) {
99
101
  return a2[0];
100
102
  }
101
103
  const result = a2[0] / a2[1];
102
- return reduceNumber(result).length <= reduceNumber(a2[0]).length + 1 + reduceNumber(a2[1]).length ? result : {
104
+ return minifyNumber(result).length <= minifyNumber(a2[0]).length + 1 + minifyNumber(a2[1]).length ? result : {
103
105
  typ: EnumToken.FractionTokenType,
104
- l: { typ: EnumToken.NumberTokenType, val: reduceNumber(a2[0]) },
105
- r: { typ: EnumToken.NumberTokenType, val: reduceNumber(a2[1]) }
106
+ l: { typ: EnumToken.NumberTokenType, val: a2[0] },
107
+ r: { typ: EnumToken.NumberTokenType, val: a2[1] }
106
108
  };
107
109
  }
108
110
  function rem(...a) {