@tbela99/css-parser 1.4.11 → 1.6.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 +39 -0
- package/README.md +18 -11
- package/dist/index-umd-web.js +6558 -5310
- package/dist/index.cjs +6576 -5313
- package/dist/index.d.ts +805 -279
- package/dist/lib/ast/clone.js +13 -8
- package/dist/lib/ast/expand.js +65 -15
- package/dist/lib/ast/features/calc.js +19 -56
- package/dist/lib/ast/features/if.js +28 -4
- package/dist/lib/ast/features/inlinecssvariables.js +6 -5
- package/dist/lib/ast/features/prefix.js +52 -73
- package/dist/lib/ast/features/shorthand.js +11 -6
- package/dist/lib/ast/features/transform.js +6 -3
- package/dist/lib/ast/find.js +7 -5
- package/dist/lib/ast/math/expression.js +190 -32
- package/dist/lib/ast/minify.js +117 -49
- package/dist/lib/ast/node.js +52 -0
- package/dist/lib/ast/transform/compute.js +63 -4
- package/dist/lib/ast/transform/minify.js +6 -6
- 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 +46 -18
- package/dist/lib/ast/types.js +1 -1
- package/dist/lib/ast/walk.js +55 -11
- package/dist/lib/fs/resolve.js +68 -27
- package/dist/lib/parser/declaration/list.js +22 -7
- package/dist/lib/parser/declaration/map.js +40 -13
- package/dist/lib/parser/declaration/set.js +3 -1
- package/dist/lib/parser/linesmap.js +2 -12
- package/dist/lib/parser/parse.js +843 -912
- package/dist/lib/parser/source.js +16 -2
- package/dist/lib/parser/tokenize.js +1489 -725
- package/dist/lib/parser/utils/at-rule-container.js +29 -196
- package/dist/lib/parser/utils/at-rule-generic.js +6 -7
- package/dist/lib/parser/utils/at-rule-import.js +17 -13
- package/dist/lib/parser/utils/at-rule-media.js +36 -19
- package/dist/lib/parser/utils/at-rule-support.js +19 -9
- package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
- package/dist/lib/parser/utils/at-rule.js +0 -18
- package/dist/lib/parser/utils/declaration.js +30 -95
- package/dist/lib/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +164 -141
- package/dist/lib/parser/utils/text.js +4 -2
- package/dist/lib/renderer/render.js +230 -125
- package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +187 -34
- package/dist/lib/syntax/color/a98rgb.js +14 -4
- package/dist/lib/syntax/color/cmyk.js +10 -18
- package/dist/lib/syntax/color/color-mix.js +21 -34
- package/dist/lib/syntax/color/color.js +32 -49
- package/dist/lib/syntax/color/hsl.js +15 -15
- package/dist/lib/syntax/color/hwb.js +18 -17
- package/dist/lib/syntax/color/lab.js +17 -17
- package/dist/lib/syntax/color/lch.js +13 -13
- package/dist/lib/syntax/color/oklab.js +10 -11
- package/dist/lib/syntax/color/oklch.js +15 -18
- package/dist/lib/syntax/color/p3.js +18 -12
- package/dist/lib/syntax/color/prophotorgb.js +26 -28
- package/dist/lib/syntax/color/rec2020.js +8 -4
- package/dist/lib/syntax/color/relative-color.js +26 -10
- package/dist/lib/syntax/color/srgb.js +5 -4
- package/dist/lib/syntax/color/utils/distance.js +1 -1
- package/dist/lib/syntax/color/xyz.js +2 -2
- package/dist/lib/syntax/color/xyzd50.js +2 -2
- package/dist/lib/syntax/constants.js +12 -2
- package/dist/lib/syntax/syntax.js +45 -258
- package/dist/lib/validation/config.json.js +3 -0
- package/dist/lib/validation/match.js +110 -27
- package/dist/node.js +55 -33
- package/dist/types.d.ts +5 -1
- package/dist/types.js +4 -0
- package/dist/utils/sync.d.ts +16 -0
- package/dist/utils/sync.js +73 -0
- package/dist/web.js +37 -30
- package/package.json +1 -1
- package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
- package/llms.txt +0 -73
package/dist/lib/ast/clone.js
CHANGED
|
@@ -14,23 +14,28 @@ function cloneNode(node, cloneChildren = false, cloneMap = null) {
|
|
|
14
14
|
cloneMap?.set?.(node, clone);
|
|
15
15
|
for (const [name, value] of Object.entries(node)) {
|
|
16
16
|
if (value == null || typeof value != "object") {
|
|
17
|
+
// @ts-ignore
|
|
17
18
|
clone[name] = value;
|
|
18
19
|
}
|
|
19
20
|
else if (Array.isArray(value)) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
clone[name] = [];
|
|
23
|
+
if (cloneChildren || name !== checkNode) {
|
|
24
|
+
for (const c of value) {
|
|
25
|
+
const newObj = cloneNode(c, cloneChildren, cloneMap);
|
|
26
|
+
cloneMap?.set?.(c, newObj);
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
clone[name].push(newObj);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
28
31
|
}
|
|
29
32
|
else {
|
|
33
|
+
// @ts-ignore
|
|
30
34
|
clone[name] = { ...value };
|
|
31
35
|
}
|
|
32
36
|
}
|
|
33
37
|
for (const symbol of Object.getOwnPropertySymbols(node)) {
|
|
38
|
+
// @ts-ignore
|
|
34
39
|
clone[symbol] = node[symbol];
|
|
35
40
|
}
|
|
36
41
|
return clone;
|
package/dist/lib/ast/expand.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { splitRule } from './minify.js';
|
|
2
|
-
import { combinators, RAW } from '../syntax/constants.js';
|
|
2
|
+
import { STATE, PARENT, combinators, RAW } from '../syntax/constants.js';
|
|
3
3
|
import { parseString } from '../parser/parse.js';
|
|
4
4
|
import { walkValues } from './walk.js';
|
|
5
5
|
import { renderValue } from '../renderer/render.js';
|
|
6
|
-
import { EnumToken } from './types.js';
|
|
6
|
+
import { EnumAstNodeStatus, EnumToken } from './types.js';
|
|
7
|
+
import { cloneNode } from './clone.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* expand css nesting ast nodes
|
|
@@ -12,12 +13,23 @@ import { EnumToken } from './types.js';
|
|
|
12
13
|
* @private
|
|
13
14
|
*/
|
|
14
15
|
function expand(ast) {
|
|
15
|
-
|
|
16
|
+
if (ast[STATE] == EnumAstNodeStatus.Invalid ||
|
|
17
|
+
ast[STATE] == EnumAstNodeStatus.Disallowed ||
|
|
18
|
+
ast[STATE] == EnumAstNodeStatus.Unknown ||
|
|
19
|
+
ast[STATE] == EnumAstNodeStatus.Unparsed ||
|
|
20
|
+
ast[STATE] == EnumAstNodeStatus.Malformed) {
|
|
21
|
+
return ast;
|
|
22
|
+
}
|
|
23
|
+
const result = Object.assign(cloneNode(ast), { chi: [] });
|
|
24
|
+
let children;
|
|
16
25
|
for (let i = 0; i < ast.chi.length; i++) {
|
|
17
|
-
|
|
26
|
+
let node = ast.chi[i];
|
|
18
27
|
if (node.typ === EnumToken.RuleNodeType) {
|
|
19
|
-
|
|
20
|
-
|
|
28
|
+
children = expandRule(node);
|
|
29
|
+
for (const child of children) {
|
|
30
|
+
child[PARENT] = result;
|
|
31
|
+
result.chi.push(child);
|
|
32
|
+
}
|
|
21
33
|
}
|
|
22
34
|
else if (node.typ == EnumToken.AtRuleNodeType && "chi" in node) {
|
|
23
35
|
let hasRule = false;
|
|
@@ -29,10 +41,23 @@ function expand(ast) {
|
|
|
29
41
|
break;
|
|
30
42
|
}
|
|
31
43
|
}
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
if (hasRule) {
|
|
45
|
+
node = expand(node);
|
|
46
|
+
for (const child of node.chi) {
|
|
47
|
+
child[PARENT] = result;
|
|
48
|
+
}
|
|
49
|
+
node[PARENT] = result;
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
result.chi.push(node);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
node[PARENT] = result;
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
result.chi.push(node);
|
|
57
|
+
}
|
|
34
58
|
}
|
|
35
59
|
else {
|
|
60
|
+
node[PARENT] = result;
|
|
36
61
|
// @ts-ignore
|
|
37
62
|
result.chi.push(node);
|
|
38
63
|
}
|
|
@@ -40,7 +65,14 @@ function expand(ast) {
|
|
|
40
65
|
return result;
|
|
41
66
|
}
|
|
42
67
|
function expandRule(node) {
|
|
43
|
-
|
|
68
|
+
if (node[STATE] == EnumAstNodeStatus.Invalid ||
|
|
69
|
+
node[STATE] == EnumAstNodeStatus.Disallowed ||
|
|
70
|
+
node[STATE] == EnumAstNodeStatus.Unknown ||
|
|
71
|
+
node[STATE] == EnumAstNodeStatus.Unparsed ||
|
|
72
|
+
node[STATE] == EnumAstNodeStatus.Malformed) {
|
|
73
|
+
return [node];
|
|
74
|
+
}
|
|
75
|
+
const ast = Object.assign(cloneNode(node), { chi: node.chi.slice() });
|
|
44
76
|
const result = [];
|
|
45
77
|
if (ast.typ == EnumToken.RuleNodeType) {
|
|
46
78
|
let i = 0;
|
|
@@ -57,7 +89,10 @@ function expandRule(node) {
|
|
|
57
89
|
ast.chi.splice(i--, 1);
|
|
58
90
|
continue;
|
|
59
91
|
}
|
|
60
|
-
|
|
92
|
+
for (let i1 = 0; i1 < selRule.length; i1++) {
|
|
93
|
+
const arr = selRule[i1];
|
|
94
|
+
combinators.includes(arr[0].charAt(0)) ? arr.unshift(arSelf) : arr.unshift(arSelf, " ");
|
|
95
|
+
}
|
|
61
96
|
rule.sel = selRule
|
|
62
97
|
.reduce((acc, curr) => {
|
|
63
98
|
acc.push(curr.join(""));
|
|
@@ -114,6 +149,13 @@ function expandRule(node) {
|
|
|
114
149
|
}
|
|
115
150
|
if (withCompound.length > 0) {
|
|
116
151
|
if (withCompound.every((t) => t[0] == "&" && t.indexOf("&", 1) == -1)) {
|
|
152
|
+
// for (const w of withCompound) {
|
|
153
|
+
// for (let m = 0; m < w.length; m++) {
|
|
154
|
+
// // for (let n = 0; n < w[m].length; n++) {
|
|
155
|
+
// withoutCompound.push(w[m].slice(1));
|
|
156
|
+
// // }
|
|
157
|
+
// }
|
|
158
|
+
// }
|
|
117
159
|
withoutCompound.push(...withCompound.map((t) => t.slice(1)));
|
|
118
160
|
withCompound.length = 0;
|
|
119
161
|
}
|
|
@@ -153,7 +195,9 @@ function expandRule(node) {
|
|
|
153
195
|
rule.sel = selectors.reduce((acc, curr) => (curr.length == 0 ? acc : acc + (acc.length > 0 ? "," : "") + curr), "");
|
|
154
196
|
}
|
|
155
197
|
ast.chi.splice(i--, 1);
|
|
156
|
-
|
|
198
|
+
for (const s of expandRule(rule)) {
|
|
199
|
+
result.push(s);
|
|
200
|
+
}
|
|
157
201
|
}
|
|
158
202
|
else if (ast.chi[i].typ == EnumToken.AtRuleNodeType) {
|
|
159
203
|
let astAtRule = ast.chi[i];
|
|
@@ -188,13 +232,19 @@ function expandRule(node) {
|
|
|
188
232
|
values.push(r);
|
|
189
233
|
}
|
|
190
234
|
else if (r.typ == EnumToken.RuleNodeType) {
|
|
191
|
-
|
|
192
|
-
|
|
235
|
+
for (const rule of expandRule(r)) {
|
|
236
|
+
// @ts-ignore
|
|
237
|
+
astAtRule.chi.push(rule);
|
|
238
|
+
}
|
|
193
239
|
}
|
|
194
240
|
}
|
|
195
241
|
}
|
|
196
|
-
|
|
197
|
-
|
|
242
|
+
if (astAtRule.chi.length > 0) {
|
|
243
|
+
result.push(astAtRule);
|
|
244
|
+
}
|
|
245
|
+
for (const r of values) {
|
|
246
|
+
result.push(r);
|
|
247
|
+
}
|
|
198
248
|
ast.chi.splice(i--, 1);
|
|
199
249
|
}
|
|
200
250
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { EnumToken } from '../types.js';
|
|
2
|
-
import { walkValues
|
|
2
|
+
import { walkValues } from '../walk.js';
|
|
3
3
|
import { evaluate } from '../math/expression.js';
|
|
4
|
-
import { renderValue } from '../../renderer/render.js';
|
|
5
4
|
import { FeatureWalkMode } from './type.js';
|
|
6
|
-
import { mathFuncs,
|
|
5
|
+
import { tokensfuncSet, mathFuncs, LOCEND, LOCSTA, LOCSRCID } from '../../syntax/constants.js';
|
|
6
|
+
import { replaceNodeOrValue } from '../../parser/utils/token.js';
|
|
7
7
|
|
|
8
8
|
class ComputeCalcExpressionFeature {
|
|
9
9
|
accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType]);
|
|
@@ -28,57 +28,15 @@ class ComputeCalcExpressionFeature {
|
|
|
28
28
|
continue;
|
|
29
29
|
}
|
|
30
30
|
const set = new Set();
|
|
31
|
-
for (const { value, parent } of walkValues(node.val, node
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
parent.typ == EnumToken.DeclarationNodeType &&
|
|
38
|
-
// @ts-ignore
|
|
39
|
-
parent.val.length == 1 &&
|
|
40
|
-
(node.typ === EnumToken.MathFunctionTokenType || node.typ === EnumToken.FunctionTokenType) &&
|
|
41
|
-
mathFuncs.includes(node.val) &&
|
|
42
|
-
node.chi.length == 1 &&
|
|
43
|
-
node.chi[0].typ == EnumToken.IdenTokenType) {
|
|
44
|
-
return WalkerOptionEnum.Ignore;
|
|
45
|
-
}
|
|
46
|
-
if ((node.typ === EnumToken.WildCardFunctionTokenType && node.val == "var") ||
|
|
47
|
-
(!mathFuncs.includes(parent.val) &&
|
|
48
|
-
[
|
|
49
|
-
EnumToken.MathFunctionTokenType,
|
|
50
|
-
EnumToken.ColorTokenType,
|
|
51
|
-
EnumToken.DeclarationNodeType,
|
|
52
|
-
EnumToken.ImageFunc,
|
|
53
|
-
EnumToken.RuleNodeType,
|
|
54
|
-
EnumToken.AtRuleNodeType,
|
|
55
|
-
EnumToken.StyleSheetNodeType,
|
|
56
|
-
].includes(parent?.typ))) {
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
31
|
+
for (const { value, parent } of walkValues(node.val, node)) {
|
|
32
|
+
if (parent?.typ == EnumToken.BinaryExpressionTokenType) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (value.typ == EnumToken.BinaryExpressionTokenType) {
|
|
59
36
|
// @ts-ignore
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
? node.val
|
|
64
|
-
: node.chi)?.slice();
|
|
65
|
-
if (slice != null &&
|
|
66
|
-
(node.typ === EnumToken.MathFunctionTokenType ||
|
|
67
|
-
(node.typ == EnumToken.FunctionTokenType &&
|
|
68
|
-
mathFuncs.includes(node.val)))) {
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
const key = "chi" in node ? "chi" : "val";
|
|
71
|
-
const str1 = renderValue({ ...node, [key]: slice });
|
|
72
|
-
const str2 = renderValue(node); // values.reduce((acc: string, curr: Token): string => acc + renderValue(curr), '');
|
|
73
|
-
if (str1.length < str2.length) {
|
|
74
|
-
// @ts-ignore
|
|
75
|
-
node[key] = slice;
|
|
76
|
-
}
|
|
77
|
-
return WalkerOptionEnum.Ignore;
|
|
78
|
-
}
|
|
79
|
-
return null;
|
|
80
|
-
},
|
|
81
|
-
})) {
|
|
37
|
+
replaceNodeOrValue(parent, value, evaluate([value]));
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
82
40
|
if (value != null && tokensfuncSet.has(value.typ)) {
|
|
83
41
|
if (!set.has(value)) {
|
|
84
42
|
set.add(value);
|
|
@@ -109,7 +67,8 @@ class ComputeCalcExpressionFeature {
|
|
|
109
67
|
// @ts-ignore
|
|
110
68
|
const children = parent.typ == EnumToken.DeclarationNodeType
|
|
111
69
|
? parent.val
|
|
112
|
-
:
|
|
70
|
+
: // @ts-ignore
|
|
71
|
+
parent.chi;
|
|
113
72
|
if (values.length == 1 && values[0].typ != EnumToken.BinaryExpressionTokenType) {
|
|
114
73
|
for (let i = 0; i < children.length; i++) {
|
|
115
74
|
if (children[i] == value) {
|
|
@@ -124,7 +83,9 @@ class ComputeCalcExpressionFeature {
|
|
|
124
83
|
typ: EnumToken.MathFunctionTokenType,
|
|
125
84
|
val: "calc",
|
|
126
85
|
chi: values,
|
|
127
|
-
[
|
|
86
|
+
[LOCSRCID]: value[LOCSRCID],
|
|
87
|
+
[LOCSTA]: value[LOCSTA],
|
|
88
|
+
[LOCEND]: value[LOCEND],
|
|
128
89
|
}
|
|
129
90
|
: values[0]);
|
|
130
91
|
break;
|
|
@@ -138,7 +99,9 @@ class ComputeCalcExpressionFeature {
|
|
|
138
99
|
typ: EnumToken.MathFunctionTokenType,
|
|
139
100
|
val: "calc",
|
|
140
101
|
chi: values,
|
|
141
|
-
[
|
|
102
|
+
[LOCSRCID]: value[LOCSRCID],
|
|
103
|
+
[LOCSTA]: value[LOCSTA],
|
|
104
|
+
[LOCEND]: value[LOCEND],
|
|
142
105
|
});
|
|
143
106
|
break;
|
|
144
107
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EnumToken } from '../types.js';
|
|
2
2
|
import { renderValue } from '../../renderer/render.js';
|
|
3
3
|
import { FeatureWalkMode } from './type.js';
|
|
4
|
-
import { PARENT, TOKENS } from '../../syntax/constants.js';
|
|
4
|
+
import { PARENT, LOCSRCID, LOCSTA, LOCEND, TOKENS } from '../../syntax/constants.js';
|
|
5
5
|
import { equalsIgnoreCase } from '../../parser/utils/text.js';
|
|
6
6
|
import { replaceNodeOrValue } from '../../parser/utils/token.js';
|
|
7
7
|
import { cloneNode } from '../clone.js';
|
|
@@ -29,6 +29,7 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
29
29
|
EnumToken.SemiColonTokenType
|
|
30
30
|
? trimArray(node.r.r.slice(0, -1))
|
|
31
31
|
: node.r.r);
|
|
32
|
+
// @ts-expect-error
|
|
32
33
|
if (targetParentWrapper.typ != EnumToken.DeclarationNodeType) {
|
|
33
34
|
let index = targetParentWrapper.chi.indexOf(targetWrapper);
|
|
34
35
|
if (index != -1) {
|
|
@@ -50,6 +51,7 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
50
51
|
.r.r.slice(0, -1))
|
|
51
52
|
: siblingWrapper.chi[k]
|
|
52
53
|
.r.r);
|
|
54
|
+
// @ts-ignore
|
|
53
55
|
cache.add(siblingWrapper.chi[k].l);
|
|
54
56
|
}
|
|
55
57
|
}
|
|
@@ -73,7 +75,9 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
73
75
|
}
|
|
74
76
|
if (left.typ === EnumToken.IdenTokenType && equalsIgnoreCase("else", left.val)) {
|
|
75
77
|
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
76
|
-
replaceNodeOrValue(nodeMap.get(parentWrapper),
|
|
78
|
+
replaceNodeOrValue(nodeMap.get(parentWrapper),
|
|
79
|
+
// @ts-expect-error
|
|
80
|
+
nodeMap.get(targetWrapper.typ === EnumToken.DeclarationNodeType ? node : targetWrapper), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType ? trimArray(node.r.slice(0, -1)) : node.r);
|
|
77
81
|
result.push(clonedDeclaration);
|
|
78
82
|
}
|
|
79
83
|
else if (left?.typ === EnumToken.WhenElseFunctionTokenType) {
|
|
@@ -82,6 +86,11 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
82
86
|
nam: left.val,
|
|
83
87
|
chi: [],
|
|
84
88
|
});
|
|
89
|
+
if (declaration[PARENT] != null) {
|
|
90
|
+
atRule[LOCSRCID] = declaration[PARENT][LOCSRCID];
|
|
91
|
+
atRule[LOCSTA] = declaration[PARENT][LOCSTA];
|
|
92
|
+
atRule[LOCEND] = declaration[PARENT][LOCEND];
|
|
93
|
+
}
|
|
85
94
|
atRule[TOKENS] = [{ typ: EnumToken.ParensTokenType, chi: left.chi.slice() }];
|
|
86
95
|
const minify = atRule.nam !== "supports";
|
|
87
96
|
const options = {
|
|
@@ -104,6 +113,11 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
104
113
|
});
|
|
105
114
|
atRule[TOKENS] = [left];
|
|
106
115
|
atRule.val = atRule[TOKENS].reduce((acc, curr) => acc + renderValue(curr), "");
|
|
116
|
+
if (declaration[PARENT] != null) {
|
|
117
|
+
atRule[LOCSRCID] = declaration[PARENT][LOCSRCID];
|
|
118
|
+
atRule[LOCSTA] = declaration[PARENT][LOCSTA];
|
|
119
|
+
atRule[LOCEND] = declaration[PARENT][LOCEND];
|
|
120
|
+
}
|
|
107
121
|
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
108
122
|
replaceNodeOrValue(nodeMap.get(targetWrapper.typ === EnumToken.WildCardFunctionTokenType ? targetParentWrapper : targetWrapper), nodeMap.get(targetWrapper.typ === EnumToken.WildCardFunctionTokenType ? targetWrapper : node), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType
|
|
109
123
|
? trimArray(node.r.slice(0, -1))
|
|
@@ -134,17 +148,27 @@ function processNode(declarationNode, cache) {
|
|
|
134
148
|
const parentWrapper = node.parent ?? parents.find((node) => !nodeMatcher(node));
|
|
135
149
|
if (node.node.typ === EnumToken.WildCardFunctionTokenType) {
|
|
136
150
|
for (i = 0; i < node.node.chi.length; i++) {
|
|
137
|
-
stack.push(
|
|
151
|
+
stack.push(
|
|
152
|
+
// @ts-expect-error
|
|
153
|
+
...substituteIfElseNode(
|
|
154
|
+
// @ts-expect-error
|
|
155
|
+
declaration, node.node.chi[i], node.node, parentWrapper, cache));
|
|
138
156
|
}
|
|
139
157
|
}
|
|
140
158
|
else {
|
|
141
|
-
stack.push(
|
|
159
|
+
stack.push(
|
|
160
|
+
// @ts-expect-error
|
|
161
|
+
...substituteIfElseNode(
|
|
162
|
+
// @ts-expect-error
|
|
163
|
+
declaration, node.node, parentWrapper, parents[parents.indexOf(parentWrapper) + 1] ?? declaration, cache));
|
|
142
164
|
}
|
|
143
165
|
}
|
|
144
166
|
if (result.length > 0) {
|
|
167
|
+
// @ts-expect-error
|
|
145
168
|
replaceNodeOrValue(declarationNode[PARENT], declarationNode, result);
|
|
146
169
|
}
|
|
147
170
|
// else remove node?
|
|
171
|
+
// @ts-expect-error
|
|
148
172
|
return result;
|
|
149
173
|
}
|
|
150
174
|
class ExpandIfFeature {
|
|
@@ -8,13 +8,14 @@ import { RAW, mathFuncs } from '../../syntax/constants.js';
|
|
|
8
8
|
function inlineExpression(token) {
|
|
9
9
|
const result = [];
|
|
10
10
|
if (token.typ == EnumToken.BinaryExpressionTokenType) {
|
|
11
|
+
const chi = inlineExpression(token.l);
|
|
12
|
+
chi.push({ typ: token.op });
|
|
13
|
+
for (const child of inlineExpression(token.r)) {
|
|
14
|
+
chi.push(child);
|
|
15
|
+
}
|
|
11
16
|
result.push({
|
|
12
17
|
typ: EnumToken.ParensTokenType,
|
|
13
|
-
chi
|
|
14
|
-
...inlineExpression(token.l),
|
|
15
|
-
{ typ: token.op },
|
|
16
|
-
...inlineExpression(token.r),
|
|
17
|
-
],
|
|
18
|
+
chi,
|
|
18
19
|
});
|
|
19
20
|
}
|
|
20
21
|
else {
|
|
@@ -57,16 +57,19 @@ function replaceAstNodes(tokens, root) {
|
|
|
57
57
|
if (key in pseudoAliasMap) {
|
|
58
58
|
const isPseudClass = pseudoAliasMap[key].startsWith("::");
|
|
59
59
|
value.val = pseudoAliasMap[key];
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
60
|
+
// if (
|
|
61
|
+
// value.typ == EnumToken.IdenTokenType &&
|
|
62
|
+
// ["min-resolution", "max-resolution"].includes((value as IdentToken).val) &&
|
|
63
|
+
// parent?.typ == EnumToken.MediaQueryConditionTokenType &&
|
|
64
|
+
// (parent as MediaQueryConditionToken).r?.[0]?.typ == EnumToken.NumberTokenType
|
|
65
|
+
// ) {
|
|
66
|
+
// Object.assign((parent as MediaQueryConditionToken).r?.[0], {
|
|
67
|
+
// typ: EnumToken.ResolutionTokenType,
|
|
68
|
+
// unit: "x",
|
|
69
|
+
// });
|
|
70
|
+
// }
|
|
71
|
+
// else
|
|
72
|
+
if (isPseudClass && value.typ == EnumToken.PseudoElementTokenType) {
|
|
70
73
|
// @ts-ignore
|
|
71
74
|
value.typ = EnumToken.PseudoClassTokenType;
|
|
72
75
|
}
|
|
@@ -78,7 +81,7 @@ function replaceAstNodes(tokens, root) {
|
|
|
78
81
|
const set = new Set();
|
|
79
82
|
const split = splitTokenList(tokens, [EnumToken.CommaTokenType]);
|
|
80
83
|
tokens.length = 0;
|
|
81
|
-
|
|
84
|
+
for (const token of split.reduce((acc, curr) => {
|
|
82
85
|
const str = curr.reduce((acc, curr) => acc + renderValue(curr), "");
|
|
83
86
|
if (set.has(str)) {
|
|
84
87
|
return acc;
|
|
@@ -90,7 +93,9 @@ function replaceAstNodes(tokens, root) {
|
|
|
90
93
|
});
|
|
91
94
|
}
|
|
92
95
|
return acc.concat(curr);
|
|
93
|
-
}, []))
|
|
96
|
+
}, [])) {
|
|
97
|
+
tokens.push(token);
|
|
98
|
+
}
|
|
94
99
|
}
|
|
95
100
|
return result;
|
|
96
101
|
}
|
|
@@ -279,32 +284,11 @@ class ComputePrefixFeature {
|
|
|
279
284
|
let commaCount;
|
|
280
285
|
let type = "";
|
|
281
286
|
let tokens = token.chi.slice();
|
|
282
|
-
// while (
|
|
283
|
-
// i < tokens.length &&
|
|
284
|
-
// (tokens[i].typ === EnumToken.WhitespaceTokenType || tokens[i].typ === EnumToken.CommentTokenType)
|
|
285
|
-
// ) {
|
|
286
|
-
// i++;
|
|
287
|
-
// }
|
|
288
|
-
// if (i >= tokens.length || tokens[i].typ !== EnumToken.IdenTokenType) {
|
|
289
|
-
// return;
|
|
290
|
-
// }
|
|
291
287
|
// linear or radial
|
|
292
288
|
if (equalsIgnoreCase(tokens[i].val, "linear")) {
|
|
293
289
|
type = "linear-gradient";
|
|
294
290
|
i++;
|
|
295
291
|
}
|
|
296
|
-
// else {
|
|
297
|
-
// return;
|
|
298
|
-
// }
|
|
299
|
-
// while (
|
|
300
|
-
// i < tokens.length &&
|
|
301
|
-
// (tokens[i].typ === EnumToken.WhitespaceTokenType || tokens[i].typ === EnumToken.CommentTokenType)
|
|
302
|
-
// ) {
|
|
303
|
-
// i++;
|
|
304
|
-
// }
|
|
305
|
-
// if (tokens[i].typ !== EnumToken.CommaTokenType) {
|
|
306
|
-
// return;
|
|
307
|
-
// }
|
|
308
292
|
tokens.splice(0, i + 1);
|
|
309
293
|
commaCount = 0;
|
|
310
294
|
for (i = 0; i < tokens.length; i++) {
|
|
@@ -329,52 +313,28 @@ class ComputePrefixFeature {
|
|
|
329
313
|
// right bottom → left top to top left
|
|
330
314
|
const replacements = [];
|
|
331
315
|
if (key === "left top left bottom") {
|
|
332
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
|
|
333
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
334
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "bottom" });
|
|
316
|
+
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "bottom" });
|
|
335
317
|
}
|
|
336
318
|
else if (key === "left bottom left top") {
|
|
337
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
|
|
338
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
339
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "top" });
|
|
319
|
+
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "top" });
|
|
340
320
|
}
|
|
341
321
|
else if (key === "left top right top") {
|
|
342
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
|
|
343
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
344
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "right" });
|
|
322
|
+
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "right" });
|
|
345
323
|
}
|
|
346
324
|
else if (key === "right top left top") {
|
|
347
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
|
|
348
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
349
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "left" });
|
|
325
|
+
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "left" });
|
|
350
326
|
}
|
|
351
327
|
else if (key === "left top right bottom") {
|
|
352
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
|
|
353
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
354
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "bottom" });
|
|
355
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
356
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "right" });
|
|
328
|
+
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "bottom" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "right" });
|
|
357
329
|
}
|
|
358
330
|
else if (key === "right top left bottom") {
|
|
359
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
|
|
360
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
361
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "bottom" });
|
|
362
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
363
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "left" });
|
|
331
|
+
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "bottom" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "left" });
|
|
364
332
|
}
|
|
365
333
|
else if (key === "left bottom right top") {
|
|
366
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
|
|
367
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
368
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "top" });
|
|
369
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
370
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "right" });
|
|
334
|
+
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "top" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "right" });
|
|
371
335
|
}
|
|
372
336
|
else if (key === "right bottom left top") {
|
|
373
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
|
|
374
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
375
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "top" });
|
|
376
|
-
replacements.push({ typ: EnumToken.WhitespaceTokenType });
|
|
377
|
-
replacements.push({ typ: EnumToken.IdenTokenType, val: "left" });
|
|
337
|
+
replacements.push({ typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "top" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "left" });
|
|
378
338
|
}
|
|
379
339
|
tokens.splice(0, i, ...replacements);
|
|
380
340
|
let checkStop = true;
|
|
@@ -387,7 +347,10 @@ class ComputePrefixFeature {
|
|
|
387
347
|
}
|
|
388
348
|
if (tokens[i].typ === EnumToken.FunctionTokenType) {
|
|
389
349
|
if (equalsIgnoreCase(tokens[i].val, "to")) {
|
|
390
|
-
colorStop.push(tokens[checkStopIndex]
|
|
350
|
+
colorStop.push(tokens[checkStopIndex]);
|
|
351
|
+
for (const token of tokens[i].chi) {
|
|
352
|
+
colorStop.push(token);
|
|
353
|
+
}
|
|
391
354
|
tokens.splice(checkStopIndex, i - checkStopIndex + 1);
|
|
392
355
|
i = checkStopIndex;
|
|
393
356
|
checkStop = false;
|
|
@@ -415,12 +378,16 @@ class ComputePrefixFeature {
|
|
|
415
378
|
}
|
|
416
379
|
}
|
|
417
380
|
if (colorStop.length > 0) {
|
|
418
|
-
|
|
381
|
+
for (const t of colorStop) {
|
|
382
|
+
tokens.push(t);
|
|
383
|
+
}
|
|
419
384
|
}
|
|
420
385
|
if (type !== "") {
|
|
421
386
|
token.val = type;
|
|
422
387
|
token.chi.length = 0;
|
|
423
|
-
|
|
388
|
+
for (const t of tokens) {
|
|
389
|
+
token.chi.push(t);
|
|
390
|
+
}
|
|
424
391
|
}
|
|
425
392
|
}
|
|
426
393
|
/**
|
|
@@ -491,7 +458,9 @@ class ComputePrefixFeature {
|
|
|
491
458
|
i++;
|
|
492
459
|
}
|
|
493
460
|
}
|
|
494
|
-
|
|
461
|
+
for (let m = i; m < tokens.length; m++) {
|
|
462
|
+
colorStops.push(tokens[m]);
|
|
463
|
+
}
|
|
495
464
|
tokens.length = 0;
|
|
496
465
|
if (form.length > 0 || size.length > 0) {
|
|
497
466
|
if (form.length === 0) {
|
|
@@ -499,17 +468,27 @@ class ComputePrefixFeature {
|
|
|
499
468
|
}
|
|
500
469
|
if (size.length > 0) {
|
|
501
470
|
form.push({ typ: EnumToken.WhitespaceTokenType });
|
|
502
|
-
|
|
471
|
+
for (const token of size) {
|
|
472
|
+
form.push(token);
|
|
473
|
+
}
|
|
503
474
|
}
|
|
504
475
|
if (positions.length > 0) {
|
|
505
|
-
form.push({ typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }
|
|
476
|
+
form.push({ typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType });
|
|
477
|
+
for (const position of positions) {
|
|
478
|
+
form.push(position);
|
|
479
|
+
}
|
|
506
480
|
}
|
|
507
|
-
|
|
481
|
+
for (const token of form) {
|
|
482
|
+
tokens.push(token);
|
|
483
|
+
}
|
|
484
|
+
tokens.push({ typ: EnumToken.CommaTokenType });
|
|
508
485
|
}
|
|
509
486
|
token.val = equalsIgnoreCase(token.val, "-webkit-repeating-radial-gradient")
|
|
510
487
|
? "repeating-radial-gradient"
|
|
511
488
|
: "radial-gradient";
|
|
512
|
-
|
|
489
|
+
for (const colorStop of colorStops) {
|
|
490
|
+
tokens.push(colorStop);
|
|
491
|
+
}
|
|
513
492
|
return tokens;
|
|
514
493
|
}
|
|
515
494
|
}
|
|
@@ -6,7 +6,7 @@ class ComputeShorthandFeature {
|
|
|
6
6
|
accept = new Set([
|
|
7
7
|
EnumToken.RuleNodeType,
|
|
8
8
|
EnumToken.AtRuleNodeType,
|
|
9
|
-
EnumToken.
|
|
9
|
+
EnumToken.KeyframesRuleNodeType,
|
|
10
10
|
]);
|
|
11
11
|
get ordering() {
|
|
12
12
|
return 10;
|
|
@@ -20,7 +20,7 @@ class ComputeShorthandFeature {
|
|
|
20
20
|
options.features.push(new ComputeShorthandFeature(options));
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
run(ast, options
|
|
23
|
+
run(ast, options) {
|
|
24
24
|
if (!("chi" in ast)) {
|
|
25
25
|
return null;
|
|
26
26
|
}
|
|
@@ -46,15 +46,20 @@ class ComputeShorthandFeature {
|
|
|
46
46
|
// @ts-ignore
|
|
47
47
|
const node = ast.chi[l];
|
|
48
48
|
if (node.typ == EnumToken.DeclarationNodeType) {
|
|
49
|
-
|
|
49
|
+
for (let m = k; m <= l; m++) {
|
|
50
|
+
properties.add(ast.chi[m]);
|
|
51
|
+
}
|
|
50
52
|
}
|
|
51
53
|
else {
|
|
52
|
-
|
|
54
|
+
for (let m = k; m <= l; m++) {
|
|
55
|
+
rules.push(ast.chi[m]);
|
|
56
|
+
}
|
|
53
57
|
}
|
|
54
58
|
k = l;
|
|
55
59
|
}
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
ast.chi.length = 0;
|
|
61
|
+
// @ts-expect-error
|
|
62
|
+
ast.chi.push(...properties, ...rules);
|
|
58
63
|
return ast;
|
|
59
64
|
}
|
|
60
65
|
}
|