@tbela99/css-parser 1.4.1 → 1.4.3
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/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.repl_history +4 -0
- package/CHANGELOG.md +168 -4
- package/README.md +82 -0
- package/badges/coverage.svg +20 -0
- package/deno.lock +2861 -0
- package/dist/config.json.js +33 -1
- package/dist/index-umd-web.js +24393 -18479
- package/dist/index.cjs +27430 -21519
- package/dist/index.d.ts +1403 -933
- package/dist/lib/ast/clone.d.ts +10 -0
- package/dist/lib/ast/clone.js +45 -0
- package/dist/lib/ast/expand.d.ts +14 -0
- package/dist/lib/ast/expand.js +89 -64
- package/dist/lib/ast/features/calc.d.ts +10 -0
- package/dist/lib/ast/features/calc.js +62 -24
- package/dist/lib/ast/features/if.d.ts +10 -0
- package/dist/lib/ast/features/if.js +215 -0
- package/dist/lib/ast/features/index.d.ts +6 -0
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.d.ts +15 -0
- package/dist/lib/ast/features/inlinecssvariables.js +32 -27
- package/dist/lib/ast/features/prefix.d.ts +8 -0
- package/dist/lib/ast/features/prefix.js +68 -43
- package/dist/lib/ast/features/shorthand.d.ts +12 -0
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.d.ts +10 -0
- package/dist/lib/ast/features/transform.js +9 -13
- package/dist/lib/ast/features/type.d.ts +15 -0
- package/dist/lib/ast/find.d.ts +165 -0
- package/dist/lib/ast/find.js +175 -0
- package/dist/lib/ast/math/expression.d.ts +18 -0
- package/dist/lib/ast/math/expression.js +140 -98
- package/dist/lib/ast/math/math.d.ts +6 -0
- package/dist/lib/ast/math/math.js +30 -41
- package/dist/lib/ast/minify.d.ts +19 -0
- package/dist/lib/ast/minify.js +543 -215
- package/dist/lib/ast/transform/compute.d.ts +8 -0
- package/dist/lib/ast/transform/compute.js +82 -69
- package/dist/lib/ast/transform/matrix.d.ts +22 -0
- package/dist/lib/ast/transform/matrix.js +12 -26
- package/dist/lib/ast/transform/minify.d.ts +5 -0
- package/dist/lib/ast/transform/minify.js +20 -20
- package/dist/lib/ast/transform/perspective.d.ts +3 -0
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.d.ts +12 -0
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.d.ts +6 -0
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.d.ts +4 -0
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.d.ts +6 -0
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.d.ts +9 -0
- package/dist/lib/ast/types.d.ts +903 -0
- package/dist/lib/ast/types.js +277 -23
- package/dist/lib/ast/walk.d.ts +162 -0
- package/dist/lib/ast/walk.js +116 -60
- package/dist/lib/fs/resolve.d.ts +20 -0
- package/dist/lib/fs/resolve.js +37 -45
- package/dist/lib/parser/declaration/list.d.ts +16 -0
- package/dist/lib/parser/declaration/list.js +26 -24
- package/dist/lib/parser/declaration/map.d.ts +15 -0
- package/dist/lib/parser/declaration/map.js +140 -95
- package/dist/lib/parser/declaration/set.d.ts +9 -0
- package/dist/lib/parser/declaration/set.js +30 -25
- package/dist/lib/parser/node.d.ts +7 -0
- package/dist/lib/parser/parse.d.ts +107 -0
- package/dist/lib/parser/parse.js +1454 -1445
- package/dist/lib/parser/tokenize.d.ts +57 -0
- package/dist/lib/parser/tokenize.js +557 -404
- package/dist/lib/parser/utils/at-rule-container.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-container.js +486 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
- package/dist/lib/parser/utils/at-rule-generic.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-generic.js +118 -0
- package/dist/lib/parser/utils/at-rule-import.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-import.js +393 -0
- package/dist/lib/parser/utils/at-rule-media.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-media.js +603 -0
- package/dist/lib/parser/utils/at-rule-page.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-page.js +28 -0
- package/dist/lib/parser/utils/at-rule-support.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-support.js +366 -0
- package/dist/lib/parser/utils/at-rule-token.d.ts +1 -0
- package/dist/lib/parser/utils/at-rule-when-else.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
- package/dist/lib/parser/utils/at-rule.d.ts +13 -0
- package/dist/lib/parser/utils/at-rule.js +37 -0
- package/dist/lib/parser/utils/cache.d.ts +6 -0
- package/dist/lib/parser/utils/cache.js +19 -0
- package/dist/lib/parser/utils/config.d.ts +2 -0
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration-list.d.ts +5 -0
- package/dist/lib/parser/utils/declaration.d.ts +18 -0
- package/dist/lib/parser/utils/declaration.js +569 -91
- package/dist/lib/parser/utils/eq.d.ts +1 -0
- package/dist/lib/parser/utils/hash.d.ts +21 -0
- package/dist/lib/parser/utils/hash.js +1 -1
- package/dist/lib/parser/utils/selector.d.ts +5 -0
- package/dist/lib/parser/utils/selector.js +476 -0
- package/dist/lib/parser/utils/text.d.ts +3 -0
- package/dist/lib/parser/utils/text.js +17 -1
- package/dist/lib/parser/utils/token.d.ts +14 -0
- package/dist/lib/parser/utils/token.js +102 -0
- package/dist/lib/parser/utils/type.d.ts +2 -0
- package/dist/lib/parser/utils/type.js +29 -18
- package/dist/lib/renderer/render.d.ts +28 -0
- package/dist/lib/renderer/render.js +421 -262
- package/dist/lib/renderer/sourcemap/lib/encode.d.ts +1 -0
- package/dist/lib/renderer/sourcemap/sourcemap.d.ts +26 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
- package/dist/lib/syntax/color/a98rgb.d.ts +2 -0
- package/dist/lib/syntax/color/a98rgb.js +8 -12
- package/dist/lib/syntax/color/cmyk.d.ts +10 -0
- package/dist/lib/syntax/color/cmyk.js +23 -21
- package/dist/lib/syntax/color/color-mix.d.ts +2 -0
- package/dist/lib/syntax/color/color-mix.js +88 -77
- package/dist/lib/syntax/color/color.d.ts +42 -0
- package/dist/lib/syntax/color/color.js +65 -68
- package/dist/lib/syntax/color/hex.d.ts +16 -0
- package/dist/lib/syntax/color/hex.js +27 -31
- package/dist/lib/syntax/color/hsl.d.ts +20 -0
- package/dist/lib/syntax/color/hsl.js +5 -12
- package/dist/lib/syntax/color/hsv.d.ts +2 -0
- package/dist/lib/syntax/color/hwb.d.ts +21 -0
- package/dist/lib/syntax/color/hwb.js +8 -21
- package/dist/lib/syntax/color/lab.d.ts +25 -0
- package/dist/lib/syntax/color/lab.js +20 -21
- package/dist/lib/syntax/color/lch.d.ts +23 -0
- package/dist/lib/syntax/color/lch.js +13 -15
- package/dist/lib/syntax/color/oklab.d.ts +22 -0
- package/dist/lib/syntax/color/oklab.js +20 -39
- package/dist/lib/syntax/color/oklch.d.ts +20 -0
- package/dist/lib/syntax/color/oklch.js +14 -16
- package/dist/lib/syntax/color/p3.d.ts +6 -0
- package/dist/lib/syntax/color/p3.js +0 -8
- package/dist/lib/syntax/color/prophotorgb.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.js +9 -13
- package/dist/lib/syntax/color/relativecolor.d.ts +13 -0
- package/dist/lib/syntax/color/relativecolor.js +68 -41
- package/dist/lib/syntax/color/rgb.d.ts +20 -0
- package/dist/lib/syntax/color/rgb.js +14 -18
- package/dist/lib/syntax/color/srgb.d.ts +23 -0
- package/dist/lib/syntax/color/srgb.js +27 -26
- package/dist/lib/syntax/color/utils/components.d.ts +2 -0
- package/dist/lib/syntax/color/utils/components.js +30 -14
- package/dist/lib/syntax/color/utils/distance.d.ts +18 -0
- package/dist/lib/syntax/color/utils/distance.js +1 -8
- package/dist/lib/syntax/color/utils/matrix.d.ts +6 -0
- package/dist/lib/syntax/color/xyz.d.ts +5 -0
- package/dist/lib/syntax/color/xyz.js +8 -20
- package/dist/lib/syntax/color/xyzd50.d.ts +4 -0
- package/dist/lib/syntax/color/xyzd50.js +6 -20
- package/dist/lib/syntax/constants.d.ts +67 -0
- package/dist/lib/syntax/constants.js +436 -0
- package/dist/lib/syntax/syntax.d.ts +38 -0
- package/dist/lib/syntax/syntax.js +533 -568
- package/dist/lib/validation/config.d.ts +14 -0
- package/dist/lib/validation/config.js +72 -33
- package/dist/lib/validation/config.json.js +1159 -74
- package/dist/lib/validation/json.d.ts +2 -0
- package/dist/lib/validation/match.d.ts +38 -0
- package/dist/lib/validation/match.js +2985 -0
- package/dist/lib/validation/parser/parse.d.ts +8 -0
- package/dist/lib/validation/parser/parse.js +684 -935
- package/dist/lib/validation/parser/typedef.d.ts +95 -0
- package/dist/lib/validation/parser/typedef.js +100 -0
- package/dist/lib/validation/utils/list.d.ts +4 -0
- package/dist/lib/validation/utils/list.js +4 -11
- package/dist/lib/validation/utils/whitespace.d.ts +2 -0
- package/dist/lib/validation/utils/whitespace.js +2 -8
- package/dist/node.d.ts +207 -0
- package/dist/node.js +53 -47
- package/dist/web.d.ts +169 -0
- package/dist/web.js +50 -41
- package/package.json +18 -13
- package/playground/index.html +1328 -0
- package/playground/sw.js +55 -0
- package/playground/tree.js +176 -0
- package/dist/lib/syntax/color/utils/constants.js +0 -214
- package/dist/lib/syntax/utils.js +0 -70
- package/dist/lib/validation/at-rules/container.js +0 -342
- package/dist/lib/validation/at-rules/counter-style.js +0 -90
- package/dist/lib/validation/at-rules/custom-media.js +0 -50
- package/dist/lib/validation/at-rules/document.js +0 -89
- package/dist/lib/validation/at-rules/else.js +0 -5
- package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
- package/dist/lib/validation/at-rules/import.js +0 -150
- package/dist/lib/validation/at-rules/keyframes.js +0 -67
- package/dist/lib/validation/at-rules/layer.js +0 -41
- package/dist/lib/validation/at-rules/media.js +0 -255
- package/dist/lib/validation/at-rules/namespace.js +0 -81
- package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
- package/dist/lib/validation/at-rules/page.js +0 -100
- package/dist/lib/validation/at-rules/supports.js +0 -295
- package/dist/lib/validation/at-rules/when.js +0 -185
- package/dist/lib/validation/atrule.js +0 -184
- package/dist/lib/validation/selector.js +0 -36
- package/dist/lib/validation/syntax.js +0 -1073
- package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
- package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
- package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
- package/dist/lib/validation/syntaxes/family-name.js +0 -57
- package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
- package/dist/lib/validation/syntaxes/layer-name.js +0 -57
- package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
- package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
- package/dist/lib/validation/syntaxes/selector-list.js +0 -5
- package/dist/lib/validation/syntaxes/selector.js +0 -5
- package/dist/lib/validation/syntaxes/url.js +0 -40
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
import { EnumToken } from '../types.js';
|
|
2
2
|
import { consumeWhitespace } from '../../validation/utils/whitespace.js';
|
|
3
|
-
import '../minify.js';
|
|
4
|
-
import '../walk.js';
|
|
5
|
-
import '../../parser/parse.js';
|
|
6
|
-
import '../../parser/tokenize.js';
|
|
7
|
-
import '../../parser/utils/config.js';
|
|
8
|
-
import '../../syntax/color/utils/constants.js';
|
|
9
|
-
import { filterValues, renderToken } from '../../renderer/render.js';
|
|
10
|
-
import '../../renderer/sourcemap/lib/encode.js';
|
|
11
3
|
import { compute } from '../transform/compute.js';
|
|
4
|
+
import { filterValues, renderToken } from '../../renderer/render.js';
|
|
12
5
|
import { minifyTransformFunctions, eqMatrix } from '../transform/minify.js';
|
|
13
6
|
import { FeatureWalkMode } from './type.js';
|
|
14
7
|
|
|
@@ -28,7 +21,7 @@ class TransformCssFeature {
|
|
|
28
21
|
}
|
|
29
22
|
}
|
|
30
23
|
run(ast) {
|
|
31
|
-
if (!(
|
|
24
|
+
if (!("chi" in ast)) {
|
|
32
25
|
return null;
|
|
33
26
|
}
|
|
34
27
|
let i = 0;
|
|
@@ -37,18 +30,21 @@ class TransformCssFeature {
|
|
|
37
30
|
for (; i < ast.chi.length; i++) {
|
|
38
31
|
// @ts-ignore
|
|
39
32
|
node = ast.chi[i];
|
|
40
|
-
if (node.typ != EnumToken.DeclarationNodeType ||
|
|
33
|
+
if (node.typ != EnumToken.DeclarationNodeType ||
|
|
34
|
+
!node.nam.match(/^(-[a-z]+-)?transform$/)) {
|
|
41
35
|
continue;
|
|
42
36
|
}
|
|
43
37
|
const children = [];
|
|
44
38
|
for (const child of node.val) {
|
|
45
|
-
children.push(child.typ == EnumToken.
|
|
39
|
+
children.push(child.typ == EnumToken.TransformFunctionTokenType
|
|
40
|
+
? minifyTransformFunctions(child)
|
|
41
|
+
: child);
|
|
46
42
|
}
|
|
47
43
|
consumeWhitespace(children);
|
|
48
44
|
let { matrix, cumulative, minified } = compute(children) ?? {
|
|
49
45
|
matrix: null,
|
|
50
46
|
cumulative: null,
|
|
51
|
-
minified: null
|
|
47
|
+
minified: null,
|
|
52
48
|
};
|
|
53
49
|
if (matrix == null || cumulative == null || minified == null) {
|
|
54
50
|
node.val = children;
|
|
@@ -63,7 +59,7 @@ class TransformCssFeature {
|
|
|
63
59
|
}
|
|
64
60
|
const l = renderToken(matrix).length;
|
|
65
61
|
node.val = r.reduce((acc, curr) => {
|
|
66
|
-
if (curr.reduce((acc, t) => acc + renderToken(t),
|
|
62
|
+
if (curr.reduce((acc, t) => acc + renderToken(t), "").length < l) {
|
|
67
63
|
return curr;
|
|
68
64
|
}
|
|
69
65
|
return acc;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { Token } from "../../@types/token.d.ts";
|
|
2
|
+
import type { AstNode, AstValueMatcher, TokenSearchResult } from "../../@types/ast.d.ts";
|
|
3
|
+
/**
|
|
4
|
+
* search the ast tree and return the first match
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
8
|
+
import { find, EnumToken, transform } from "@tbela99/css-parser";
|
|
9
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
10
|
+
|
|
11
|
+
* const css = `
|
|
12
|
+
|
|
13
|
+
button {
|
|
14
|
+
aspect-ratio: 1;
|
|
15
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
16
|
+
}
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
// find declaration which contain a '30px'
|
|
20
|
+
const nodeMatcher = (node: AstNode) =>
|
|
21
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
22
|
+
|
|
23
|
+
const result = await transform(css);
|
|
24
|
+
const node = find(result.ast, nodeMatcher);
|
|
25
|
+
|
|
26
|
+
console.log({node});
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
*
|
|
30
|
+
* @param ast
|
|
31
|
+
* @param matcher
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
export declare function find(ast: AstNode, matcher: (node: AstNode) => boolean): AstNode | null;
|
|
35
|
+
/**
|
|
36
|
+
* search the ast tree by checking each node's value and return the first match
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* // find the first ast node which contains the length token '30px'
|
|
40
|
+
import { findByValue, EnumToken, transform } from "@tbela99/css-parser";
|
|
41
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
42
|
+
|
|
43
|
+
* const css = `
|
|
44
|
+
|
|
45
|
+
button {
|
|
46
|
+
aspect-ratio: 1;
|
|
47
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
48
|
+
}
|
|
49
|
+
`;
|
|
50
|
+
|
|
51
|
+
// find declaration which contain a '30px'
|
|
52
|
+
const nodeMatcher = (value: Token) =>
|
|
53
|
+
return value.typ == EnumToken.LengthTokenType && (value as LengthToken).val == 30 && (value as LengthToken).unit == 'px' ;
|
|
54
|
+
|
|
55
|
+
const result = await transform(css);
|
|
56
|
+
const { node, value } = findByValue(result.ast, nodeMatcher) ?? {};
|
|
57
|
+
|
|
58
|
+
console.log({node, value});
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
*
|
|
62
|
+
* @param ast
|
|
63
|
+
* @param matcher
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
export declare function findByValue(ast: AstNode, matcher: AstValueMatcher): {
|
|
67
|
+
node: AstNode;
|
|
68
|
+
value: TokenSearchResult;
|
|
69
|
+
} | null;
|
|
70
|
+
/**
|
|
71
|
+
* search the ast tree and return all matches
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
75
|
+
import { findAll, EnumToken, transform } from "@tbela99/css-parser";
|
|
76
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
77
|
+
|
|
78
|
+
* const css = `
|
|
79
|
+
|
|
80
|
+
button {
|
|
81
|
+
aspect-ratio: 1;
|
|
82
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
83
|
+
}
|
|
84
|
+
`;
|
|
85
|
+
|
|
86
|
+
// find declaration which contain a '30px'
|
|
87
|
+
const nodeMatcher = (node: AstNode) =>
|
|
88
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
89
|
+
|
|
90
|
+
const result = await transform(css);
|
|
91
|
+
const nodes = findAll(result.ast, nodeMatcher);
|
|
92
|
+
|
|
93
|
+
console.log({nodes});
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
*
|
|
97
|
+
* @param ast
|
|
98
|
+
* @param matcher
|
|
99
|
+
* @returns
|
|
100
|
+
*/
|
|
101
|
+
export declare function findAll(ast: AstNode, matcher: (node: AstNode) => boolean): AstNode[];
|
|
102
|
+
/**
|
|
103
|
+
* search the ast tree and return the last match
|
|
104
|
+
*
|
|
105
|
+
* ```ts
|
|
106
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
107
|
+
import { findLast, EnumToken, transform } from "@tbela99/css-parser";
|
|
108
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
109
|
+
|
|
110
|
+
* const css = `
|
|
111
|
+
|
|
112
|
+
button {
|
|
113
|
+
aspect-ratio: 1;
|
|
114
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
115
|
+
}
|
|
116
|
+
`;
|
|
117
|
+
|
|
118
|
+
// find declaration which contain a '30px'
|
|
119
|
+
const nodeMatcher = (node: AstNode) =>
|
|
120
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
121
|
+
|
|
122
|
+
const result = await transform(css);
|
|
123
|
+
const node = findLast(result.ast, nodeMatcher);
|
|
124
|
+
|
|
125
|
+
console.log({node});
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
*
|
|
129
|
+
* @param ast
|
|
130
|
+
* @param matcher
|
|
131
|
+
* @returns
|
|
132
|
+
*/
|
|
133
|
+
export declare function findLast(ast: AstNode, matcher: (node: AstNode) => boolean): AstNode | null;
|
|
134
|
+
/**
|
|
135
|
+
* find the node's value token of the specified ast node
|
|
136
|
+
*
|
|
137
|
+
* ```ts
|
|
138
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
139
|
+
import { findValue, EnumToken, transform } from "@tbela99/css-parser";
|
|
140
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
141
|
+
|
|
142
|
+
* const css = `
|
|
143
|
+
|
|
144
|
+
button {
|
|
145
|
+
aspect-ratio: 1;
|
|
146
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
147
|
+
}
|
|
148
|
+
`;
|
|
149
|
+
|
|
150
|
+
// find declaration which contain a '30px'
|
|
151
|
+
const nodeMatcher = (node: AstNode) =>
|
|
152
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
153
|
+
|
|
154
|
+
const result = await transform(css);
|
|
155
|
+
const found = findValue(result.ast.chi[0], nodeMatcher);
|
|
156
|
+
|
|
157
|
+
console.log({found}); // 'button' token of the selector
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
*
|
|
161
|
+
* @param ast
|
|
162
|
+
* @param matcher
|
|
163
|
+
* @returns
|
|
164
|
+
*/
|
|
165
|
+
export declare function findValue(ast: AstNode | Token, matcher: (node: AstNode, parent?: AstNode | Token | null, root?: AstNode | null, parents?: Generator<Token> | null) => boolean): TokenSearchResult | null;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { EnumToken } from './types.js';
|
|
2
|
+
import { walk, walkValues } from './walk.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* search the ast tree and return the first match
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
9
|
+
import { find, EnumToken, transform } from "@tbela99/css-parser";
|
|
10
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
11
|
+
|
|
12
|
+
* const css = `
|
|
13
|
+
|
|
14
|
+
button {
|
|
15
|
+
aspect-ratio: 1;
|
|
16
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
17
|
+
}
|
|
18
|
+
`;
|
|
19
|
+
|
|
20
|
+
// find declaration which contain a '30px'
|
|
21
|
+
const nodeMatcher = (node: AstNode) =>
|
|
22
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
23
|
+
|
|
24
|
+
const result = await transform(css);
|
|
25
|
+
const node = find(result.ast, nodeMatcher);
|
|
26
|
+
|
|
27
|
+
console.log({node});
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
*
|
|
31
|
+
* @param ast
|
|
32
|
+
* @param matcher
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
function find(ast, matcher) {
|
|
36
|
+
for (const { node } of walk(ast)) {
|
|
37
|
+
if (matcher(node)) {
|
|
38
|
+
return node;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* search the ast tree by checking each node's value and return the first match
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* // find the first ast node which contains the length token '30px'
|
|
48
|
+
import { findByValue, EnumToken, transform } from "@tbela99/css-parser";
|
|
49
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
50
|
+
|
|
51
|
+
* const css = `
|
|
52
|
+
|
|
53
|
+
button {
|
|
54
|
+
aspect-ratio: 1;
|
|
55
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
// find declaration which contain a '30px'
|
|
60
|
+
const nodeMatcher = (value: Token) =>
|
|
61
|
+
return value.typ == EnumToken.LengthTokenType && (value as LengthToken).val == 30 && (value as LengthToken).unit == 'px' ;
|
|
62
|
+
|
|
63
|
+
const result = await transform(css);
|
|
64
|
+
const { node, value } = findByValue(result.ast, nodeMatcher) ?? {};
|
|
65
|
+
|
|
66
|
+
console.log({node, value});
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
*
|
|
70
|
+
* @param ast
|
|
71
|
+
* @param matcher
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
function findByValue(ast, matcher) {
|
|
75
|
+
let source;
|
|
76
|
+
for (const { node } of walk(ast)) {
|
|
77
|
+
source = null;
|
|
78
|
+
if (node.typ === EnumToken.DeclarationNodeType) {
|
|
79
|
+
source = node.val;
|
|
80
|
+
}
|
|
81
|
+
else if (Array.isArray(node.tokens)) {
|
|
82
|
+
source = node.tokens;
|
|
83
|
+
}
|
|
84
|
+
if (source == null) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
for (const { value, parent, root: rootNode, parents } of walkValues(source, node)) {
|
|
88
|
+
if (matcher(value, node)) {
|
|
89
|
+
return { node, value: { node: value, parent, root: rootNode, parents } };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* search the ast tree and return all matches
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
100
|
+
import { findAll, EnumToken, transform } from "@tbela99/css-parser";
|
|
101
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
102
|
+
|
|
103
|
+
* const css = `
|
|
104
|
+
|
|
105
|
+
button {
|
|
106
|
+
aspect-ratio: 1;
|
|
107
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
108
|
+
}
|
|
109
|
+
`;
|
|
110
|
+
|
|
111
|
+
// find declaration which contain a '30px'
|
|
112
|
+
const nodeMatcher = (node: AstNode) =>
|
|
113
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
114
|
+
|
|
115
|
+
const result = await transform(css);
|
|
116
|
+
const nodes = findAll(result.ast, nodeMatcher);
|
|
117
|
+
|
|
118
|
+
console.log({nodes});
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
*
|
|
122
|
+
* @param ast
|
|
123
|
+
* @param matcher
|
|
124
|
+
* @returns
|
|
125
|
+
*/
|
|
126
|
+
function findAll(ast, matcher) {
|
|
127
|
+
const result = [];
|
|
128
|
+
for (const { node } of walk(ast)) {
|
|
129
|
+
if (matcher(node)) {
|
|
130
|
+
result.push(node);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* search the ast tree and return the last match
|
|
137
|
+
*
|
|
138
|
+
* ```ts
|
|
139
|
+
* // find the first ast declaration node which name is 'aspect-ratio'
|
|
140
|
+
import { findLast, EnumToken, transform } from "@tbela99/css-parser";
|
|
141
|
+
import type { AstNode } from "@tbela99/css-parser";
|
|
142
|
+
|
|
143
|
+
* const css = `
|
|
144
|
+
|
|
145
|
+
button {
|
|
146
|
+
aspect-ratio: 1;
|
|
147
|
+
width: if(media(any-pointer: fine): 30px; else: 44px);
|
|
148
|
+
}
|
|
149
|
+
`;
|
|
150
|
+
|
|
151
|
+
// find declaration which contain a '30px'
|
|
152
|
+
const nodeMatcher = (node: AstNode) =>
|
|
153
|
+
return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
|
|
154
|
+
|
|
155
|
+
const result = await transform(css);
|
|
156
|
+
const node = findLast(result.ast, nodeMatcher);
|
|
157
|
+
|
|
158
|
+
console.log({node});
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
*
|
|
162
|
+
* @param ast
|
|
163
|
+
* @param matcher
|
|
164
|
+
* @returns
|
|
165
|
+
*/
|
|
166
|
+
function findLast(ast, matcher) {
|
|
167
|
+
for (const { node } of walk(ast, null, true)) {
|
|
168
|
+
if (matcher(node)) {
|
|
169
|
+
return node;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export { find, findAll, findByValue, findLast };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { BinaryExpressionToken, FunctionToken, Token } from "../../../@types/index.d.ts";
|
|
2
|
+
/**
|
|
3
|
+
* evaluate an array of tokens
|
|
4
|
+
* @param tokens
|
|
5
|
+
*/
|
|
6
|
+
export declare function evaluate(tokens: Token[]): Token[];
|
|
7
|
+
export declare function evaluateFunc(token: FunctionToken): Token[] | null;
|
|
8
|
+
/**
|
|
9
|
+
* convert BinaryExpression into an array
|
|
10
|
+
* @param token
|
|
11
|
+
*/
|
|
12
|
+
export declare function inlineExpression(token: Token): Token[];
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* generate a binary expression tree
|
|
16
|
+
* @param tokens
|
|
17
|
+
*/
|
|
18
|
+
export declare function buildExpression(tokens: Token[]): BinaryExpressionToken;
|