@tbela99/css-parser 1.8.2 → 2.0.1
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 +66 -0
- package/README.md +129 -31
- package/dist/index-umd-web.js +27383 -24083
- package/dist/index.d.ts +2737 -2200
- package/dist/package.json.js +2 -2
- package/dist/src/data/properties.json.js +1 -1
- package/dist/src/data/validation.json.js +1 -1
- package/dist/src/lib/ast/arena-node.js +548 -0
- package/dist/src/lib/ast/arena.js +3198 -0
- package/dist/src/lib/ast/cache.js +333 -0
- package/dist/src/lib/ast/features/calc.js +85 -76
- package/dist/src/lib/ast/features/if.js +155 -115
- package/dist/src/lib/ast/features/index.js +1 -1
- package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
- package/dist/src/lib/ast/features/prefix.js +184 -183
- package/dist/src/lib/ast/features/shorthand.js +40 -46
- package/dist/src/lib/ast/features/transform.js +24 -24
- package/dist/src/lib/ast/features/type.js +1 -1
- package/dist/src/lib/ast/math/expression.js +458 -543
- package/dist/src/lib/ast/math/math.js +19 -41
- package/dist/src/lib/ast/minify.js +590 -647
- package/dist/src/lib/ast/transform/compute.js +82 -85
- package/dist/src/lib/ast/transform/matrix.js +21 -38
- package/dist/src/lib/ast/transform/minify.js +82 -193
- package/dist/src/lib/ast/transform/perspective.js +2 -3
- package/dist/src/lib/ast/transform/rotate.js +1 -1
- package/dist/src/lib/ast/transform/scale.js +1 -1
- package/dist/src/lib/ast/transform/skew.js +1 -1
- package/dist/src/lib/ast/transform/translate.js +1 -1
- package/dist/src/lib/ast/transform/utils.js +2 -2
- package/dist/src/lib/ast/types.js +263 -258
- package/dist/src/lib/ast/walk.js +246 -610
- package/dist/src/lib/fs/resolve.js +1 -1
- package/dist/src/lib/parser/declaration/list.js +223 -139
- package/dist/src/lib/parser/declaration/map.js +251 -238
- package/dist/src/lib/parser/declaration/set.js +74 -66
- package/dist/src/lib/parser/linesmap.js +1 -1
- package/dist/src/lib/parser/parse.js +1379 -1658
- package/dist/src/lib/parser/source.js +7 -4
- package/dist/src/lib/parser/tokenize.js +406 -199
- package/dist/src/lib/parser/utils/angle.js +11 -13
- package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
- package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
- package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
- package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
- package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
- package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
- package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
- package/dist/src/lib/parser/utils/at-rule.js +5 -5
- package/dist/src/lib/parser/utils/base64.js +2 -2
- package/dist/src/lib/parser/utils/cache.js +1 -1
- package/dist/src/lib/parser/utils/config.js +1 -1
- package/dist/src/lib/parser/utils/declaration.js +233 -234
- package/dist/src/lib/parser/utils/eq.js +50 -3
- package/dist/src/lib/parser/utils/hash.js +2 -35
- package/dist/src/lib/parser/utils/selector.js +309 -387
- package/dist/src/lib/parser/utils/text.js +18 -4
- package/dist/src/lib/parser/utils/token.js +16 -69
- package/dist/src/lib/parser/utils/type.js +37 -24
- package/dist/src/lib/printer/render.js +965 -511
- package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
- package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
- package/dist/src/lib/syntax/color/a98rgb.js +1 -1
- package/dist/src/lib/syntax/color/alpha.js +36 -37
- package/dist/src/lib/syntax/color/cmyk.js +35 -52
- package/dist/src/lib/syntax/color/color-mix.js +96 -132
- package/dist/src/lib/syntax/color/color.js +222 -231
- package/dist/src/lib/syntax/color/hex.js +45 -55
- package/dist/src/lib/syntax/color/hsl.js +54 -62
- package/dist/src/lib/syntax/color/hwb.js +60 -68
- package/dist/src/lib/syntax/color/lab.js +59 -67
- package/dist/src/lib/syntax/color/lch.js +59 -67
- package/dist/src/lib/syntax/color/oklab.js +60 -68
- package/dist/src/lib/syntax/color/oklch.js +60 -67
- package/dist/src/lib/syntax/color/p3.js +1 -1
- package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
- package/dist/src/lib/syntax/color/rec2020.js +1 -1
- package/dist/src/lib/syntax/color/relative-color.js +94 -144
- package/dist/src/lib/syntax/color/rgb.js +54 -57
- package/dist/src/lib/syntax/color/srgb.js +55 -53
- package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
- package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
- package/dist/src/lib/syntax/color/utils/components.js +19 -20
- package/dist/src/lib/syntax/color/utils/distance.js +15 -13
- package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
- package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
- package/dist/src/lib/syntax/color/xyz.js +1 -1
- package/dist/src/lib/syntax/color/xyzd50.js +1 -1
- package/dist/src/lib/syntax/constants.js +2 -16
- package/dist/src/lib/syntax/syntax.js +301 -407
- package/dist/src/lib/validation/config.js +1 -1
- package/dist/src/lib/validation/match.js +959 -723
- package/dist/src/lib/validation/parser/parse.js +230 -112
- package/dist/src/lib/validation/parser/typedef.js +1 -1
- package/dist/src/lib/validation/utils/list.js +27 -12
- package/dist/src/node.js +12 -72
- package/dist/src/types.js +1 -1
- package/dist/src/utils/sync.js +9 -6
- package/dist/src/web.js +11 -73
- package/package.json +9 -10
- package/dist/index.cjs +0 -35870
- package/dist/src/lib/ast/clone.js +0 -51
- package/dist/src/lib/ast/expand.js +0 -311
- package/dist/src/lib/ast/find.js +0 -192
- package/dist/src/lib/ast/node.js +0 -59
package/dist/src/lib/ast/walk.js
CHANGED
|
@@ -2,657 +2,293 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.1
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
import { EnumToken } from './types.js';
|
|
8
|
+
import { WalkerOptionEnum, EnumAstNodeStatus, WalkerEvent, EnumToken } from './types.js';
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
|
-
*
|
|
11
|
+
* push the direct value tokens of a token onto a stack, from the linked lists: the token list, then the fields (the
|
|
12
|
+
* tokens of the list fields) of a structured token. they are pushed in order, then reversed in place (unless reverse
|
|
13
|
+
* is true) so that they are popped in order: no array is created
|
|
14
|
+
* @param arena
|
|
15
|
+
* @param stack
|
|
16
|
+
* @param value
|
|
17
|
+
* @param reverse
|
|
13
18
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
19
|
+
function pushValues(arena, stack, value, reverse) {
|
|
20
|
+
const first = stack.length;
|
|
21
|
+
let token = arena.firstToken[value];
|
|
22
|
+
while (token != 0) {
|
|
23
|
+
stack.push(token);
|
|
24
|
+
token = arena.nextSibling[token];
|
|
25
|
+
}
|
|
26
|
+
if ((arena.flags[value] & EnumAstNodeStatus.HasChildren) == 0) {
|
|
27
|
+
let child = arena.firstChild[value];
|
|
28
|
+
while (child != 0) {
|
|
29
|
+
if (arena.dataTypes[child] == EnumToken.ListTokenType) {
|
|
30
|
+
token = arena.firstToken[child];
|
|
31
|
+
while (token != 0) {
|
|
32
|
+
stack.push(token);
|
|
33
|
+
token = arena.nextSibling[token];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
stack.push(child);
|
|
38
|
+
}
|
|
39
|
+
child = arena.nextSibling[child];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (!reverse) {
|
|
43
|
+
for (let i = first, j = stack.length - 1; i < j; i++, j--) {
|
|
44
|
+
const swap = stack[i];
|
|
45
|
+
stack[i] = stack[j];
|
|
46
|
+
stack[j] = swap;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
33
50
|
/**
|
|
34
|
-
*
|
|
51
|
+
* iterator of {@link AstArena.walk}: node ids (and value tokens when `attributes` is set) in document order. the
|
|
52
|
+
* children of a node are read when the next node is requested: the node may be modified by the caller
|
|
35
53
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
54
|
+
class NodeWalker {
|
|
55
|
+
arena;
|
|
56
|
+
stack;
|
|
57
|
+
filter = null;
|
|
58
|
+
reverse = false;
|
|
59
|
+
children = true;
|
|
60
|
+
attributes = false;
|
|
61
|
+
// iteration result, reused
|
|
62
|
+
iteration = { done: false, value: 0 };
|
|
63
|
+
// node returned by the last call of next(): its children are pushed by the next call
|
|
64
|
+
current = 0;
|
|
65
|
+
// value returned by the filter for the current node
|
|
66
|
+
option = null;
|
|
67
|
+
constructor(arena, node, filter, reverse) {
|
|
68
|
+
this.arena = arena;
|
|
69
|
+
this.stack = [node];
|
|
70
|
+
if (filter != null && typeof filter == "object") {
|
|
71
|
+
this.filter = filter.filter ?? null;
|
|
72
|
+
this.reverse = filter.reverse === true;
|
|
73
|
+
this.attributes = filter.attributes === true;
|
|
74
|
+
this.children = filter.children ?? true;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this.filter = filter ?? null;
|
|
78
|
+
this.reverse = reverse === true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
[Symbol.iterator]() {
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
42
84
|
/**
|
|
43
|
-
*
|
|
85
|
+
* push the value tokens and the children of a node
|
|
86
|
+
* @param node
|
|
87
|
+
* @param option
|
|
44
88
|
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
* Walk ast nodes
|
|
49
|
-
* @param node initial node
|
|
50
|
-
* @param filter control the walk process
|
|
51
|
-
* @param reverse walk in reverse order
|
|
52
|
-
*
|
|
53
|
-
* @private
|
|
54
|
-
*
|
|
55
|
-
* ```ts
|
|
56
|
-
*
|
|
57
|
-
* import {walk} from '@tbela99/css-parser';
|
|
58
|
-
*
|
|
59
|
-
* const css = `
|
|
60
|
-
* body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); }
|
|
61
|
-
*
|
|
62
|
-
* html,
|
|
63
|
-
* body {
|
|
64
|
-
* line-height: 1.474;
|
|
65
|
-
* }
|
|
66
|
-
*
|
|
67
|
-
* .ruler {
|
|
68
|
-
*
|
|
69
|
-
* height: 10px;
|
|
70
|
-
* }
|
|
71
|
-
* `;
|
|
72
|
-
*
|
|
73
|
-
* for (const {node, parent, root} of walk(ast)) {
|
|
74
|
-
*
|
|
75
|
-
* // do something with node
|
|
76
|
-
* }
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* Using a {@link filter} function to control the ast traversal. the filter function returns a value of type {@link WalkerOption}.
|
|
80
|
-
*
|
|
81
|
-
* ```ts
|
|
82
|
-
* import {EnumToken, transform, walk, WalkerOptionEnum} from '@tbela99/css-parser';
|
|
83
|
-
*
|
|
84
|
-
* const css = `
|
|
85
|
-
* body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); }
|
|
86
|
-
*
|
|
87
|
-
* html,
|
|
88
|
-
* body {
|
|
89
|
-
* line-height: 1.474;
|
|
90
|
-
* }
|
|
91
|
-
*
|
|
92
|
-
* .ruler {
|
|
93
|
-
*
|
|
94
|
-
* height: 10px;
|
|
95
|
-
* }
|
|
96
|
-
* `;
|
|
97
|
-
*
|
|
98
|
-
* function filter(node) {
|
|
99
|
-
*
|
|
100
|
-
* if (node.typ == EnumToken.AstRule && node.sel.includes('html')) {
|
|
101
|
-
*
|
|
102
|
-
* // skip the children of the current node
|
|
103
|
-
* return WalkerOptionEnum.IgnoreChildren;
|
|
104
|
-
* }
|
|
105
|
-
* }
|
|
106
|
-
*
|
|
107
|
-
* const result = await transform(css);
|
|
108
|
-
* for (const {node} of walk(result.ast, filter)) {
|
|
109
|
-
*
|
|
110
|
-
* console.error([EnumToken[node.typ]]);
|
|
111
|
-
* }
|
|
112
|
-
*
|
|
113
|
-
* // [ "StyleSheetNodeType" ]
|
|
114
|
-
* // [ "RuleNodeType" ]
|
|
115
|
-
* // [ "DeclarationNodeType" ]
|
|
116
|
-
* // [ "RuleNodeType" ]
|
|
117
|
-
* // [ "DeclarationNodeType" ]
|
|
118
|
-
* // [ "RuleNodeType" ]
|
|
119
|
-
* // [ "DeclarationNodeType" ]
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
function* walk(node, filter, reverse) {
|
|
123
|
-
const parents = [node];
|
|
124
|
-
const root = node;
|
|
125
|
-
let options = filter;
|
|
126
|
-
let isNumeric = false;
|
|
127
|
-
let children = true;
|
|
128
|
-
let attributes = false;
|
|
129
|
-
if (options != null && typeof options == "object") {
|
|
130
|
-
filter = options.filter;
|
|
131
|
-
reverse = options.reverse;
|
|
132
|
-
attributes = options.attributes;
|
|
133
|
-
children = options.children ?? true;
|
|
134
|
-
}
|
|
135
|
-
// @ts-expect-error
|
|
136
|
-
const result = {
|
|
137
|
-
node: null,
|
|
138
|
-
parent: null,
|
|
139
|
-
root,
|
|
140
|
-
parents: function* () {
|
|
141
|
-
let parent = node[PARENT];
|
|
142
|
-
while (parent != null) {
|
|
143
|
-
yield parent;
|
|
144
|
-
parent = parent[PARENT];
|
|
145
|
-
}
|
|
146
|
-
},
|
|
147
|
-
};
|
|
148
|
-
let option = null;
|
|
149
|
-
let source = [];
|
|
150
|
-
while (parents.length > 0) {
|
|
151
|
-
node = parents.pop();
|
|
152
|
-
// enter node
|
|
153
|
-
if (filter != null) {
|
|
154
|
-
// @ts-expect-error
|
|
155
|
-
option = filter(node);
|
|
156
|
-
isNumeric = typeof option == "number";
|
|
157
|
-
if (isNumeric) {
|
|
158
|
-
if (option & WalkerOptionEnum.Ignore) {
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
if (option & WalkerOptionEnum.Stop) {
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
89
|
+
expand(node, option) {
|
|
90
|
+
if (typeof option == "number" && option & WalkerOptionEnum.IgnoreChildren) {
|
|
91
|
+
return;
|
|
165
92
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
93
|
+
const arena = this.arena;
|
|
94
|
+
const stack = this.stack;
|
|
95
|
+
if (this.attributes) {
|
|
96
|
+
// prelude tokens, declaration values or token children
|
|
97
|
+
pushValues(arena, stack, node, this.reverse);
|
|
171
98
|
}
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
parents.push(token);
|
|
178
|
-
token[PARENT] = node;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
for (let i = node[TOKENS].length - 1; i >= 0; i--) {
|
|
183
|
-
parents.push(node[TOKENS][i]);
|
|
184
|
-
node[TOKENS][i][PARENT] = node;
|
|
185
|
-
}
|
|
99
|
+
if (this.children && arena.flags[node] & EnumAstNodeStatus.HasChildren) {
|
|
100
|
+
// children are pushed in reverse so that they are popped in document order
|
|
101
|
+
if (this.reverse) {
|
|
102
|
+
for (let child = arena.firstChild[node]; child != 0; child = arena.nextSibling[child]) {
|
|
103
|
+
stack.push(child);
|
|
186
104
|
}
|
|
187
105
|
}
|
|
188
|
-
else
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
parents.push(token);
|
|
192
|
-
token[PARENT] = node;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
for (let i = node.val.length - 1; i >= 0; i--) {
|
|
197
|
-
parents.push(node.val[i]);
|
|
198
|
-
node.val[i][PARENT] = node;
|
|
199
|
-
}
|
|
106
|
+
else {
|
|
107
|
+
for (let child = arena.lastChild[node]; child != 0; child = arena.previousSibling[child]) {
|
|
108
|
+
stack.push(child);
|
|
200
109
|
}
|
|
201
110
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
case EnumToken.MediaQueryConditionTokenType:
|
|
222
|
-
for (let i = 0; i < node.l.length; i++) {
|
|
223
|
-
const element = node.l[i];
|
|
224
|
-
source.push(element);
|
|
225
|
-
element[PARENT] = node;
|
|
226
|
-
}
|
|
227
|
-
source.push(node.op);
|
|
228
|
-
node.op[PARENT] = node;
|
|
229
|
-
for (let i = 0; i < node.r.length; i++) {
|
|
230
|
-
const element = node.r[i];
|
|
231
|
-
source.push(element);
|
|
232
|
-
element[PARENT] = node;
|
|
233
|
-
}
|
|
234
|
-
break;
|
|
235
|
-
case EnumToken.WhenElseQueryConditionTokenType:
|
|
236
|
-
source.push(node.l);
|
|
237
|
-
node.l[PARENT] = node;
|
|
238
|
-
for (let i = 0; i < node.r.length; i++) {
|
|
239
|
-
const element = node.r[i];
|
|
240
|
-
source.push(element);
|
|
241
|
-
element[PARENT] = node;
|
|
242
|
-
}
|
|
243
|
-
break;
|
|
244
|
-
case EnumToken.ComposesSelectorNodeType:
|
|
245
|
-
for (let i = 0; i < node.l.length; i++) {
|
|
246
|
-
const element = node.l[i];
|
|
247
|
-
source.push(element);
|
|
248
|
-
element[PARENT] = node;
|
|
249
|
-
}
|
|
250
|
-
if (node.r != null) {
|
|
251
|
-
source.push(node.r);
|
|
252
|
-
node.r[PARENT] = node;
|
|
253
|
-
}
|
|
254
|
-
break;
|
|
255
|
-
case EnumToken.IfConditionTokenType:
|
|
256
|
-
for (let i = 0; i < node.l.length; i++) {
|
|
257
|
-
const element = node.l[i];
|
|
258
|
-
source.push(element);
|
|
259
|
-
element[PARENT] = node;
|
|
260
|
-
}
|
|
261
|
-
for (let i = 0; i < node.r.length; i++) {
|
|
262
|
-
const element = node.r[i];
|
|
263
|
-
source.push(element);
|
|
264
|
-
element[PARENT] = node;
|
|
265
|
-
}
|
|
266
|
-
break;
|
|
267
|
-
case EnumToken.ContainerStyleRangeTokenType:
|
|
268
|
-
for (let i = 0; i < node.l.length; i++) {
|
|
269
|
-
const element = node.l[i];
|
|
270
|
-
source.push(element);
|
|
271
|
-
element[PARENT] = node;
|
|
272
|
-
}
|
|
273
|
-
for (let i = 0; i < node.op.length; i++) {
|
|
274
|
-
const element = node.op[i];
|
|
275
|
-
source.push(element);
|
|
276
|
-
element[PARENT] = node;
|
|
277
|
-
}
|
|
278
|
-
for (let i = 0; i < node.r.length; i++) {
|
|
279
|
-
const element = node.r[i];
|
|
280
|
-
source.push(element);
|
|
281
|
-
element[PARENT] = node;
|
|
282
|
-
}
|
|
283
|
-
break;
|
|
284
|
-
case EnumToken.MediaRangeQueryTokenType:
|
|
285
|
-
for (let i = 0; i < node.l.length; i++) {
|
|
286
|
-
const element = node.l[i];
|
|
287
|
-
source.push(element);
|
|
288
|
-
element[PARENT] = node;
|
|
289
|
-
}
|
|
290
|
-
source.push(node.op1);
|
|
291
|
-
node.op1[PARENT] = node;
|
|
292
|
-
for (let i = 0; i < node.val.length; i++) {
|
|
293
|
-
const element = node.val[i];
|
|
294
|
-
source.push(element);
|
|
295
|
-
element[PARENT] = node;
|
|
296
|
-
}
|
|
297
|
-
source.push(node.op2);
|
|
298
|
-
node.op2[PARENT] = node;
|
|
299
|
-
for (let i = 0; i < node.r.length; i++) {
|
|
300
|
-
const element = node.r[i];
|
|
301
|
-
source.push(element);
|
|
302
|
-
element[PARENT] = node;
|
|
303
|
-
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
next() {
|
|
114
|
+
const stack = this.stack;
|
|
115
|
+
const filter = this.filter;
|
|
116
|
+
if (this.current != 0) {
|
|
117
|
+
this.expand(this.current, this.option);
|
|
118
|
+
this.current = 0;
|
|
119
|
+
}
|
|
120
|
+
while (stack.length > 0) {
|
|
121
|
+
const node = stack.pop();
|
|
122
|
+
let option = null;
|
|
123
|
+
if (filter != null) {
|
|
124
|
+
option = filter(this.arena, node);
|
|
125
|
+
if (typeof option == "number") {
|
|
126
|
+
if (option & WalkerOptionEnum.Ignore) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (option & WalkerOptionEnum.Stop) {
|
|
304
130
|
break;
|
|
305
|
-
}
|
|
306
|
-
if (source.length > 0) {
|
|
307
|
-
if (reverse) {
|
|
308
|
-
for (let i = 0; i < source.length; i++) {
|
|
309
|
-
parents.push(source[i]);
|
|
310
|
-
source[i][PARENT] = node;
|
|
311
|
-
}
|
|
312
131
|
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}
|
|
132
|
+
if (option & WalkerOptionEnum.Children) {
|
|
133
|
+
// the node is not returned, its children are walked
|
|
134
|
+
this.expand(node, option);
|
|
135
|
+
continue;
|
|
318
136
|
}
|
|
319
137
|
}
|
|
320
138
|
}
|
|
139
|
+
this.current = node;
|
|
140
|
+
this.option = option;
|
|
141
|
+
this.iteration.value = node;
|
|
142
|
+
return this.iteration;
|
|
321
143
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
else {
|
|
336
|
-
// @ts-expect-error
|
|
337
|
-
for (let i = node.chi.length - 1; i >= 0; i--) {
|
|
338
|
-
// @ts-expect-error
|
|
339
|
-
parents.push(node.chi[i]);
|
|
340
|
-
// @ts-expect-error
|
|
341
|
-
node.chi[i][PARENT] = node;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
}
|
|
144
|
+
return this.return();
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* stop the iteration (break of a for ... of loop)
|
|
148
|
+
*/
|
|
149
|
+
return(value) {
|
|
150
|
+
this.stack.length = 0;
|
|
151
|
+
this.current = 0;
|
|
152
|
+
return { done: true, value: value };
|
|
153
|
+
}
|
|
154
|
+
throw(error) {
|
|
155
|
+
this.return();
|
|
156
|
+
throw error;
|
|
345
157
|
}
|
|
346
158
|
}
|
|
347
159
|
/**
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
* @param root
|
|
351
|
-
* @param filter
|
|
352
|
-
* @param reverse
|
|
353
|
-
*
|
|
354
|
-
* Example:
|
|
355
|
-
*
|
|
356
|
-
* ```ts
|
|
357
|
-
*
|
|
358
|
-
* import {AstDeclaration, EnumToken, transform, walkValues} from '@tbela99/css-parser';
|
|
359
|
-
*
|
|
360
|
-
* const css = `
|
|
361
|
-
* body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); }
|
|
362
|
-
* `;
|
|
363
|
-
*
|
|
364
|
-
* const result = await transform(css);
|
|
365
|
-
* const declaration = result.ast.chi[0].chi[0] as AstDeclaration;
|
|
366
|
-
*
|
|
367
|
-
* // walk the node attribute's tokens in reverse order
|
|
368
|
-
* for (const {value} of walkValues(declaration.val, null, null,true)) {
|
|
369
|
-
*
|
|
370
|
-
* console.error([EnumToken[value.typ], value.val]);
|
|
371
|
-
* }
|
|
372
|
-
*
|
|
373
|
-
* // [ "Color", "color" ]
|
|
374
|
-
* // [ "FunctionTokenType", "calc" ]
|
|
375
|
-
* // [ "Number", 0.15 ]
|
|
376
|
-
* // [ "Add", undefined ]
|
|
377
|
-
* // [ "Iden", "b" ]
|
|
378
|
-
* // [ "Whitespace", undefined ]
|
|
379
|
-
* // [ "FunctionTokenType", "calc" ]
|
|
380
|
-
* // [ "Number", 0.24 ]
|
|
381
|
-
* // [ "Add", undefined ]
|
|
382
|
-
* // [ "Iden", "g" ]
|
|
383
|
-
* // [ "Whitespace", undefined ]
|
|
384
|
-
* // [ "Iden", "r" ]
|
|
385
|
-
* // [ "Whitespace", undefined ]
|
|
386
|
-
* // [ "Iden", "display-p3" ]
|
|
387
|
-
* // [ "Whitespace", undefined ]
|
|
388
|
-
* // [ "FunctionTokenType", "var" ]
|
|
389
|
-
* // [ "DashedIden", "--base-color" ]
|
|
390
|
-
* // [ "Whitespace", undefined ]
|
|
391
|
-
* // [ "Iden", "from" ]
|
|
392
|
-
* ```
|
|
160
|
+
* iterator of {@link AstArena.walkValues}: token ids in document order. the children of a token are read when the
|
|
161
|
+
* next token is requested: the token may be modified by the caller
|
|
393
162
|
*/
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
163
|
+
class ValueWalker {
|
|
164
|
+
arena;
|
|
165
|
+
stack = [];
|
|
166
|
+
fn = null;
|
|
167
|
+
type = null;
|
|
168
|
+
eventType = WalkerEvent.Enter;
|
|
169
|
+
reverse;
|
|
170
|
+
// iteration result, reused
|
|
171
|
+
iteration = { done: false, value: 0 };
|
|
172
|
+
// token returned by the last call of next(): its children are visited by the next call
|
|
173
|
+
current = 0;
|
|
174
|
+
// value returned by the filter for the current token
|
|
175
|
+
option = null;
|
|
176
|
+
constructor(arena, tokens, filter, reverse) {
|
|
177
|
+
this.arena = arena;
|
|
178
|
+
this.reverse = reverse === true;
|
|
179
|
+
if (typeof tokens == "number") {
|
|
180
|
+
// the value tokens of a node or a token
|
|
181
|
+
pushValues(arena, this.stack, tokens, this.reverse);
|
|
400
182
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
183
|
+
else if (this.reverse) {
|
|
184
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
185
|
+
this.stack.push(tokens[i]);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
for (let i = tokens.length - 1; i >= 0; i--) {
|
|
190
|
+
this.stack.push(tokens[i]);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// the filter is read once: no filter object is created
|
|
194
|
+
if (typeof filter == "function") {
|
|
195
|
+
this.fn = filter;
|
|
196
|
+
}
|
|
197
|
+
else if (filter != null) {
|
|
198
|
+
this.fn = filter.fn ?? null;
|
|
199
|
+
this.type = filter.type ?? null;
|
|
200
|
+
this.eventType = filter.event ?? WalkerEvent.Enter;
|
|
405
201
|
}
|
|
406
202
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
event: WalkerEvent.Enter,
|
|
410
|
-
fn: filter,
|
|
411
|
-
};
|
|
203
|
+
[Symbol.iterator]() {
|
|
204
|
+
return this;
|
|
412
205
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
206
|
+
/**
|
|
207
|
+
* the filter function is called for the token
|
|
208
|
+
* @param token
|
|
209
|
+
*/
|
|
210
|
+
isValid(token) {
|
|
211
|
+
const type = this.type;
|
|
212
|
+
return (type == null ||
|
|
213
|
+
this.arena.dataTypes[token] == type ||
|
|
214
|
+
(Array.isArray(type) && type.includes(this.arena.dataTypes[token])) ||
|
|
215
|
+
(typeof type == "function" && type(this.arena, token)));
|
|
417
216
|
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
value: null,
|
|
427
|
-
parent: null,
|
|
428
|
-
previousValue: null,
|
|
429
|
-
nextValue: null,
|
|
430
|
-
//
|
|
431
|
-
root: root ?? null,
|
|
432
|
-
parents: function* () {
|
|
433
|
-
// @ts-ignore
|
|
434
|
-
let result = root;
|
|
435
|
-
while (result != null) {
|
|
436
|
-
yield result;
|
|
437
|
-
result = result[PARENT] ?? null;
|
|
217
|
+
/**
|
|
218
|
+
* the tokens returned by the filter are visited too
|
|
219
|
+
* @param option
|
|
220
|
+
*/
|
|
221
|
+
pushOption(option) {
|
|
222
|
+
if (this.reverse) {
|
|
223
|
+
for (let i = 0; i < option.length; i++) {
|
|
224
|
+
this.stack.push(option[i]);
|
|
438
225
|
}
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
option = null;
|
|
444
|
-
if (filter.fn != null && eventType & WalkerEvent.Enter) {
|
|
445
|
-
const isValid = filter.type == null ||
|
|
446
|
-
value.typ == filter.type ||
|
|
447
|
-
(Array.isArray(filter.type) && filter.type.includes(value.typ)) ||
|
|
448
|
-
(typeof filter.type == "function" && filter.type(value));
|
|
449
|
-
if (isValid) {
|
|
450
|
-
// @ts-expect-error
|
|
451
|
-
option = filter.fn(value, value[PARENT] ?? root, WalkerEvent.Enter,
|
|
452
|
-
// @ts-expect-error
|
|
453
|
-
function* () {
|
|
454
|
-
// @ts-expect-error
|
|
455
|
-
let parent = node[PARENT];
|
|
456
|
-
while (parent != null) {
|
|
457
|
-
yield parent;
|
|
458
|
-
parent = parent[PARENT] ?? null;
|
|
459
|
-
}
|
|
460
|
-
});
|
|
461
|
-
isNumeric = typeof option == "number";
|
|
462
|
-
if (isNumeric && option & WalkerOptionEnum.Stop) {
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
if (isNumeric && option & WalkerOptionEnum.Ignore) {
|
|
466
|
-
continue;
|
|
467
|
-
}
|
|
468
|
-
//
|
|
469
|
-
if (option != null && typeof option == "object" && ("typ" in option || Array.isArray(option))) {
|
|
470
|
-
const op = Array.isArray(option) ? option : [option];
|
|
471
|
-
if (reverse) {
|
|
472
|
-
for (let i = 0; i < op.length; i++) {
|
|
473
|
-
nodes.push(op[i]);
|
|
474
|
-
op[i][PARENT] = value;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
else {
|
|
478
|
-
for (let i = op.length - 1; i >= 0; i--) {
|
|
479
|
-
nodes.push(op[i]);
|
|
480
|
-
op[i][PARENT] = value;
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
}
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
for (let i = option.length - 1; i >= 0; i--) {
|
|
229
|
+
this.stack.push(option[i]);
|
|
484
230
|
}
|
|
485
231
|
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
}
|
|
501
|
-
else {
|
|
502
|
-
for (let i = source.length - 1; i >= 0; i--) {
|
|
503
|
-
nodes.push(source[i]);
|
|
504
|
-
source[i][PARENT] = value;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* children of the token returned by the previous call, then the leave event
|
|
235
|
+
*/
|
|
236
|
+
leave(value) {
|
|
237
|
+
const option = this.option;
|
|
238
|
+
// the children are read after the token is visited (it may have been modified)
|
|
239
|
+
if (typeof option != "number" || (option & WalkerOptionEnum.IgnoreChildren) === 0) {
|
|
240
|
+
pushValues(this.arena, this.stack, value, this.reverse);
|
|
241
|
+
}
|
|
242
|
+
if (this.eventType & WalkerEvent.Leave && this.fn != null && this.isValid(value)) {
|
|
243
|
+
const option = this.fn(this.arena, value, WalkerEvent.Leave);
|
|
244
|
+
if (Array.isArray(option)) {
|
|
245
|
+
this.pushOption(option);
|
|
507
246
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
value.r[PARENT] = value;
|
|
525
|
-
break;
|
|
526
|
-
case EnumToken.SupportsQueryConditionTokenType:
|
|
527
|
-
case EnumToken.MediaQueryConditionTokenType:
|
|
528
|
-
for (let i = 0; i < value.l.length; i++) {
|
|
529
|
-
const element = value.l[i];
|
|
530
|
-
values.push(element);
|
|
531
|
-
element[PARENT] = value;
|
|
532
|
-
}
|
|
533
|
-
values.push(value.op);
|
|
534
|
-
value.op[PARENT] = value;
|
|
535
|
-
for (let i = 0; i < value.r.length; i++) {
|
|
536
|
-
const element = value.r[i];
|
|
537
|
-
values.push(element);
|
|
538
|
-
element[PARENT] = value;
|
|
539
|
-
}
|
|
540
|
-
break;
|
|
541
|
-
case EnumToken.ComposesSelectorNodeType:
|
|
542
|
-
for (let i = 0; i < value.l.length; i++) {
|
|
543
|
-
const element = value.l[i];
|
|
544
|
-
values.push(element);
|
|
545
|
-
element[PARENT] = value;
|
|
546
|
-
}
|
|
547
|
-
if (value.r != null) {
|
|
548
|
-
values.push(value.r);
|
|
549
|
-
value.r[PARENT] = value;
|
|
550
|
-
}
|
|
551
|
-
break;
|
|
552
|
-
case EnumToken.WhenElseQueryConditionTokenType:
|
|
553
|
-
values.push(value.l);
|
|
554
|
-
value.l[PARENT] = value;
|
|
555
|
-
for (let i = 0; i < value.r.length; i++) {
|
|
556
|
-
const element = value.r[i];
|
|
557
|
-
values.push(element);
|
|
558
|
-
element[PARENT] = value;
|
|
559
|
-
}
|
|
560
|
-
break;
|
|
561
|
-
case EnumToken.IfConditionTokenType:
|
|
562
|
-
for (let i = 0; i < value.l.length; i++) {
|
|
563
|
-
const element = value.l[i];
|
|
564
|
-
values.push(element);
|
|
565
|
-
element[PARENT] = value;
|
|
566
|
-
}
|
|
567
|
-
for (let i = 0; i < value.r.length; i++) {
|
|
568
|
-
const element = value.r[i];
|
|
569
|
-
values.push(element);
|
|
570
|
-
element[PARENT] = value;
|
|
571
|
-
}
|
|
572
|
-
break;
|
|
573
|
-
case EnumToken.ContainerStyleRangeTokenType:
|
|
574
|
-
for (let i = 0; i < value.l.length; i++) {
|
|
575
|
-
const element = value.l[i];
|
|
576
|
-
values.push(element);
|
|
577
|
-
element[PARENT] = value;
|
|
578
|
-
}
|
|
579
|
-
for (let i = 0; i < value.op.length; i++) {
|
|
580
|
-
const element = value.op[i];
|
|
581
|
-
values.push(element);
|
|
582
|
-
element[PARENT] = value;
|
|
583
|
-
}
|
|
584
|
-
for (let i = 0; i < value.r.length; i++) {
|
|
585
|
-
const element = value.r[i];
|
|
586
|
-
values.push(element);
|
|
587
|
-
element[PARENT] = value;
|
|
588
|
-
}
|
|
589
|
-
break;
|
|
590
|
-
case EnumToken.MediaRangeQueryTokenType:
|
|
591
|
-
for (let i = 0; i < value.l.length; i++) {
|
|
592
|
-
const element = value.l[i];
|
|
593
|
-
values.push(element);
|
|
594
|
-
element[PARENT] = value;
|
|
595
|
-
}
|
|
596
|
-
values.push(value.op1);
|
|
597
|
-
value.op1[PARENT] = value;
|
|
598
|
-
for (let i = 0; i < value.val.length; i++) {
|
|
599
|
-
const element = value.val[i];
|
|
600
|
-
values.push(element);
|
|
601
|
-
element[PARENT] = value;
|
|
602
|
-
}
|
|
603
|
-
values.push(value.op2);
|
|
604
|
-
value.op2[PARENT] = value;
|
|
605
|
-
for (let i = 0; i < value.r.length; i++) {
|
|
606
|
-
const element = value.r[i];
|
|
607
|
-
values.push(element);
|
|
608
|
-
element[PARENT] = value;
|
|
609
|
-
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
next() {
|
|
250
|
+
const stack = this.stack;
|
|
251
|
+
if (this.current != 0) {
|
|
252
|
+
this.leave(this.current);
|
|
253
|
+
this.current = 0;
|
|
254
|
+
}
|
|
255
|
+
while (stack.length > 0) {
|
|
256
|
+
const value = stack.pop();
|
|
257
|
+
this.option = null;
|
|
258
|
+
if (this.fn != null && this.eventType & WalkerEvent.Enter && this.isValid(value)) {
|
|
259
|
+
const option = this.fn(this.arena, value, WalkerEvent.Enter);
|
|
260
|
+
this.option = option;
|
|
261
|
+
if (typeof option == "number") {
|
|
262
|
+
if (option & WalkerOptionEnum.Stop) {
|
|
610
263
|
break;
|
|
611
|
-
}
|
|
612
|
-
if (values.length > 0) {
|
|
613
|
-
if (reverse) {
|
|
614
|
-
for (let i = 0; i < values.length; i++) {
|
|
615
|
-
nodes.push(values[i]);
|
|
616
|
-
values[i][PARENT] = value;
|
|
617
|
-
}
|
|
618
264
|
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
nodes.push(values[i]);
|
|
622
|
-
values[i][PARENT] = value;
|
|
623
|
-
}
|
|
265
|
+
if (option & WalkerOptionEnum.Ignore) {
|
|
266
|
+
continue;
|
|
624
267
|
}
|
|
625
268
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
if (eventType & WalkerEvent.Leave && filter.fn != null) {
|
|
629
|
-
const isValid = filter.type == null ||
|
|
630
|
-
value.typ == filter.type ||
|
|
631
|
-
(Array.isArray(filter.type) && filter.type.includes(value.typ)) ||
|
|
632
|
-
(typeof filter.type == "function" && filter.type(value));
|
|
633
|
-
if (isValid) {
|
|
634
|
-
// @ts-ignore
|
|
635
|
-
option = filter.fn(value, value[PARENT], WalkerEvent.Leave);
|
|
636
|
-
// @ts-ignore
|
|
637
|
-
if (option != null && ("typ" in option || Array.isArray(option))) {
|
|
638
|
-
const op = Array.isArray(option) ? option : [option];
|
|
639
|
-
if (reverse) {
|
|
640
|
-
for (let i = 0; i < op.length; i++) {
|
|
641
|
-
nodes.push(op[i]);
|
|
642
|
-
op[i][PARENT] = value;
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
else {
|
|
646
|
-
for (let i = op.length - 1; i >= 0; i--) {
|
|
647
|
-
nodes.push(op[i]);
|
|
648
|
-
op[i][PARENT] = value;
|
|
649
|
-
}
|
|
650
|
-
}
|
|
269
|
+
else if (Array.isArray(option)) {
|
|
270
|
+
this.pushOption(option);
|
|
651
271
|
}
|
|
652
272
|
}
|
|
273
|
+
this.current = value;
|
|
274
|
+
this.iteration.value = value;
|
|
275
|
+
return this.iteration;
|
|
653
276
|
}
|
|
654
|
-
|
|
277
|
+
return this.return();
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* stop the iteration (break of a for ... of loop)
|
|
281
|
+
* @param value
|
|
282
|
+
*/
|
|
283
|
+
return(value) {
|
|
284
|
+
this.stack.length = 0;
|
|
285
|
+
this.current = 0;
|
|
286
|
+
return { done: true, value: value };
|
|
287
|
+
}
|
|
288
|
+
throw(error) {
|
|
289
|
+
this.return();
|
|
290
|
+
throw error;
|
|
655
291
|
}
|
|
656
292
|
}
|
|
657
293
|
|
|
658
|
-
export {
|
|
294
|
+
export { NodeWalker, ValueWalker };
|