-
Notifications
You must be signed in to change notification settings - Fork 973
/
Copy pathmatcher.ts
360 lines (350 loc) · 9.11 KB
/
matcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import {
parseComponentName,
type Instance,
type Instances,
type Matcher,
type MatcherOperation,
type MatcherRelation,
type WebstudioFragment,
} from "@webstudio-is/sdk";
import type { InstanceSelector } from "./tree-utils";
import type { WsComponentMeta } from "@webstudio-is/react-sdk";
const isNegated = (operation?: MatcherOperation) => {
return operation?.$neq !== undefined || operation?.$nin !== undefined;
};
const isMatching = (
value: undefined | string,
operation: undefined | MatcherOperation
) => {
if (operation?.$eq) {
return operation.$eq === value;
}
if (operation?.$neq) {
return operation.$neq === value;
}
if (operation?.$in && value) {
return operation.$in.includes(value);
}
if (operation?.$nin && value) {
return operation.$nin.includes(value);
}
return false;
};
const isDepthMatchingRelation = (depth: number, relation: MatcherRelation) => {
return (
(relation === "self" && depth === 0) ||
(relation === "parent" && depth === 1) ||
(relation === "ancestor" && depth >= 1)
);
};
const isLevelMatchingRelation = (level: number, relation: MatcherRelation) => {
return (
(relation === "self" && level === 0) ||
(relation === "child" && level === 1) ||
(relation === "descendant" && level >= 1)
);
};
const formatList = (operation: MatcherOperation) => {
const listFormat = new Intl.ListFormat("en", {
type: "disjunction",
});
const list: string[] = [];
if (operation.$eq) {
list.push(operation.$eq);
}
if (operation.$in) {
list.push(...operation.$in);
}
if (operation.$neq) {
list.push(operation.$neq);
}
if (operation.$nin) {
list.push(...operation.$nin);
}
return listFormat.format(
list.map((component) => {
const [_namespace, name] = parseComponentName(component);
return name;
})
);
};
/**
* @todo following features are missing
* - tag matcher
*/
export const isInstanceMatching = ({
instances,
instanceSelector,
query,
onError,
}: {
instances: Instances;
instanceSelector: InstanceSelector;
query: undefined | Matcher | Matcher[];
onError?: (message: string) => void;
}): boolean => {
// fast path to the lack of constraints
if (query === undefined) {
return true;
}
const queries = Array.isArray(query) ? query : [query];
const matchesByMatcher = new Map<Matcher, boolean>();
let aborted = false;
const matchInstance = (instance: Instance, matcher: Matcher) => {
let matches = isMatching(instance.component, matcher.component);
if (isNegated(matcher.component)) {
if (matches) {
aborted = true;
if (matcher.component) {
onError?.(`${formatList(matcher.component)} is not acceptable`);
}
return;
}
// inverse negated match
matches = true;
}
matchesByMatcher.set(
matcher,
(matchesByMatcher.get(matcher) ?? false) || matches
);
};
let index = 0;
for (const instanceId of instanceSelector) {
const instance = instances.get(instanceId);
for (const matcher of queries) {
if (isDepthMatchingRelation(index, matcher.relation) && instance) {
matchInstance(instance, matcher);
}
}
index += 1;
}
const populateDescendants = (
instanceId: Instance["id"],
matcher: Matcher,
level: number
) => {
const instance = instances.get(instanceId);
if (instance === undefined) {
return;
}
// ignore self matchers which already handled above
if (level > 0 && isLevelMatchingRelation(level, matcher.relation)) {
matchInstance(instance, matcher);
}
for (const child of instance.children) {
if (child.type === "id") {
populateDescendants(child.value, matcher, level + 1);
}
}
};
for (const matcher of queries) {
// setup default in case no children found
matchesByMatcher.set(
matcher,
// negated operations matches by default
matchesByMatcher.get(matcher) ?? isNegated(matcher.component)
);
populateDescendants(instanceSelector[0], matcher, 0);
}
if (aborted) {
return false;
}
for (const matcher of queries) {
const matches = matchesByMatcher.get(matcher) ?? false;
if (matches === false) {
if (matcher.component) {
onError?.(`${formatList(matcher.component)} is missing`);
}
return false;
}
}
return true;
};
export const isTreeMatching = ({
instances,
metas,
instanceSelector,
onError,
level = 0,
}: {
instances: Instances;
metas: Map<string, WsComponentMeta>;
instanceSelector: InstanceSelector;
onError?: (message: string) => void;
level?: number;
}): boolean => {
const [instanceId] = instanceSelector;
const instance = instances.get(instanceId);
// collection item can be undefined
if (instance === undefined) {
return true;
}
const meta = metas.get(instance.component);
// check self
let matches = isInstanceMatching({
instances,
instanceSelector,
query: meta?.constraints,
onError,
});
// check ancestors only on the first run
if (level === 0) {
// skip self
for (let index = 1; index < instanceSelector.length; index += 1) {
const instance = instances.get(instanceSelector[index]);
if (instance === undefined) {
continue;
}
const meta = metas.get(instance.component);
const matches = isInstanceMatching({
instances,
instanceSelector: instanceSelector.slice(index),
query: meta?.constraints,
onError,
});
if (matches === false) {
return false;
}
}
}
if (matches === false) {
return false;
}
for (const child of instance.children) {
if (child.type === "id") {
matches = isTreeMatching({
instances,
metas,
instanceSelector: [child.value, ...instanceSelector],
level: level + 1,
onError,
});
if (matches === false) {
return false;
}
}
}
return matches;
};
export const findClosestInstanceMatchingFragment = ({
instances,
metas,
instanceSelector,
fragment,
onError,
}: {
instances: Instances;
metas: Map<string, WsComponentMeta>;
instanceSelector: InstanceSelector;
// require only subset of fragment
fragment: Pick<WebstudioFragment, "children" | "instances">;
onError?: (message: string) => void;
}) => {
const mergedInstances = new Map(instances);
for (const instance of fragment.instances) {
mergedInstances.set(instance.id, instance);
}
let firstError = "";
for (let index = 0; index < instanceSelector.length; index += 1) {
const instanceId = instanceSelector[index];
const instance = instances.get(instanceId);
// collection item can be undefined
if (instance === undefined) {
continue;
}
const meta = metas.get(instance.component);
if (meta === undefined) {
continue;
}
let matches = true;
for (const child of fragment.children) {
if (child.type === "id") {
matches &&= isTreeMatching({
instances: mergedInstances,
metas,
instanceSelector: [child.value, ...instanceSelector.slice(index)],
onError: (message) => {
if (firstError === "") {
firstError = message;
}
},
});
}
}
if (matches) {
return index;
}
}
onError?.(firstError);
return -1;
};
export const findClosestContainer = ({
metas,
instances,
instanceSelector,
}: {
metas: Map<string, WsComponentMeta>;
instances: Instances;
instanceSelector: InstanceSelector;
}) => {
// page root with text can be used as container
if (instanceSelector.length === 1) {
return 0;
}
for (let index = 0; index < instanceSelector.length; index += 1) {
const instanceId = instanceSelector[index];
const instance = instances.get(instanceId);
// collection item can be undefined
if (instance === undefined) {
continue;
}
const meta = metas.get(instance.component);
if (meta?.type === "container") {
return index;
}
}
return -1;
};
export const findClosestNonTextualContainer = ({
metas,
instances,
instanceSelector,
}: {
metas: Map<string, WsComponentMeta>;
instances: Instances;
instanceSelector: InstanceSelector;
}) => {
// page root with text can be used as container
if (instanceSelector.length === 1) {
return 0;
}
for (let index = 0; index < instanceSelector.length; index += 1) {
const instanceId = instanceSelector[index];
const instance = instances.get(instanceId);
// collection item can be undefined
if (instance === undefined) {
continue;
}
let hasText = false;
for (const child of instance.children) {
if (child.type === "text" || child.type === "expression") {
hasText = true;
}
if (child.type === "id") {
const childInstance = instances.get(child.value);
const meta = metas.get(childInstance?.component ?? "");
if (meta?.type === "rich-text-child") {
hasText = true;
}
}
}
if (hasText) {
continue;
}
const meta = metas.get(instance.component);
if (meta?.type === "container") {
return index;
}
}
return -1;
};