@tbela99/css-parser 1.4.2 → 1.4.3
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.
- package/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.repl_history +4 -0
- package/CHANGELOG.md +164 -4
- package/README.md +43 -0
- package/badges/coverage.svg +20 -0
- package/deno.lock +2861 -0
- package/dist/config.json.js +33 -1
- package/dist/index-umd-web.js +24382 -18476
- package/dist/index.cjs +24522 -18618
- package/dist/index.d.ts +1396 -929
- package/dist/lib/ast/clone.d.ts +10 -0
- package/dist/lib/ast/clone.js +45 -0
- package/dist/lib/ast/expand.d.ts +14 -0
- package/dist/lib/ast/expand.js +89 -64
- package/dist/lib/ast/features/calc.d.ts +10 -0
- package/dist/lib/ast/features/calc.js +62 -24
- package/dist/lib/ast/features/if.d.ts +10 -0
- package/dist/lib/ast/features/if.js +215 -0
- package/dist/lib/ast/features/index.d.ts +6 -0
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.d.ts +15 -0
- package/dist/lib/ast/features/inlinecssvariables.js +32 -27
- package/dist/lib/ast/features/prefix.d.ts +8 -0
- package/dist/lib/ast/features/prefix.js +68 -43
- package/dist/lib/ast/features/shorthand.d.ts +12 -0
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.d.ts +10 -0
- package/dist/lib/ast/features/transform.js +9 -13
- package/dist/lib/ast/features/type.d.ts +15 -0
- package/dist/lib/ast/find.d.ts +165 -0
- package/dist/lib/ast/find.js +175 -0
- package/dist/lib/ast/math/expression.d.ts +18 -0
- package/dist/lib/ast/math/expression.js +140 -98
- package/dist/lib/ast/math/math.d.ts +6 -0
- package/dist/lib/ast/math/math.js +30 -41
- package/dist/lib/ast/minify.d.ts +19 -0
- package/dist/lib/ast/minify.js +541 -217
- package/dist/lib/ast/transform/compute.d.ts +8 -0
- package/dist/lib/ast/transform/compute.js +82 -69
- package/dist/lib/ast/transform/matrix.d.ts +22 -0
- package/dist/lib/ast/transform/matrix.js +12 -26
- package/dist/lib/ast/transform/minify.d.ts +5 -0
- package/dist/lib/ast/transform/minify.js +20 -20
- package/dist/lib/ast/transform/perspective.d.ts +3 -0
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.d.ts +12 -0
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.d.ts +6 -0
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.d.ts +4 -0
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.d.ts +6 -0
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.d.ts +9 -0
- package/dist/lib/ast/types.d.ts +903 -0
- package/dist/lib/ast/types.js +277 -23
- package/dist/lib/ast/walk.d.ts +162 -0
- package/dist/lib/ast/walk.js +116 -60
- package/dist/lib/fs/resolve.d.ts +20 -0
- package/dist/lib/fs/resolve.js +37 -45
- package/dist/lib/parser/declaration/list.d.ts +16 -0
- package/dist/lib/parser/declaration/list.js +26 -24
- package/dist/lib/parser/declaration/map.d.ts +15 -0
- package/dist/lib/parser/declaration/map.js +140 -95
- package/dist/lib/parser/declaration/set.d.ts +9 -0
- package/dist/lib/parser/declaration/set.js +30 -25
- package/dist/lib/parser/node.d.ts +7 -0
- package/dist/lib/parser/parse.d.ts +107 -0
- package/dist/lib/parser/parse.js +1454 -1445
- package/dist/lib/parser/tokenize.d.ts +57 -0
- package/dist/lib/parser/tokenize.js +557 -404
- package/dist/lib/parser/utils/at-rule-container.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-container.js +486 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
- package/dist/lib/parser/utils/at-rule-generic.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-generic.js +118 -0
- package/dist/lib/parser/utils/at-rule-import.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-import.js +393 -0
- package/dist/lib/parser/utils/at-rule-media.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-media.js +603 -0
- package/dist/lib/parser/utils/at-rule-page.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-page.js +28 -0
- package/dist/lib/parser/utils/at-rule-support.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-support.js +366 -0
- package/dist/lib/parser/utils/at-rule-token.d.ts +1 -0
- package/dist/lib/parser/utils/at-rule-when-else.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
- package/dist/lib/parser/utils/at-rule.d.ts +13 -0
- package/dist/lib/parser/utils/at-rule.js +37 -0
- package/dist/lib/parser/utils/cache.d.ts +6 -0
- package/dist/lib/parser/utils/cache.js +19 -0
- package/dist/lib/parser/utils/config.d.ts +2 -0
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration-list.d.ts +5 -0
- package/dist/lib/parser/utils/declaration.d.ts +18 -0
- package/dist/lib/parser/utils/declaration.js +569 -91
- package/dist/lib/parser/utils/eq.d.ts +1 -0
- package/dist/lib/parser/utils/hash.d.ts +21 -0
- package/dist/lib/parser/utils/hash.js +1 -1
- package/dist/lib/parser/utils/selector.d.ts +5 -0
- package/dist/lib/parser/utils/selector.js +476 -0
- package/dist/lib/parser/utils/text.d.ts +3 -0
- package/dist/lib/parser/utils/text.js +17 -1
- package/dist/lib/parser/utils/token.d.ts +14 -0
- package/dist/lib/parser/utils/token.js +102 -0
- package/dist/lib/parser/utils/type.d.ts +2 -0
- package/dist/lib/parser/utils/type.js +29 -18
- package/dist/lib/renderer/render.d.ts +28 -0
- package/dist/lib/renderer/render.js +421 -262
- package/dist/lib/renderer/sourcemap/lib/encode.d.ts +1 -0
- package/dist/lib/renderer/sourcemap/sourcemap.d.ts +26 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
- package/dist/lib/syntax/color/a98rgb.d.ts +2 -0
- package/dist/lib/syntax/color/a98rgb.js +8 -12
- package/dist/lib/syntax/color/cmyk.d.ts +10 -0
- package/dist/lib/syntax/color/cmyk.js +23 -21
- package/dist/lib/syntax/color/color-mix.d.ts +2 -0
- package/dist/lib/syntax/color/color-mix.js +88 -77
- package/dist/lib/syntax/color/color.d.ts +42 -0
- package/dist/lib/syntax/color/color.js +65 -68
- package/dist/lib/syntax/color/hex.d.ts +16 -0
- package/dist/lib/syntax/color/hex.js +27 -31
- package/dist/lib/syntax/color/hsl.d.ts +20 -0
- package/dist/lib/syntax/color/hsl.js +5 -12
- package/dist/lib/syntax/color/hsv.d.ts +2 -0
- package/dist/lib/syntax/color/hwb.d.ts +21 -0
- package/dist/lib/syntax/color/hwb.js +8 -21
- package/dist/lib/syntax/color/lab.d.ts +25 -0
- package/dist/lib/syntax/color/lab.js +20 -21
- package/dist/lib/syntax/color/lch.d.ts +23 -0
- package/dist/lib/syntax/color/lch.js +13 -15
- package/dist/lib/syntax/color/oklab.d.ts +22 -0
- package/dist/lib/syntax/color/oklab.js +20 -39
- package/dist/lib/syntax/color/oklch.d.ts +20 -0
- package/dist/lib/syntax/color/oklch.js +14 -16
- package/dist/lib/syntax/color/p3.d.ts +6 -0
- package/dist/lib/syntax/color/p3.js +0 -8
- package/dist/lib/syntax/color/prophotorgb.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.js +9 -13
- package/dist/lib/syntax/color/relativecolor.d.ts +13 -0
- package/dist/lib/syntax/color/relativecolor.js +68 -41
- package/dist/lib/syntax/color/rgb.d.ts +20 -0
- package/dist/lib/syntax/color/rgb.js +14 -18
- package/dist/lib/syntax/color/srgb.d.ts +23 -0
- package/dist/lib/syntax/color/srgb.js +27 -26
- package/dist/lib/syntax/color/utils/components.d.ts +2 -0
- package/dist/lib/syntax/color/utils/components.js +30 -14
- package/dist/lib/syntax/color/utils/distance.d.ts +18 -0
- package/dist/lib/syntax/color/utils/distance.js +1 -8
- package/dist/lib/syntax/color/utils/matrix.d.ts +6 -0
- package/dist/lib/syntax/color/xyz.d.ts +5 -0
- package/dist/lib/syntax/color/xyz.js +8 -20
- package/dist/lib/syntax/color/xyzd50.d.ts +4 -0
- package/dist/lib/syntax/color/xyzd50.js +6 -20
- package/dist/lib/syntax/constants.d.ts +67 -0
- package/dist/lib/syntax/constants.js +436 -0
- package/dist/lib/syntax/syntax.d.ts +38 -0
- package/dist/lib/syntax/syntax.js +533 -568
- package/dist/lib/validation/config.d.ts +14 -0
- package/dist/lib/validation/config.js +72 -33
- package/dist/lib/validation/config.json.js +1159 -74
- package/dist/lib/validation/json.d.ts +2 -0
- package/dist/lib/validation/match.d.ts +38 -0
- package/dist/lib/validation/match.js +2985 -0
- package/dist/lib/validation/parser/parse.d.ts +8 -0
- package/dist/lib/validation/parser/parse.js +684 -935
- package/dist/lib/validation/parser/typedef.d.ts +95 -0
- package/dist/lib/validation/parser/typedef.js +100 -0
- package/dist/lib/validation/utils/list.d.ts +4 -0
- package/dist/lib/validation/utils/list.js +4 -11
- package/dist/lib/validation/utils/whitespace.d.ts +2 -0
- package/dist/lib/validation/utils/whitespace.js +2 -8
- package/dist/node.d.ts +207 -0
- package/dist/node.js +42 -39
- package/dist/web.d.ts +169 -0
- package/dist/web.js +38 -33
- package/package.json +15 -12
- package/playground/index.html +1328 -0
- package/playground/sw.js +55 -0
- package/playground/tree.js +176 -0
- package/dist/lib/syntax/color/utils/constants.js +0 -214
- package/dist/lib/syntax/utils.js +0 -70
- package/dist/lib/validation/at-rules/container.js +0 -342
- package/dist/lib/validation/at-rules/counter-style.js +0 -90
- package/dist/lib/validation/at-rules/custom-media.js +0 -50
- package/dist/lib/validation/at-rules/document.js +0 -89
- package/dist/lib/validation/at-rules/else.js +0 -5
- package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
- package/dist/lib/validation/at-rules/import.js +0 -150
- package/dist/lib/validation/at-rules/keyframes.js +0 -67
- package/dist/lib/validation/at-rules/layer.js +0 -41
- package/dist/lib/validation/at-rules/media.js +0 -255
- package/dist/lib/validation/at-rules/namespace.js +0 -81
- package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
- package/dist/lib/validation/at-rules/page.js +0 -100
- package/dist/lib/validation/at-rules/supports.js +0 -295
- package/dist/lib/validation/at-rules/when.js +0 -185
- package/dist/lib/validation/atrule.js +0 -184
- package/dist/lib/validation/selector.js +0 -36
- package/dist/lib/validation/syntax.js +0 -1073
- package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
- package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
- package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
- package/dist/lib/validation/syntaxes/family-name.js +0 -57
- package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
- package/dist/lib/validation/syntaxes/layer-name.js +0 -57
- package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
- package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
- package/dist/lib/validation/syntaxes/selector-list.js +0 -5
- package/dist/lib/validation/syntaxes/selector.js +0 -5
- package/dist/lib/validation/syntaxes/url.js +0 -40
|
@@ -1,13 +1,6 @@
|
|
|
1
|
+
import { mathFuncs } from '../../syntax/constants.js';
|
|
1
2
|
import { EnumToken } from '../types.js';
|
|
2
3
|
import { rem, compute } from './math.js';
|
|
3
|
-
import { mathFuncs } from '../../syntax/syntax.js';
|
|
4
|
-
import '../minify.js';
|
|
5
|
-
import '../walk.js';
|
|
6
|
-
import '../../parser/parse.js';
|
|
7
|
-
import '../../parser/tokenize.js';
|
|
8
|
-
import '../../parser/utils/config.js';
|
|
9
|
-
import '../../syntax/color/utils/constants.js';
|
|
10
|
-
import '../../renderer/sourcemap/lib/encode.js';
|
|
11
4
|
|
|
12
5
|
/**
|
|
13
6
|
* evaluate an array of tokens
|
|
@@ -15,7 +8,9 @@ import '../../renderer/sourcemap/lib/encode.js';
|
|
|
15
8
|
*/
|
|
16
9
|
function evaluate(tokens) {
|
|
17
10
|
let nodes;
|
|
18
|
-
if (tokens.length == 1 &&
|
|
11
|
+
if (tokens.length == 1 &&
|
|
12
|
+
(tokens[0].typ == EnumToken.MathFunctionTokenType || tokens[0].typ == EnumToken.FunctionTokenType) &&
|
|
13
|
+
mathFuncs.includes(tokens[0].val)) {
|
|
19
14
|
const chi = tokens[0].chi.reduce((acc, t) => {
|
|
20
15
|
if (acc.length == 0 || t.typ == EnumToken.CommaTokenType) {
|
|
21
16
|
acc.push([]);
|
|
@@ -36,7 +31,16 @@ function evaluate(tokens) {
|
|
|
36
31
|
acc.push(...t);
|
|
37
32
|
return acc;
|
|
38
33
|
});
|
|
39
|
-
|
|
34
|
+
const result = evaluateFunc(tokens[0]);
|
|
35
|
+
if (result == null) {
|
|
36
|
+
return tokens;
|
|
37
|
+
}
|
|
38
|
+
if (result[0].typ === EnumToken.MathFunctionTokenType &&
|
|
39
|
+
result[0].val === "calc" &&
|
|
40
|
+
result[0].chi.length === 1) {
|
|
41
|
+
return result[0].chi.slice();
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
40
44
|
}
|
|
41
45
|
nodes = inlineExpression(evaluateExpression(buildExpression(tokens)));
|
|
42
46
|
if (nodes.length <= 1) {
|
|
@@ -45,23 +49,27 @@ function evaluate(tokens) {
|
|
|
45
49
|
return inlineExpression(nodes[0]);
|
|
46
50
|
}
|
|
47
51
|
// @ts-ignore
|
|
48
|
-
if (nodes[0].typ == EnumToken.IdenTokenType &&
|
|
49
|
-
|
|
52
|
+
if (nodes[0].typ == EnumToken.IdenTokenType &&
|
|
53
|
+
// @ts-expect-error
|
|
54
|
+
typeof Math[nodes[0].val.toUpperCase()] == "number") {
|
|
55
|
+
return [
|
|
56
|
+
{
|
|
50
57
|
...nodes[0],
|
|
51
58
|
// @ts-ignore
|
|
52
59
|
val: Math[nodes[0].val.toUpperCase()],
|
|
53
|
-
typ: EnumToken.NumberTokenType
|
|
54
|
-
}
|
|
60
|
+
typ: EnumToken.NumberTokenType,
|
|
61
|
+
},
|
|
62
|
+
];
|
|
55
63
|
}
|
|
56
64
|
}
|
|
57
65
|
return nodes;
|
|
58
66
|
}
|
|
59
|
-
const map = new Map;
|
|
67
|
+
const map = new Map();
|
|
60
68
|
let token;
|
|
61
69
|
let i;
|
|
62
70
|
for (i = 0; i < nodes.length; i++) {
|
|
63
71
|
token = nodes[i];
|
|
64
|
-
if (token.typ == EnumToken.Add) {
|
|
72
|
+
if (token.typ == EnumToken.Add || token.typ == EnumToken.Plus) {
|
|
65
73
|
continue;
|
|
66
74
|
}
|
|
67
75
|
if (token.typ == EnumToken.Sub) {
|
|
@@ -83,7 +91,7 @@ function evaluate(tokens) {
|
|
|
83
91
|
return [...map].reduce((acc, curr) => {
|
|
84
92
|
const token = curr[1].reduce((acc, curr) => doEvaluate(acc, curr, EnumToken.Add));
|
|
85
93
|
if (token.typ != EnumToken.BinaryExpressionTokenType) {
|
|
86
|
-
if (
|
|
94
|
+
if ("val" in token && +token.val < 0) {
|
|
87
95
|
acc.push({ typ: EnumToken.Sub }, { ...token, val: -token.val });
|
|
88
96
|
return acc;
|
|
89
97
|
}
|
|
@@ -106,42 +114,52 @@ function doEvaluate(l, r, op) {
|
|
|
106
114
|
typ: EnumToken.BinaryExpressionTokenType,
|
|
107
115
|
op,
|
|
108
116
|
l,
|
|
109
|
-
r
|
|
117
|
+
r,
|
|
110
118
|
};
|
|
111
|
-
if (!isScalarToken(l) || !isScalarToken(r) || (l.typ == r.typ &&
|
|
119
|
+
if (!isScalarToken(l) || !isScalarToken(r) || (l.typ == r.typ && "unit" in l && "unit" in r && l.unit != r.unit)) {
|
|
112
120
|
return defaultReturn;
|
|
113
121
|
}
|
|
114
|
-
if (r.typ == EnumToken.FunctionTokenType) {
|
|
122
|
+
if (r.typ == EnumToken.FunctionTokenType || r.typ == EnumToken.MathFunctionTokenType) {
|
|
115
123
|
const val = evaluateFunc(r);
|
|
124
|
+
if (val == null) {
|
|
125
|
+
return defaultReturn;
|
|
126
|
+
}
|
|
116
127
|
if (val.length == 1) {
|
|
117
128
|
r = val[0];
|
|
118
129
|
}
|
|
119
130
|
}
|
|
120
|
-
if (
|
|
131
|
+
if (op == EnumToken.Add || op == EnumToken.Plus || op == EnumToken.Sub) {
|
|
121
132
|
// @ts-ignore
|
|
122
133
|
if (l.typ != r.typ) {
|
|
123
134
|
return defaultReturn;
|
|
124
135
|
}
|
|
125
136
|
}
|
|
126
|
-
let typ = l.typ == EnumToken.NumberTokenType
|
|
127
|
-
|
|
137
|
+
let typ = l.typ == EnumToken.NumberTokenType
|
|
138
|
+
? r.typ
|
|
139
|
+
: r.typ == EnumToken.NumberTokenType
|
|
140
|
+
? l.typ
|
|
141
|
+
: l.typ == EnumToken.PercentageTokenType
|
|
142
|
+
? r.typ
|
|
143
|
+
: l.typ;
|
|
144
|
+
// @ts-expect-error
|
|
128
145
|
let v1 = l.val?.typ == EnumToken.FractionTokenType ? l.val : getValue(l);
|
|
129
|
-
|
|
130
|
-
|
|
146
|
+
let v2 =
|
|
147
|
+
// @ts-expect-error
|
|
148
|
+
r.val?.typ == EnumToken.FractionTokenType ? r.val : getValue(r);
|
|
131
149
|
if (op == EnumToken.Mul) {
|
|
132
150
|
if (l.typ != EnumToken.NumberTokenType && r.typ != EnumToken.NumberTokenType) {
|
|
133
|
-
if (typeof v1 ==
|
|
151
|
+
if (typeof v1 == "number" && l.typ == EnumToken.PercentageTokenType) {
|
|
134
152
|
v1 = {
|
|
135
153
|
typ: EnumToken.FractionTokenType,
|
|
136
154
|
l: { typ: EnumToken.NumberTokenType, val: v1 },
|
|
137
|
-
r: { typ: EnumToken.NumberTokenType, val: 100 }
|
|
155
|
+
r: { typ: EnumToken.NumberTokenType, val: 100 },
|
|
138
156
|
};
|
|
139
157
|
}
|
|
140
|
-
else if (typeof v2 ==
|
|
158
|
+
else if (typeof v2 == "number" && r.typ == EnumToken.PercentageTokenType) {
|
|
141
159
|
v2 = {
|
|
142
160
|
typ: EnumToken.FractionTokenType,
|
|
143
161
|
l: { typ: EnumToken.NumberTokenType, val: v2 },
|
|
144
|
-
r: { typ: EnumToken.NumberTokenType, val: 100 }
|
|
162
|
+
r: { typ: EnumToken.NumberTokenType, val: 100 },
|
|
145
163
|
};
|
|
146
164
|
}
|
|
147
165
|
}
|
|
@@ -151,7 +169,7 @@ function doEvaluate(l, r, op) {
|
|
|
151
169
|
const token = {
|
|
152
170
|
...(l.typ == EnumToken.NumberTokenType ? r : l),
|
|
153
171
|
typ,
|
|
154
|
-
val /* : typeof val == 'number' ? minifyNumber(val) : val
|
|
172
|
+
val /* : typeof val == 'number' ? minifyNumber(val) : val */,
|
|
155
173
|
};
|
|
156
174
|
if (token.typ == EnumToken.IdenTokenType) {
|
|
157
175
|
// @ts-ignore
|
|
@@ -170,47 +188,55 @@ function getValue(t) {
|
|
|
170
188
|
function evaluateFunc(token) {
|
|
171
189
|
const values = token.chi.slice();
|
|
172
190
|
switch (token.val) {
|
|
173
|
-
case
|
|
174
|
-
case
|
|
175
|
-
case
|
|
176
|
-
case
|
|
177
|
-
case
|
|
178
|
-
case
|
|
179
|
-
case
|
|
180
|
-
case
|
|
181
|
-
case
|
|
182
|
-
case
|
|
191
|
+
case "abs":
|
|
192
|
+
case "sin":
|
|
193
|
+
case "cos":
|
|
194
|
+
case "tan":
|
|
195
|
+
case "asin":
|
|
196
|
+
case "acos":
|
|
197
|
+
case "atan":
|
|
198
|
+
case "sign":
|
|
199
|
+
case "sqrt":
|
|
200
|
+
case "exp": {
|
|
183
201
|
const value = evaluate(values);
|
|
184
202
|
// @ts-ignore
|
|
185
|
-
let val = value[0].typ == EnumToken.NumberTokenType
|
|
186
|
-
|
|
203
|
+
let val = value[0].typ == EnumToken.NumberTokenType
|
|
204
|
+
? +value[0].val
|
|
205
|
+
// @ts-expect-error
|
|
206
|
+
: value[0].l.val / value[0].r.val;
|
|
207
|
+
return [
|
|
208
|
+
{
|
|
187
209
|
typ: EnumToken.NumberTokenType,
|
|
188
|
-
val: Math[token.val](val)
|
|
189
|
-
}
|
|
210
|
+
val: Math[token.val](val),
|
|
211
|
+
},
|
|
212
|
+
];
|
|
190
213
|
}
|
|
191
|
-
case
|
|
192
|
-
const chi = values.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ));
|
|
214
|
+
case "hypot": {
|
|
215
|
+
const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ));
|
|
193
216
|
let all = [];
|
|
194
217
|
let ref = chi[0];
|
|
195
218
|
let value = 0;
|
|
196
219
|
for (let i = 0; i < chi.length; i++) {
|
|
197
220
|
// @ts-ignore
|
|
198
221
|
const val = getValue(chi[i]);
|
|
222
|
+
if (Number.isNaN(val)) {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
199
225
|
all.push(val);
|
|
200
226
|
value += val * val;
|
|
201
227
|
}
|
|
202
228
|
return [
|
|
203
229
|
{
|
|
204
230
|
...ref,
|
|
205
|
-
val: +
|
|
206
|
-
}
|
|
231
|
+
val: +Math.sqrt(value).toFixed(rem(...all)),
|
|
232
|
+
},
|
|
207
233
|
];
|
|
208
234
|
}
|
|
209
|
-
case
|
|
210
|
-
case
|
|
211
|
-
case
|
|
212
|
-
case
|
|
213
|
-
const chi = values.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
|
|
235
|
+
case "atan2":
|
|
236
|
+
case "pow":
|
|
237
|
+
case "rem":
|
|
238
|
+
case "mod": {
|
|
239
|
+
const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
|
|
214
240
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/mod
|
|
215
241
|
const v1 = evaluate([chi[0]]);
|
|
216
242
|
const v2 = evaluate([chi[2]]);
|
|
@@ -218,52 +244,57 @@ function evaluateFunc(token) {
|
|
|
218
244
|
const val1 = getValue(v1[0]);
|
|
219
245
|
// @ts-ignore
|
|
220
246
|
const val2 = getValue(v2[0]);
|
|
221
|
-
if (token.val ==
|
|
247
|
+
if (token.val == "rem") {
|
|
222
248
|
return [
|
|
223
249
|
{
|
|
224
250
|
...v1[0],
|
|
225
|
-
val: +(
|
|
226
|
-
}
|
|
251
|
+
val: +(val1 % val2).toFixed(rem(val1, val2)),
|
|
252
|
+
},
|
|
227
253
|
];
|
|
228
254
|
}
|
|
229
|
-
if (token.val ==
|
|
255
|
+
if (token.val == "pow") {
|
|
230
256
|
return [
|
|
231
257
|
{
|
|
232
258
|
...v1[0],
|
|
233
|
-
val: Math.pow(val1, val2)
|
|
234
|
-
}
|
|
259
|
+
val: Math.pow(val1, val2),
|
|
260
|
+
},
|
|
235
261
|
];
|
|
236
262
|
}
|
|
237
|
-
if (token.val ==
|
|
263
|
+
if (token.val == "atan2") {
|
|
238
264
|
return [
|
|
239
265
|
{
|
|
240
|
-
...{},
|
|
241
|
-
|
|
242
|
-
|
|
266
|
+
...{},
|
|
267
|
+
...v1[0],
|
|
268
|
+
val: Math.atan2(val1, val2),
|
|
269
|
+
},
|
|
243
270
|
];
|
|
244
271
|
}
|
|
245
272
|
return [
|
|
246
273
|
{
|
|
247
274
|
...v1[0],
|
|
248
|
-
val: val2 == 0 ? val1 : val1 -
|
|
249
|
-
}
|
|
275
|
+
val: val2 == 0 ? val1 : val1 - Math.floor(val1 / val2) * val2,
|
|
276
|
+
},
|
|
250
277
|
];
|
|
251
278
|
}
|
|
252
|
-
case
|
|
279
|
+
case "clamp":
|
|
253
280
|
token.chi = values;
|
|
254
281
|
return [token];
|
|
255
|
-
case
|
|
256
|
-
case
|
|
257
|
-
case
|
|
258
|
-
case
|
|
259
|
-
const strategy = token.val ==
|
|
260
|
-
|
|
282
|
+
case "log":
|
|
283
|
+
case "round":
|
|
284
|
+
case "min":
|
|
285
|
+
case "max": {
|
|
286
|
+
const strategy = token.val == "round" && values[0]?.typ == EnumToken.IdenTokenType
|
|
287
|
+
? values.shift().val
|
|
288
|
+
: null;
|
|
289
|
+
const valuesMap = new Map();
|
|
261
290
|
for (const curr of values) {
|
|
262
|
-
if (curr.typ == EnumToken.CommaTokenType ||
|
|
291
|
+
if (curr.typ == EnumToken.CommaTokenType ||
|
|
292
|
+
curr.typ == EnumToken.WhitespaceTokenType ||
|
|
293
|
+
curr.typ == EnumToken.CommentTokenType) {
|
|
263
294
|
continue;
|
|
264
295
|
}
|
|
265
296
|
const result = evaluate([curr]);
|
|
266
|
-
const key = result[0].typ + (
|
|
297
|
+
const key = result[0].typ + ("unit" in result[0] ? result[0].unit : "");
|
|
267
298
|
if (!valuesMap.has(key)) {
|
|
268
299
|
valuesMap.set(key, []);
|
|
269
300
|
}
|
|
@@ -271,41 +302,46 @@ function evaluateFunc(token) {
|
|
|
271
302
|
}
|
|
272
303
|
if (valuesMap.size == 1) {
|
|
273
304
|
const values = valuesMap.values().next().value;
|
|
274
|
-
if (token.val ==
|
|
305
|
+
if (token.val == "log") {
|
|
275
306
|
const val1 = getValue(values[0]);
|
|
276
307
|
const val2 = values.length == 2 ? getValue(values[1]) : null;
|
|
277
308
|
return [
|
|
278
309
|
{
|
|
279
310
|
...values[0],
|
|
280
|
-
val: Math.log(val1) / Math.log(val2)
|
|
281
|
-
}
|
|
311
|
+
val: Math.log(val1) / Math.log(val2),
|
|
312
|
+
},
|
|
282
313
|
];
|
|
283
314
|
}
|
|
284
|
-
if (token.val ==
|
|
315
|
+
if (token.val == "min" || token.val == "max") {
|
|
285
316
|
let val = getValue(values[0]);
|
|
286
317
|
let val2 = val;
|
|
287
318
|
let ret = values[0];
|
|
288
319
|
for (const curr of values.slice(1)) {
|
|
289
320
|
val2 = getValue(curr);
|
|
290
|
-
if (val2 < val && token.val ==
|
|
321
|
+
if (val2 < val && token.val == "min") {
|
|
291
322
|
val = val2;
|
|
292
323
|
ret = curr;
|
|
293
324
|
}
|
|
294
|
-
else if (val2 > val && token.val ==
|
|
325
|
+
else if (val2 > val && token.val == "max") {
|
|
295
326
|
val = val2;
|
|
296
327
|
ret = curr;
|
|
297
328
|
}
|
|
298
329
|
}
|
|
299
330
|
return [ret];
|
|
300
331
|
}
|
|
301
|
-
if (token.val ==
|
|
332
|
+
if (token.val == "round") {
|
|
302
333
|
let val = getValue(values[0]);
|
|
303
334
|
let val2 = getValue(values[1]);
|
|
304
|
-
if (strategy == null || strategy ==
|
|
335
|
+
if (strategy == null || strategy == "down") {
|
|
305
336
|
val = val - (val % val2);
|
|
306
337
|
}
|
|
307
338
|
else {
|
|
308
|
-
val =
|
|
339
|
+
val =
|
|
340
|
+
strategy == "to-zero"
|
|
341
|
+
? Math.trunc(val / val2) * val2
|
|
342
|
+
: strategy == "nearest"
|
|
343
|
+
? Math.round(val / val2) * val2
|
|
344
|
+
: Math.ceil(val / val2) * val2;
|
|
309
345
|
}
|
|
310
346
|
// @ts-ignore
|
|
311
347
|
return [{ ...values[0], val }];
|
|
@@ -343,19 +379,20 @@ function evaluateExpression(token) {
|
|
|
343
379
|
return token;
|
|
344
380
|
}
|
|
345
381
|
if (token.r.typ == EnumToken.BinaryExpressionTokenType) {
|
|
346
|
-
token.r = evaluateExpression(token.r);
|
|
382
|
+
token.r = (evaluateExpression(token.r));
|
|
347
383
|
}
|
|
348
384
|
if (token.l.typ == EnumToken.BinaryExpressionTokenType) {
|
|
349
|
-
token.l = evaluateExpression(token.l);
|
|
385
|
+
token.l = (evaluateExpression(token.l));
|
|
350
386
|
}
|
|
351
387
|
return doEvaluate(token.l, token.r, token.op);
|
|
352
388
|
}
|
|
353
389
|
function isScalarToken(token) {
|
|
354
|
-
return
|
|
390
|
+
return ("unit" in token ||
|
|
391
|
+
token.typ == EnumToken.MathFunctionTokenType ||
|
|
355
392
|
(token.typ == EnumToken.FunctionTokenType && mathFuncs.includes(token.val)) ||
|
|
356
393
|
// @ts-ignore
|
|
357
|
-
(token.typ == EnumToken.IdenTokenType && typeof Math[token.val.toUpperCase()] ==
|
|
358
|
-
[EnumToken.NumberTokenType, EnumToken.FractionTokenType, EnumToken.PercentageTokenType].includes(token.typ);
|
|
394
|
+
(token.typ == EnumToken.IdenTokenType && typeof Math[token.val.toUpperCase()] == "number") ||
|
|
395
|
+
[EnumToken.NumberTokenType, EnumToken.FractionTokenType, EnumToken.PercentageTokenType].includes(token.typ));
|
|
359
396
|
}
|
|
360
397
|
/**
|
|
361
398
|
*
|
|
@@ -363,16 +400,16 @@ function isScalarToken(token) {
|
|
|
363
400
|
* @param tokens
|
|
364
401
|
*/
|
|
365
402
|
function buildExpression(tokens) {
|
|
366
|
-
return factor(factor(tokens.filter(t => t.typ != EnumToken.WhitespaceTokenType), [
|
|
403
|
+
return factor(factor(tokens.filter((t) => t.typ != EnumToken.WhitespaceTokenType), ["/", "*"]), ["+", "-"])[0];
|
|
367
404
|
}
|
|
368
405
|
function getArithmeticOperation(op) {
|
|
369
|
-
if (op ==
|
|
406
|
+
if (op == "+") {
|
|
370
407
|
return EnumToken.Add;
|
|
371
408
|
}
|
|
372
|
-
if (op ==
|
|
409
|
+
if (op == "-") {
|
|
373
410
|
return EnumToken.Sub;
|
|
374
411
|
}
|
|
375
|
-
if (op ==
|
|
412
|
+
if (op == "/") {
|
|
376
413
|
return EnumToken.Div;
|
|
377
414
|
}
|
|
378
415
|
return EnumToken.Mul;
|
|
@@ -383,8 +420,11 @@ function getArithmeticOperation(op) {
|
|
|
383
420
|
* @param token
|
|
384
421
|
*/
|
|
385
422
|
function factorToken(token) {
|
|
386
|
-
if (token.typ == EnumToken.ParensTokenType ||
|
|
387
|
-
|
|
423
|
+
if (token.typ == EnumToken.ParensTokenType ||
|
|
424
|
+
((token.typ == EnumToken.MathFunctionTokenType || token.typ == EnumToken.FunctionTokenType) &&
|
|
425
|
+
token.val == "calc")) {
|
|
426
|
+
if ((token.typ == EnumToken.MathFunctionTokenType || token.typ == EnumToken.FunctionTokenType) &&
|
|
427
|
+
token.val == "calc") {
|
|
388
428
|
token = { ...token, typ: EnumToken.ParensTokenType };
|
|
389
429
|
// @ts-ignore
|
|
390
430
|
delete token.val;
|
|
@@ -400,7 +440,7 @@ function factorToken(token) {
|
|
|
400
440
|
*/
|
|
401
441
|
function factor(tokens, ops) {
|
|
402
442
|
let isOp;
|
|
403
|
-
const opList = ops.map(x => getArithmeticOperation(x));
|
|
443
|
+
const opList = ops.map((x) => getArithmeticOperation(x));
|
|
404
444
|
if (tokens.length == 1) {
|
|
405
445
|
return [factorToken(tokens[0])];
|
|
406
446
|
}
|
|
@@ -409,15 +449,17 @@ function factor(tokens, ops) {
|
|
|
409
449
|
// @ts-ignore
|
|
410
450
|
tokens.splice(i, 1, ...tokens[i].chi);
|
|
411
451
|
}
|
|
412
|
-
isOp = opList.includes(tokens[i].typ);
|
|
452
|
+
isOp = opList.includes(tokens[i].typ === EnumToken.Plus ? EnumToken.Add : tokens[i].typ);
|
|
413
453
|
if (isOp ||
|
|
414
454
|
// @ts-ignore
|
|
415
455
|
(tokens[i].typ == EnumToken.LiteralTokenType && ops.includes(tokens[i].val))) {
|
|
416
456
|
tokens.splice(i - 1, 3, {
|
|
417
457
|
typ: EnumToken.BinaryExpressionTokenType,
|
|
418
|
-
op: isOp
|
|
458
|
+
op: isOp
|
|
459
|
+
? (tokens[i].typ === EnumToken.Plus ? EnumToken.Add : tokens[i].typ)
|
|
460
|
+
: getArithmeticOperation(tokens[i].val),
|
|
419
461
|
l: factorToken(tokens[i - 1]),
|
|
420
|
-
r: factorToken(tokens[i + 1])
|
|
462
|
+
r: factorToken(tokens[i + 1]),
|
|
421
463
|
});
|
|
422
464
|
i--;
|
|
423
465
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { FractionToken } from "../../../@types/index.d.ts";
|
|
2
|
+
import { EnumToken } from "../types.ts";
|
|
3
|
+
export declare function gcd(x: number, y: number): number;
|
|
4
|
+
export declare function compute(a: number | FractionToken, b: number | FractionToken, op: EnumToken.Add | EnumToken.Sub | EnumToken.Mul | EnumToken.Div): number | FractionToken;
|
|
5
|
+
export declare function rem(...a: number[]): number;
|
|
6
|
+
export declare function simplify(a: number, b: number): [number, number];
|
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import { EnumToken } from '../types.js';
|
|
2
|
-
import '../minify.js';
|
|
3
|
-
import '../walk.js';
|
|
4
|
-
import '../../parser/parse.js';
|
|
5
|
-
import '../../parser/tokenize.js';
|
|
6
|
-
import '../../parser/utils/config.js';
|
|
7
|
-
import '../../renderer/sourcemap/lib/encode.js';
|
|
8
|
-
import '../../syntax/color/utils/constants.js';
|
|
9
|
-
import { minifyNumber } from '../../syntax/utils.js';
|
|
1
|
+
import { EnumToken, minifyNumber } from '../types.js';
|
|
10
2
|
|
|
11
3
|
function gcd(x, y) {
|
|
12
4
|
x = Math.abs(x);
|
|
@@ -15,15 +7,6 @@ function gcd(x, y) {
|
|
|
15
7
|
return x;
|
|
16
8
|
}
|
|
17
9
|
let t;
|
|
18
|
-
// if (x == 0) {
|
|
19
|
-
//
|
|
20
|
-
// return y;
|
|
21
|
-
// }
|
|
22
|
-
//
|
|
23
|
-
// if (y == 0) {
|
|
24
|
-
//
|
|
25
|
-
// return x;
|
|
26
|
-
// }
|
|
27
10
|
if (y > x) {
|
|
28
11
|
[x, y] = [y, x];
|
|
29
12
|
}
|
|
@@ -35,7 +18,7 @@ function gcd(x, y) {
|
|
|
35
18
|
return x;
|
|
36
19
|
}
|
|
37
20
|
function compute(a, b, op) {
|
|
38
|
-
if (typeof a ==
|
|
21
|
+
if (typeof a == "number" && typeof b == "number") {
|
|
39
22
|
switch (op) {
|
|
40
23
|
case EnumToken.Add:
|
|
41
24
|
return a + b;
|
|
@@ -49,24 +32,28 @@ function compute(a, b, op) {
|
|
|
49
32
|
return r[0];
|
|
50
33
|
}
|
|
51
34
|
const result = a / b;
|
|
52
|
-
const r2 = minifyNumber(r[0]) +
|
|
53
|
-
return minifyNumber(result).length < r2.length
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
35
|
+
const r2 = minifyNumber(r[0]) + "/" + minifyNumber(r[1]);
|
|
36
|
+
return minifyNumber(result).length < r2.length
|
|
37
|
+
? result
|
|
38
|
+
: {
|
|
39
|
+
typ: EnumToken.FractionTokenType,
|
|
40
|
+
l: { typ: EnumToken.NumberTokenType, val: r[0] },
|
|
41
|
+
r: { typ: EnumToken.NumberTokenType, val: r[1] },
|
|
42
|
+
};
|
|
58
43
|
}
|
|
59
44
|
}
|
|
60
|
-
let l1 = typeof a ==
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
45
|
+
let l1 = typeof a == "number"
|
|
46
|
+
? {
|
|
47
|
+
l: { val: a },
|
|
48
|
+
r: { val: 1 },
|
|
49
|
+
}
|
|
50
|
+
: a;
|
|
51
|
+
let r1 = typeof b == "number"
|
|
52
|
+
? {
|
|
53
|
+
l: { val: b },
|
|
54
|
+
r: { val: 1 },
|
|
55
|
+
}
|
|
56
|
+
: b;
|
|
70
57
|
let l2;
|
|
71
58
|
let r2;
|
|
72
59
|
switch (op) {
|
|
@@ -101,15 +88,17 @@ function compute(a, b, op) {
|
|
|
101
88
|
return a2[0];
|
|
102
89
|
}
|
|
103
90
|
const result = a2[0] / a2[1];
|
|
104
|
-
return minifyNumber(result).length <= minifyNumber(a2[0]).length + 1 + minifyNumber(a2[1]).length
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
91
|
+
return minifyNumber(result).length <= minifyNumber(a2[0]).length + 1 + minifyNumber(a2[1]).length
|
|
92
|
+
? result
|
|
93
|
+
: {
|
|
94
|
+
typ: EnumToken.FractionTokenType,
|
|
95
|
+
l: { typ: EnumToken.NumberTokenType, val: a2[0] },
|
|
96
|
+
r: { typ: EnumToken.NumberTokenType, val: a2[1] },
|
|
97
|
+
};
|
|
109
98
|
}
|
|
110
99
|
function rem(...a) {
|
|
111
100
|
if (a.some((i) => !Number.isInteger(i))) {
|
|
112
|
-
return a.reduce((a, b) => Math.max(a, String(b).split(
|
|
101
|
+
return a.reduce((a, b) => Math.max(a, String(b).split(".")[1]?.length ?? 0), 0);
|
|
113
102
|
}
|
|
114
103
|
return 0;
|
|
115
104
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AstNode, ErrorDescription, MinifyFeatureOptions, ParserOptions } from "../../@types/index.d.ts";
|
|
2
|
+
/**
|
|
3
|
+
* apply minification rules to the ast tree
|
|
4
|
+
* @param ast
|
|
5
|
+
* @param options
|
|
6
|
+
* @param recursive
|
|
7
|
+
* @param errors
|
|
8
|
+
* @param nestingContent
|
|
9
|
+
*
|
|
10
|
+
* @private
|
|
11
|
+
*/
|
|
12
|
+
export declare function minify(ast: AstNode, options: ParserOptions | MinifyFeatureOptions, recursive: boolean, errors?: ErrorDescription[], nestingContent?: boolean): AstNode;
|
|
13
|
+
/**
|
|
14
|
+
* split selector string
|
|
15
|
+
* @param buffer
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function splitRule(buffer: string): string[][];
|