@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.
- package/CHANGELOG.md +354 -4
- package/README.md +69 -1077
- package/dist/config.json.js +76 -3
- package/dist/index-umd-web.js +26838 -18743
- package/dist/index.cjs +26819 -18731
- package/dist/index.d.ts +2471 -1884
- package/dist/lib/ast/clone.js +46 -0
- package/dist/lib/ast/expand.js +90 -65
- package/dist/lib/ast/features/calc.js +86 -43
- package/dist/lib/ast/features/if.js +221 -0
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.js +33 -28
- package/dist/lib/ast/features/prefix.js +406 -44
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.js +14 -15
- package/dist/lib/ast/find.js +175 -0
- package/dist/lib/ast/math/expression.js +145 -99
- package/dist/lib/ast/math/math.js +30 -41
- package/dist/lib/ast/minify.js +551 -232
- package/dist/lib/ast/transform/compute.js +82 -69
- package/dist/lib/ast/transform/matrix.js +12 -26
- package/dist/lib/ast/transform/minify.js +154 -150
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.js +3 -2
- package/dist/lib/ast/types.js +345 -30
- package/dist/lib/ast/walk.js +119 -64
- package/dist/lib/fs/resolve.js +94 -88
- package/dist/lib/parser/declaration/list.js +117 -26
- package/dist/lib/parser/declaration/map.js +141 -96
- package/dist/lib/parser/declaration/set.js +30 -25
- package/dist/lib/parser/parse.js +1810 -1739
- package/dist/lib/parser/tokenize.js +662 -403
- package/dist/lib/parser/utils/angle.js +29 -0
- package/dist/lib/parser/utils/at-rule-container.js +486 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
- package/dist/lib/parser/utils/at-rule-generic.js +118 -0
- package/dist/lib/parser/utils/at-rule-import.js +393 -0
- package/dist/lib/parser/utils/at-rule-media.js +603 -0
- package/dist/lib/parser/utils/at-rule-support.js +431 -0
- package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
- package/dist/lib/parser/utils/at-rule.js +31 -0
- package/dist/lib/parser/utils/cache.js +19 -0
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration.js +672 -91
- package/dist/lib/parser/utils/hash.js +1 -1
- package/dist/lib/parser/utils/selector.js +518 -0
- package/dist/lib/parser/utils/text.js +17 -1
- package/dist/lib/parser/utils/token.js +104 -0
- package/dist/lib/parser/utils/type.js +29 -18
- package/dist/lib/renderer/render.js +870 -269
- package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
- package/dist/lib/syntax/color/a98rgb.js +8 -12
- package/dist/lib/syntax/color/alpha.js +64 -0
- package/dist/lib/syntax/color/cmyk.js +23 -21
- package/dist/lib/syntax/color/color-mix.js +311 -237
- package/dist/lib/syntax/color/color.js +98 -92
- package/dist/lib/syntax/color/hex.js +29 -33
- package/dist/lib/syntax/color/hsl.js +7 -14
- package/dist/lib/syntax/color/hwb.js +14 -27
- package/dist/lib/syntax/color/lab.js +23 -24
- package/dist/lib/syntax/color/lch.js +16 -18
- package/dist/lib/syntax/color/oklab.js +21 -40
- package/dist/lib/syntax/color/oklch.js +16 -18
- package/dist/lib/syntax/color/p3.js +0 -8
- package/dist/lib/syntax/color/rec2020.js +9 -13
- package/dist/lib/syntax/color/relativecolor.js +160 -48
- package/dist/lib/syntax/color/rgb.js +14 -18
- package/dist/lib/syntax/color/srgb.js +30 -29
- package/dist/lib/syntax/color/utils/color-type.js +18 -0
- package/dist/lib/syntax/color/utils/colorspace.js +46 -0
- package/dist/lib/syntax/color/utils/components.js +36 -16
- package/dist/lib/syntax/color/utils/distance.js +21 -11
- package/dist/lib/syntax/color/utils/make-color.js +56 -0
- package/dist/lib/syntax/color/xyz.js +10 -22
- package/dist/lib/syntax/color/xyzd50.js +6 -20
- package/dist/lib/syntax/constants.js +464 -0
- package/dist/lib/syntax/syntax.js +825 -592
- package/dist/lib/validation/config.js +72 -33
- package/dist/lib/validation/config.json.js +1185 -85
- package/dist/lib/validation/match.js +3120 -0
- package/dist/lib/validation/parser/parse.js +683 -935
- package/dist/lib/validation/parser/typedef.js +106 -0
- package/dist/lib/validation/utils/list.js +4 -11
- package/dist/lib/validation/utils/whitespace.js +2 -8
- package/dist/node.js +102 -57
- package/dist/web.js +100 -48
- package/package.json +31 -17
- 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
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { EnumToken } from './types.js';
|
|
2
|
+
import { walk, walkValues } from './walk.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Search the ast tree and return the first match
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
9
|
+
import { find, EnumToken, transform } from "@tbela99/css-parser";
|
|
10
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
11
|
+
|
|
12
|
+
* const css = `
|
|
13
|
+
|
|
14
|
+
button {
|
|
15
|
+
aspect-ratio: 1;
|
|
16
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
17
|
+
}
|
|
18
|
+
`;
|
|
19
|
+
|
|
20
|
+
// find declaration which contain a '30px'
|
|
21
|
+
const nodeMatcher = (node: AstNode) =>
|
|
22
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
23
|
+
|
|
24
|
+
const result = await transform(css);
|
|
25
|
+
const node = find(result.ast, nodeMatcher);
|
|
26
|
+
|
|
27
|
+
console.log({node});
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
*
|
|
31
|
+
* @param ast
|
|
32
|
+
* @param matcher
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
function find(ast, matcher) {
|
|
36
|
+
for (const { node } of walk(ast)) {
|
|
37
|
+
if (matcher(node)) {
|
|
38
|
+
return node;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Search the ast tree by checking each node's value token and return the first match
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* // find the first ast node which contains the length token '30px'
|
|
48
|
+
import { findByValue, EnumToken, transform } from "@tbela99/css-parser";
|
|
49
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
50
|
+
|
|
51
|
+
* const css = `
|
|
52
|
+
|
|
53
|
+
button {
|
|
54
|
+
aspect-ratio: 1;
|
|
55
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
// find declaration which contain the length token '30px'
|
|
60
|
+
const nodeMatcher = (value: Token) =>
|
|
61
|
+
return value.typ == EnumToken.LengthTokenType && (value as LengthToken).val == 30 && (value as LengthToken).unit == 'px' ;
|
|
62
|
+
|
|
63
|
+
const result = await transform(css);
|
|
64
|
+
const { node, value } = findByValue(result.ast, nodeMatcher) ?? {};
|
|
65
|
+
|
|
66
|
+
console.log({node, value});
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
*
|
|
70
|
+
* @param ast
|
|
71
|
+
* @param matcher
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
function findByValue(ast, matcher) {
|
|
75
|
+
let source;
|
|
76
|
+
for (const { node } of walk(ast)) {
|
|
77
|
+
source = null;
|
|
78
|
+
if (node.typ === EnumToken.DeclarationNodeType) {
|
|
79
|
+
source = node.val;
|
|
80
|
+
}
|
|
81
|
+
else if (Array.isArray(node.tokens)) {
|
|
82
|
+
source = node.tokens;
|
|
83
|
+
}
|
|
84
|
+
if (source == null) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
for (const { value, parent, root: rootNode, parents } of walkValues(source, node)) {
|
|
88
|
+
if (matcher(value, node)) {
|
|
89
|
+
return { node, value: { node: value, parent, root: rootNode, parents } };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Search the ast tree and return all matches
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
100
|
+
import { findAll, EnumToken, transform } from "@tbela99/css-parser";
|
|
101
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
102
|
+
|
|
103
|
+
* const css = `
|
|
104
|
+
|
|
105
|
+
button {
|
|
106
|
+
aspect-ratio: 1;
|
|
107
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
108
|
+
}
|
|
109
|
+
`;
|
|
110
|
+
|
|
111
|
+
// find declaration which contain a '30px'
|
|
112
|
+
const nodeMatcher = (node: AstNode) =>
|
|
113
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
114
|
+
|
|
115
|
+
const result = await transform(css);
|
|
116
|
+
const nodes = findAll(result.ast, nodeMatcher);
|
|
117
|
+
|
|
118
|
+
console.log({nodes});
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
*
|
|
122
|
+
* @param ast
|
|
123
|
+
* @param matcher
|
|
124
|
+
* @returns
|
|
125
|
+
*/
|
|
126
|
+
function findAll(ast, matcher) {
|
|
127
|
+
const result = [];
|
|
128
|
+
for (const { node } of walk(ast)) {
|
|
129
|
+
if (matcher(node)) {
|
|
130
|
+
result.push(node);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Search the ast tree and return the last match.
|
|
137
|
+
*
|
|
138
|
+
* ```ts
|
|
139
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
140
|
+
import { findLast, EnumToken, transform } from "@tbela99/css-parser";
|
|
141
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
142
|
+
|
|
143
|
+
* const css = `
|
|
144
|
+
|
|
145
|
+
button {
|
|
146
|
+
aspect-ratio: 1;
|
|
147
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
148
|
+
}
|
|
149
|
+
`;
|
|
150
|
+
|
|
151
|
+
// find declaration which contain a '30px'
|
|
152
|
+
const nodeMatcher = (node: AstNode) =>
|
|
153
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
154
|
+
|
|
155
|
+
const result = await transform(css);
|
|
156
|
+
const node = findLast(result.ast, nodeMatcher);
|
|
157
|
+
|
|
158
|
+
console.log({node});
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
*
|
|
162
|
+
* @param ast
|
|
163
|
+
* @param matcher
|
|
164
|
+
* @returns
|
|
165
|
+
*/
|
|
166
|
+
function findLast(ast, matcher) {
|
|
167
|
+
for (const { node } of walk(ast, null, true)) {
|
|
168
|
+
if (matcher(node)) {
|
|
169
|
+
return node;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export { find, findAll, findByValue, findLast };
|
|
@@ -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,53 @@ 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 || l.typ === EnumToken.IdenTokenType
|
|
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 = r.val?.typ == EnumToken.FractionTokenType
|
|
147
|
+
? // @ts-expect-error
|
|
148
|
+
r.val
|
|
149
|
+
: getValue(r);
|
|
131
150
|
if (op == EnumToken.Mul) {
|
|
132
151
|
if (l.typ != EnumToken.NumberTokenType && r.typ != EnumToken.NumberTokenType) {
|
|
133
|
-
if (typeof v1 ==
|
|
152
|
+
if (typeof v1 == "number" && l.typ == EnumToken.PercentageTokenType) {
|
|
134
153
|
v1 = {
|
|
135
154
|
typ: EnumToken.FractionTokenType,
|
|
136
155
|
l: { typ: EnumToken.NumberTokenType, val: v1 },
|
|
137
|
-
r: { typ: EnumToken.NumberTokenType, val: 100 }
|
|
156
|
+
r: { typ: EnumToken.NumberTokenType, val: 100 },
|
|
138
157
|
};
|
|
139
158
|
}
|
|
140
|
-
else if (typeof v2 ==
|
|
159
|
+
else if (typeof v2 == "number" && r.typ == EnumToken.PercentageTokenType) {
|
|
141
160
|
v2 = {
|
|
142
161
|
typ: EnumToken.FractionTokenType,
|
|
143
162
|
l: { typ: EnumToken.NumberTokenType, val: v2 },
|
|
144
|
-
r: { typ: EnumToken.NumberTokenType, val: 100 }
|
|
163
|
+
r: { typ: EnumToken.NumberTokenType, val: 100 },
|
|
145
164
|
};
|
|
146
165
|
}
|
|
147
166
|
}
|
|
@@ -149,9 +168,9 @@ function doEvaluate(l, r, op) {
|
|
|
149
168
|
// @ts-ignore
|
|
150
169
|
const val = compute(v1, v2, op);
|
|
151
170
|
const token = {
|
|
152
|
-
...(l.typ
|
|
171
|
+
...(l.typ === EnumToken.NumberTokenType || l.typ === EnumToken.IdenTokenType ? r : l),
|
|
153
172
|
typ,
|
|
154
|
-
val /* : typeof val == 'number' ? minifyNumber(val) : val
|
|
173
|
+
val /* : typeof val == 'number' ? minifyNumber(val) : val */,
|
|
155
174
|
};
|
|
156
175
|
if (token.typ == EnumToken.IdenTokenType) {
|
|
157
176
|
// @ts-ignore
|
|
@@ -170,47 +189,55 @@ function getValue(t) {
|
|
|
170
189
|
function evaluateFunc(token) {
|
|
171
190
|
const values = token.chi.slice();
|
|
172
191
|
switch (token.val) {
|
|
173
|
-
case
|
|
174
|
-
case
|
|
175
|
-
case
|
|
176
|
-
case
|
|
177
|
-
case
|
|
178
|
-
case
|
|
179
|
-
case
|
|
180
|
-
case
|
|
181
|
-
case
|
|
182
|
-
case
|
|
192
|
+
case "abs":
|
|
193
|
+
case "sin":
|
|
194
|
+
case "cos":
|
|
195
|
+
case "tan":
|
|
196
|
+
case "asin":
|
|
197
|
+
case "acos":
|
|
198
|
+
case "atan":
|
|
199
|
+
case "sign":
|
|
200
|
+
case "sqrt":
|
|
201
|
+
case "exp": {
|
|
183
202
|
const value = evaluate(values);
|
|
184
203
|
// @ts-ignore
|
|
185
|
-
let val = value[0].typ == EnumToken.NumberTokenType
|
|
186
|
-
|
|
204
|
+
let val = value[0].typ == EnumToken.NumberTokenType
|
|
205
|
+
? +value[0].val
|
|
206
|
+
: // @ts-expect-error
|
|
207
|
+
value[0].l.val / value[0].r.val;
|
|
208
|
+
return [
|
|
209
|
+
{
|
|
187
210
|
typ: EnumToken.NumberTokenType,
|
|
188
|
-
val: Math[token.val](val)
|
|
189
|
-
}
|
|
211
|
+
val: Math[token.val](val),
|
|
212
|
+
},
|
|
213
|
+
];
|
|
190
214
|
}
|
|
191
|
-
case
|
|
192
|
-
const chi = values.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ));
|
|
215
|
+
case "hypot": {
|
|
216
|
+
const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ));
|
|
193
217
|
let all = [];
|
|
194
218
|
let ref = chi[0];
|
|
195
219
|
let value = 0;
|
|
196
220
|
for (let i = 0; i < chi.length; i++) {
|
|
197
221
|
// @ts-ignore
|
|
198
222
|
const val = getValue(chi[i]);
|
|
223
|
+
if (Number.isNaN(val)) {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
199
226
|
all.push(val);
|
|
200
227
|
value += val * val;
|
|
201
228
|
}
|
|
202
229
|
return [
|
|
203
230
|
{
|
|
204
231
|
...ref,
|
|
205
|
-
val: +
|
|
206
|
-
}
|
|
232
|
+
val: +Math.sqrt(value).toFixed(rem(...all)),
|
|
233
|
+
},
|
|
207
234
|
];
|
|
208
235
|
}
|
|
209
|
-
case
|
|
210
|
-
case
|
|
211
|
-
case
|
|
212
|
-
case
|
|
213
|
-
const chi = values.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
|
|
236
|
+
case "atan2":
|
|
237
|
+
case "pow":
|
|
238
|
+
case "rem":
|
|
239
|
+
case "mod": {
|
|
240
|
+
const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
|
|
214
241
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/mod
|
|
215
242
|
const v1 = evaluate([chi[0]]);
|
|
216
243
|
const v2 = evaluate([chi[2]]);
|
|
@@ -218,52 +245,57 @@ function evaluateFunc(token) {
|
|
|
218
245
|
const val1 = getValue(v1[0]);
|
|
219
246
|
// @ts-ignore
|
|
220
247
|
const val2 = getValue(v2[0]);
|
|
221
|
-
if (token.val ==
|
|
248
|
+
if (token.val == "rem") {
|
|
222
249
|
return [
|
|
223
250
|
{
|
|
224
251
|
...v1[0],
|
|
225
|
-
val: +(
|
|
226
|
-
}
|
|
252
|
+
val: +(val1 % val2).toFixed(rem(val1, val2)),
|
|
253
|
+
},
|
|
227
254
|
];
|
|
228
255
|
}
|
|
229
|
-
if (token.val ==
|
|
256
|
+
if (token.val == "pow") {
|
|
230
257
|
return [
|
|
231
258
|
{
|
|
232
259
|
...v1[0],
|
|
233
|
-
val: Math.pow(val1, val2)
|
|
234
|
-
}
|
|
260
|
+
val: Math.pow(val1, val2),
|
|
261
|
+
},
|
|
235
262
|
];
|
|
236
263
|
}
|
|
237
|
-
if (token.val ==
|
|
264
|
+
if (token.val == "atan2") {
|
|
238
265
|
return [
|
|
239
266
|
{
|
|
240
|
-
...{},
|
|
241
|
-
|
|
242
|
-
|
|
267
|
+
...{},
|
|
268
|
+
...v1[0],
|
|
269
|
+
val: Math.atan2(val1, val2),
|
|
270
|
+
},
|
|
243
271
|
];
|
|
244
272
|
}
|
|
245
273
|
return [
|
|
246
274
|
{
|
|
247
275
|
...v1[0],
|
|
248
|
-
val: val2 == 0 ? val1 : val1 -
|
|
249
|
-
}
|
|
276
|
+
val: val2 == 0 ? val1 : val1 - Math.floor(val1 / val2) * val2,
|
|
277
|
+
},
|
|
250
278
|
];
|
|
251
279
|
}
|
|
252
|
-
case
|
|
280
|
+
case "clamp":
|
|
253
281
|
token.chi = values;
|
|
254
282
|
return [token];
|
|
255
|
-
case
|
|
256
|
-
case
|
|
257
|
-
case
|
|
258
|
-
case
|
|
259
|
-
const strategy = token.val ==
|
|
260
|
-
|
|
283
|
+
case "log":
|
|
284
|
+
case "round":
|
|
285
|
+
case "min":
|
|
286
|
+
case "max": {
|
|
287
|
+
const strategy = token.val == "round" && values[0]?.typ == EnumToken.IdenTokenType
|
|
288
|
+
? values.shift().val
|
|
289
|
+
: null;
|
|
290
|
+
const valuesMap = new Map();
|
|
261
291
|
for (const curr of values) {
|
|
262
|
-
if (curr.typ == EnumToken.CommaTokenType ||
|
|
292
|
+
if (curr.typ == EnumToken.CommaTokenType ||
|
|
293
|
+
curr.typ == EnumToken.WhitespaceTokenType ||
|
|
294
|
+
curr.typ == EnumToken.CommentTokenType) {
|
|
263
295
|
continue;
|
|
264
296
|
}
|
|
265
297
|
const result = evaluate([curr]);
|
|
266
|
-
const key = result[0].typ + (
|
|
298
|
+
const key = result[0].typ + ("unit" in result[0] ? result[0].unit : "");
|
|
267
299
|
if (!valuesMap.has(key)) {
|
|
268
300
|
valuesMap.set(key, []);
|
|
269
301
|
}
|
|
@@ -271,41 +303,46 @@ function evaluateFunc(token) {
|
|
|
271
303
|
}
|
|
272
304
|
if (valuesMap.size == 1) {
|
|
273
305
|
const values = valuesMap.values().next().value;
|
|
274
|
-
if (token.val ==
|
|
306
|
+
if (token.val == "log") {
|
|
275
307
|
const val1 = getValue(values[0]);
|
|
276
308
|
const val2 = values.length == 2 ? getValue(values[1]) : null;
|
|
277
309
|
return [
|
|
278
310
|
{
|
|
279
311
|
...values[0],
|
|
280
|
-
val: Math.log(val1) / Math.log(val2)
|
|
281
|
-
}
|
|
312
|
+
val: Math.log(val1) / Math.log(val2),
|
|
313
|
+
},
|
|
282
314
|
];
|
|
283
315
|
}
|
|
284
|
-
if (token.val ==
|
|
316
|
+
if (token.val == "min" || token.val == "max") {
|
|
285
317
|
let val = getValue(values[0]);
|
|
286
318
|
let val2 = val;
|
|
287
319
|
let ret = values[0];
|
|
288
320
|
for (const curr of values.slice(1)) {
|
|
289
321
|
val2 = getValue(curr);
|
|
290
|
-
if (val2 < val && token.val ==
|
|
322
|
+
if (val2 < val && token.val == "min") {
|
|
291
323
|
val = val2;
|
|
292
324
|
ret = curr;
|
|
293
325
|
}
|
|
294
|
-
else if (val2 > val && token.val ==
|
|
326
|
+
else if (val2 > val && token.val == "max") {
|
|
295
327
|
val = val2;
|
|
296
328
|
ret = curr;
|
|
297
329
|
}
|
|
298
330
|
}
|
|
299
331
|
return [ret];
|
|
300
332
|
}
|
|
301
|
-
if (token.val ==
|
|
333
|
+
if (token.val == "round") {
|
|
302
334
|
let val = getValue(values[0]);
|
|
303
335
|
let val2 = getValue(values[1]);
|
|
304
|
-
if (strategy == null || strategy ==
|
|
336
|
+
if (strategy == null || strategy == "down") {
|
|
305
337
|
val = val - (val % val2);
|
|
306
338
|
}
|
|
307
339
|
else {
|
|
308
|
-
val =
|
|
340
|
+
val =
|
|
341
|
+
strategy == "to-zero"
|
|
342
|
+
? Math.trunc(val / val2) * val2
|
|
343
|
+
: strategy == "nearest"
|
|
344
|
+
? Math.round(val / val2) * val2
|
|
345
|
+
: Math.ceil(val / val2) * val2;
|
|
309
346
|
}
|
|
310
347
|
// @ts-ignore
|
|
311
348
|
return [{ ...values[0], val }];
|
|
@@ -343,19 +380,20 @@ function evaluateExpression(token) {
|
|
|
343
380
|
return token;
|
|
344
381
|
}
|
|
345
382
|
if (token.r.typ == EnumToken.BinaryExpressionTokenType) {
|
|
346
|
-
token.r = evaluateExpression(token.r);
|
|
383
|
+
token.r = (evaluateExpression(token.r));
|
|
347
384
|
}
|
|
348
385
|
if (token.l.typ == EnumToken.BinaryExpressionTokenType) {
|
|
349
|
-
token.l = evaluateExpression(token.l);
|
|
386
|
+
token.l = (evaluateExpression(token.l));
|
|
350
387
|
}
|
|
351
388
|
return doEvaluate(token.l, token.r, token.op);
|
|
352
389
|
}
|
|
353
390
|
function isScalarToken(token) {
|
|
354
|
-
return
|
|
391
|
+
return ("unit" in token ||
|
|
392
|
+
token.typ == EnumToken.MathFunctionTokenType ||
|
|
355
393
|
(token.typ == EnumToken.FunctionTokenType && mathFuncs.includes(token.val)) ||
|
|
356
394
|
// @ts-ignore
|
|
357
|
-
(token.typ == EnumToken.IdenTokenType && typeof Math[token.val.toUpperCase()] ==
|
|
358
|
-
[EnumToken.NumberTokenType, EnumToken.FractionTokenType, EnumToken.PercentageTokenType].includes(token.typ);
|
|
395
|
+
(token.typ == EnumToken.IdenTokenType && typeof Math[token.val.toUpperCase()] == "number") ||
|
|
396
|
+
[EnumToken.NumberTokenType, EnumToken.FractionTokenType, EnumToken.PercentageTokenType].includes(token.typ));
|
|
359
397
|
}
|
|
360
398
|
/**
|
|
361
399
|
*
|
|
@@ -363,16 +401,16 @@ function isScalarToken(token) {
|
|
|
363
401
|
* @param tokens
|
|
364
402
|
*/
|
|
365
403
|
function buildExpression(tokens) {
|
|
366
|
-
return factor(factor(tokens.filter(t => t.typ != EnumToken.WhitespaceTokenType), [
|
|
404
|
+
return factor(factor(tokens.filter((t) => t.typ != EnumToken.WhitespaceTokenType), ["/", "*"]), ["+", "-"])[0];
|
|
367
405
|
}
|
|
368
406
|
function getArithmeticOperation(op) {
|
|
369
|
-
if (op ==
|
|
407
|
+
if (op == "+") {
|
|
370
408
|
return EnumToken.Add;
|
|
371
409
|
}
|
|
372
|
-
if (op ==
|
|
410
|
+
if (op == "-") {
|
|
373
411
|
return EnumToken.Sub;
|
|
374
412
|
}
|
|
375
|
-
if (op ==
|
|
413
|
+
if (op == "/") {
|
|
376
414
|
return EnumToken.Div;
|
|
377
415
|
}
|
|
378
416
|
return EnumToken.Mul;
|
|
@@ -383,8 +421,11 @@ function getArithmeticOperation(op) {
|
|
|
383
421
|
* @param token
|
|
384
422
|
*/
|
|
385
423
|
function factorToken(token) {
|
|
386
|
-
if (token.typ == EnumToken.ParensTokenType ||
|
|
387
|
-
|
|
424
|
+
if (token.typ == EnumToken.ParensTokenType ||
|
|
425
|
+
((token.typ == EnumToken.MathFunctionTokenType || token.typ == EnumToken.FunctionTokenType) &&
|
|
426
|
+
token.val == "calc")) {
|
|
427
|
+
if ((token.typ == EnumToken.MathFunctionTokenType || token.typ == EnumToken.FunctionTokenType) &&
|
|
428
|
+
token.val == "calc") {
|
|
388
429
|
token = { ...token, typ: EnumToken.ParensTokenType };
|
|
389
430
|
// @ts-ignore
|
|
390
431
|
delete token.val;
|
|
@@ -400,7 +441,7 @@ function factorToken(token) {
|
|
|
400
441
|
*/
|
|
401
442
|
function factor(tokens, ops) {
|
|
402
443
|
let isOp;
|
|
403
|
-
const opList = ops.map(x => getArithmeticOperation(x));
|
|
444
|
+
const opList = ops.map((x) => getArithmeticOperation(x));
|
|
404
445
|
if (tokens.length == 1) {
|
|
405
446
|
return [factorToken(tokens[0])];
|
|
406
447
|
}
|
|
@@ -409,15 +450,20 @@ function factor(tokens, ops) {
|
|
|
409
450
|
// @ts-ignore
|
|
410
451
|
tokens.splice(i, 1, ...tokens[i].chi);
|
|
411
452
|
}
|
|
412
|
-
isOp = opList.includes(tokens[i].typ);
|
|
453
|
+
isOp = opList.includes(tokens[i].typ === EnumToken.Plus ? EnumToken.Add : tokens[i].typ);
|
|
413
454
|
if (isOp ||
|
|
455
|
+
tokens[i].typ === EnumToken.Star ||
|
|
414
456
|
// @ts-ignore
|
|
415
457
|
(tokens[i].typ == EnumToken.LiteralTokenType && ops.includes(tokens[i].val))) {
|
|
416
458
|
tokens.splice(i - 1, 3, {
|
|
417
459
|
typ: EnumToken.BinaryExpressionTokenType,
|
|
418
|
-
op: isOp
|
|
460
|
+
op: isOp
|
|
461
|
+
? tokens[i].typ === EnumToken.Plus
|
|
462
|
+
? EnumToken.Add
|
|
463
|
+
: tokens[i].typ
|
|
464
|
+
: getArithmeticOperation(tokens[i].val),
|
|
419
465
|
l: factorToken(tokens[i - 1]),
|
|
420
|
-
r: factorToken(tokens[i + 1])
|
|
466
|
+
r: factorToken(tokens[i + 1]),
|
|
421
467
|
});
|
|
422
468
|
i--;
|
|
423
469
|
}
|