@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.
Files changed (105) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27383 -24083
  4. package/dist/index.d.ts +2737 -2200
  5. package/dist/package.json.js +2 -2
  6. package/dist/src/data/properties.json.js +1 -1
  7. package/dist/src/data/validation.json.js +1 -1
  8. package/dist/src/lib/ast/arena-node.js +548 -0
  9. package/dist/src/lib/ast/arena.js +3198 -0
  10. package/dist/src/lib/ast/features/calc.js +85 -76
  11. package/dist/src/lib/ast/features/if.js +155 -115
  12. package/dist/src/lib/ast/features/index.js +1 -1
  13. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  14. package/dist/src/lib/ast/features/prefix.js +184 -183
  15. package/dist/src/lib/ast/features/shorthand.js +40 -46
  16. package/dist/src/lib/ast/features/transform.js +24 -24
  17. package/dist/src/lib/ast/features/type.js +1 -1
  18. package/dist/src/lib/ast/fingerprint.js +333 -0
  19. package/dist/src/lib/ast/math/expression.js +458 -543
  20. package/dist/src/lib/ast/math/math.js +19 -41
  21. package/dist/src/lib/ast/minify.js +590 -647
  22. package/dist/src/lib/ast/transform/compute.js +82 -85
  23. package/dist/src/lib/ast/transform/matrix.js +21 -38
  24. package/dist/src/lib/ast/transform/minify.js +82 -193
  25. package/dist/src/lib/ast/transform/perspective.js +2 -3
  26. package/dist/src/lib/ast/transform/rotate.js +1 -1
  27. package/dist/src/lib/ast/transform/scale.js +1 -1
  28. package/dist/src/lib/ast/transform/skew.js +1 -1
  29. package/dist/src/lib/ast/transform/translate.js +1 -1
  30. package/dist/src/lib/ast/transform/utils.js +2 -2
  31. package/dist/src/lib/ast/types.js +263 -258
  32. package/dist/src/lib/ast/walk.js +246 -610
  33. package/dist/src/lib/fs/resolve.js +1 -1
  34. package/dist/src/lib/parser/declaration/list.js +223 -139
  35. package/dist/src/lib/parser/declaration/map.js +251 -238
  36. package/dist/src/lib/parser/declaration/set.js +74 -66
  37. package/dist/src/lib/parser/linesmap.js +1 -1
  38. package/dist/src/lib/parser/parse.js +1379 -1658
  39. package/dist/src/lib/parser/source.js +7 -4
  40. package/dist/src/lib/parser/tokenize.js +406 -199
  41. package/dist/src/lib/parser/utils/angle.js +11 -13
  42. package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
  43. package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
  44. package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
  45. package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
  46. package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
  47. package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
  48. package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
  49. package/dist/src/lib/parser/utils/at-rule.js +5 -5
  50. package/dist/src/lib/parser/utils/base64.js +2 -2
  51. package/dist/src/lib/parser/utils/cache.js +1 -1
  52. package/dist/src/lib/parser/utils/config.js +1 -1
  53. package/dist/src/lib/parser/utils/declaration.js +233 -234
  54. package/dist/src/lib/parser/utils/eq.js +50 -3
  55. package/dist/src/lib/parser/utils/hash.js +2 -35
  56. package/dist/src/lib/parser/utils/selector.js +309 -387
  57. package/dist/src/lib/parser/utils/text.js +18 -4
  58. package/dist/src/lib/parser/utils/token.js +16 -69
  59. package/dist/src/lib/parser/utils/type.js +37 -24
  60. package/dist/src/lib/printer/render.js +965 -511
  61. package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
  62. package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
  63. package/dist/src/lib/syntax/color/a98rgb.js +1 -1
  64. package/dist/src/lib/syntax/color/alpha.js +36 -37
  65. package/dist/src/lib/syntax/color/cmyk.js +35 -52
  66. package/dist/src/lib/syntax/color/color-mix.js +96 -132
  67. package/dist/src/lib/syntax/color/color.js +222 -231
  68. package/dist/src/lib/syntax/color/hex.js +45 -55
  69. package/dist/src/lib/syntax/color/hsl.js +54 -62
  70. package/dist/src/lib/syntax/color/hwb.js +60 -68
  71. package/dist/src/lib/syntax/color/lab.js +59 -67
  72. package/dist/src/lib/syntax/color/lch.js +59 -67
  73. package/dist/src/lib/syntax/color/oklab.js +60 -68
  74. package/dist/src/lib/syntax/color/oklch.js +60 -67
  75. package/dist/src/lib/syntax/color/p3.js +1 -1
  76. package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
  77. package/dist/src/lib/syntax/color/rec2020.js +1 -1
  78. package/dist/src/lib/syntax/color/relative-color.js +94 -144
  79. package/dist/src/lib/syntax/color/rgb.js +54 -57
  80. package/dist/src/lib/syntax/color/srgb.js +55 -53
  81. package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
  82. package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
  83. package/dist/src/lib/syntax/color/utils/components.js +19 -20
  84. package/dist/src/lib/syntax/color/utils/distance.js +15 -13
  85. package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
  86. package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
  87. package/dist/src/lib/syntax/color/xyz.js +1 -1
  88. package/dist/src/lib/syntax/color/xyzd50.js +1 -1
  89. package/dist/src/lib/syntax/constants.js +2 -16
  90. package/dist/src/lib/syntax/syntax.js +301 -407
  91. package/dist/src/lib/validation/config.js +1 -1
  92. package/dist/src/lib/validation/match.js +959 -723
  93. package/dist/src/lib/validation/parser/parse.js +230 -112
  94. package/dist/src/lib/validation/parser/typedef.js +1 -1
  95. package/dist/src/lib/validation/utils/list.js +27 -12
  96. package/dist/src/node.js +12 -72
  97. package/dist/src/types.js +1 -1
  98. package/dist/src/utils/sync.js +9 -6
  99. package/dist/src/web.js +11 -73
  100. package/package.json +9 -10
  101. package/dist/index.cjs +0 -35870
  102. package/dist/src/lib/ast/clone.js +0 -51
  103. package/dist/src/lib/ast/expand.js +0 -311
  104. package/dist/src/lib/ast/find.js +0 -192
  105. package/dist/src/lib/ast/node.js +0 -59
@@ -1,51 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 1.8.2
6
- License: MIT
7
- */
8
- import { EnumToken } from './types.js';
9
-
10
- /**
11
- *
12
- * Clone an ast node or value
13
- * @param node
14
- * @param cloneChildren
15
- * @param cloneMap
16
- * @returns
17
- */
18
- function cloneNode(node, cloneChildren = false, cloneMap = null) {
19
- const checkNode = node.typ == EnumToken.DeclarationNodeType ? "val" : "chi";
20
- const clone = {};
21
- cloneMap?.set?.(node, clone);
22
- for (const [name, value] of Object.entries(node)) {
23
- if (value == null || typeof value != "object") {
24
- // @ts-ignore
25
- clone[name] = value;
26
- }
27
- else if (Array.isArray(value)) {
28
- // @ts-ignore
29
- clone[name] = [];
30
- if (cloneChildren || name !== checkNode) {
31
- for (const c of value) {
32
- const newObj = cloneNode(c, cloneChildren, cloneMap);
33
- cloneMap?.set?.(c, newObj);
34
- // @ts-ignore
35
- clone[name].push(newObj);
36
- }
37
- }
38
- }
39
- else {
40
- // @ts-ignore
41
- clone[name] = { ...value };
42
- }
43
- }
44
- for (const symbol of Object.getOwnPropertySymbols(node)) {
45
- // @ts-ignore
46
- clone[symbol] = node[symbol];
47
- }
48
- return clone;
49
- }
50
-
51
- export { cloneNode };
@@ -1,311 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 1.8.2
6
- License: MIT
7
- */
8
- import { splitRule } from './minify.js';
9
- import { STATE, PARENT, combinators, RAW } from '../syntax/constants.js';
10
- import { parseString } from '../parser/parse.js';
11
- import { walkValues } from './walk.js';
12
- import { renderValue } from '../printer/render.js';
13
- import { EnumAstNodeStatus, EnumToken } from './types.js';
14
- import { cloneNode } from './clone.js';
15
-
16
- /**
17
- * expand css nesting ast nodes
18
- * @param ast
19
- *
20
- * @private
21
- */
22
- function expand(ast) {
23
- if (ast[STATE] &
24
- (EnumAstNodeStatus.Invalid |
25
- EnumAstNodeStatus.Disallowed |
26
- EnumAstNodeStatus.Unknown |
27
- EnumAstNodeStatus.Unparsed |
28
- EnumAstNodeStatus.Malformed)) {
29
- return ast;
30
- }
31
- const result = Object.assign(cloneNode(ast), { chi: [] });
32
- let children;
33
- for (let i = 0; i < ast.chi.length; i++) {
34
- let node = ast.chi[i];
35
- if (node.typ === EnumToken.RuleNodeType) {
36
- children = expandRule(node);
37
- for (const child of children) {
38
- child[PARENT] = result;
39
- result.chi.push(child);
40
- }
41
- }
42
- else if (node.typ == EnumToken.AtRuleNodeType && "chi" in node) {
43
- let hasRule = false;
44
- let j = node.chi.length;
45
- while (j--) {
46
- // @ts-ignore
47
- if (node.chi[j].typ == EnumToken.RuleNodeType || node.chi[j].typ == EnumToken.AtRuleNodeType) {
48
- hasRule = true;
49
- break;
50
- }
51
- }
52
- if (hasRule) {
53
- node = expand(node);
54
- for (const child of node.chi) {
55
- child[PARENT] = result;
56
- }
57
- node[PARENT] = result;
58
- // @ts-ignore
59
- result.chi.push(node);
60
- }
61
- else {
62
- node[PARENT] = result;
63
- // @ts-ignore
64
- result.chi.push(node);
65
- }
66
- }
67
- else {
68
- node[PARENT] = result;
69
- // @ts-ignore
70
- result.chi.push(node);
71
- }
72
- }
73
- return result;
74
- }
75
- function expandRule(node) {
76
- if (node[STATE] &
77
- (EnumAstNodeStatus.Invalid |
78
- EnumAstNodeStatus.Disallowed |
79
- EnumAstNodeStatus.Unknown |
80
- EnumAstNodeStatus.Unparsed |
81
- EnumAstNodeStatus.Malformed)) {
82
- return [node];
83
- }
84
- const ast = Object.assign(cloneNode(node), { chi: node.chi.slice() });
85
- const result = [];
86
- if (ast.typ == EnumToken.RuleNodeType) {
87
- let i = 0;
88
- for (; i < ast.chi.length; i++) {
89
- if (ast.chi[i].typ == EnumToken.RuleNodeType) {
90
- const rule = ast.chi[i];
91
- if (!rule.sel.includes("&")) {
92
- const selRule = splitRule(rule.sel);
93
- const arSelf = splitRule(ast.sel)
94
- .filter((r) => r.every((t) => t != ":before" && t != ":after" && !t.startsWith("::")))
95
- .reduce((acc, curr) => acc.concat([curr.join("")]), [])
96
- .join(",");
97
- if (arSelf.length == 0) {
98
- ast.chi.splice(i--, 1);
99
- continue;
100
- }
101
- for (let i1 = 0; i1 < selRule.length; i1++) {
102
- const arr = selRule[i1];
103
- combinators.includes(arr[0].charAt(0)) ? arr.unshift(arSelf) : arr.unshift(arSelf, " ");
104
- }
105
- rule.sel = selRule
106
- .reduce((acc, curr) => {
107
- acc.push(curr.join(""));
108
- return acc;
109
- }, [])
110
- .join(",");
111
- }
112
- else {
113
- let childSelectorCompound = [];
114
- let withCompound = [];
115
- let withoutCompound = [];
116
- // pseudo elements cannot be used with '&'
117
- // https://www.w3.org/TR/css-nesting-1/#example-7145ff1e
118
- const rules = splitRule(ast.sel).filter((r) => r.every((t) => t != ":before" && t != ":after" && !t.startsWith("::")));
119
- const parentSelector = !node.sel.includes("&");
120
- if (rules.length == 0) {
121
- ast.chi.splice(i--, 1);
122
- continue;
123
- }
124
- for (const sel of rule[RAW] ?? splitRule(rule.sel)) {
125
- const s = sel.join("");
126
- if (s.includes("&") || parentSelector) {
127
- if (s.indexOf("&", 1) == -1) {
128
- if (s.at(0) == "&") {
129
- if (s.at(1) == " ") {
130
- childSelectorCompound.push(s.slice(2));
131
- }
132
- else {
133
- if (s == "&" || parentSelector) {
134
- withCompound.push(s);
135
- }
136
- }
137
- }
138
- else {
139
- withoutCompound.push(s);
140
- }
141
- }
142
- else {
143
- withCompound.push(s);
144
- }
145
- }
146
- }
147
- const selectors = [];
148
- const selector = rules.length > 1 ? ":is(" + rules.map((a) => a.join("")).join(",") + ")" : rules[0].join("");
149
- if (childSelectorCompound.length > 0) {
150
- if (childSelectorCompound.length == 1) {
151
- selectors.push(replaceCompound("& " + childSelectorCompound[0].trim(), selector));
152
- }
153
- else {
154
- selectors.push(replaceCompound("& :is(" +
155
- childSelectorCompound.reduce((acc, curr) => acc + (acc.length > 0 ? "," : "") + curr.trim(), "") +
156
- ")", selector));
157
- }
158
- }
159
- if (withCompound.length > 0) {
160
- if (withCompound.every((t) => t[0] == "&" && t.indexOf("&", 1) == -1)) {
161
- // for (const w of withCompound) {
162
- // for (let m = 0; m < w.length; m++) {
163
- // // for (let n = 0; n < w[m].length; n++) {
164
- // withoutCompound.push(w[m].slice(1));
165
- // // }
166
- // }
167
- // }
168
- withoutCompound.push(...withCompound.map((t) => t.slice(1)));
169
- withCompound.length = 0;
170
- }
171
- }
172
- if (withoutCompound.length > 0) {
173
- if (withoutCompound.length == 1) {
174
- const useIs = rules.length == 1 &&
175
- selector.match(/^[a-zA-Z.:]/) != null &&
176
- selector.includes(" ") &&
177
- withoutCompound.length == 1 &&
178
- withoutCompound[0].match(/^[a-zA-Z]+$/) != null;
179
- const compound = useIs ? ":is(&)" : "&";
180
- selectors.push(replaceCompound(rules.length == 1
181
- ? useIs
182
- ? withoutCompound[0] + ":is(&)"
183
- : selector.match(/^[.:]/) && withoutCompound[0].match(/^[a-zA-Z]+$/)
184
- ? withoutCompound[0] + compound
185
- : compound + withoutCompound[0]
186
- : withoutCompound[0].match(/^[a-zA-Z:]+$/)
187
- ? withoutCompound[0].trim() + compound
188
- : "&" +
189
- (withoutCompound[0].match(/^\S+$/)
190
- ? withoutCompound[0].trim()
191
- : ":is(" + withoutCompound[0].trim() + ")"), selector));
192
- }
193
- else {
194
- selectors.push(replaceCompound("&:is(" +
195
- withoutCompound.reduce((acc, curr) => acc + (acc.length > 0 ? "," : "") + curr.trim(), "") +
196
- ")", selector));
197
- }
198
- }
199
- if (withCompound.length > 0) {
200
- if (withCompound.length == 1) {
201
- selectors.push(replaceCompound(withCompound[0], selector));
202
- }
203
- }
204
- rule.sel = selectors.reduce((acc, curr) => (curr.length == 0 ? acc : acc + (acc.length > 0 ? "," : "") + curr), "");
205
- }
206
- ast.chi.splice(i--, 1);
207
- for (const s of expandRule(rule)) {
208
- result.push(s);
209
- }
210
- }
211
- else if (ast.chi[i].typ == EnumToken.AtRuleNodeType) {
212
- let astAtRule = ast.chi[i];
213
- const values = [];
214
- if (astAtRule.nam === "scope") {
215
- if (astAtRule.val.includes("&")) {
216
- astAtRule.val = replaceCompound(astAtRule.val, ast.sel);
217
- }
218
- const slice = astAtRule.chi
219
- .slice()
220
- .filter((t) => t.typ == EnumToken.RuleNodeType && t.sel.includes("&"));
221
- if (slice.length > 0) {
222
- expandRule({ ...node, chi: astAtRule.chi.slice() });
223
- }
224
- }
225
- else {
226
- // @ts-ignore
227
- const clone = { ...ast, chi: astAtRule.chi.slice() };
228
- // @ts-ignore
229
- astAtRule.chi.length = 0;
230
- for (const r of expandRule(clone)) {
231
- if (r.typ == EnumToken.AtRuleNodeType && "chi" in r) {
232
- if (astAtRule.val !== "" && r.val !== "") {
233
- if (astAtRule.nam === "media" && r.nam === "media") {
234
- r.val = astAtRule.val + " and " + r.val;
235
- }
236
- else if (astAtRule.nam == "layer" && r.nam == "layer") {
237
- r.val = astAtRule.val + "." + r.val;
238
- }
239
- }
240
- // @ts-ignore
241
- values.push(r);
242
- }
243
- else if (r.typ == EnumToken.RuleNodeType) {
244
- for (const rule of expandRule(r)) {
245
- // @ts-ignore
246
- astAtRule.chi.push(rule);
247
- }
248
- }
249
- }
250
- }
251
- if (astAtRule.chi.length > 0) {
252
- result.push(astAtRule);
253
- }
254
- for (const r of values) {
255
- result.push(r);
256
- }
257
- ast.chi.splice(i--, 1);
258
- }
259
- }
260
- }
261
- // @ts-ignore
262
- return ast.chi.length > 0 ? [ast].concat(result) : result;
263
- }
264
- /**
265
- * replace compound selector
266
- * @param input
267
- * @param replace
268
- */
269
- function replaceCompound(input, replace) {
270
- const tokens = parseString(input);
271
- let replacement = null;
272
- for (const t of walkValues(tokens)) {
273
- if (t.value.typ == EnumToken.NestingSelectorTokenType) {
274
- if (tokens.length == 2) {
275
- if (replacement == null) {
276
- replacement = parseString(replace);
277
- }
278
- Object.assign(t.value, {
279
- typ: EnumToken.LiteralTokenType,
280
- val: replaceCompoundLiteral(t.value.val, replace),
281
- });
282
- continue;
283
- }
284
- const rule = splitRule(replace);
285
- Object.assign(t.value, {
286
- typ: EnumToken.LiteralTokenType,
287
- val: rule.length > 1 ? ":is(" + replace + ")" : replace,
288
- });
289
- }
290
- }
291
- return tokens.reduce((acc, curr) => acc + renderValue(curr), "");
292
- }
293
- function replaceCompoundLiteral(selector, replace) {
294
- const tokens = [""];
295
- let i = 0;
296
- for (; i < selector.length; i++) {
297
- if (selector.charAt(i) == "&") {
298
- tokens.push("&", "");
299
- }
300
- }
301
- return tokens
302
- .sort((a, b) => {
303
- if (a == "&") {
304
- return 1;
305
- }
306
- return b == "&" ? -1 : 0;
307
- })
308
- .reduce((acc, curr) => acc + (curr == "&" ? replace : curr), "");
309
- }
310
-
311
- export { expand, replaceCompound };
@@ -1,192 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 1.8.2
6
- License: MIT
7
- */
8
- import { EnumToken } from './types.js';
9
- import { walk, walkValues } from './walk.js';
10
- import { PARENT, TOKENS } from '../syntax/constants.js';
11
-
12
- /**
13
- * Search the ast subtree and return the first match
14
- *
15
- * ```ts
16
- * // find the first ast declaration node which name is 'aspect-ratio'
17
- import { find, EnumToken, transform } from "@tbela99/css-parser";
18
- import type { AstNode } from "@tbela99/css-parser";
19
-
20
- * const css = `
21
-
22
- button {
23
- aspect-ratio: 1;
24
- width: if(media(any-pointer: fine): 30px; else: 44px);
25
- }
26
- `;
27
-
28
- // find declaration which contain a '30px'
29
- const nodeMatcher = (node: AstNode) =>
30
- return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
31
-
32
- const result = await transform(css);
33
- const node = find(result.ast, nodeMatcher);
34
-
35
- console.log({node});
36
-
37
- ```
38
- *
39
- * @param ast
40
- * @param matcher
41
- * @returns
42
- */
43
- function find(ast, matcher) {
44
- for (const r of walk(ast)) {
45
- if (matcher(r.node, r.parent)) {
46
- return r.node;
47
- }
48
- }
49
- return null;
50
- }
51
- /**
52
- *
53
- * @param ast
54
- * @param matcher
55
- * @returns
56
- *
57
- * Search the ast sub-tree by checking each node's value token and return the first match
58
- *
59
- ```ts
60
- // find the first ast node which contains the length token '30px'
61
- import { findByValue, EnumToken, transform } from "@tbela99/css-parser";
62
- import type { AstNode } from "@tbela99/css-parser";
63
-
64
- const css = `
65
-
66
- button {
67
- aspect-ratio: 1;
68
- width: if(media(any-pointer: fine): 30px; else: 44px);
69
- }
70
- `;
71
-
72
- // find declaration which contain the length token '30px'
73
- const nodeMatcher = (value: Token) =>
74
- return value.typ == EnumToken.LengthTokenType && (value as LengthToken).val == 30 && (value as LengthToken).unit == 'px' ;
75
-
76
- const result = await transform(css);
77
- const { node, value } = findByValue(result.ast, nodeMatcher) ?? {};
78
-
79
- console.log({node, value});
80
-
81
- ```
82
- */
83
- function findByValue(ast, matcher) {
84
- let source;
85
- for (const r1 of walk(ast)) {
86
- source = null;
87
- if (r1.node.typ === EnumToken.DeclarationNodeType) {
88
- source = r1.node.val;
89
- }
90
- else if (Array.isArray(r1.node[TOKENS])) {
91
- source = r1.node[TOKENS];
92
- }
93
- if (source == null) {
94
- continue;
95
- }
96
- for (const r2 of walkValues(source, r1.node)) {
97
- if (matcher(r2.value, r1.node)) {
98
- return {
99
- // @ts-ignore
100
- node: r1.node,
101
- // @ts-ignore
102
- value: { node: r2.value, parent: r2.parent, root: r2.root, parents: r2.parents },
103
- };
104
- }
105
- }
106
- }
107
- return null;
108
- }
109
- /**
110
- * Search the ast sub-tree and return all matches
111
- *
112
- * ```ts
113
- * // find the first ast declaration node which name is 'aspect-ratio'
114
- * import { findAll, EnumToken, transform } from "@tbela99/css-parser";
115
- * import type { AstNode } from "@tbela99/css-parser";
116
-
117
- const css = `
118
-
119
- button {
120
- aspect-ratio: 1;
121
- width: if(media(any-pointer: fine): 30px; else: 44px);
122
- }
123
- button-small {
124
- aspect-ratio: 1;
125
- width: if(media(any-pointer: fine): 20px; else: 30px);
126
- }
127
- `;
128
-
129
- const nodeMatcher = (node: AstNode) =>
130
- return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
131
-
132
- const result = await transform(css);
133
- const nodes = findAll(result.ast, nodeMatcher);
134
-
135
- console.log({nodes});
136
-
137
- ```
138
- *
139
- * @param ast
140
- * @param matcher
141
- * @returns
142
- */
143
- function findAll(ast, matcher) {
144
- const result = [];
145
- for (const r of walk(ast)) {
146
- if (matcher(r.node, r.node[PARENT])) {
147
- result.push(r.node);
148
- }
149
- }
150
- return result;
151
- }
152
- /**
153
- * Search the ast sub-tree and return the last match.
154
- *
155
- * ```ts
156
- * // find the first ast declaration node which name is 'aspect-ratio'
157
- import { findLast, EnumToken, transform } from "@tbela99/css-parser";
158
- import type { AstNode } from "@tbela99/css-parser";
159
-
160
- * const css = `
161
-
162
- button {
163
- aspect-ratio: 1;
164
- width: if(media(any-pointer: fine): 30px; else: 44px);
165
- }
166
- `;
167
-
168
- const nodeMatcher = (node: AstNode) =>
169
- return node.typ == EnumToken.DeclarationNodeType && (node as AstDeclaration).nam == 'aspect-ratio';
170
-
171
- const result = await transform(css);
172
- const node = findLast(result.ast, nodeMatcher);
173
-
174
- console.log({node});
175
-
176
- ```
177
- *
178
- * @param ast
179
- * @param matcher
180
- * @returns
181
- */
182
- function findLast(ast, matcher) {
183
- for (const result of walk(ast, null, true))
184
- // for (const { node, parent } of walk(ast as AstNode, null, true)) {
185
- if (matcher(result.node, result.parent)) {
186
- return result.node;
187
- }
188
- // }
189
- return null;
190
- }
191
-
192
- export { find, findAll, findByValue, findLast };
@@ -1,59 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 1.8.2
6
- License: MIT
7
- */
8
- import { TOKENS, ERRORS, STATE, LOCSRCID, LOCSTA, LOCEND, PARENT } from '../syntax/constants.js';
9
-
10
- /**
11
- *
12
- * @param node
13
- * @param key
14
- * @returns
15
- */
16
- function getNodeProperty(node, key) {
17
- switch (key) {
18
- case "parent":
19
- return node[PARENT];
20
- case "location":
21
- return node[LOCSRCID] == null && node[LOCSTA] == null && node[LOCEND] == null ? null : { srcId: node[LOCSRCID], sta: node[LOCSTA], end: node[LOCEND] };
22
- case "state":
23
- return node[STATE];
24
- case "errors":
25
- return node[ERRORS];
26
- case "tokens":
27
- return node[TOKENS];
28
- }
29
- return undefined;
30
- }
31
- /**
32
- *
33
- * @param node
34
- * @param key
35
- * @param value
36
- */
37
- function setNodeProperty(node, key, value) {
38
- switch (key) {
39
- case "parent":
40
- node[PARENT] = value;
41
- break;
42
- case "location":
43
- node[LOCSRCID] = value.srcId;
44
- node[LOCSTA] = value.sta;
45
- node[LOCEND] = value.end;
46
- break;
47
- case "state":
48
- node[STATE] = value;
49
- break;
50
- case "errors":
51
- node[ERRORS] = value;
52
- break;
53
- case "tokens":
54
- node[TOKENS] = value;
55
- break;
56
- }
57
- }
58
-
59
- export { getNodeProperty, setNodeProperty };