-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.js
More file actions
85 lines (78 loc) · 2.38 KB
/
Copy pathshared.js
File metadata and controls
85 lines (78 loc) · 2.38 KB
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
export function createRuntimeController(controller, signal) {
if (signal) {
if (signal.aborted) {
controller.stop();
} else {
signal.addEventListener("abort", () => controller.stop(), { once: true });
}
}
return controller;
}
export function normalizeRuntimeStartArgs(rootOrOptions, planArg, optionsArg) {
if (rootOrOptions && typeof rootOrOptions === "object" && "root" in rootOrOptions) {
return {
root: requireRoot(rootOrOptions.root),
plan: rootOrOptions.plan,
options: {
...rootOrOptions.options,
...withoutKeys(rootOrOptions, ["root", "plan", "options"])
}
};
}
return {
root: requireRoot(rootOrOptions),
plan: planArg,
options: optionsArg ?? {}
};
}
export function resolveElements(root, locators, options = {}) {
if (!Array.isArray(locators)) {
throw new TypeError("Runtime element locators must be an array.");
}
return locators.map((locator, index) => resolveElement(root, locator, index, options));
}
function resolveElement(root, locator, index, options) {
const record = normalizeLocator(locator);
const element = root.querySelector(record.selector);
if (!element && !record.optional) {
throw new Error(`Runtime locator ${index} did not match: ${record.selector}`);
}
if (!element) {
options.onDiagnostic?.({
type: "missing-optional-locator",
index,
selector: record.selector
});
}
return element;
}
function normalizeLocator(locator) {
if (typeof locator === "string") {
return { selector: locator, optional: false };
}
if (locator && typeof locator === "object" && typeof locator.selector === "string") {
return {
selector: locator.selector,
optional: Boolean(locator.optional)
};
}
throw new TypeError("Runtime element locator must be a selector string or selector record.");
}
function requireRoot(root) {
if (!root || typeof root.querySelector !== "function") {
throw new TypeError("Runtime root must be a Document, Element, or DocumentFragment with querySelector.");
}
if (typeof root.addEventListener !== "function") {
throw new TypeError("Runtime root must support addEventListener.");
}
return root;
}
function withoutKeys(source, keys) {
const result = {};
for (const [key, value] of Object.entries(source)) {
if (!keys.includes(key)) {
result[key] = value;
}
}
return result;
}