@tbela99/css-parser 1.8.2 → 2.0.0
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 +62 -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/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/fingerprint.js +333 -0
- 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,15 +2,15 @@
|
|
|
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.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import { TokenField, numericTokenTypes } from '../arena.js';
|
|
9
|
+
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
10
10
|
import { evaluate } from '../math/expression.js';
|
|
11
11
|
import { FeatureWalkMode } from './type.js';
|
|
12
|
-
import { tokensfuncSet, mathFuncs
|
|
13
|
-
import {
|
|
12
|
+
import { tokensfuncSet, mathFuncs } from '../../syntax/constants.js';
|
|
13
|
+
import { ValueWalker } from '../walk.js';
|
|
14
14
|
|
|
15
15
|
class ComputeCalcExpressionFeature {
|
|
16
16
|
accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType]);
|
|
@@ -26,98 +26,79 @@ class ComputeCalcExpressionFeature {
|
|
|
26
26
|
options.features.push(new ComputeCalcExpressionFeature());
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
run(ast) {
|
|
30
|
-
|
|
31
|
-
if (ast.chi == null) {
|
|
29
|
+
run(arena, ast) {
|
|
30
|
+
if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren)) {
|
|
32
31
|
return null;
|
|
33
32
|
}
|
|
34
|
-
for (
|
|
35
|
-
if (node
|
|
33
|
+
for (let node = arena.firstChild[ast]; node != 0; node = arena.nextSibling[node]) {
|
|
34
|
+
if (arena.dataTypes[node] != EnumToken.DeclarationNodeType) {
|
|
36
35
|
continue;
|
|
37
36
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
if (!hasExpression(arena, node)) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
// function tokens already evaluated, created on demand
|
|
41
|
+
let set = null;
|
|
42
|
+
// inlined: arena.walkValues(node)
|
|
43
|
+
for (const value of new ValueWalker(arena, node)) {
|
|
44
|
+
// the parent of a detached token is the declaration
|
|
45
|
+
const parent = arena.getValueParent(value) || node;
|
|
46
|
+
if (parent != null && arena.dataTypes[parent] == EnumToken.BinaryExpressionTokenType) {
|
|
41
47
|
continue;
|
|
42
48
|
}
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
const typ = arena.dataTypes[value];
|
|
50
|
+
if (typ == EnumToken.BinaryExpressionTokenType) {
|
|
51
|
+
const result = evaluate(arena, [value]);
|
|
45
52
|
try {
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
if (result.length > 0) {
|
|
54
|
+
arena.replaceToken(value, ...result);
|
|
55
|
+
}
|
|
48
56
|
}
|
|
49
57
|
catch (e) { }
|
|
50
58
|
continue;
|
|
51
59
|
}
|
|
52
|
-
if (
|
|
60
|
+
if (tokensfuncSet.has(typ)) {
|
|
61
|
+
set ??= new Set();
|
|
53
62
|
if (!set.has(value)) {
|
|
54
63
|
set.add(value);
|
|
55
64
|
if (parent != null) {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
const name = arena.strings[arena.stringValue[value]];
|
|
66
|
+
const shouldEvaluate = typ === EnumToken.MathFunctionTokenType ||
|
|
67
|
+
(typ == EnumToken.FunctionTokenType && mathFuncs.includes(name));
|
|
59
68
|
if (shouldEvaluate) {
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
? [value]
|
|
63
|
-
: value.typ == EnumToken.DeclarationNodeType
|
|
64
|
-
? value.val
|
|
65
|
-
: value.chi;
|
|
66
|
-
const values = evaluate(cp);
|
|
69
|
+
const cp = name != "calc" ? [value] : arena.tokens(value);
|
|
70
|
+
const values = evaluate(arena, cp);
|
|
67
71
|
// fix a + -b into a - b
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
// inlined: arena.walkValues(values)
|
|
73
|
+
for (const value of new ValueWalker(arena, values)) {
|
|
74
|
+
if (arena.dataTypes[value] === EnumToken.BinaryExpressionTokenType) {
|
|
75
|
+
// inlined: arena.getRight(value)
|
|
76
|
+
const r = (arena.getChildAt(value, TokenField.R) == null ? null : arena.fieldTokens(value, TokenField.R))[0];
|
|
77
|
+
if (arena.numericValue[value] === EnumToken.Add &&
|
|
78
|
+
// inlined: arena.isNumeric(r)
|
|
79
|
+
numericTokenTypes.has(arena.dataTypes[r]) &&
|
|
80
|
+
Math.sign(arena.numericValue[r]) == -1) {
|
|
81
|
+
arena.numericValue[value] = EnumToken.Sub;
|
|
82
|
+
arena.numericValue[r] *= -1;
|
|
76
83
|
}
|
|
77
84
|
}
|
|
78
85
|
}
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
(values[0].typ == EnumToken.FunctionTokenType &&
|
|
93
|
-
mathFuncs.includes(values[0].val)))
|
|
94
|
-
? {
|
|
95
|
-
typ: EnumToken.MathFunctionTokenType,
|
|
96
|
-
val: "calc",
|
|
97
|
-
chi: values,
|
|
98
|
-
[LOCSRCID]: value[LOCSRCID],
|
|
99
|
-
[LOCSTA]: value[LOCSTA],
|
|
100
|
-
[LOCEND]: value[LOCEND],
|
|
101
|
-
}
|
|
102
|
-
: values[0]);
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
86
|
+
// the value is replaced by the computed tokens or wrapped into a calc() function
|
|
87
|
+
if (values.length == 1 &&
|
|
88
|
+
arena.dataTypes[values[0]] != EnumToken.BinaryExpressionTokenType) {
|
|
89
|
+
const parentType = arena.dataTypes[parent];
|
|
90
|
+
const wrap = !(parentType === EnumToken.MathFunctionTokenType ||
|
|
91
|
+
(parentType == EnumToken.FunctionTokenType &&
|
|
92
|
+
arena.strings[arena.stringValue[parent]] == "calc")) &&
|
|
93
|
+
// inlined: arena.isNumeric(values[0])
|
|
94
|
+
(!numericTokenTypes.has(arena.dataTypes[values[0]]) || arena.hasFraction(values[0])) &&
|
|
95
|
+
!(arena.dataTypes[values[0]] == EnumToken.MathFunctionTokenType ||
|
|
96
|
+
(arena.dataTypes[values[0]] == EnumToken.FunctionTokenType &&
|
|
97
|
+
mathFuncs.includes(arena.strings[arena.stringValue[values[0]]])));
|
|
98
|
+
arena.replaceToken(value, wrap ? createCalc(arena, value, values) : values[0]);
|
|
106
99
|
}
|
|
107
100
|
else {
|
|
108
|
-
|
|
109
|
-
if (children[i] == value) {
|
|
110
|
-
children.splice(i, 1, {
|
|
111
|
-
typ: EnumToken.MathFunctionTokenType,
|
|
112
|
-
val: "calc",
|
|
113
|
-
chi: values,
|
|
114
|
-
[LOCSRCID]: value[LOCSRCID],
|
|
115
|
-
[LOCSTA]: value[LOCSTA],
|
|
116
|
-
[LOCEND]: value[LOCEND],
|
|
117
|
-
});
|
|
118
|
-
break;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
101
|
+
arena.replaceToken(value, createCalc(arena, value, values));
|
|
121
102
|
}
|
|
122
103
|
}
|
|
123
104
|
}
|
|
@@ -128,5 +109,33 @@ class ComputeCalcExpressionFeature {
|
|
|
128
109
|
return null;
|
|
129
110
|
}
|
|
130
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* wrap tokens into a calc() function
|
|
114
|
+
* @param arena
|
|
115
|
+
* @param source token the location is copied from
|
|
116
|
+
* @param values
|
|
117
|
+
*/
|
|
118
|
+
function createCalc(arena, source, values) {
|
|
119
|
+
const calc = arena.createFunction(EnumToken.MathFunctionTokenType, "calc", values);
|
|
120
|
+
arena.copyLocation(calc, source);
|
|
121
|
+
return calc;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* true if a value token of the declaration is a function, an expression or has children: the values of the other
|
|
125
|
+
* declarations are not walked
|
|
126
|
+
* @param arena
|
|
127
|
+
* @param node
|
|
128
|
+
*/
|
|
129
|
+
function hasExpression(arena, node) {
|
|
130
|
+
for (let token = arena.firstToken[node]; token != 0; token = arena.nextSibling[token]) {
|
|
131
|
+
if (arena.dataTypes[token] == EnumToken.BinaryExpressionTokenType ||
|
|
132
|
+
tokensfuncSet.has(arena.dataTypes[token]) ||
|
|
133
|
+
arena.firstToken[token] != 0 ||
|
|
134
|
+
arena.firstChild[token] != 0) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
131
140
|
|
|
132
141
|
export { ComputeCalcExpressionFeature };
|
|
@@ -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.0
|
|
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
|
|