@tbela99/css-parser 1.4.10 → 1.5.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 +11 -1
- package/README.md +11 -4
- package/dist/index-umd-web.js +2118 -1701
- package/dist/index.cjs +2130 -1696
- package/dist/index.d.ts +781 -269
- package/dist/lib/ast/clone.js +13 -8
- package/dist/lib/ast/expand.js +45 -9
- package/dist/lib/ast/features/calc.js +2 -1
- package/dist/lib/ast/features/if.js +24 -4
- package/dist/lib/ast/features/prefix.js +13 -31
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +5 -2
- package/dist/lib/ast/find.js +7 -5
- package/dist/lib/ast/minify.js +80 -35
- package/dist/lib/ast/node.js +50 -0
- package/dist/lib/ast/types.js +1 -1
- package/dist/lib/ast/walk.js +27 -7
- package/dist/lib/fs/resolve.js +64 -32
- package/dist/lib/parser/declaration/map.js +14 -5
- package/dist/lib/parser/linesmap.js +5 -13
- package/dist/lib/parser/parse.js +489 -719
- package/dist/lib/parser/source.js +16 -2
- package/dist/lib/parser/tokenize.js +382 -278
- package/dist/lib/parser/utils/at-rule-container.js +2 -1
- package/dist/lib/parser/utils/selector.js +88 -10
- package/dist/lib/renderer/render.js +174 -99
- package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +190 -34
- package/dist/lib/syntax/syntax.js +14 -151
- package/dist/lib/validation/match.js +81 -1
- package/dist/node.js +41 -26
- package/dist/utils/sync.d.ts +16 -0
- package/dist/utils/sync.js +52 -0
- package/dist/web.js +26 -28
- package/llms.txt +12 -5
- package/package.json +1 -1
- package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
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,24 @@ 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) {
|
|
28
|
+
children = expandRule(node);
|
|
29
|
+
for (const child of children) {
|
|
30
|
+
child[PARENT] = result;
|
|
31
|
+
}
|
|
19
32
|
// @ts-ignore
|
|
20
|
-
result.chi.push(...
|
|
33
|
+
result.chi.push(...children);
|
|
21
34
|
}
|
|
22
35
|
else if (node.typ == EnumToken.AtRuleNodeType && "chi" in node) {
|
|
23
36
|
let hasRule = false;
|
|
@@ -29,10 +42,23 @@ function expand(ast) {
|
|
|
29
42
|
break;
|
|
30
43
|
}
|
|
31
44
|
}
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
if (hasRule) {
|
|
46
|
+
node = expand(node);
|
|
47
|
+
for (const child of node.chi) {
|
|
48
|
+
child[PARENT] = result;
|
|
49
|
+
}
|
|
50
|
+
node[PARENT] = result;
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
result.chi.push(node);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
node[PARENT] = result;
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
result.chi.push(node);
|
|
58
|
+
}
|
|
34
59
|
}
|
|
35
60
|
else {
|
|
61
|
+
node[PARENT] = result;
|
|
36
62
|
// @ts-ignore
|
|
37
63
|
result.chi.push(node);
|
|
38
64
|
}
|
|
@@ -40,7 +66,14 @@ function expand(ast) {
|
|
|
40
66
|
return result;
|
|
41
67
|
}
|
|
42
68
|
function expandRule(node) {
|
|
43
|
-
|
|
69
|
+
if (node[STATE] == EnumAstNodeStatus.Invalid ||
|
|
70
|
+
node[STATE] == EnumAstNodeStatus.Disallowed ||
|
|
71
|
+
node[STATE] == EnumAstNodeStatus.Unknown ||
|
|
72
|
+
node[STATE] == EnumAstNodeStatus.Unparsed ||
|
|
73
|
+
node[STATE] == EnumAstNodeStatus.Malformed) {
|
|
74
|
+
return [node];
|
|
75
|
+
}
|
|
76
|
+
const ast = Object.assign(cloneNode(node), { chi: node.chi.slice() });
|
|
44
77
|
const result = [];
|
|
45
78
|
if (ast.typ == EnumToken.RuleNodeType) {
|
|
46
79
|
let i = 0;
|
|
@@ -57,7 +90,10 @@ function expandRule(node) {
|
|
|
57
90
|
ast.chi.splice(i--, 1);
|
|
58
91
|
continue;
|
|
59
92
|
}
|
|
60
|
-
|
|
93
|
+
for (let i1 = 0; i1 < selRule.length; i1++) {
|
|
94
|
+
const arr = selRule[i1];
|
|
95
|
+
combinators.includes(arr[0].charAt(0)) ? arr.unshift(arSelf) : arr.unshift(arSelf, " ");
|
|
96
|
+
}
|
|
61
97
|
rule.sel = selRule
|
|
62
98
|
.reduce((acc, curr) => {
|
|
63
99
|
acc.push(curr.join(""));
|
|
@@ -109,7 +109,8 @@ class ComputeCalcExpressionFeature {
|
|
|
109
109
|
// @ts-ignore
|
|
110
110
|
const children = parent.typ == EnumToken.DeclarationNodeType
|
|
111
111
|
? parent.val
|
|
112
|
-
:
|
|
112
|
+
: // @ts-ignore
|
|
113
|
+
parent.chi;
|
|
113
114
|
if (values.length == 1 && values[0].typ != EnumToken.BinaryExpressionTokenType) {
|
|
114
115
|
for (let i = 0; i < children.length; i++) {
|
|
115
116
|
if (children[i] == value) {
|
|
@@ -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, LOC, 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,9 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
82
86
|
nam: left.val,
|
|
83
87
|
chi: [],
|
|
84
88
|
});
|
|
89
|
+
if (declaration[PARENT] != null) {
|
|
90
|
+
atRule[LOC] = declaration[PARENT][LOC];
|
|
91
|
+
}
|
|
85
92
|
atRule[TOKENS] = [{ typ: EnumToken.ParensTokenType, chi: left.chi.slice() }];
|
|
86
93
|
const minify = atRule.nam !== "supports";
|
|
87
94
|
const options = {
|
|
@@ -104,6 +111,9 @@ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache)
|
|
|
104
111
|
});
|
|
105
112
|
atRule[TOKENS] = [left];
|
|
106
113
|
atRule.val = atRule[TOKENS].reduce((acc, curr) => acc + renderValue(curr), "");
|
|
114
|
+
if (declaration[PARENT] != null) {
|
|
115
|
+
atRule[LOC] = declaration[PARENT][LOC];
|
|
116
|
+
}
|
|
107
117
|
clonedDeclaration = cloneNode(declaration, true, nodeMap);
|
|
108
118
|
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
119
|
? trimArray(node.r.slice(0, -1))
|
|
@@ -134,17 +144,27 @@ function processNode(declarationNode, cache) {
|
|
|
134
144
|
const parentWrapper = node.parent ?? parents.find((node) => !nodeMatcher(node));
|
|
135
145
|
if (node.node.typ === EnumToken.WildCardFunctionTokenType) {
|
|
136
146
|
for (i = 0; i < node.node.chi.length; i++) {
|
|
137
|
-
stack.push(
|
|
147
|
+
stack.push(
|
|
148
|
+
// @ts-expect-error
|
|
149
|
+
...substituteIfElseNode(
|
|
150
|
+
// @ts-expect-error
|
|
151
|
+
declaration, node.node.chi[i], node.node, parentWrapper, cache));
|
|
138
152
|
}
|
|
139
153
|
}
|
|
140
154
|
else {
|
|
141
|
-
stack.push(
|
|
155
|
+
stack.push(
|
|
156
|
+
// @ts-expect-error
|
|
157
|
+
...substituteIfElseNode(
|
|
158
|
+
// @ts-expect-error
|
|
159
|
+
declaration, node.node, parentWrapper, parents[parents.indexOf(parentWrapper) + 1] ?? declaration, cache));
|
|
142
160
|
}
|
|
143
161
|
}
|
|
144
162
|
if (result.length > 0) {
|
|
163
|
+
// @ts-expect-error
|
|
145
164
|
replaceNodeOrValue(declarationNode[PARENT], declarationNode, result);
|
|
146
165
|
}
|
|
147
166
|
// else remove node?
|
|
167
|
+
// @ts-expect-error
|
|
148
168
|
return result;
|
|
149
169
|
}
|
|
150
170
|
class ExpandIfFeature {
|
|
@@ -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
|
}
|
|
@@ -279,32 +282,11 @@ class ComputePrefixFeature {
|
|
|
279
282
|
let commaCount;
|
|
280
283
|
let type = "";
|
|
281
284
|
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
285
|
// linear or radial
|
|
292
286
|
if (equalsIgnoreCase(tokens[i].val, "linear")) {
|
|
293
287
|
type = "linear-gradient";
|
|
294
288
|
i++;
|
|
295
289
|
}
|
|
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
290
|
tokens.splice(0, i + 1);
|
|
309
291
|
commaCount = 0;
|
|
310
292
|
for (i = 0; i < tokens.length; i++) {
|
|
@@ -6,7 +6,11 @@ import { FeatureWalkMode } from './type.js';
|
|
|
6
6
|
import { STATE } from '../../syntax/constants.js';
|
|
7
7
|
|
|
8
8
|
class TransformCssFeature {
|
|
9
|
-
accept = new Set([
|
|
9
|
+
accept = new Set([
|
|
10
|
+
EnumToken.RuleNodeType,
|
|
11
|
+
EnumToken.AtRuleNodeType,
|
|
12
|
+
EnumToken.KeyframesRuleNodeType,
|
|
13
|
+
]);
|
|
10
14
|
get ordering() {
|
|
11
15
|
return 3;
|
|
12
16
|
}
|
|
@@ -43,7 +47,6 @@ class TransformCssFeature {
|
|
|
43
47
|
? minifyTransformFunctions(child)
|
|
44
48
|
: child);
|
|
45
49
|
}
|
|
46
|
-
// consumeWhitespace(children);
|
|
47
50
|
let { matrix, cumulative, minified } = compute(children) ?? {
|
|
48
51
|
matrix: null,
|
|
49
52
|
cumulative: null,
|
package/dist/lib/ast/find.js
CHANGED
|
@@ -3,7 +3,7 @@ import { walk, walkValues } from './walk.js';
|
|
|
3
3
|
import { TOKENS } from '../syntax/constants.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Search the ast
|
|
6
|
+
* Search the ast subtree and return the first match
|
|
7
7
|
*
|
|
8
8
|
* ```ts
|
|
9
9
|
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
@@ -42,6 +42,11 @@ function find(ast, matcher) {
|
|
|
42
42
|
return null;
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
|
+
*
|
|
46
|
+
* @param ast
|
|
47
|
+
* @param matcher
|
|
48
|
+
* @returns
|
|
49
|
+
*
|
|
45
50
|
* Search the ast sub-tree by checking each node's value token and return the first match
|
|
46
51
|
*
|
|
47
52
|
```ts
|
|
@@ -67,10 +72,6 @@ button {
|
|
|
67
72
|
console.log({node, value});
|
|
68
73
|
|
|
69
74
|
```
|
|
70
|
-
*
|
|
71
|
-
* @param ast
|
|
72
|
-
* @param matcher
|
|
73
|
-
* @returns
|
|
74
75
|
*/
|
|
75
76
|
function findByValue(ast, matcher) {
|
|
76
77
|
let source;
|
|
@@ -87,6 +88,7 @@ function findByValue(ast, matcher) {
|
|
|
87
88
|
}
|
|
88
89
|
for (const { value, parent, root: rootNode, parents } of walkValues(source, node)) {
|
|
89
90
|
if (matcher(value, node)) {
|
|
91
|
+
// @ts-ignore
|
|
90
92
|
return { node, value: { node: value, parent, root: rootNode, parents } };
|
|
91
93
|
}
|
|
92
94
|
}
|