@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,221 @@
|
|
|
1
|
+
import { EnumToken } from '../types.js';
|
|
2
|
+
import { renderValue } from '../../renderer/render.js';
|
|
3
|
+
import { FeatureWalkMode } from './type.js';
|
|
4
|
+
import { definedPropertySettings } from '../../syntax/constants.js';
|
|
5
|
+
import { equalsIgnoreCase } from '../../parser/utils/text.js';
|
|
6
|
+
import { replaceNodeOrValue } from '../../parser/utils/token.js';
|
|
7
|
+
import { cloneNode } from '../clone.js';
|
|
8
|
+
import { trimArray } from '../../validation/match.js';
|
|
9
|
+
import { findByValue } from '../find.js';
|
|
10
|
+
import { walk } from '../walk.js';
|
|
11
|
+
import { eq } from '../../parser/utils/eq.js';
|
|
12
|
+
|
|
13
|
+
const nodeMatcher = (value) => value.typ === EnumToken.IfConditionTokenType ||
|
|
14
|
+
(value.typ === EnumToken.WildCardFunctionTokenType && equalsIgnoreCase("if", value.val));
|
|
15
|
+
function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache) {
|
|
16
|
+
const result = [];
|
|
17
|
+
let nodeMap = new Map();
|
|
18
|
+
let clonedDeclaration;
|
|
19
|
+
let targetParentWrapper = parentWrapper;
|
|
20
|
+
let targetWrapper = wrapper;
|
|
21
|
+
if (node.typ === EnumToken.IfElseConditionTokenType) {
|
|
22
|
+
//
|
|
23
|
+
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
24
|
+
let replaceRight = true;
|
|
25
|
+
// replace else: ... with the actual value
|
|
26
|
+
if (node.r.typ === EnumToken.IfConditionTokenType) {
|
|
27
|
+
const target = node.r.l.find((t) => t.typ != EnumToken.CommentTokenType && t.typ != EnumToken.WhitespaceTokenType);
|
|
28
|
+
if (target == null) {
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
//
|
|
32
|
+
if (target.typ === EnumToken.IdenTokenType && equalsIgnoreCase("else", target.val)) {
|
|
33
|
+
replaceNodeOrValue(nodeMap.get(targetParentWrapper), nodeMap.get(targetWrapper), node.r.r.at(-1)?.typ ===
|
|
34
|
+
EnumToken.SemiColonTokenType
|
|
35
|
+
? trimArray(node.r.r.slice(0, -1))
|
|
36
|
+
: node.r.r);
|
|
37
|
+
if (targetParentWrapper.typ != EnumToken.DeclarationNodeType) {
|
|
38
|
+
let index = targetParentWrapper.chi.indexOf(targetWrapper);
|
|
39
|
+
if (index != -1) {
|
|
40
|
+
let i;
|
|
41
|
+
let k;
|
|
42
|
+
let siblingWrapper;
|
|
43
|
+
let left = node.l.l.find((t) => t.typ != EnumToken.CommentTokenType && t.typ != EnumToken.WhitespaceTokenType);
|
|
44
|
+
for (i = index + 1; i < targetParentWrapper.chi.length; i++) {
|
|
45
|
+
if (targetParentWrapper.chi[i].typ === targetWrapper.typ) {
|
|
46
|
+
siblingWrapper = targetParentWrapper.chi[i];
|
|
47
|
+
for (k = 0; k < siblingWrapper.chi.length; k++) {
|
|
48
|
+
if (siblingWrapper.chi[k].typ === EnumToken.IfElseConditionTokenType) {
|
|
49
|
+
let leftSide = siblingWrapper.chi[k].l.l.find((t) => t.typ != EnumToken.CommentTokenType &&
|
|
50
|
+
t.typ != EnumToken.WhitespaceTokenType);
|
|
51
|
+
if (eq(left, leftSide)) {
|
|
52
|
+
replaceNodeOrValue(nodeMap.get(targetParentWrapper), nodeMap.get(targetParentWrapper.chi[i]), siblingWrapper.chi[k]
|
|
53
|
+
.r.r.at(-1)?.typ === EnumToken.SemiColonTokenType
|
|
54
|
+
? trimArray(siblingWrapper.chi[k]
|
|
55
|
+
.r.r.slice(0, -1))
|
|
56
|
+
: siblingWrapper.chi[k]
|
|
57
|
+
.r.r);
|
|
58
|
+
cache.add(siblingWrapper.chi[k].l);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
replaceRight = false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (replaceRight) {
|
|
70
|
+
replaceNodeOrValue(nodeMap.get(targetParentWrapper), nodeMap.get(targetWrapper), node.r);
|
|
71
|
+
}
|
|
72
|
+
result.push(clonedDeclaration);
|
|
73
|
+
nodeMap.clear();
|
|
74
|
+
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
75
|
+
replaceNodeOrValue(nodeMap.get(targetParentWrapper), nodeMap.get(targetWrapper), node.l);
|
|
76
|
+
result.push(clonedDeclaration);
|
|
77
|
+
}
|
|
78
|
+
else if (node.typ === EnumToken.IfConditionTokenType) {
|
|
79
|
+
const left = node.l.find((t) => t.typ != EnumToken.CommentTokenType && t.typ != EnumToken.WhitespaceTokenType);
|
|
80
|
+
if (left == null) {
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
if (left.typ === EnumToken.IdenTokenType && equalsIgnoreCase("else", left.val)) {
|
|
84
|
+
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
85
|
+
replaceNodeOrValue(nodeMap.get(parentWrapper), nodeMap.get(targetWrapper.typ === EnumToken.DeclarationNodeType ? node : targetWrapper), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType ? trimArray(node.r.slice(0, -1)) : node.r);
|
|
86
|
+
result.push(clonedDeclaration);
|
|
87
|
+
}
|
|
88
|
+
else if (left?.typ === EnumToken.WhenElseFunctionTokenType) {
|
|
89
|
+
const atRule = Object.assign(cloneNode(declaration), {
|
|
90
|
+
typ: EnumToken.AtRuleNodeType,
|
|
91
|
+
nam: left.val,
|
|
92
|
+
chi: [],
|
|
93
|
+
});
|
|
94
|
+
Object.defineProperty(atRule, "tokens", {
|
|
95
|
+
...definedPropertySettings,
|
|
96
|
+
value: [{ typ: EnumToken.ParensTokenType, chi: left.chi.slice() }],
|
|
97
|
+
});
|
|
98
|
+
const minify = atRule.nam !== "supports";
|
|
99
|
+
const options = {
|
|
100
|
+
minify,
|
|
101
|
+
convertColor: minify,
|
|
102
|
+
};
|
|
103
|
+
atRule.val = atRule.tokens.reduce((acc, curr) => acc + renderValue(curr, options), "");
|
|
104
|
+
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
105
|
+
replaceNodeOrValue(nodeMap.get(targetWrapper), nodeMap.get(node), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType ? trimArray(node.r.slice(0, -1)) : node.r);
|
|
106
|
+
clonedDeclaration.parent = atRule;
|
|
107
|
+
atRule.chi.push(clonedDeclaration);
|
|
108
|
+
result.push(atRule);
|
|
109
|
+
processNode(clonedDeclaration, cache);
|
|
110
|
+
// nodeMap.clear();
|
|
111
|
+
}
|
|
112
|
+
else if (left.typ === EnumToken.ContainerFunctionTokenType) {
|
|
113
|
+
const atRule = Object.assign(cloneNode(declaration), {
|
|
114
|
+
typ: EnumToken.AtRuleNodeType,
|
|
115
|
+
nam: "container",
|
|
116
|
+
chi: [],
|
|
117
|
+
});
|
|
118
|
+
Object.defineProperty(atRule, "tokens", { ...definedPropertySettings, value: [left] });
|
|
119
|
+
atRule.val = atRule.tokens.reduce((acc, curr) => acc + renderValue(curr), "");
|
|
120
|
+
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
121
|
+
replaceNodeOrValue(nodeMap.get(targetWrapper.typ === EnumToken.WildCardFunctionTokenType ? targetParentWrapper : targetWrapper), nodeMap.get(targetWrapper.typ === EnumToken.WildCardFunctionTokenType ? targetWrapper : node), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType
|
|
122
|
+
? trimArray(node.r.slice(0, -1))
|
|
123
|
+
: node.r);
|
|
124
|
+
clonedDeclaration.parent = atRule;
|
|
125
|
+
atRule.chi.push(clonedDeclaration);
|
|
126
|
+
result.push(atRule);
|
|
127
|
+
processNode(clonedDeclaration, cache);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else if (wrapper.typ === EnumToken.WildCardFunctionTokenType) {
|
|
131
|
+
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
132
|
+
replaceNodeOrValue(nodeMap.get(parentWrapper), nodeMap.get(wrapper), node);
|
|
133
|
+
result.push(clonedDeclaration);
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
function processNode(declarationNode, cache) {
|
|
138
|
+
let i;
|
|
139
|
+
let k = -1;
|
|
140
|
+
let astNode;
|
|
141
|
+
const result = [];
|
|
142
|
+
const stack = [declarationNode];
|
|
143
|
+
while (++k < stack.length) {
|
|
144
|
+
astNode = stack[k];
|
|
145
|
+
const { node: declaration, value: node } = findByValue(astNode, nodeMatcher) ?? {};
|
|
146
|
+
if (node != null && cache.has(node.node)) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (declaration == null || node == null) {
|
|
150
|
+
while (astNode.parent != null && astNode.parent != declarationNode.parent) {
|
|
151
|
+
astNode = astNode.parent;
|
|
152
|
+
}
|
|
153
|
+
result.push(astNode);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
// @ts-expect-error
|
|
157
|
+
const parents = [...node.parents?.()];
|
|
158
|
+
const parentWrapper = node.parent ?? parents.find((node) => !nodeMatcher(node));
|
|
159
|
+
if (node.node.typ === EnumToken.WildCardFunctionTokenType) {
|
|
160
|
+
for (i = 0; i < node.node.chi.length; i++) {
|
|
161
|
+
if (cache.has(node.node.chi[i])) {
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
stack.push(...substituteIfElseNode(declaration, node.node.chi[i], node.node, parentWrapper, cache));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
stack.push(...substituteIfElseNode(declaration, node.node, parentWrapper, parents[parents.indexOf(parentWrapper) + 1] ?? declaration, cache));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (result.length > 0) {
|
|
172
|
+
replaceNodeOrValue(declarationNode.parent, declarationNode, result);
|
|
173
|
+
}
|
|
174
|
+
// else remove node?
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
class ExpandIfFeature {
|
|
178
|
+
accept = new Set([EnumToken.DeclarationNodeType]);
|
|
179
|
+
get ordering() {
|
|
180
|
+
return 5;
|
|
181
|
+
}
|
|
182
|
+
get processMode() {
|
|
183
|
+
return FeatureWalkMode.Pre;
|
|
184
|
+
}
|
|
185
|
+
static register(options) {
|
|
186
|
+
if (options.expandIfSyntax) {
|
|
187
|
+
// @ts-ignore
|
|
188
|
+
options.features.push(new ExpandIfFeature());
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
run(declaration) {
|
|
192
|
+
processNode(declaration, new Set());
|
|
193
|
+
let i;
|
|
194
|
+
for (const n of declaration.parent.chi) {
|
|
195
|
+
for (const { node } of walk(n)) {
|
|
196
|
+
if (node.typ === EnumToken.AtRuleNodeType && Array.isArray(node.chi)) {
|
|
197
|
+
for (i = 0; i < node.chi.length; i++) {
|
|
198
|
+
if (node.chi[i].typ === EnumToken.AtRuleNodeType &&
|
|
199
|
+
node.chi[i + 1]?.typ === EnumToken.AtRuleNodeType &&
|
|
200
|
+
node.chi[i].nam === node.chi[i + 1].nam &&
|
|
201
|
+
node.chi[i].val === node.chi[i + 1].val) {
|
|
202
|
+
node.chi[i].chi.push(...node.chi[i + 1].chi);
|
|
203
|
+
node.chi.splice(i + 1, 1);
|
|
204
|
+
i--;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (i = 0; i < node.chi.length; i++) {
|
|
208
|
+
if (node.chi[i].typ === EnumToken.AtRuleNodeType &&
|
|
209
|
+
node.chi[i].nam === node.nam &&
|
|
210
|
+
node.chi[i].val === node.val) {
|
|
211
|
+
node.chi.splice(i, 1, ...node.chi[i].chi);
|
|
212
|
+
i--;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export { ExpandIfFeature };
|
|
@@ -3,3 +3,4 @@ export { InlineCssVariablesFeature } from './inlinecssvariables.js';
|
|
|
3
3
|
export { ComputeShorthandFeature } from './shorthand.js';
|
|
4
4
|
export { ComputeCalcExpressionFeature } from './calc.js';
|
|
5
5
|
export { TransformCssFeature } from './transform.js';
|
|
6
|
+
export { ExpandIfFeature } from './if.js';
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { EnumToken } from '../types.js';
|
|
2
2
|
import { walkValues } from '../walk.js';
|
|
3
|
-
import {
|
|
4
|
-
import '../../renderer/sourcemap/lib/encode.js';
|
|
5
|
-
import { mathFuncs } from '../../syntax/syntax.js';
|
|
3
|
+
import { renderValue } from '../../renderer/render.js';
|
|
6
4
|
import { splitRule } from '../minify.js';
|
|
7
|
-
import '../../parser/parse.js';
|
|
8
|
-
import '../../parser/tokenize.js';
|
|
9
|
-
import '../../parser/utils/config.js';
|
|
10
5
|
import { FeatureWalkMode } from './type.js';
|
|
6
|
+
import { mathFuncs } from '../../syntax/constants.js';
|
|
11
7
|
|
|
12
8
|
function inlineExpression(token) {
|
|
13
9
|
const result = [];
|
|
14
10
|
if (token.typ == EnumToken.BinaryExpressionTokenType) {
|
|
15
11
|
result.push({
|
|
16
12
|
typ: EnumToken.ParensTokenType,
|
|
17
|
-
chi: [
|
|
13
|
+
chi: [
|
|
14
|
+
...inlineExpression(token.l),
|
|
15
|
+
{ typ: token.op },
|
|
16
|
+
...inlineExpression(token.r),
|
|
17
|
+
],
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
else {
|
|
@@ -24,15 +24,17 @@ function inlineExpression(token) {
|
|
|
24
24
|
}
|
|
25
25
|
function replace(node, variableScope) {
|
|
26
26
|
for (const { value, parent: parentValue } of walkValues(node.val)) {
|
|
27
|
-
if (value.typ == EnumToken.BinaryExpressionTokenType && parentValue != null &&
|
|
27
|
+
if (value.typ == EnumToken.BinaryExpressionTokenType && parentValue != null && "chi" in parentValue) {
|
|
28
28
|
// @ts-ignore
|
|
29
29
|
parentValue.chi.splice(parentValue.chi.indexOf(value), 1, ...inlineExpression(value));
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
for (const { value, parent: parentValue } of walkValues(node.val)) {
|
|
33
|
-
if (value.typ == EnumToken.
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
if (value.typ == EnumToken.MathFunctionTokenDefType ||
|
|
34
|
+
(value.typ == EnumToken.WildCardFunctionTokenType && value.val == "var")) {
|
|
35
|
+
if (value.chi.length == 1 &&
|
|
36
|
+
value.chi[0].typ == EnumToken.DashedIdenTokenType) {
|
|
37
|
+
const info = (variableScope.get(value.chi[0].val));
|
|
36
38
|
if (info?.replaceable) {
|
|
37
39
|
if (parentValue != null) {
|
|
38
40
|
let i = 0;
|
|
@@ -66,24 +68,25 @@ class InlineCssVariablesFeature {
|
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
70
|
run(ast, options = {}, parent, context) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
context.variableScope = new Map;
|
|
71
|
+
if (!("chi" in ast)) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
if (!("variableScope" in context)) {
|
|
75
|
+
context.variableScope = new Map();
|
|
75
76
|
}
|
|
76
77
|
// [':root', 'html']
|
|
77
|
-
const isRoot = parent.typ == EnumToken.StyleSheetNodeType &&
|
|
78
|
+
const isRoot = parent.typ == EnumToken.StyleSheetNodeType &&
|
|
79
|
+
ast.typ == EnumToken.RuleNodeType &&
|
|
80
|
+
(ast.raw ?? splitRule(ast.sel)).some((segment) => segment.some((s) => s == ":root" || s == "html"));
|
|
78
81
|
const variableScope = context.variableScope;
|
|
79
82
|
// @ts-ignore
|
|
80
83
|
for (const node of ast.chi) {
|
|
81
|
-
if (node.typ
|
|
84
|
+
if (node.typ !== EnumToken.DeclarationNodeType) {
|
|
82
85
|
continue;
|
|
83
86
|
}
|
|
84
87
|
// css variable
|
|
85
|
-
if (node.nam.startsWith(
|
|
86
|
-
if (!variableScope.has(node.
|
|
88
|
+
if (node.nam.startsWith("--")) {
|
|
89
|
+
if (!variableScope.has(node.val)) {
|
|
87
90
|
const info = {
|
|
88
91
|
globalScope: isRoot,
|
|
89
92
|
// @ts-ignore
|
|
@@ -91,13 +94,16 @@ class InlineCssVariablesFeature {
|
|
|
91
94
|
declarationCount: 1,
|
|
92
95
|
replaceable: isRoot,
|
|
93
96
|
node: node,
|
|
94
|
-
values: structuredClone(node.val)
|
|
97
|
+
values: structuredClone(node.val),
|
|
95
98
|
};
|
|
96
99
|
info.parent.add(ast);
|
|
97
100
|
variableScope.set(node.nam, info);
|
|
98
101
|
let recursive = false;
|
|
99
102
|
for (const { value } of walkValues(node.val)) {
|
|
100
|
-
if (value?.typ == EnumToken.
|
|
103
|
+
if (value?.typ == EnumToken.MathFunctionTokenDefType ||
|
|
104
|
+
(value?.typ == EnumToken.WildCardFunctionTokenType &&
|
|
105
|
+
(mathFuncs.includes(value.val) ||
|
|
106
|
+
value.val === "var"))) {
|
|
101
107
|
recursive = true;
|
|
102
108
|
break;
|
|
103
109
|
}
|
|
@@ -115,10 +121,9 @@ class InlineCssVariablesFeature {
|
|
|
115
121
|
}
|
|
116
122
|
cleanup(ast, options = {}, context) {
|
|
117
123
|
const variableScope = context.variableScope;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// }
|
|
124
|
+
if (variableScope == null) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
122
127
|
for (const info of variableScope.values()) {
|
|
123
128
|
if (info.replaceable) {
|
|
124
129
|
let i;
|
|
@@ -130,7 +135,7 @@ class InlineCssVariablesFeature {
|
|
|
130
135
|
// @ts-ignore
|
|
131
136
|
parent.chi.splice(i, 1, {
|
|
132
137
|
typ: EnumToken.CommentTokenType,
|
|
133
|
-
val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc +
|
|
138
|
+
val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc + renderValue(curr, { convertColor: false }), "")} */`,
|
|
134
139
|
});
|
|
135
140
|
break;
|
|
136
141
|
}
|