@tbela99/css-parser 1.4.8 → 1.4.10
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 +32 -3
- package/README.md +1 -1
- package/dist/index-umd-web.js +12950 -13310
- package/dist/index.cjs +16447 -16809
- package/dist/index.d.ts +2481 -1754
- package/dist/lib/ast/features/calc.js +3 -1
- package/dist/lib/ast/features/if.js +2 -96
- package/dist/lib/ast/features/prefix.js +7 -2
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/find.js +6 -6
- package/dist/lib/ast/math/expression.js +30 -12
- package/dist/lib/ast/math/math.js +2 -1
- package/dist/lib/ast/minify.js +34 -5
- package/dist/lib/ast/transform/compute.js +2 -1
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/utils.js +29 -21
- package/dist/lib/ast/types.js +9 -66
- package/dist/lib/fs/resolve.js +1 -16
- package/dist/lib/parser/declaration/list.js +6 -1
- package/dist/lib/parser/{linemap.js → linesmap.js} +25 -9
- package/dist/lib/parser/parse.js +1264 -151
- package/dist/lib/parser/source.js +48 -17
- package/dist/lib/parser/tokenize.js +112 -133
- package/dist/lib/parser/utils/at-rule-container.js +12 -44
- package/dist/lib/parser/utils/at-rule-generic.js +12 -24
- package/dist/lib/parser/utils/at-rule-import.js +4 -172
- package/dist/lib/parser/utils/at-rule-media.js +14 -251
- package/dist/lib/parser/utils/at-rule-support.js +8 -258
- package/dist/lib/parser/utils/at-rule-when-else.js +4 -214
- package/dist/lib/parser/utils/declaration.js +5 -95
- package/dist/lib/parser/utils/hash.js +54 -1
- package/dist/lib/parser/utils/selector.js +1 -1
- package/dist/lib/parser/utils/token.js +1 -0
- package/dist/lib/renderer/render.js +79 -87
- package/dist/lib/renderer/sourcemap/sourcemap.js +47 -43
- package/dist/lib/syntax/color/cmyk.js +3 -3
- package/dist/lib/syntax/color/color-mix.js +7 -7
- package/dist/lib/syntax/color/color.js +9 -27
- package/dist/lib/syntax/color/hsl.js +6 -6
- package/dist/lib/syntax/color/hwb.js +6 -6
- package/dist/lib/syntax/color/lab.js +5 -5
- package/dist/lib/syntax/color/lch.js +6 -6
- package/dist/lib/syntax/color/oklab.js +5 -5
- package/dist/lib/syntax/color/oklch.js +5 -5
- package/dist/lib/syntax/color/relative-color.js +8 -1
- package/dist/lib/syntax/color/utils/distance.js +2 -1
- package/dist/lib/syntax/constants.js +2 -1
- package/dist/lib/syntax/syntax.js +96 -9
- package/dist/lib/validation/config.json.js +27 -6
- package/dist/lib/validation/match.js +42 -845
- package/dist/node.js +134 -9
- package/dist/web.js +133 -6
- package/llms.txt +73 -0
- package/package.json +13 -16
- package/dist/lib/parser/arena.js +0 -206
|
@@ -3,7 +3,7 @@ import { walkValues, WalkerEvent, WalkerOptionEnum } from '../walk.js';
|
|
|
3
3
|
import { evaluate } from '../math/expression.js';
|
|
4
4
|
import { renderValue } from '../../renderer/render.js';
|
|
5
5
|
import { FeatureWalkMode } from './type.js';
|
|
6
|
-
import { mathFuncs, tokensfuncSet } from '../../syntax/constants.js';
|
|
6
|
+
import { mathFuncs, tokensfuncSet, LOC } from '../../syntax/constants.js';
|
|
7
7
|
|
|
8
8
|
class ComputeCalcExpressionFeature {
|
|
9
9
|
accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType]);
|
|
@@ -124,6 +124,7 @@ class ComputeCalcExpressionFeature {
|
|
|
124
124
|
typ: EnumToken.MathFunctionTokenType,
|
|
125
125
|
val: "calc",
|
|
126
126
|
chi: values,
|
|
127
|
+
[LOC]: value[LOC],
|
|
127
128
|
}
|
|
128
129
|
: values[0]);
|
|
129
130
|
break;
|
|
@@ -137,6 +138,7 @@ class ComputeCalcExpressionFeature {
|
|
|
137
138
|
typ: EnumToken.MathFunctionTokenType,
|
|
138
139
|
val: "calc",
|
|
139
140
|
chi: values,
|
|
141
|
+
[LOC]: value[LOC],
|
|
140
142
|
});
|
|
141
143
|
break;
|
|
142
144
|
}
|
|
@@ -7,7 +7,6 @@ import { replaceNodeOrValue } from '../../parser/utils/token.js';
|
|
|
7
7
|
import { cloneNode } from '../clone.js';
|
|
8
8
|
import { trimArray } from '../../validation/match.js';
|
|
9
9
|
import { findByValue } from '../find.js';
|
|
10
|
-
import { walk } from '../walk.js';
|
|
11
10
|
import { eq } from '../../parser/utils/eq.js';
|
|
12
11
|
|
|
13
12
|
const nodeMatcher = (value) => value.typ === EnumToken.IfConditionTokenType ||
|
|
@@ -24,9 +23,6 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
24
23
|
// replace else: ... with the actual value
|
|
25
24
|
if (node.r.typ === EnumToken.IfConditionTokenType) {
|
|
26
25
|
const target = node.r.l.find((t) => t.typ != EnumToken.CommentTokenType && t.typ != EnumToken.WhitespaceTokenType);
|
|
27
|
-
// if (target == null) {
|
|
28
|
-
// return result;
|
|
29
|
-
// }
|
|
30
26
|
//
|
|
31
27
|
if (target.typ === EnumToken.IdenTokenType && equalsIgnoreCase("else", target.val)) {
|
|
32
28
|
replaceNodeOrValue(nodeMap.get(targetParentWrapper), nodeMap.get(targetWrapper), node.r.r.at(-1)?.typ ===
|
|
@@ -64,13 +60,6 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
64
60
|
}
|
|
65
61
|
}
|
|
66
62
|
}
|
|
67
|
-
// if (replaceRight) {
|
|
68
|
-
// replaceNodeOrValue(
|
|
69
|
-
// nodeMap.get(targetParentWrapper),
|
|
70
|
-
// nodeMap.get(targetWrapper),
|
|
71
|
-
// (node as IfElseConditionToken).r,
|
|
72
|
-
// );
|
|
73
|
-
// }
|
|
74
63
|
result.push(clonedDeclaration);
|
|
75
64
|
nodeMap.clear();
|
|
76
65
|
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
@@ -125,11 +114,6 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
125
114
|
processNode(clonedDeclaration, cache);
|
|
126
115
|
}
|
|
127
116
|
}
|
|
128
|
-
// else if (wrapper.typ === EnumToken.WildCardFunctionTokenType) {
|
|
129
|
-
// clonedDeclaration = cloneNode(declaration, true, nodeMap) as AstDeclaration;
|
|
130
|
-
// replaceNodeOrValue(nodeMap.get(parentWrapper), nodeMap.get(wrapper), node);
|
|
131
|
-
// result.push(clonedDeclaration);
|
|
132
|
-
// }
|
|
133
117
|
return result;
|
|
134
118
|
}
|
|
135
119
|
function processNode(declarationNode, cache) {
|
|
@@ -141,13 +125,7 @@ function processNode(declarationNode, cache) {
|
|
|
141
125
|
while (++k < stack.length) {
|
|
142
126
|
astNode = stack[k];
|
|
143
127
|
const { node: declaration, value: node } = findByValue(astNode, nodeMatcher) ?? {};
|
|
144
|
-
// if (node != null && cache.has(node.node)) {
|
|
145
|
-
// continue;
|
|
146
|
-
// }
|
|
147
128
|
if (declaration == null || node == null) {
|
|
148
|
-
// while (astNode[PARENT] != null && astNode[PARENT] != declarationNode[PARENT]) {
|
|
149
|
-
// astNode = astNode[PARENT];
|
|
150
|
-
// }
|
|
151
129
|
result.push(astNode);
|
|
152
130
|
continue;
|
|
153
131
|
}
|
|
@@ -156,9 +134,6 @@ function processNode(declarationNode, cache) {
|
|
|
156
134
|
const parentWrapper = node.parent ?? parents.find((node) => !nodeMatcher(node));
|
|
157
135
|
if (node.node.typ === EnumToken.WildCardFunctionTokenType) {
|
|
158
136
|
for (i = 0; i < node.node.chi.length; i++) {
|
|
159
|
-
// if (cache.has((node!.node as FunctionToken).chi[i])) {
|
|
160
|
-
// continue;
|
|
161
|
-
// }
|
|
162
137
|
stack.push(...substituteIfElseNode(declaration, node.node.chi[i], node.node, parentWrapper, cache));
|
|
163
138
|
}
|
|
164
139
|
}
|
|
@@ -167,49 +142,7 @@ function processNode(declarationNode, cache) {
|
|
|
167
142
|
}
|
|
168
143
|
}
|
|
169
144
|
if (result.length > 0) {
|
|
170
|
-
// let invalidTokensTypes = new Set([
|
|
171
|
-
// EnumToken.WhitespaceTokenType,
|
|
172
|
-
// EnumToken.SemiColonTokenType,
|
|
173
|
-
// EnumToken.ColonTokenType,
|
|
174
|
-
// ]);
|
|
175
|
-
// for (i = 0; i < result.length; i++) {
|
|
176
|
-
// if (result[i].typ === EnumToken.DeclarationNodeType) {
|
|
177
|
-
// for (const { value } of walkValues((result[i] as AstDeclaration).val, result[i])) {
|
|
178
|
-
// if (
|
|
179
|
-
// value.typ === EnumToken.ImageFunctionTokenType &&
|
|
180
|
-
// (value as FunctionToken).val.includes("-gradient")
|
|
181
|
-
// ) {
|
|
182
|
-
// let valid: boolean = true;
|
|
183
|
-
// let j: number;
|
|
184
|
-
// for (j = 0; j < (value as FunctionToken).chi.length; j++) {
|
|
185
|
-
// if (
|
|
186
|
-
// (value as FunctionToken).chi[j].typ === EnumToken.IdenTokenType &&
|
|
187
|
-
// "else" == ((value as FunctionToken).chi[j] as IdentToken).val
|
|
188
|
-
// ) {
|
|
189
|
-
// valid = false;
|
|
190
|
-
// break;
|
|
191
|
-
// }
|
|
192
|
-
// if (
|
|
193
|
-
// invalidTokensTypes.has((value as FunctionToken).chi[j].typ) &&
|
|
194
|
-
// ((value as FunctionToken).chi[j + 1]?.typ === EnumToken.CommaTokenType ||
|
|
195
|
-
// j == (value as FunctionToken).chi.length - 1) &&
|
|
196
|
-
// (j == 0 || EnumToken.CommaTokenType == (value as FunctionToken).chi[j - 1]?.typ)
|
|
197
|
-
// ) {
|
|
198
|
-
// valid = false;
|
|
199
|
-
// break;
|
|
200
|
-
// }
|
|
201
|
-
// }
|
|
202
|
-
// if (!valid) {
|
|
203
|
-
// result.splice(i--, 1);
|
|
204
|
-
// break;
|
|
205
|
-
// }
|
|
206
|
-
// }
|
|
207
|
-
// }
|
|
208
|
-
// }
|
|
209
|
-
// }
|
|
210
|
-
// if (result.length > 0) {
|
|
211
145
|
replaceNodeOrValue(declarationNode[PARENT], declarationNode, result);
|
|
212
|
-
// }
|
|
213
146
|
}
|
|
214
147
|
// else remove node?
|
|
215
148
|
return result;
|
|
@@ -217,7 +150,7 @@ function processNode(declarationNode, cache) {
|
|
|
217
150
|
class ExpandIfFeature {
|
|
218
151
|
accept = new Set([EnumToken.DeclarationNodeType]);
|
|
219
152
|
get ordering() {
|
|
220
|
-
return
|
|
153
|
+
return 4;
|
|
221
154
|
}
|
|
222
155
|
get processMode() {
|
|
223
156
|
return FeatureWalkMode.Pre;
|
|
@@ -229,34 +162,7 @@ class ExpandIfFeature {
|
|
|
229
162
|
}
|
|
230
163
|
}
|
|
231
164
|
run(declaration) {
|
|
232
|
-
processNode(declaration, new Set());
|
|
233
|
-
let i;
|
|
234
|
-
for (const n of declaration[PARENT].chi) {
|
|
235
|
-
for (const { node } of walk(n)) {
|
|
236
|
-
if (node.typ === EnumToken.AtRuleNodeType && Array.isArray(node.chi)) {
|
|
237
|
-
// for (i = 0; i < (node as AstAtRule).chi!.length; i++) {
|
|
238
|
-
// if (
|
|
239
|
-
// (node as AstAtRule).chi![i]!.typ === EnumToken.AtRuleNodeType &&
|
|
240
|
-
// (node as AstAtRule).chi![i + 1]?.typ === EnumToken.AtRuleNodeType &&
|
|
241
|
-
// (node as AstAtRule).chi![i]!.nam === (node as AstAtRule).chi![i + 1]!.nam &&
|
|
242
|
-
// (node as AstAtRule).chi![i]!.val === (node as AstAtRule).chi![i + 1]!.val
|
|
243
|
-
// ) {
|
|
244
|
-
// (node as AstAtRule).chi![i]!.chi.push(...(node as AstAtRule).chi![i + 1]!.chi);
|
|
245
|
-
// (node as AstAtRule).chi!.splice(i + 1, 1);
|
|
246
|
-
// i--;
|
|
247
|
-
// }
|
|
248
|
-
// }
|
|
249
|
-
for (i = 0; i < node.chi.length; i++) {
|
|
250
|
-
if (node.chi[i].typ === EnumToken.AtRuleNodeType &&
|
|
251
|
-
node.chi[i].nam === node.nam &&
|
|
252
|
-
node.chi[i].val === node.val) {
|
|
253
|
-
node.chi.splice(i, 1, ...node.chi[i].chi);
|
|
254
|
-
i--;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
165
|
+
return processNode(declaration, new Set());
|
|
260
166
|
}
|
|
261
167
|
}
|
|
262
168
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { EnumToken } from '../types.js';
|
|
1
|
+
import { EnumAstNodeStatus, EnumToken } from '../types.js';
|
|
2
2
|
import { walkValues } from '../walk.js';
|
|
3
3
|
import { pseudoAliasMap } from '../../syntax/syntax.js';
|
|
4
4
|
import { splitRule } from '../minify.js';
|
|
5
5
|
import { renderValue } from '../../renderer/render.js';
|
|
6
|
-
import { TOKENS, funcLike, regMatchLinearGradient, regMatchRadialGradient } from '../../syntax/constants.js';
|
|
6
|
+
import { STATE, TOKENS, funcLike, regMatchLinearGradient, regMatchRadialGradient } from '../../syntax/constants.js';
|
|
7
7
|
import { FeatureWalkMode } from './type.js';
|
|
8
8
|
import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
|
|
9
9
|
import { getSyntaxConfig } from '../../validation/config.js';
|
|
@@ -108,6 +108,11 @@ class ComputePrefixFeature {
|
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
110
|
run(node) {
|
|
111
|
+
if (node[STATE] == EnumAstNodeStatus.Disallowed ||
|
|
112
|
+
node[STATE] == EnumAstNodeStatus.Invalid ||
|
|
113
|
+
node[STATE] == EnumAstNodeStatus.Malformed) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
111
116
|
if (node.typ == EnumToken.RuleNodeType) {
|
|
112
117
|
node.sel = replacePseudo(splitRule(node.sel)).reduce((acc, curr, index) => acc + (index > 0 ? "," : "") + curr.join(""), "");
|
|
113
118
|
if (node[TOKENS] != null) {
|
|
@@ -8,7 +8,7 @@ import { STATE } from '../../syntax/constants.js';
|
|
|
8
8
|
class TransformCssFeature {
|
|
9
9
|
accept = new Set([EnumToken.RuleNodeType, EnumToken.KeyFramesRuleNodeType]);
|
|
10
10
|
get ordering() {
|
|
11
|
-
return
|
|
11
|
+
return 3;
|
|
12
12
|
}
|
|
13
13
|
get processMode() {
|
|
14
14
|
return FeatureWalkMode.Post;
|
package/dist/lib/ast/find.js
CHANGED
|
@@ -34,8 +34,8 @@ button {
|
|
|
34
34
|
* @returns
|
|
35
35
|
*/
|
|
36
36
|
function find(ast, matcher) {
|
|
37
|
-
for (const { node } of walk(ast)) {
|
|
38
|
-
if (matcher(node)) {
|
|
37
|
+
for (const { node, parent } of walk(ast)) {
|
|
38
|
+
if (matcher(node, parent)) {
|
|
39
39
|
return node;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
@@ -129,8 +129,8 @@ button-small {
|
|
|
129
129
|
*/
|
|
130
130
|
function findAll(ast, matcher) {
|
|
131
131
|
const result = [];
|
|
132
|
-
for (const { node } of walk(ast)) {
|
|
133
|
-
if (matcher(node)) {
|
|
132
|
+
for (const { node, parent } of walk(ast)) {
|
|
133
|
+
if (matcher(node, parent)) {
|
|
134
134
|
result.push(node);
|
|
135
135
|
}
|
|
136
136
|
}
|
|
@@ -167,8 +167,8 @@ button {
|
|
|
167
167
|
* @returns
|
|
168
168
|
*/
|
|
169
169
|
function findLast(ast, matcher) {
|
|
170
|
-
for (const { node } of walk(ast, null, true)) {
|
|
171
|
-
if (matcher(node)) {
|
|
170
|
+
for (const { node, parent } of walk(ast, null, true)) {
|
|
171
|
+
if (matcher(node, parent)) {
|
|
172
172
|
return node;
|
|
173
173
|
}
|
|
174
174
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mathFuncs } from '../../syntax/constants.js';
|
|
1
|
+
import { mathFuncs, LOC } from '../../syntax/constants.js';
|
|
2
2
|
import { EnumToken } from '../types.js';
|
|
3
3
|
import { rem, compute } from './math.js';
|
|
4
4
|
|
|
@@ -58,6 +58,7 @@ function evaluate(tokens) {
|
|
|
58
58
|
// @ts-ignore
|
|
59
59
|
val: Math[nodes[0].val.toUpperCase()],
|
|
60
60
|
typ: EnumToken.NumberTokenType,
|
|
61
|
+
[LOC]: nodes[0][LOC],
|
|
61
62
|
},
|
|
62
63
|
];
|
|
63
64
|
}
|
|
@@ -74,10 +75,14 @@ function evaluate(tokens) {
|
|
|
74
75
|
}
|
|
75
76
|
if (token.typ == EnumToken.Sub) {
|
|
76
77
|
if (!isScalarToken(nodes[i + 1])) {
|
|
77
|
-
token = {
|
|
78
|
+
token = {
|
|
79
|
+
typ: EnumToken.ListToken,
|
|
80
|
+
chi: [nodes[i], nodes[i + 1]],
|
|
81
|
+
[LOC]: { ...nodes[i][LOC], end: nodes[i + 1][LOC].end },
|
|
82
|
+
};
|
|
78
83
|
}
|
|
79
84
|
else {
|
|
80
|
-
token = doEvaluate(nodes[i + 1], { typ: EnumToken.NumberTokenType, val: -1 }, EnumToken.Mul);
|
|
85
|
+
token = doEvaluate(nodes[i + 1], { typ: EnumToken.NumberTokenType, val: -1, [LOC]: nodes[i + 1][LOC] }, EnumToken.Mul);
|
|
81
86
|
}
|
|
82
87
|
i++;
|
|
83
88
|
}
|
|
@@ -92,12 +97,16 @@ function evaluate(tokens) {
|
|
|
92
97
|
const token = curr[1].reduce((acc, curr) => doEvaluate(acc, curr, EnumToken.Add));
|
|
93
98
|
if (token.typ != EnumToken.BinaryExpressionTokenType) {
|
|
94
99
|
if ("val" in token && +token.val < 0) {
|
|
95
|
-
acc.push({ typ: EnumToken.Sub
|
|
100
|
+
acc.push({ typ: EnumToken.Sub, [LOC]: token[LOC] }, {
|
|
101
|
+
...token,
|
|
102
|
+
val: -token.val,
|
|
103
|
+
[LOC]: token[LOC],
|
|
104
|
+
});
|
|
96
105
|
return acc;
|
|
97
106
|
}
|
|
98
107
|
}
|
|
99
108
|
if (acc.length > 0 && curr[0] != EnumToken.ListToken) {
|
|
100
|
-
acc.push({ typ: EnumToken.Add });
|
|
109
|
+
acc.push({ typ: EnumToken.Add, [LOC]: token[LOC] });
|
|
101
110
|
}
|
|
102
111
|
acc.push(token);
|
|
103
112
|
return acc;
|
|
@@ -115,6 +124,7 @@ function doEvaluate(l, r, op) {
|
|
|
115
124
|
op,
|
|
116
125
|
l,
|
|
117
126
|
r,
|
|
127
|
+
[LOC]: { ...l[LOC], end: (r?.[LOC] ?? l[LOC])?.end },
|
|
118
128
|
};
|
|
119
129
|
if (!isScalarToken(l) || !isScalarToken(r) || (l.typ == r.typ && "unit" in l && "unit" in r && l.unit != r.unit)) {
|
|
120
130
|
return defaultReturn;
|
|
@@ -152,15 +162,15 @@ function doEvaluate(l, r, op) {
|
|
|
152
162
|
if (typeof v1 == "number" && l.typ == EnumToken.PercentageTokenType) {
|
|
153
163
|
v1 = {
|
|
154
164
|
typ: EnumToken.FractionTokenType,
|
|
155
|
-
l: { typ: EnumToken.NumberTokenType, val: v1 },
|
|
156
|
-
r: { typ: EnumToken.NumberTokenType, val: 100 },
|
|
165
|
+
l: { typ: EnumToken.NumberTokenType, val: v1, [LOC]: l[LOC] },
|
|
166
|
+
r: { typ: EnumToken.NumberTokenType, val: 100, [LOC]: r[LOC] },
|
|
157
167
|
};
|
|
158
168
|
}
|
|
159
169
|
else if (typeof v2 == "number" && r.typ == EnumToken.PercentageTokenType) {
|
|
160
170
|
v2 = {
|
|
161
171
|
typ: EnumToken.FractionTokenType,
|
|
162
|
-
l: { typ: EnumToken.NumberTokenType, val: v2 },
|
|
163
|
-
r: { typ: EnumToken.NumberTokenType, val: 100 },
|
|
172
|
+
l: { typ: EnumToken.NumberTokenType, val: v2, [LOC]: l[LOC] },
|
|
173
|
+
r: { typ: EnumToken.NumberTokenType, val: 100, [LOC]: r[LOC] },
|
|
164
174
|
};
|
|
165
175
|
}
|
|
166
176
|
}
|
|
@@ -171,6 +181,7 @@ function doEvaluate(l, r, op) {
|
|
|
171
181
|
...(l.typ === EnumToken.NumberTokenType || l.typ === EnumToken.IdenTokenType ? r : l),
|
|
172
182
|
typ,
|
|
173
183
|
val /* : typeof val == 'number' ? minifyNumber(val) : val */,
|
|
184
|
+
[LOC]: { ...l[LOC], end: (r?.[LOC] ?? l?.[LOC])?.end },
|
|
174
185
|
};
|
|
175
186
|
if (token.typ == EnumToken.IdenTokenType) {
|
|
176
187
|
// @ts-ignore
|
|
@@ -209,6 +220,7 @@ function evaluateFunc(token) {
|
|
|
209
220
|
{
|
|
210
221
|
typ: EnumToken.NumberTokenType,
|
|
211
222
|
val: Math[token.val](val),
|
|
223
|
+
[LOC]: value[0][LOC],
|
|
212
224
|
},
|
|
213
225
|
];
|
|
214
226
|
}
|
|
@@ -230,6 +242,7 @@ function evaluateFunc(token) {
|
|
|
230
242
|
{
|
|
231
243
|
...ref,
|
|
232
244
|
val: +Math.sqrt(value).toFixed(rem(...all)),
|
|
245
|
+
[LOC]: token[LOC],
|
|
233
246
|
},
|
|
234
247
|
];
|
|
235
248
|
}
|
|
@@ -258,6 +271,7 @@ function evaluateFunc(token) {
|
|
|
258
271
|
{
|
|
259
272
|
...v1[0],
|
|
260
273
|
val: Math.pow(val1, val2),
|
|
274
|
+
[LOC]: token[LOC],
|
|
261
275
|
},
|
|
262
276
|
];
|
|
263
277
|
}
|
|
@@ -267,6 +281,7 @@ function evaluateFunc(token) {
|
|
|
267
281
|
...{},
|
|
268
282
|
...v1[0],
|
|
269
283
|
val: Math.atan2(val1, val2),
|
|
284
|
+
[LOC]: token[LOC],
|
|
270
285
|
},
|
|
271
286
|
];
|
|
272
287
|
}
|
|
@@ -274,6 +289,7 @@ function evaluateFunc(token) {
|
|
|
274
289
|
{
|
|
275
290
|
...v1[0],
|
|
276
291
|
val: val2 == 0 ? val1 : val1 - Math.floor(val1 / val2) * val2,
|
|
292
|
+
[LOC]: token[LOC],
|
|
277
293
|
},
|
|
278
294
|
];
|
|
279
295
|
}
|
|
@@ -310,6 +326,7 @@ function evaluateFunc(token) {
|
|
|
310
326
|
{
|
|
311
327
|
...values[0],
|
|
312
328
|
val: Math.log(val1) / Math.log(val2),
|
|
329
|
+
[LOC]: token[LOC],
|
|
313
330
|
},
|
|
314
331
|
];
|
|
315
332
|
}
|
|
@@ -345,7 +362,7 @@ function evaluateFunc(token) {
|
|
|
345
362
|
: Math.ceil(val / val2) * val2;
|
|
346
363
|
}
|
|
347
364
|
// @ts-ignore
|
|
348
|
-
return [{ ...values[0], val }];
|
|
365
|
+
return [{ ...values[0], val, [LOC]: token[LOC] }];
|
|
349
366
|
}
|
|
350
367
|
}
|
|
351
368
|
}
|
|
@@ -363,7 +380,7 @@ function inlineExpression(token) {
|
|
|
363
380
|
result.push(token);
|
|
364
381
|
}
|
|
365
382
|
else {
|
|
366
|
-
result.push(...inlineExpression(token.l), { typ: token.op }, ...inlineExpression(token.r));
|
|
383
|
+
result.push(...inlineExpression(token.l), { typ: token.op, [LOC]: token[LOC] }, ...inlineExpression(token.r));
|
|
367
384
|
}
|
|
368
385
|
}
|
|
369
386
|
else {
|
|
@@ -426,7 +443,7 @@ function factorToken(token) {
|
|
|
426
443
|
token.val == "calc")) {
|
|
427
444
|
if ((token.typ == EnumToken.MathFunctionTokenType || token.typ == EnumToken.FunctionTokenType) &&
|
|
428
445
|
token.val == "calc") {
|
|
429
|
-
token = { ...token, typ: EnumToken.ParensTokenType };
|
|
446
|
+
token = { ...token, typ: EnumToken.ParensTokenType, [LOC]: token[LOC] };
|
|
430
447
|
// @ts-ignore
|
|
431
448
|
delete token.val;
|
|
432
449
|
}
|
|
@@ -464,6 +481,7 @@ function factor(tokens, ops) {
|
|
|
464
481
|
: getArithmeticOperation(tokens[i].val),
|
|
465
482
|
l: factorToken(tokens[i - 1]),
|
|
466
483
|
r: factorToken(tokens[i + 1]),
|
|
484
|
+
[LOC]: { ...tokens[i - 1][LOC], end: tokens[i + 1][LOC]?.end },
|
|
467
485
|
});
|
|
468
486
|
i--;
|
|
469
487
|
}
|
package/dist/lib/ast/minify.js
CHANGED
|
@@ -81,13 +81,16 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
81
81
|
replacement = result;
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
|
-
if (replacement !=
|
|
84
|
+
if (replacement != null &&
|
|
85
|
+
(!Array.isArray(replacement) || replacement.length > 0) &&
|
|
86
|
+
replacement != parent &&
|
|
87
|
+
parent[PARENT] != null) {
|
|
85
88
|
replaceNodeOrValue(parent[PARENT], parent, replacement);
|
|
86
89
|
}
|
|
87
90
|
if ("chi" in replacement) {
|
|
88
91
|
// @ts-ignore
|
|
89
92
|
for (const node of replacement.chi) {
|
|
90
|
-
node[PARENT] = replacement;
|
|
93
|
+
// node[PARENT] = replacement;
|
|
91
94
|
parents.add(node);
|
|
92
95
|
}
|
|
93
96
|
}
|
|
@@ -118,13 +121,16 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
118
121
|
}
|
|
119
122
|
}
|
|
120
123
|
}
|
|
121
|
-
if (replacement != null &&
|
|
124
|
+
if (replacement != null &&
|
|
125
|
+
(!Array.isArray(replacement) || replacement.length > 0) &&
|
|
126
|
+
replacement != parent &&
|
|
127
|
+
parent[PARENT] != null) {
|
|
122
128
|
// @ts-ignore
|
|
123
129
|
replaceNodeOrValue(parent[PARENT], parent, replacement);
|
|
124
130
|
}
|
|
125
131
|
if ("chi" in replacement) {
|
|
126
132
|
for (const node of replacement.chi) {
|
|
127
|
-
node[PARENT] = replacement;
|
|
133
|
+
// node[PARENT] = replacement;
|
|
128
134
|
parents.add(node);
|
|
129
135
|
}
|
|
130
136
|
}
|
|
@@ -417,6 +423,13 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
417
423
|
: renderValue(curr)), "");
|
|
418
424
|
}
|
|
419
425
|
}
|
|
426
|
+
else if (ast.typ === node.typ &&
|
|
427
|
+
ast.nam === node.nam &&
|
|
428
|
+
ast.val === node.val) {
|
|
429
|
+
replaceNodeOrValue(ast, node, node.chi);
|
|
430
|
+
i--;
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
420
433
|
if (previous?.typ == EnumToken.AtRuleNodeType &&
|
|
421
434
|
node.nam != "font-face" &&
|
|
422
435
|
previous.nam === node.nam &&
|
|
@@ -432,7 +445,10 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
432
445
|
ast?.chi?.splice(i--, 1);
|
|
433
446
|
continue;
|
|
434
447
|
}
|
|
435
|
-
if (!hasDeclaration(node)) {
|
|
448
|
+
// if (!hasDeclaration(node as AstAtRule)) {
|
|
449
|
+
// doMinify(node, options, recursive, errors, nestingContent, context);
|
|
450
|
+
// }
|
|
451
|
+
if ("chi" in node) {
|
|
436
452
|
doMinify(node, options, recursive, errors, nestingContent, context);
|
|
437
453
|
}
|
|
438
454
|
previous = node;
|
|
@@ -490,6 +506,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
490
506
|
node.sel = node[OPTIMIZED].selector.reduce(reducer, []).join(",");
|
|
491
507
|
// @ts-ignore
|
|
492
508
|
node[RAW] = node[OPTIMIZED].selector.slice();
|
|
509
|
+
node[TOKENS] = null;
|
|
493
510
|
// @ts-ignore
|
|
494
511
|
wrapper.chi.push(node);
|
|
495
512
|
// @ts-ignore
|
|
@@ -504,12 +521,14 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
504
521
|
")";
|
|
505
522
|
const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
|
|
506
523
|
node.sel = sel1.length < sel2.length ? sel1 : sel2;
|
|
524
|
+
node[TOKENS] = null;
|
|
507
525
|
}
|
|
508
526
|
else if (node[OPTIMIZED].optimized.length === 0) {
|
|
509
527
|
const testIdent = /^[a-zA-Z]/;
|
|
510
528
|
node.sel = node[OPTIMIZED].selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") +
|
|
511
529
|
(nestingContent && testIdent.test(curr[0]) ? "& " : "") +
|
|
512
530
|
curr.join(""), "");
|
|
531
|
+
node[TOKENS] = null;
|
|
513
532
|
}
|
|
514
533
|
}
|
|
515
534
|
}
|
|
@@ -571,6 +590,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
571
590
|
let sel = wrap ? node[OPTIMIZED].optimized.join("") + `:is(${rule})` : rule;
|
|
572
591
|
if (sel.length < node.sel.length) {
|
|
573
592
|
node.sel = sel;
|
|
593
|
+
node[TOKENS] = null;
|
|
574
594
|
}
|
|
575
595
|
}
|
|
576
596
|
else if (node[OPTIMIZED]?.reducible) {
|
|
@@ -581,12 +601,14 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
581
601
|
")";
|
|
582
602
|
const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
|
|
583
603
|
node.sel = sel1.length < sel2.length ? sel1 : sel2;
|
|
604
|
+
node[TOKENS] = null;
|
|
584
605
|
}
|
|
585
606
|
else if (node[OPTIMIZED].optimized.length === 0) {
|
|
586
607
|
const testIdent = /^[a-zA-Z]/;
|
|
587
608
|
node.sel = node[OPTIMIZED].selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") +
|
|
588
609
|
(nestingContent && testIdent.test(curr[0]) ? "& " : "") +
|
|
589
610
|
curr.join(""), "");
|
|
611
|
+
node[TOKENS] = null;
|
|
590
612
|
}
|
|
591
613
|
}
|
|
592
614
|
else if (node[OPTIMIZED]?.optimized.length > 0) {
|
|
@@ -594,6 +616,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
594
616
|
if (sel.length < node.sel.length) {
|
|
595
617
|
node.sel = sel;
|
|
596
618
|
node[RAW] = [node[OPTIMIZED].optimized.slice()];
|
|
619
|
+
node[TOKENS] = null;
|
|
597
620
|
}
|
|
598
621
|
}
|
|
599
622
|
doMinify(node, options, recursive, errors, nestingContent, context);
|
|
@@ -1107,6 +1130,7 @@ function fixSelector(node) {
|
|
|
1107
1130
|
}
|
|
1108
1131
|
}
|
|
1109
1132
|
node.sel = attributes.reduce((acc, curr) => acc + renderValue(curr), "");
|
|
1133
|
+
node[TOKENS] = null;
|
|
1110
1134
|
}
|
|
1111
1135
|
}
|
|
1112
1136
|
/**
|
|
@@ -1149,9 +1173,12 @@ function wrapNodes(previous, node, match, ast, reducer, i, nodeIndex) {
|
|
|
1149
1173
|
ast.chi.splice(nodeIndex, 1);
|
|
1150
1174
|
previous.sel = pSel;
|
|
1151
1175
|
previous[RAW] = match.selector1;
|
|
1176
|
+
previous[TOKENS] = null;
|
|
1152
1177
|
node.sel = nSel;
|
|
1153
1178
|
node[RAW] = match.selector2;
|
|
1179
|
+
node[TOKENS] = null;
|
|
1154
1180
|
reduceRuleSelector(wrapper);
|
|
1181
|
+
wrapper[TOKENS] = null;
|
|
1155
1182
|
return wrapper;
|
|
1156
1183
|
}
|
|
1157
1184
|
/**
|
|
@@ -1315,6 +1342,7 @@ function diff(n1, n2, options = {}) {
|
|
|
1315
1342
|
const sel = match + ":is(" + replaceCompound(rule, match) + ")";
|
|
1316
1343
|
if (sel.length < result.sel.length) {
|
|
1317
1344
|
result.sel = sel;
|
|
1345
|
+
result[TOKENS] = null;
|
|
1318
1346
|
}
|
|
1319
1347
|
}
|
|
1320
1348
|
}
|
|
@@ -1364,6 +1392,7 @@ function reduceRuleSelector(node) {
|
|
|
1364
1392
|
if (sel.length < node.sel.length) {
|
|
1365
1393
|
node.sel = sel;
|
|
1366
1394
|
node[RAW] = raw;
|
|
1395
|
+
node[TOKENS] = null;
|
|
1367
1396
|
}
|
|
1368
1397
|
}
|
|
1369
1398
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { multiply, toZero, identity } from './utils.js';
|
|
2
|
-
import { EnumToken
|
|
2
|
+
import { EnumToken } from '../types.js';
|
|
3
3
|
import { stripCommaToken } from '../../validation/utils/list.js';
|
|
4
4
|
import { translateX, translateY, translateZ, translate, translate3d } from './translate.js';
|
|
5
5
|
import { getNumber, getAngle } from '../../syntax/color/color.js';
|
|
@@ -10,6 +10,7 @@ import { skew, skewX, skewY } from './skew.js';
|
|
|
10
10
|
import { serialize, matrix } from './matrix.js';
|
|
11
11
|
import { perspective } from './perspective.js';
|
|
12
12
|
import { transformFunctions } from '../../syntax/constants.js';
|
|
13
|
+
import { length2Px } from '../../syntax/syntax.js';
|
|
13
14
|
|
|
14
15
|
function compute(transformLists) {
|
|
15
16
|
transformLists = transformLists.slice();
|
|
@@ -3,7 +3,7 @@ import { multiply, identity } from './utils.js';
|
|
|
3
3
|
function perspective(x, from) {
|
|
4
4
|
const matrix = identity();
|
|
5
5
|
// @ts-ignore
|
|
6
|
-
matrix[2 * 4 + 3] = typeof x ==
|
|
6
|
+
matrix[2 * 4 + 3] = typeof x == "object" && x.val == "none" ? 0 : x == 0 ? Number.NEGATIVE_INFINITY : -1 / x;
|
|
7
7
|
return multiply(from, matrix);
|
|
8
8
|
}
|
|
9
9
|
|