@tbela99/css-parser 1.8.2 → 2.0.1
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 +66 -0
- package/README.md +129 -31
- package/dist/index-umd-web.js +27383 -24083
- package/dist/index.d.ts +2737 -2200
- package/dist/package.json.js +2 -2
- package/dist/src/data/properties.json.js +1 -1
- package/dist/src/data/validation.json.js +1 -1
- package/dist/src/lib/ast/arena-node.js +548 -0
- package/dist/src/lib/ast/arena.js +3198 -0
- package/dist/src/lib/ast/cache.js +333 -0
- package/dist/src/lib/ast/features/calc.js +85 -76
- package/dist/src/lib/ast/features/if.js +155 -115
- package/dist/src/lib/ast/features/index.js +1 -1
- package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
- package/dist/src/lib/ast/features/prefix.js +184 -183
- package/dist/src/lib/ast/features/shorthand.js +40 -46
- package/dist/src/lib/ast/features/transform.js +24 -24
- package/dist/src/lib/ast/features/type.js +1 -1
- package/dist/src/lib/ast/math/expression.js +458 -543
- package/dist/src/lib/ast/math/math.js +19 -41
- package/dist/src/lib/ast/minify.js +590 -647
- package/dist/src/lib/ast/transform/compute.js +82 -85
- package/dist/src/lib/ast/transform/matrix.js +21 -38
- package/dist/src/lib/ast/transform/minify.js +82 -193
- package/dist/src/lib/ast/transform/perspective.js +2 -3
- package/dist/src/lib/ast/transform/rotate.js +1 -1
- package/dist/src/lib/ast/transform/scale.js +1 -1
- package/dist/src/lib/ast/transform/skew.js +1 -1
- package/dist/src/lib/ast/transform/translate.js +1 -1
- package/dist/src/lib/ast/transform/utils.js +2 -2
- package/dist/src/lib/ast/types.js +263 -258
- package/dist/src/lib/ast/walk.js +246 -610
- package/dist/src/lib/fs/resolve.js +1 -1
- package/dist/src/lib/parser/declaration/list.js +223 -139
- package/dist/src/lib/parser/declaration/map.js +251 -238
- package/dist/src/lib/parser/declaration/set.js +74 -66
- package/dist/src/lib/parser/linesmap.js +1 -1
- package/dist/src/lib/parser/parse.js +1379 -1658
- package/dist/src/lib/parser/source.js +7 -4
- package/dist/src/lib/parser/tokenize.js +406 -199
- package/dist/src/lib/parser/utils/angle.js +11 -13
- package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
- package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
- package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
- package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
- package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
- package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
- package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
- package/dist/src/lib/parser/utils/at-rule.js +5 -5
- package/dist/src/lib/parser/utils/base64.js +2 -2
- package/dist/src/lib/parser/utils/cache.js +1 -1
- package/dist/src/lib/parser/utils/config.js +1 -1
- package/dist/src/lib/parser/utils/declaration.js +233 -234
- package/dist/src/lib/parser/utils/eq.js +50 -3
- package/dist/src/lib/parser/utils/hash.js +2 -35
- package/dist/src/lib/parser/utils/selector.js +309 -387
- package/dist/src/lib/parser/utils/text.js +18 -4
- package/dist/src/lib/parser/utils/token.js +16 -69
- package/dist/src/lib/parser/utils/type.js +37 -24
- package/dist/src/lib/printer/render.js +965 -511
- package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
- package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
- package/dist/src/lib/syntax/color/a98rgb.js +1 -1
- package/dist/src/lib/syntax/color/alpha.js +36 -37
- package/dist/src/lib/syntax/color/cmyk.js +35 -52
- package/dist/src/lib/syntax/color/color-mix.js +96 -132
- package/dist/src/lib/syntax/color/color.js +222 -231
- package/dist/src/lib/syntax/color/hex.js +45 -55
- package/dist/src/lib/syntax/color/hsl.js +54 -62
- package/dist/src/lib/syntax/color/hwb.js +60 -68
- package/dist/src/lib/syntax/color/lab.js +59 -67
- package/dist/src/lib/syntax/color/lch.js +59 -67
- package/dist/src/lib/syntax/color/oklab.js +60 -68
- package/dist/src/lib/syntax/color/oklch.js +60 -67
- package/dist/src/lib/syntax/color/p3.js +1 -1
- package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
- package/dist/src/lib/syntax/color/rec2020.js +1 -1
- package/dist/src/lib/syntax/color/relative-color.js +94 -144
- package/dist/src/lib/syntax/color/rgb.js +54 -57
- package/dist/src/lib/syntax/color/srgb.js +55 -53
- package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
- package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
- package/dist/src/lib/syntax/color/utils/components.js +19 -20
- package/dist/src/lib/syntax/color/utils/distance.js +15 -13
- package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
- package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
- package/dist/src/lib/syntax/color/xyz.js +1 -1
- package/dist/src/lib/syntax/color/xyzd50.js +1 -1
- package/dist/src/lib/syntax/constants.js +2 -16
- package/dist/src/lib/syntax/syntax.js +301 -407
- package/dist/src/lib/validation/config.js +1 -1
- package/dist/src/lib/validation/match.js +959 -723
- package/dist/src/lib/validation/parser/parse.js +230 -112
- package/dist/src/lib/validation/parser/typedef.js +1 -1
- package/dist/src/lib/validation/utils/list.js +27 -12
- package/dist/src/node.js +12 -72
- package/dist/src/types.js +1 -1
- package/dist/src/utils/sync.js +9 -6
- package/dist/src/web.js +11 -73
- package/package.json +9 -10
- package/dist/index.cjs +0 -35870
- package/dist/src/lib/ast/clone.js +0 -51
- package/dist/src/lib/ast/expand.js +0 -311
- package/dist/src/lib/ast/find.js +0 -192
- package/dist/src/lib/ast/node.js +0 -59
|
@@ -2,64 +2,116 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.1
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken } from '../types.js';
|
|
9
9
|
import { renderValue } from '../../printer/render.js';
|
|
10
10
|
import { FeatureWalkMode } from './type.js';
|
|
11
|
-
import { PARENT, LOCSRCID, LOCSTA, LOCEND, TOKENS } from '../../syntax/constants.js';
|
|
12
11
|
import { equalsIgnoreCase } from '../../parser/utils/text.js';
|
|
13
|
-
import { replaceNodeOrValue } from '../../parser/utils/token.js';
|
|
14
|
-
import { cloneNode } from '../clone.js';
|
|
15
12
|
import { trimArray } from '../../validation/match.js';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
13
|
+
import { eqToken } from '../../parser/utils/eq.js';
|
|
14
|
+
import { TokenField } from '../arena.js';
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
/**
|
|
17
|
+
* true if the token is an if() function or an if condition
|
|
18
|
+
* @param arena
|
|
19
|
+
* @param value
|
|
20
|
+
*/
|
|
21
|
+
function isIfToken(arena, value) {
|
|
22
|
+
return (arena.dataTypes[value] === EnumToken.IfConditionTokenType ||
|
|
23
|
+
(arena.dataTypes[value] === EnumToken.WildCardFunctionTokenType &&
|
|
24
|
+
equalsIgnoreCase("if", arena.strings[arena.stringValue[value]])));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* deep clone a declaration. the clone map is filled with the cloned value tokens
|
|
28
|
+
* @param arena
|
|
29
|
+
* @param declaration
|
|
30
|
+
* @param nodeMap
|
|
31
|
+
*/
|
|
32
|
+
function cloneDeclaration(arena, declaration, nodeMap) {
|
|
33
|
+
nodeMap.clear();
|
|
34
|
+
return arena.clone(declaration, true, nodeMap);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* create an at-rule wrapping a declaration. the location is inherited from the declaration parent
|
|
38
|
+
* @param arena
|
|
39
|
+
* @param declaration
|
|
40
|
+
* @param parent parent of the declaration when the declaration is detached
|
|
41
|
+
* @param nam
|
|
42
|
+
* @param tokens
|
|
43
|
+
* @param options
|
|
44
|
+
*/
|
|
45
|
+
function createAtRule(arena, declaration, parent, nam, tokens, options = {}) {
|
|
46
|
+
const atRule = arena.createAtRule(EnumToken.AtRuleNodeType, nam, tokens.reduce((acc, curr) => acc + renderValue(arena, curr, options), ""), true, tokens, null, null);
|
|
47
|
+
if (arena.parents[declaration] != 0) {
|
|
48
|
+
parent = arena.parents[declaration];
|
|
49
|
+
}
|
|
50
|
+
if (parent != 0) {
|
|
51
|
+
arena.copyLocation(atRule, parent);
|
|
52
|
+
}
|
|
53
|
+
return atRule;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* first token of the left side of an if condition that is not a comment or a whitespace
|
|
57
|
+
* @param arena
|
|
58
|
+
* @param condition
|
|
59
|
+
*/
|
|
60
|
+
function conditionLeft(arena, condition) {
|
|
61
|
+
return (arena
|
|
62
|
+
.fieldTokens(condition, TokenField.L)
|
|
63
|
+
.find((t) => arena.dataTypes[t] != EnumToken.CommentTokenType &&
|
|
64
|
+
arena.dataTypes[t] != EnumToken.WhitespaceTokenType) ?? null);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* value tokens of an if condition (right side). the trailing semicolon is removed
|
|
68
|
+
* @param arena
|
|
69
|
+
* @param condition
|
|
70
|
+
* @param nodeMap map of the tokens to their clones
|
|
71
|
+
*/
|
|
72
|
+
function conditionValue(arena, condition, nodeMap) {
|
|
73
|
+
let tokens = arena.fieldTokens(condition, TokenField.R);
|
|
74
|
+
if (tokens.length > 0 && arena.dataTypes[tokens[tokens.length - 1]] === EnumToken.SemiColonTokenType) {
|
|
75
|
+
tokens = trimArray(arena, tokens.slice(0, -1));
|
|
76
|
+
}
|
|
77
|
+
return tokens.map((t) => nodeMap.get(t));
|
|
78
|
+
}
|
|
79
|
+
function substituteIfElseNode(arena, declaration, node, wrapper, parentWrapper, cache, parent) {
|
|
22
80
|
const result = [];
|
|
23
|
-
|
|
81
|
+
const nodeMap = new Map();
|
|
24
82
|
let clonedDeclaration;
|
|
25
83
|
let targetParentWrapper = parentWrapper;
|
|
26
84
|
let targetWrapper = wrapper;
|
|
27
|
-
|
|
85
|
+
// map a token or the declaration itself to its clone
|
|
86
|
+
const mapped = (target) => target == declaration ? clonedDeclaration : nodeMap.get(target);
|
|
87
|
+
if (arena.dataTypes[node] === EnumToken.IfElseConditionTokenType) {
|
|
28
88
|
//
|
|
29
|
-
clonedDeclaration =
|
|
89
|
+
clonedDeclaration = cloneDeclaration(arena, declaration, nodeMap);
|
|
90
|
+
const elseCondition = arena.field(node, TokenField.R);
|
|
30
91
|
// replace else: ... with the actual value
|
|
31
|
-
if (
|
|
32
|
-
const target =
|
|
92
|
+
if (arena.dataTypes[elseCondition] === EnumToken.IfConditionTokenType) {
|
|
93
|
+
const target = conditionLeft(arena, elseCondition);
|
|
33
94
|
//
|
|
34
|
-
if (target
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
let index = targetParentWrapper.chi.indexOf(targetWrapper);
|
|
95
|
+
if (target != null &&
|
|
96
|
+
arena.dataTypes[target] === EnumToken.IdenTokenType &&
|
|
97
|
+
equalsIgnoreCase("else", arena.strings[arena.stringValue[target]])) {
|
|
98
|
+
arena.replaceToken(mapped(targetWrapper), ...conditionValue(arena, elseCondition, nodeMap));
|
|
99
|
+
if (targetParentWrapper != declaration) {
|
|
100
|
+
const siblings = arena.tokens(targetParentWrapper);
|
|
101
|
+
let index = siblings.indexOf(targetWrapper);
|
|
42
102
|
if (index != -1) {
|
|
43
103
|
let i;
|
|
44
|
-
let k;
|
|
45
104
|
let siblingWrapper;
|
|
46
|
-
let left =
|
|
47
|
-
for (i = index + 1; i <
|
|
48
|
-
if (
|
|
49
|
-
siblingWrapper =
|
|
50
|
-
for (
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
.r.r.at(-1)?.typ === EnumToken.SemiColonTokenType
|
|
57
|
-
? trimArray(siblingWrapper.chi[k]
|
|
58
|
-
.r.r.slice(0, -1))
|
|
59
|
-
: siblingWrapper.chi[k]
|
|
60
|
-
.r.r);
|
|
61
|
-
// @ts-ignore
|
|
62
|
-
cache.add(siblingWrapper.chi[k].l);
|
|
105
|
+
let left = conditionLeft(arena, arena.field(node, TokenField.L));
|
|
106
|
+
for (i = index + 1; i < siblings.length; i++) {
|
|
107
|
+
if (arena.dataTypes[siblings[i]] === arena.dataTypes[targetWrapper]) {
|
|
108
|
+
siblingWrapper = siblings[i];
|
|
109
|
+
for (const child of arena.tokens(siblingWrapper)) {
|
|
110
|
+
if (arena.dataTypes[child] === EnumToken.IfElseConditionTokenType) {
|
|
111
|
+
const leftSide = conditionLeft(arena, arena.field(child, TokenField.L));
|
|
112
|
+
if (left != null && leftSide != null && eqToken(arena, left, leftSide)) {
|
|
113
|
+
arena.replaceToken(mapped(siblings[i]), ...conditionValue(arena, arena.field(child, TokenField.R), nodeMap));
|
|
114
|
+
cache.add(arena.field(child, TokenField.L));
|
|
63
115
|
}
|
|
64
116
|
}
|
|
65
117
|
}
|
|
@@ -70,112 +122,100 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
70
122
|
}
|
|
71
123
|
}
|
|
72
124
|
result.push(clonedDeclaration);
|
|
73
|
-
nodeMap
|
|
74
|
-
|
|
75
|
-
replaceNodeOrValue(nodeMap.get(targetParentWrapper), nodeMap.get(targetWrapper), node.l);
|
|
125
|
+
clonedDeclaration = cloneDeclaration(arena, declaration, nodeMap);
|
|
126
|
+
arena.replaceToken(mapped(targetWrapper), mapped(arena.field(node, TokenField.L)));
|
|
76
127
|
result.push(clonedDeclaration);
|
|
77
128
|
}
|
|
78
|
-
else if (node
|
|
79
|
-
const left =
|
|
129
|
+
else if (arena.dataTypes[node] === EnumToken.IfConditionTokenType) {
|
|
130
|
+
const left = conditionLeft(arena, node);
|
|
80
131
|
if (left == null) {
|
|
81
132
|
return result;
|
|
82
133
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
nodeMap.get(targetWrapper.typ === EnumToken.DeclarationNodeType ? node : targetWrapper), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType ? trimArray(node.r.slice(0, -1)) : node.r);
|
|
134
|
+
const leftType = arena.dataTypes[left];
|
|
135
|
+
if (leftType === EnumToken.IdenTokenType && equalsIgnoreCase("else", arena.strings[arena.stringValue[left]])) {
|
|
136
|
+
clonedDeclaration = cloneDeclaration(arena, declaration, nodeMap);
|
|
137
|
+
arena.replaceToken(mapped(targetWrapper == declaration ? node : targetWrapper), ...conditionValue(arena, node, nodeMap));
|
|
88
138
|
result.push(clonedDeclaration);
|
|
89
139
|
}
|
|
90
|
-
else if (
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (declaration[PARENT] != null) {
|
|
97
|
-
atRule[LOCSRCID] = declaration[PARENT][LOCSRCID];
|
|
98
|
-
atRule[LOCSTA] = declaration[PARENT][LOCSTA];
|
|
99
|
-
atRule[LOCEND] = declaration[PARENT][LOCEND];
|
|
100
|
-
}
|
|
101
|
-
atRule[TOKENS] = [{ typ: EnumToken.ParensTokenType, chi: left.chi.slice() }];
|
|
102
|
-
const minify = atRule.nam !== "supports";
|
|
103
|
-
const options = {
|
|
140
|
+
else if (leftType === EnumToken.WhenElseFunctionTokenType) {
|
|
141
|
+
const name = arena.strings[arena.stringValue[left]];
|
|
142
|
+
const minify = name !== "supports";
|
|
143
|
+
const atRule = createAtRule(arena, declaration, parent, name, [
|
|
144
|
+
arena.createList(EnumToken.ParensTokenType, arena.tokens(left).map((t) => arena.clone(t, true))),
|
|
145
|
+
], {
|
|
104
146
|
minify,
|
|
105
147
|
convertColor: minify,
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
clonedDeclaration
|
|
111
|
-
atRule.chi.push(clonedDeclaration);
|
|
148
|
+
});
|
|
149
|
+
clonedDeclaration = cloneDeclaration(arena, declaration, nodeMap);
|
|
150
|
+
arena.replaceToken(mapped(node), ...conditionValue(arena, node, nodeMap));
|
|
151
|
+
// inlined: arena.appendChildren(atRule, clonedDeclaration)
|
|
152
|
+
arena.appendChildList(atRule, [clonedDeclaration]);
|
|
112
153
|
result.push(atRule);
|
|
113
|
-
processNode(clonedDeclaration, cache);
|
|
154
|
+
processNode(arena, clonedDeclaration, cache, parent);
|
|
114
155
|
}
|
|
115
|
-
else if (
|
|
116
|
-
const atRule =
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
atRule
|
|
122
|
-
atRule.val = atRule[TOKENS].reduce((acc, curr) => acc + renderValue(curr), "");
|
|
123
|
-
if (declaration[PARENT] != null) {
|
|
124
|
-
atRule[LOCSRCID] = declaration[PARENT][LOCSRCID];
|
|
125
|
-
atRule[LOCSTA] = declaration[PARENT][LOCSTA];
|
|
126
|
-
atRule[LOCEND] = declaration[PARENT][LOCEND];
|
|
127
|
-
}
|
|
128
|
-
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
129
|
-
replaceNodeOrValue(nodeMap.get(targetWrapper.typ === EnumToken.WildCardFunctionTokenType ? targetParentWrapper : targetWrapper), nodeMap.get(targetWrapper.typ === EnumToken.WildCardFunctionTokenType ? targetWrapper : node), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType
|
|
130
|
-
? trimArray(node.r.slice(0, -1))
|
|
131
|
-
: node.r);
|
|
132
|
-
clonedDeclaration[PARENT] = atRule;
|
|
133
|
-
atRule.chi.push(clonedDeclaration);
|
|
156
|
+
else if (leftType === EnumToken.ContainerFunctionTokenType) {
|
|
157
|
+
const atRule = createAtRule(arena, declaration, parent, "container", [arena.clone(left, true)]);
|
|
158
|
+
clonedDeclaration = cloneDeclaration(arena, declaration, nodeMap);
|
|
159
|
+
const isWildCard = targetWrapper != declaration && arena.dataTypes[targetWrapper] === EnumToken.WildCardFunctionTokenType;
|
|
160
|
+
arena.replaceToken(mapped(isWildCard ? targetWrapper : node), ...conditionValue(arena, node, nodeMap));
|
|
161
|
+
// inlined: arena.appendChildren(atRule, clonedDeclaration)
|
|
162
|
+
arena.appendChildList(atRule, [clonedDeclaration]);
|
|
134
163
|
result.push(atRule);
|
|
135
|
-
processNode(clonedDeclaration, cache);
|
|
164
|
+
processNode(arena, clonedDeclaration, cache, parent);
|
|
136
165
|
}
|
|
137
166
|
}
|
|
138
167
|
return result;
|
|
139
168
|
}
|
|
140
|
-
|
|
169
|
+
/**
|
|
170
|
+
* expand if() conditions into declarations / at-rules
|
|
171
|
+
* @param arena
|
|
172
|
+
* @param declarationNode
|
|
173
|
+
* @param cache
|
|
174
|
+
* @param parent parent of the declaration when the declaration is detached
|
|
175
|
+
*/
|
|
176
|
+
function processNode(arena, declarationNode, cache, parent = 0) {
|
|
141
177
|
let i;
|
|
142
178
|
let k = -1;
|
|
143
179
|
let astNode;
|
|
144
180
|
const result = [];
|
|
145
181
|
const stack = [declarationNode];
|
|
182
|
+
// the generated nodes are detached: remember the parent for the location of the generated at-rules
|
|
183
|
+
if (arena.parents[declarationNode] != 0) {
|
|
184
|
+
parent = arena.parents[declarationNode];
|
|
185
|
+
}
|
|
146
186
|
while (++k < stack.length) {
|
|
147
187
|
astNode = stack[k];
|
|
148
|
-
const
|
|
149
|
-
if (
|
|
188
|
+
const found = arena.findByValue(astNode, isIfToken);
|
|
189
|
+
if (found == null) {
|
|
150
190
|
result.push(astNode);
|
|
151
191
|
continue;
|
|
152
192
|
}
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
declaration, node.node.chi[i], node.node, parentWrapper, cache));
|
|
193
|
+
const declaration = found.node;
|
|
194
|
+
const value = found.value;
|
|
195
|
+
// the parent of a detached token is the declaration
|
|
196
|
+
const parentWrapper = arena.getValueParent(value) || declaration;
|
|
197
|
+
const grandParent = parentWrapper == declaration ? declaration : arena.getValueParent(parentWrapper) || declaration;
|
|
198
|
+
if (arena.dataTypes[value] === EnumToken.WildCardFunctionTokenType) {
|
|
199
|
+
const conditions = arena.tokens(value);
|
|
200
|
+
for (i = 0; i < conditions.length; i++) {
|
|
201
|
+
stack.push(...substituteIfElseNode(arena, declaration, conditions[i], value, parentWrapper, cache, parent));
|
|
163
202
|
}
|
|
164
203
|
}
|
|
165
204
|
else {
|
|
166
|
-
stack.push(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
205
|
+
stack.push(...substituteIfElseNode(arena, declaration, value, parentWrapper, grandParent, cache, parent));
|
|
206
|
+
}
|
|
207
|
+
// the node is substituted: release it unless it is the node being replaced
|
|
208
|
+
if (astNode != declarationNode) {
|
|
209
|
+
arena.detach(astNode);
|
|
171
210
|
}
|
|
172
211
|
}
|
|
173
|
-
if (result.length > 0) {
|
|
174
|
-
|
|
175
|
-
|
|
212
|
+
if (result.length > 0 && arena.parents[declarationNode] != 0) {
|
|
213
|
+
arena.replaceChild(declarationNode, ...result);
|
|
214
|
+
if (!result.includes(declarationNode)) {
|
|
215
|
+
arena.detach(declarationNode);
|
|
216
|
+
}
|
|
176
217
|
}
|
|
177
218
|
// else remove node?
|
|
178
|
-
// @ts-expect-error
|
|
179
219
|
return result;
|
|
180
220
|
}
|
|
181
221
|
class ExpandIfFeature {
|
|
@@ -192,8 +232,8 @@ class ExpandIfFeature {
|
|
|
192
232
|
options.features.push(new ExpandIfFeature());
|
|
193
233
|
}
|
|
194
234
|
}
|
|
195
|
-
run(declaration) {
|
|
196
|
-
return processNode(declaration, new Set());
|
|
235
|
+
run(arena, declaration) {
|
|
236
|
+
return processNode(arena, declaration, new Set());
|
|
197
237
|
}
|
|
198
238
|
}
|
|
199
239
|
|
|
@@ -2,61 +2,61 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.1
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
|
-
import { EnumToken } from '../types.js';
|
|
9
|
-
import { walkValues } from '../walk.js';
|
|
8
|
+
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
10
9
|
import { renderValue } from '../../printer/render.js';
|
|
11
10
|
import { splitRule } from '../minify.js';
|
|
12
11
|
import { FeatureWalkMode } from './type.js';
|
|
13
|
-
import {
|
|
12
|
+
import { mathFuncs } from '../../syntax/constants.js';
|
|
13
|
+
import { TokenField } from '../arena.js';
|
|
14
|
+
import { ValueWalker } from '../walk.js';
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
/**
|
|
17
|
+
* inline a binary expression into a parens token: (l op r)
|
|
18
|
+
* @param arena
|
|
19
|
+
* @param token
|
|
20
|
+
*/
|
|
21
|
+
function inlineExpression(arena, token) {
|
|
16
22
|
const result = [];
|
|
17
|
-
if (token
|
|
18
|
-
const chi = inlineExpression(token.
|
|
19
|
-
chi.push(
|
|
20
|
-
for (const child of inlineExpression(token.
|
|
23
|
+
if (arena.dataTypes[token] == EnumToken.BinaryExpressionTokenType) {
|
|
24
|
+
const chi = inlineExpression(arena, arena.field(token, TokenField.L));
|
|
25
|
+
chi.push(arena.createToken(arena.numericValue[token]));
|
|
26
|
+
for (const child of inlineExpression(arena, arena.field(token, TokenField.R))) {
|
|
21
27
|
chi.push(child);
|
|
22
28
|
}
|
|
23
|
-
result.push(
|
|
24
|
-
typ: EnumToken.ParensTokenType,
|
|
25
|
-
chi,
|
|
26
|
-
});
|
|
29
|
+
result.push(arena.createList(EnumToken.ParensTokenType, chi));
|
|
27
30
|
}
|
|
28
31
|
else {
|
|
29
32
|
result.push(token);
|
|
30
33
|
}
|
|
31
34
|
return result;
|
|
32
35
|
}
|
|
33
|
-
function replace(node, variableScope) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
function replace(arena, node, variableScope) {
|
|
37
|
+
// inlined: arena.walkValues(node)
|
|
38
|
+
for (const value of new ValueWalker(arena, node)) {
|
|
39
|
+
// the parent of a detached token is the declaration
|
|
40
|
+
const parentValue = arena.getValueParent(value) || node;
|
|
41
|
+
if (arena.dataTypes[value] == EnumToken.BinaryExpressionTokenType &&
|
|
42
|
+
parentValue != null &&
|
|
43
|
+
parentValue != node &&
|
|
44
|
+
// the token belongs to the parent token list (function arguments)
|
|
45
|
+
arena.parents[value] == parentValue) {
|
|
46
|
+
arena.replaceToken(value, ...inlineExpression(arena, value));
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (source[i] == value) {
|
|
52
|
-
source.splice(i, 1, ...info.node.val);
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
node.val = info.node.val.slice();
|
|
59
|
-
}
|
|
49
|
+
// inlined: arena.walkValues(node)
|
|
50
|
+
for (const value of new ValueWalker(arena, node)) {
|
|
51
|
+
const typ = arena.dataTypes[value];
|
|
52
|
+
if (typ == EnumToken.MathFunctionTokenType ||
|
|
53
|
+
(typ == EnumToken.WildCardFunctionTokenType && arena.strings[arena.stringValue[value]] == "var")) {
|
|
54
|
+
const first = arena.firstToken[value];
|
|
55
|
+
if (first != 0 && arena.nextSibling[first] == 0 && arena.dataTypes[first] == EnumToken.DashedIdenTokenType) {
|
|
56
|
+
const info = (variableScope.get(arena.strings[arena.stringValue[first]]));
|
|
57
|
+
// the variable declaration may have been released
|
|
58
|
+
if (info?.replaceable && arena.name[info.node] != 0 && arena.parents[value] != 0) {
|
|
59
|
+
arena.replaceToken(value, ...arena.tokens(info.node).map((t) => arena.clone(t, true)));
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
@@ -76,78 +76,71 @@ class InlineCssVariablesFeature {
|
|
|
76
76
|
options.features.push(new InlineCssVariablesFeature());
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
|
-
run(ast,
|
|
80
|
-
if (!(
|
|
79
|
+
run(arena, ast, _options = {}, parent, context) {
|
|
80
|
+
if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren)) {
|
|
81
81
|
return null;
|
|
82
82
|
}
|
|
83
83
|
if (!("variableScope" in context)) {
|
|
84
84
|
context.variableScope = new Map();
|
|
85
85
|
}
|
|
86
86
|
// [':root', 'html']
|
|
87
|
-
const isRoot = parent
|
|
88
|
-
ast
|
|
89
|
-
(ast
|
|
87
|
+
const isRoot = arena.dataTypes[parent] == EnumToken.StyleSheetNodeType &&
|
|
88
|
+
arena.dataTypes[ast] == EnumToken.RuleNodeType &&
|
|
89
|
+
(arena.raw[ast] ?? splitRule(arena.strings[arena.name[ast]])).some((segment) => segment.some((s) => s == ":root" || s == "html"));
|
|
90
90
|
const variableScope = context.variableScope;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (node.typ !== EnumToken.DeclarationNodeType) {
|
|
91
|
+
for (let node = arena.firstChild[ast]; node != 0; node = arena.nextSibling[node]) {
|
|
92
|
+
if (arena.dataTypes[node] !== EnumToken.DeclarationNodeType) {
|
|
94
93
|
continue;
|
|
95
94
|
}
|
|
95
|
+
const name = arena.strings[arena.name[node]];
|
|
96
96
|
// css variable
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
if (recursive) {
|
|
121
|
-
replace(node, variableScope);
|
|
97
|
+
if (name.startsWith("--")) {
|
|
98
|
+
// the last declaration wins
|
|
99
|
+
const info = {
|
|
100
|
+
globalScope: isRoot,
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
parent: new Set(),
|
|
103
|
+
declarationCount: 1,
|
|
104
|
+
replaceable: isRoot,
|
|
105
|
+
node: node,
|
|
106
|
+
values: arena.tokens(node).map((t) => arena.clone(t, true)),
|
|
107
|
+
};
|
|
108
|
+
info.parent.add(ast);
|
|
109
|
+
variableScope.set(name, info);
|
|
110
|
+
let recursive = false;
|
|
111
|
+
// inlined: arena.walkValues(node)
|
|
112
|
+
for (const value of new ValueWalker(arena, node)) {
|
|
113
|
+
if (arena.dataTypes[value] == EnumToken.MathFunctionTokenDefType ||
|
|
114
|
+
(arena.dataTypes[value] == EnumToken.WildCardFunctionTokenType &&
|
|
115
|
+
(mathFuncs.includes(arena.strings[arena.stringValue[value]]) ||
|
|
116
|
+
arena.strings[arena.stringValue[value]] === "var"))) {
|
|
117
|
+
recursive = true;
|
|
118
|
+
break;
|
|
122
119
|
}
|
|
123
120
|
}
|
|
121
|
+
if (recursive) {
|
|
122
|
+
replace(arena, node, variableScope);
|
|
123
|
+
}
|
|
124
124
|
}
|
|
125
125
|
else {
|
|
126
|
-
replace(node, variableScope);
|
|
126
|
+
replace(arena, node, variableScope);
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
return null;
|
|
130
130
|
}
|
|
131
|
-
cleanup(
|
|
131
|
+
cleanup(arena, _ast, _options = {}, context) {
|
|
132
132
|
const variableScope = context.variableScope;
|
|
133
133
|
if (variableScope == null) {
|
|
134
134
|
return;
|
|
135
135
|
}
|
|
136
136
|
for (const info of variableScope.values()) {
|
|
137
137
|
if (info.replaceable) {
|
|
138
|
-
let i;
|
|
139
138
|
// drop declarations from :root{}
|
|
140
139
|
for (const parent of info.parent) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
parent.chi.splice(i, 1, {
|
|
146
|
-
typ: EnumToken.CommentTokenType,
|
|
147
|
-
val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc + renderValue(curr, { convertColor: false }), "")} */`,
|
|
148
|
-
});
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
140
|
+
if (arena.parents[info.node] == parent) {
|
|
141
|
+
const comment = arena.createComment(EnumToken.CommentTokenType, `/* ${arena.strings[arena.name[info.node]]}: ${info.values.reduce((acc, curr) => acc + renderValue(arena, curr, { convertColor: false }), "")} */`);
|
|
142
|
+
arena.replaceChild(info.node, comment);
|
|
143
|
+
arena.detach(info.node);
|
|
151
144
|
}
|
|
152
145
|
}
|
|
153
146
|
}
|