@inflatable-cookie/longhorn 0.5.0 → 0.7.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.
- package/README.md +2 -2
- package/package.json +1 -7
- package/src/agent-control/generated/protocol.ts +228 -0
- package/src/agent-control/index.ts +1 -5
- package/src/agent-control/inject.ts +4 -1
- package/src/agent-control/selection.ts +42 -25
- package/src/agent-control/shim.ts +428 -198
- package/src/bridge/direct.ts +1 -1
- package/src/bridge/generated/fields.ts +4 -3
- package/src/bridge/generated/protocol.ts +62 -7
- package/src/bridge/generated/variant-fields.ts +1 -1
- package/src/bridge/job.ts +49 -12
- package/src/bridge/loopback.ts +7 -25
- package/src/bridge/operation.ts +13 -43
- package/src/bridge/router.ts +10 -23
- package/src/bridge/runtime/connection.ts +78 -109
- package/src/bridge/runtime/policy.ts +8 -28
- package/src/bridge/runtime/retry.ts +6 -18
- package/src/bridge/session.ts +4 -14
- package/src/bridge/stream/connection.ts +24 -55
- package/src/bridge/stream/source.ts +2 -6
- package/src/bridge/stream/tracker.ts +12 -12
- package/src/bridge/supervision.ts +104 -97
- package/src/bridge/validation/base.ts +21 -63
- package/src/bridge/validation/jobs.ts +11 -25
- package/src/bridge/validation/negotiation.ts +28 -43
- package/src/bridge/validation/operations.ts +29 -65
- package/src/bridge/validation/streams.ts +3 -10
- package/src/commands/client.ts +37 -28
- package/src/commands/controller.ts +119 -96
- package/src/commands/generated/fields.ts +7 -6
- package/src/commands/generated/protocol.ts +28 -1
- package/src/commands/generated/variant-fields.ts +1 -1
- package/src/commands/keyboard.ts +94 -100
- package/src/commands/physical-code.ts +11 -0
- package/src/commands/ports.ts +8 -13
- package/src/commands/projectors.ts +100 -75
- package/src/commands/validation.ts +434 -241
- package/src/config/client.ts +9 -22
- package/src/config/generated/base.ts +22 -6
- package/src/config/generated/fields.ts +4 -3
- package/src/config/generated/label-template.ts +1 -1
- package/src/config/generated/labels.ts +1 -1
- package/src/config/generated/protocol.ts +1 -1
- package/src/config/generated/restore.ts +35 -12
- package/src/config/generated/variant-fields.ts +1 -1
- package/src/config/names.ts +6 -12
- package/src/config/validation/commands.ts +11 -25
- package/src/config/validation/outcomes.ts +163 -70
- package/src/config/validation/primitives.ts +35 -80
- package/src/config/validation/projection.ts +100 -128
- package/src/core/connection.ts +187 -83
- package/src/core/controller-lifecycle.ts +144 -0
- package/src/core/generated/identifiers.ts +4 -0
- package/src/core/identifiers.ts +40 -0
- package/src/core/index.ts +10 -1
- package/src/core/json-value.ts +58 -0
- package/src/core/listeners.ts +35 -0
- package/src/core/record.ts +80 -0
- package/src/core/transport.ts +9 -11
- package/src/core/wire-copy.ts +7 -0
- package/src/history/client.ts +11 -13
- package/src/history/controller.ts +106 -108
- package/src/history/direct.ts +1 -3
- package/src/history/generated/fields.ts +4 -3
- package/src/history/generated/protocol.ts +8 -5
- package/src/history/generated/variant-fields.ts +1 -1
- package/src/history/ports.ts +7 -4
- package/src/history/serialized.ts +9 -13
- package/src/history/validation.ts +69 -75
- package/src/history-tree/client.ts +89 -11
- package/src/history-tree/controller.ts +380 -96
- package/src/history-tree/direct.ts +3 -1
- package/src/history-tree/generated/fields.ts +5 -3
- package/src/history-tree/generated/protocol.ts +43 -7
- package/src/history-tree/generated/variant-fields.ts +7 -1
- package/src/history-tree/ports.ts +12 -5
- package/src/history-tree/serialized.ts +39 -11
- package/src/history-tree/validation.ts +286 -78
- package/src/layout/generated/protocol.ts +4 -3
- package/src/layout/generated/variant-fields.ts +47 -0
- package/src/layout/index.ts +1 -1
- package/src/layout/ratio.ts +10 -9
- package/src/layout/validation.ts +98 -32
- package/src/layout/visibility.ts +1 -2
- package/src/licence/client.ts +48 -7
- package/src/licence/controller.ts +127 -70
- package/src/licence/direct.ts +3 -1
- package/src/licence/generated/fields.ts +4 -3
- package/src/licence/generated/key-conformance.json +28 -0
- package/src/licence/generated/protocol.ts +9 -5
- package/src/licence/generated/variant-fields.ts +1 -1
- package/src/licence/key.ts +42 -18
- package/src/licence/ports.ts +10 -3
- package/src/licence/serialized.ts +32 -9
- package/src/licence/validation.ts +134 -43
- package/src/native-content/client.ts +207 -234
- package/src/native-content/generated/fields.ts +6 -6
- package/src/native-content/generated/protocol.ts +16 -5
- package/src/native-content/generated/variant-fields.ts +1 -1
- package/src/native-content/ports.ts +11 -16
- package/src/native-content/serialized.ts +12 -19
- package/src/native-content/validation/base.ts +57 -80
- package/src/native-content/validation/messages.ts +24 -35
- package/src/native-content/validation/state.ts +31 -33
- package/src/notifications/client.ts +85 -51
- package/src/notifications/controller.ts +400 -145
- package/src/notifications/direct.ts +3 -1
- package/src/notifications/generated/fields.ts +4 -3
- package/src/notifications/generated/labels.ts +1 -1
- package/src/notifications/generated/protocol.ts +5 -1
- package/src/notifications/generated/variant-fields.ts +1 -1
- package/src/notifications/ports.ts +9 -4
- package/src/notifications/serialized.ts +11 -9
- package/src/notifications/tone.ts +1 -1
- package/src/notifications/validation.ts +165 -71
- package/src/operation/bridge.ts +187 -60
- package/src/operation/client.ts +48 -62
- package/src/operation/controller.ts +67 -97
- package/src/operation/direct.ts +1 -3
- package/src/operation/generated/fields.ts +4 -3
- package/src/operation/generated/labels.ts +1 -1
- package/src/operation/generated/protocol.ts +5 -1
- package/src/operation/generated/variant-fields.ts +1 -1
- package/src/operation/ports.ts +12 -10
- package/src/operation/serialized.ts +8 -9
- package/src/operation/validation.ts +189 -104
- package/src/settings/client.ts +11 -44
- package/src/settings/connection.ts +35 -53
- package/src/settings/generated/fields.ts +5 -4
- package/src/settings/generated/protocol.ts +14 -5
- package/src/settings/generated/variant-fields.ts +1 -1
- package/src/settings/names.ts +2 -4
- package/src/settings/registry.ts +23 -25
- package/src/settings/validation/authority.ts +21 -19
- package/src/settings/validation/command.ts +22 -41
- package/src/settings/validation/error.ts +2 -8
- package/src/settings/validation/primitives.ts +46 -97
- package/src/settings/validation/projection.ts +8 -24
- package/src/settings/validation/registry.ts +33 -79
- package/src/surface-transfer/client.ts +6 -18
- package/src/surface-transfer/generated/fields.ts +3 -3
- package/src/surface-transfer/generated/protocol.ts +3 -3
- package/src/surface-transfer/generated/variant-fields.ts +1 -1
- package/src/surface-transfer/validation.ts +91 -86
- package/src/surfaces/client.ts +33 -55
- package/src/surfaces/generated/fields.ts +3 -3
- package/src/surfaces/generated/protocol.ts +1 -1
- package/src/surfaces/generated/variant-fields.ts +1 -1
- package/src/surfaces/validation.ts +227 -99
- package/src/transfer/client.ts +26 -60
- package/src/transfer/connection.ts +29 -51
- package/src/transfer/generated/fields.ts +7 -6
- package/src/transfer/generated/protocol.ts +13 -5
- package/src/transfer/generated/variant-fields.ts +1 -1
- package/src/transfer/index.ts +8 -1
- package/src/transfer/payload.ts +20 -35
- package/src/transfer/validation.ts +169 -119
- package/src/update/client.ts +86 -9
- package/src/update/controller.ts +264 -116
- package/src/update/direct.ts +3 -1
- package/src/update/generated/fields.ts +5 -4
- package/src/update/generated/protocol.ts +25 -9
- package/src/update/generated/variant-fields.ts +4 -2
- package/src/update/ports.ts +14 -6
- package/src/update/serialized.ts +36 -10
- package/src/update/validation.ts +118 -48
|
@@ -6,6 +6,17 @@
|
|
|
6
6
|
//! table that outlives the DOM. Truncation is a sentinel child with role
|
|
7
7
|
//! `truncated` (the core vocabulary has no truncation field; a silent cap
|
|
8
8
|
//! is forbidden). Synthetic events are untrusted: `isTrusted` stays false.
|
|
9
|
+
//! Every shape that crosses to Rust is a generated binding (contract 010).
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
FileInputFile,
|
|
13
|
+
PageEvent,
|
|
14
|
+
PageEventBatch,
|
|
15
|
+
PageState,
|
|
16
|
+
SemanticNode,
|
|
17
|
+
ToolError,
|
|
18
|
+
WaitPredicate,
|
|
19
|
+
} from "./generated/protocol.ts";
|
|
9
20
|
|
|
10
21
|
export const REF_ATTR = "data-longhorn-agent-ref";
|
|
11
22
|
export const REF_SEQ_ATTR = "data-longhorn-agent-ref-seq";
|
|
@@ -29,72 +40,27 @@ const SKIP_TAGS = new Set([
|
|
|
29
40
|
"IFRAME",
|
|
30
41
|
]);
|
|
31
42
|
|
|
32
|
-
type FormControl = Element & {
|
|
33
|
-
value: string;
|
|
34
|
-
disabled?: boolean;
|
|
35
|
-
checked?: boolean;
|
|
36
|
-
readOnly?: boolean;
|
|
37
|
-
type?: string;
|
|
38
|
-
selectionStart?: number | null;
|
|
39
|
-
selectionEnd?: number | null;
|
|
40
|
-
setSelectionRange?: (start: number, end: number) => void;
|
|
41
|
-
selectedOptions?: ArrayLike<Element>;
|
|
42
|
-
selected?: boolean;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
function isFormControl(element: Element): element is FormControl {
|
|
46
|
-
const tag = element.tagName;
|
|
47
|
-
return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || tag === "OPTION";
|
|
48
|
-
}
|
|
49
|
-
|
|
50
43
|
function prop<T>(element: Element, name: string): T | undefined {
|
|
51
|
-
|
|
44
|
+
// DOM access is structural across webviews and test windows. Callers select each property by element role.
|
|
45
|
+
// oxlint-disable-next-line quality/no-reflect-get,typescript/no-unsafe-type-assertion -- The selected DOM interface is the contract for the property.
|
|
46
|
+
return Reflect.get(element, name) as T | undefined;
|
|
52
47
|
}
|
|
53
48
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
| { error: "unsupported"; message: string };
|
|
49
|
+
/** The tool failures the page itself can report. */
|
|
50
|
+
export type ToolErrorBody = Extract<ToolError, { error: "unresolvedRef" | "unsupported" }>;
|
|
57
51
|
|
|
58
52
|
export type ShimOk<T> = { ok: true } & T;
|
|
59
53
|
export type ShimErr = { ok: false; error: ToolErrorBody };
|
|
60
54
|
export type ShimResult<T> = ShimOk<T> | ShimErr;
|
|
61
55
|
export type ActionOk = ShimOk<Record<string, never>>;
|
|
62
56
|
|
|
63
|
-
export type SemanticNode = {
|
|
64
|
-
elementRef: string;
|
|
65
|
-
role: string;
|
|
66
|
-
name?: string;
|
|
67
|
-
value?: string;
|
|
68
|
-
states: string[];
|
|
69
|
-
children: SemanticNode[];
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export type PageState = { url: string; title: string };
|
|
73
|
-
|
|
74
|
-
export type PageEvent =
|
|
75
|
-
| { seq: number; kind: "console"; level: string; text: string }
|
|
76
|
-
| { seq: number; kind: "error"; message: string }
|
|
77
|
-
| { seq: number; kind: "navigation"; url: string };
|
|
78
|
-
|
|
79
|
-
export type WaitPredicate =
|
|
80
|
-
| { predicate: "refResolve"; element: string }
|
|
81
|
-
| { predicate: "refAbsent"; element: string }
|
|
82
|
-
| { predicate: "pageUrlContains"; needle: string }
|
|
83
|
-
| { predicate: "pageTitleContains"; needle: string };
|
|
84
|
-
|
|
85
|
-
export type FileInputFile = {
|
|
86
|
-
name: string;
|
|
87
|
-
mediaType?: string;
|
|
88
|
-
contentBase64: string;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
57
|
export type AgentControlApi = {
|
|
92
58
|
snapshot: () => ShimResult<{ page: PageState; root: SemanticNode }>;
|
|
93
59
|
click: (element: string) => ShimResult<Record<string, never>>;
|
|
94
60
|
type: (element: string, text: string) => ShimResult<Record<string, never>>;
|
|
95
61
|
press: (
|
|
96
62
|
key: string,
|
|
97
|
-
modifiers?: string
|
|
63
|
+
modifiers?: Array<string>,
|
|
98
64
|
element?: string | null,
|
|
99
65
|
) => ShimResult<Record<string, never>>;
|
|
100
66
|
scroll: (
|
|
@@ -103,18 +69,15 @@ export type AgentControlApi = {
|
|
|
103
69
|
element?: string | null,
|
|
104
70
|
) => ShimResult<Record<string, never>>;
|
|
105
71
|
drag: (source: string, target: string) => ShimResult<Record<string, never>>;
|
|
106
|
-
setFileInput: (element: string, files: FileInputFile
|
|
72
|
+
setFileInput: (element: string, files: Array<FileInputFile>) => ShimResult<Record<string, never>>;
|
|
107
73
|
waitFor: (predicate: WaitPredicate) => ShimResult<{ holds: boolean }>;
|
|
108
74
|
markAgentOrigin: () => void;
|
|
109
75
|
isAgentOriginated: () => boolean;
|
|
110
|
-
readEvents: (sinceSeq?: number) =>
|
|
111
|
-
events: PageEvent[];
|
|
112
|
-
nextSeq: number;
|
|
113
|
-
dropped: number;
|
|
114
|
-
};
|
|
76
|
+
readEvents: (sinceSeq?: number, sinceEpoch?: string | null) => PageEventBatch;
|
|
115
77
|
};
|
|
116
78
|
|
|
117
79
|
export type ShimWorld = {
|
|
80
|
+
[SHIM_GLOBAL]?: AgentControlApi;
|
|
118
81
|
document: Document;
|
|
119
82
|
location: { href: string };
|
|
120
83
|
console: Pick<Console, "log" | "info" | "warn" | "error" | "debug">;
|
|
@@ -144,17 +107,35 @@ function unsupported(message: string): ShimResult<never> {
|
|
|
144
107
|
}
|
|
145
108
|
|
|
146
109
|
function ok(): ActionOk {
|
|
110
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- The wire success marker has no payload; the public ActionOk type represents that as Record<string, never>.
|
|
147
111
|
return { ok: true } as ActionOk;
|
|
148
112
|
}
|
|
149
113
|
|
|
150
114
|
function resolveRef(document: Document, element: string): Element | null {
|
|
151
|
-
if (element === TRUNCATED_REF || element.length === 0)
|
|
152
|
-
|
|
115
|
+
if (element === TRUNCATED_REF || element.length === 0) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
return findByAttribute(document.querySelectorAll(`[${REF_ATTR}]`), REF_ATTR, element);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function findByAttribute(
|
|
122
|
+
candidates: NodeListOf<Element>,
|
|
123
|
+
attribute: string,
|
|
124
|
+
value: string,
|
|
125
|
+
): Element | null {
|
|
126
|
+
// Attribute values are data and may contain characters that CSS normalizes.
|
|
127
|
+
for (const candidate of candidates) {
|
|
128
|
+
if (candidate.getAttribute(attribute) === value) {
|
|
129
|
+
return candidate;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
153
133
|
}
|
|
154
134
|
|
|
155
135
|
function refPrefix(document: Document): string {
|
|
156
|
-
const view = document.defaultView
|
|
157
|
-
|
|
136
|
+
const view = document.defaultView;
|
|
137
|
+
// oxlint-disable-next-line quality/no-reflect-get -- The prefix is stamped on this window by the Rust host.
|
|
138
|
+
const value: unknown = view ? Reflect.get(view, REF_PREFIX_GLOBAL) : undefined;
|
|
158
139
|
return typeof value === "string" ? value : "";
|
|
159
140
|
}
|
|
160
141
|
|
|
@@ -170,7 +151,9 @@ function allocRef(document: Document): string {
|
|
|
170
151
|
|
|
171
152
|
function stamp(element: Element, document: Document): string {
|
|
172
153
|
const existing = element.getAttribute(REF_ATTR);
|
|
173
|
-
if (existing)
|
|
154
|
+
if (existing) {
|
|
155
|
+
return existing;
|
|
156
|
+
}
|
|
174
157
|
const id = allocRef(document);
|
|
175
158
|
element.setAttribute(REF_ATTR, id);
|
|
176
159
|
return id;
|
|
@@ -182,67 +165,141 @@ function computedStyle(element: Element): CSSStyleDeclaration | null {
|
|
|
182
165
|
}
|
|
183
166
|
|
|
184
167
|
function isVisible(element: Element): boolean {
|
|
185
|
-
if (element.hasAttribute("hidden"))
|
|
186
|
-
|
|
168
|
+
if (element.hasAttribute("hidden")) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
if (element.getAttribute("aria-hidden") === "true") {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
187
174
|
const style = computedStyle(element);
|
|
188
|
-
if (!style)
|
|
175
|
+
if (!style) {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
189
178
|
return style.display !== "none" && style.visibility !== "hidden";
|
|
190
179
|
}
|
|
191
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Whether this element itself declares an editable host. `false` and invalid
|
|
183
|
+
* values do not: `contenteditable="false"` is how a page opts a region out.
|
|
184
|
+
*/
|
|
185
|
+
function declaresEditable(element: Element): boolean {
|
|
186
|
+
const value = element.getAttribute("contenteditable")?.toLowerCase();
|
|
187
|
+
return value === "" || value === "true" || value === "plaintext-only";
|
|
188
|
+
}
|
|
189
|
+
|
|
192
190
|
function implicitRole(element: Element): string | null {
|
|
193
191
|
const tag = element.tagName;
|
|
194
|
-
if (tag === "A")
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (tag === "
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
if (tag === "
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
if (tag === "
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (tag === "
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if (tag === "
|
|
210
|
-
|
|
211
|
-
|
|
192
|
+
if (tag === "A") {
|
|
193
|
+
return element.hasAttribute("href") ? "link" : null;
|
|
194
|
+
}
|
|
195
|
+
if (tag === "BUTTON") {
|
|
196
|
+
return "button";
|
|
197
|
+
}
|
|
198
|
+
if (tag === "TEXTAREA") {
|
|
199
|
+
return "textbox";
|
|
200
|
+
}
|
|
201
|
+
if (tag === "SELECT") {
|
|
202
|
+
return "combobox";
|
|
203
|
+
}
|
|
204
|
+
if (tag === "OPTION") {
|
|
205
|
+
return "option";
|
|
206
|
+
}
|
|
207
|
+
if (tag === "SUMMARY") {
|
|
208
|
+
return "button";
|
|
209
|
+
}
|
|
210
|
+
if (tag === "DETAILS") {
|
|
211
|
+
return "group";
|
|
212
|
+
}
|
|
213
|
+
if (tag === "NAV") {
|
|
214
|
+
return "navigation";
|
|
215
|
+
}
|
|
216
|
+
if (tag === "MAIN") {
|
|
217
|
+
return "main";
|
|
218
|
+
}
|
|
219
|
+
if (tag === "HEADER") {
|
|
220
|
+
return "banner";
|
|
221
|
+
}
|
|
222
|
+
if (tag === "FOOTER") {
|
|
223
|
+
return "contentinfo";
|
|
224
|
+
}
|
|
225
|
+
if (tag === "FORM") {
|
|
226
|
+
return "form";
|
|
227
|
+
}
|
|
228
|
+
if (tag === "UL" || tag === "OL") {
|
|
229
|
+
return "list";
|
|
230
|
+
}
|
|
231
|
+
if (tag === "LI") {
|
|
232
|
+
return "listitem";
|
|
233
|
+
}
|
|
234
|
+
if (tag === "TABLE") {
|
|
235
|
+
return "table";
|
|
236
|
+
}
|
|
237
|
+
if (tag === "DIALOG") {
|
|
238
|
+
return "dialog";
|
|
239
|
+
}
|
|
240
|
+
if (tag === "IMG") {
|
|
241
|
+
return "image";
|
|
242
|
+
}
|
|
243
|
+
if (
|
|
244
|
+
tag === "H1" ||
|
|
245
|
+
tag === "H2" ||
|
|
246
|
+
tag === "H3" ||
|
|
247
|
+
tag === "H4" ||
|
|
248
|
+
tag === "H5" ||
|
|
249
|
+
tag === "H6"
|
|
250
|
+
) {
|
|
212
251
|
return "heading";
|
|
213
252
|
}
|
|
214
253
|
if (tag === "INPUT") {
|
|
215
254
|
const type = (element.getAttribute("type") ?? "text").toLowerCase();
|
|
216
|
-
if (type === "checkbox")
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (type === "
|
|
255
|
+
if (type === "checkbox") {
|
|
256
|
+
return "checkbox";
|
|
257
|
+
}
|
|
258
|
+
if (type === "radio") {
|
|
259
|
+
return "radio";
|
|
260
|
+
}
|
|
261
|
+
if (type === "number") {
|
|
262
|
+
return "spinbutton";
|
|
263
|
+
}
|
|
264
|
+
if (type === "range") {
|
|
265
|
+
return "slider";
|
|
266
|
+
}
|
|
220
267
|
if (type === "button" || type === "submit" || type === "reset" || type === "file") {
|
|
221
268
|
return "button";
|
|
222
269
|
}
|
|
223
|
-
if (type === "hidden")
|
|
270
|
+
if (type === "hidden") {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
return "textbox";
|
|
274
|
+
}
|
|
275
|
+
if (declaresEditable(element)) {
|
|
224
276
|
return "textbox";
|
|
225
277
|
}
|
|
226
|
-
if (element.hasAttribute("contenteditable")) return "textbox";
|
|
227
278
|
return null;
|
|
228
279
|
}
|
|
229
280
|
|
|
230
281
|
function roleOf(element: Element): string | null {
|
|
231
282
|
const explicit = element.getAttribute("role");
|
|
232
|
-
if (explicit === "none" || explicit === "presentation")
|
|
233
|
-
|
|
283
|
+
if (explicit === "none" || explicit === "presentation") {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
if (explicit) {
|
|
287
|
+
return explicit;
|
|
288
|
+
}
|
|
234
289
|
return implicitRole(element);
|
|
235
290
|
}
|
|
236
291
|
|
|
237
292
|
function labelledByText(element: Element): string | undefined {
|
|
238
293
|
const ids = element.getAttribute("aria-labelledby");
|
|
239
|
-
if (!ids)
|
|
294
|
+
if (!ids) {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
240
297
|
const document = element.ownerDocument;
|
|
241
298
|
const text = ids
|
|
242
299
|
.split(/\s+/)
|
|
243
300
|
.map((id) => document.getElementById(id)?.textContent ?? "")
|
|
244
301
|
.join(" ")
|
|
245
|
-
.
|
|
302
|
+
.replaceAll(/\s+/g, " ")
|
|
246
303
|
.trim();
|
|
247
304
|
return text.length > 0 ? text : undefined;
|
|
248
305
|
}
|
|
@@ -251,28 +308,40 @@ function associatedLabel(element: Element): string | undefined {
|
|
|
251
308
|
const id = element.id;
|
|
252
309
|
const document = element.ownerDocument;
|
|
253
310
|
if (id) {
|
|
254
|
-
const label = document.
|
|
255
|
-
const text = label?.textContent?.
|
|
256
|
-
if (text)
|
|
311
|
+
const label = findByAttribute(document.querySelectorAll("label[for]"), "for", id);
|
|
312
|
+
const text = label?.textContent?.replaceAll(/\s+/g, " ").trim();
|
|
313
|
+
if (text) {
|
|
314
|
+
return text;
|
|
315
|
+
}
|
|
257
316
|
}
|
|
258
317
|
const parent = element.closest("label");
|
|
259
318
|
if (parent) {
|
|
260
|
-
const text = parent.textContent?.
|
|
261
|
-
if (text)
|
|
319
|
+
const text = parent.textContent?.replaceAll(/\s+/g, " ").trim();
|
|
320
|
+
if (text) {
|
|
321
|
+
return text;
|
|
322
|
+
}
|
|
262
323
|
}
|
|
263
324
|
return undefined;
|
|
264
325
|
}
|
|
265
326
|
|
|
266
327
|
function accessibleName(element: Element): string | undefined {
|
|
267
328
|
const labelled = labelledByText(element);
|
|
268
|
-
if (labelled)
|
|
329
|
+
if (labelled) {
|
|
330
|
+
return labelled;
|
|
331
|
+
}
|
|
269
332
|
const aria = element.getAttribute("aria-label")?.trim();
|
|
270
|
-
if (aria)
|
|
333
|
+
if (aria) {
|
|
334
|
+
return aria;
|
|
335
|
+
}
|
|
271
336
|
const label = associatedLabel(element);
|
|
272
|
-
if (label)
|
|
337
|
+
if (label) {
|
|
338
|
+
return label;
|
|
339
|
+
}
|
|
273
340
|
if (element.tagName === "IMG") {
|
|
274
341
|
const alt = element.getAttribute("alt")?.trim();
|
|
275
|
-
if (alt)
|
|
342
|
+
if (alt) {
|
|
343
|
+
return alt;
|
|
344
|
+
}
|
|
276
345
|
}
|
|
277
346
|
const role = roleOf(element);
|
|
278
347
|
if (
|
|
@@ -283,39 +352,53 @@ function accessibleName(element: Element): string | undefined {
|
|
|
283
352
|
role === "tab" ||
|
|
284
353
|
role === "listitem"
|
|
285
354
|
) {
|
|
286
|
-
const text = element.textContent?.
|
|
287
|
-
if (text)
|
|
355
|
+
const text = element.textContent?.replaceAll(/\s+/g, " ").trim();
|
|
356
|
+
if (text) {
|
|
357
|
+
return text;
|
|
358
|
+
}
|
|
288
359
|
}
|
|
289
360
|
const title = element.getAttribute("title")?.trim();
|
|
290
|
-
if (title)
|
|
361
|
+
if (title) {
|
|
362
|
+
return title;
|
|
363
|
+
}
|
|
291
364
|
const placeholder = element.getAttribute("placeholder")?.trim();
|
|
292
|
-
if (placeholder)
|
|
365
|
+
if (placeholder) {
|
|
366
|
+
return placeholder;
|
|
367
|
+
}
|
|
293
368
|
return undefined;
|
|
294
369
|
}
|
|
295
370
|
|
|
296
371
|
function valueOf(element: Element): string | undefined {
|
|
297
372
|
const now = element.getAttribute("aria-valuenow");
|
|
298
|
-
if (now)
|
|
373
|
+
if (now) {
|
|
374
|
+
return now;
|
|
375
|
+
}
|
|
299
376
|
if (element.tagName === "INPUT") {
|
|
300
377
|
const type = (prop<string>(element, "type") ?? "text").toLowerCase();
|
|
301
|
-
if (type === "checkbox" || type === "radio")
|
|
378
|
+
if (type === "checkbox" || type === "radio") {
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
return prop<string>(element, "value");
|
|
382
|
+
}
|
|
383
|
+
if (element.tagName === "TEXTAREA") {
|
|
302
384
|
return prop<string>(element, "value");
|
|
303
385
|
}
|
|
304
|
-
if (element.tagName === "TEXTAREA") return prop<string>(element, "value");
|
|
305
386
|
if (element.tagName === "SELECT") {
|
|
306
387
|
const selected = prop<ArrayLike<Element>>(element, "selectedOptions");
|
|
307
388
|
const first = selected && selected.length > 0 ? selected[0] : undefined;
|
|
308
389
|
return first?.textContent?.trim() ?? prop<string>(element, "value");
|
|
309
390
|
}
|
|
310
|
-
if (element
|
|
391
|
+
if (declaresEditable(element)) {
|
|
311
392
|
return element.textContent ?? undefined;
|
|
312
393
|
}
|
|
313
394
|
return undefined;
|
|
314
395
|
}
|
|
315
396
|
|
|
316
|
-
function statesOf(element: Element, document: Document): string
|
|
317
|
-
const states: string
|
|
318
|
-
if (isVisible(element))
|
|
397
|
+
function statesOf(element: Element, document: Document): Array<string> {
|
|
398
|
+
const states: Array<string> = [];
|
|
399
|
+
if (isVisible(element)) {
|
|
400
|
+
states.push("visible");
|
|
401
|
+
}
|
|
319
402
|
if (
|
|
320
403
|
element.hasAttribute("disabled") ||
|
|
321
404
|
element.getAttribute("aria-disabled") === "true" ||
|
|
@@ -323,12 +406,21 @@ function statesOf(element: Element, document: Document): string[] {
|
|
|
323
406
|
) {
|
|
324
407
|
states.push("disabled");
|
|
325
408
|
}
|
|
326
|
-
if (
|
|
409
|
+
if (
|
|
410
|
+
prop<boolean>(element, "checked") === true ||
|
|
411
|
+
element.getAttribute("aria-checked") === "true"
|
|
412
|
+
) {
|
|
327
413
|
states.push("checked");
|
|
328
414
|
}
|
|
329
|
-
if (document.activeElement === element)
|
|
330
|
-
|
|
331
|
-
|
|
415
|
+
if (document.activeElement === element) {
|
|
416
|
+
states.push("focused");
|
|
417
|
+
}
|
|
418
|
+
if (element.getAttribute("aria-expanded") === "true") {
|
|
419
|
+
states.push("expanded");
|
|
420
|
+
}
|
|
421
|
+
if (element.getAttribute("aria-pressed") === "true") {
|
|
422
|
+
states.push("pressed");
|
|
423
|
+
}
|
|
332
424
|
if (
|
|
333
425
|
element.getAttribute("aria-selected") === "true" ||
|
|
334
426
|
prop<boolean>(element, "selected") === true
|
|
@@ -345,13 +437,25 @@ function statesOf(element: Element, document: Document): string[] {
|
|
|
345
437
|
}
|
|
346
438
|
|
|
347
439
|
function isInteresting(element: Element): boolean {
|
|
348
|
-
if (SKIP_TAGS.has(element.tagName))
|
|
440
|
+
if (SKIP_TAGS.has(element.tagName)) {
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
349
443
|
// Labels contribute their text to the control's name; they are not nodes.
|
|
350
|
-
if (element.tagName === "LABEL")
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
if (element
|
|
354
|
-
|
|
444
|
+
if (element.tagName === "LABEL") {
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
if (roleOf(element)) {
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
if (accessibleName(element)) {
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
if (element.tagName === "INPUT") {
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
if (element.hasAttribute("tabindex")) {
|
|
457
|
+
return true;
|
|
458
|
+
}
|
|
355
459
|
return false;
|
|
356
460
|
}
|
|
357
461
|
|
|
@@ -359,6 +463,8 @@ function truncatedNode(): SemanticNode {
|
|
|
359
463
|
return {
|
|
360
464
|
elementRef: TRUNCATED_REF,
|
|
361
465
|
role: TRUNCATED_ROLE,
|
|
466
|
+
name: null,
|
|
467
|
+
value: null,
|
|
362
468
|
states: [],
|
|
363
469
|
children: [],
|
|
364
470
|
};
|
|
@@ -369,14 +475,18 @@ function walk(
|
|
|
369
475
|
document: Document,
|
|
370
476
|
depth: number,
|
|
371
477
|
budget: Budget,
|
|
372
|
-
): SemanticNode
|
|
373
|
-
if (SKIP_TAGS.has(element.tagName) || !isVisible(element))
|
|
478
|
+
): Array<SemanticNode> {
|
|
479
|
+
if (SKIP_TAGS.has(element.tagName) || !isVisible(element)) {
|
|
480
|
+
return [];
|
|
481
|
+
}
|
|
374
482
|
const interesting = isInteresting(element) || depth === 0;
|
|
375
483
|
if (!interesting) {
|
|
376
|
-
const promoted: SemanticNode
|
|
484
|
+
const promoted: Array<SemanticNode> = [];
|
|
377
485
|
for (const child of Array.from(element.children)) {
|
|
378
486
|
promoted.push(...walk(child, document, depth, budget));
|
|
379
|
-
if (budget.truncated)
|
|
487
|
+
if (budget.truncated) {
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
380
490
|
}
|
|
381
491
|
return promoted;
|
|
382
492
|
}
|
|
@@ -388,13 +498,12 @@ function walk(
|
|
|
388
498
|
const node: SemanticNode = {
|
|
389
499
|
elementRef: stamp(element, document),
|
|
390
500
|
role: roleOf(element) ?? element.tagName.toLowerCase(),
|
|
501
|
+
name: accessibleName(element) ?? null,
|
|
502
|
+
// An empty value is reported the same way as no value.
|
|
503
|
+
value: valueOf(element) || null,
|
|
391
504
|
states: statesOf(element, document),
|
|
392
505
|
children: [],
|
|
393
506
|
};
|
|
394
|
-
const name = accessibleName(element);
|
|
395
|
-
if (name) node.name = name;
|
|
396
|
-
const value = valueOf(element);
|
|
397
|
-
if (value !== undefined && value.length > 0) node.value = value;
|
|
398
507
|
if (depth >= MAX_DEPTH) {
|
|
399
508
|
if (element.children.length > 0) {
|
|
400
509
|
budget.truncated = true;
|
|
@@ -425,7 +534,7 @@ function snapshot(world: ShimWorld): ShimResult<{ page: PageState; root: Semanti
|
|
|
425
534
|
};
|
|
426
535
|
}
|
|
427
536
|
|
|
428
|
-
function centerOf(element: Element)
|
|
537
|
+
function centerOf(element: Element) {
|
|
429
538
|
const rect = element.getBoundingClientRect();
|
|
430
539
|
return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
|
|
431
540
|
}
|
|
@@ -456,7 +565,7 @@ function dispatchKey(
|
|
|
456
565
|
element: Element,
|
|
457
566
|
type: string,
|
|
458
567
|
key: string,
|
|
459
|
-
modifiers: string
|
|
568
|
+
modifiers: Array<string>,
|
|
460
569
|
): boolean {
|
|
461
570
|
const event = new world.KeyboardEvent(type, {
|
|
462
571
|
bubbles: true,
|
|
@@ -494,7 +603,9 @@ function focus(element: Element): void {
|
|
|
494
603
|
|
|
495
604
|
function click(world: ShimWorld, element: string): ShimResult<Record<string, never>> {
|
|
496
605
|
const node = resolveRef(world.document, element);
|
|
497
|
-
if (!node)
|
|
606
|
+
if (!node) {
|
|
607
|
+
return unresolved(element);
|
|
608
|
+
}
|
|
498
609
|
focus(node);
|
|
499
610
|
dispatchMouse(world, node, "pointerdown");
|
|
500
611
|
dispatchMouse(world, node, "mousedown");
|
|
@@ -504,28 +615,87 @@ function click(world: ShimWorld, element: string): ShimResult<Record<string, nev
|
|
|
504
615
|
// (submit, checkbox toggle, link navigation) that a synthetic MouseEvent
|
|
505
616
|
// does not. isTrusted stays false.
|
|
506
617
|
const nativeClick = prop<(this: Element) => void>(node, "click");
|
|
507
|
-
if (nativeClick)
|
|
508
|
-
|
|
618
|
+
if (nativeClick) {
|
|
619
|
+
nativeClick.call(node);
|
|
620
|
+
} else {
|
|
621
|
+
dispatchMouse(world, node, "click");
|
|
622
|
+
}
|
|
509
623
|
return ok();
|
|
510
624
|
}
|
|
511
625
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
626
|
+
/** Input types whose value is not typed text. Keys reach them; nothing is inserted. */
|
|
627
|
+
const UNTYPED_INPUT_TYPES = new Set([
|
|
628
|
+
"button",
|
|
629
|
+
"checkbox",
|
|
630
|
+
"hidden",
|
|
631
|
+
"image",
|
|
632
|
+
"radio",
|
|
633
|
+
"reset",
|
|
634
|
+
"submit",
|
|
635
|
+
]);
|
|
636
|
+
|
|
637
|
+
/** Input types that hold one structured value, which typed text replaces. */
|
|
638
|
+
const REPLACED_INPUT_TYPES = new Set([
|
|
639
|
+
"color",
|
|
640
|
+
"date",
|
|
641
|
+
"datetime-local",
|
|
642
|
+
"month",
|
|
643
|
+
"range",
|
|
644
|
+
"time",
|
|
645
|
+
"week",
|
|
646
|
+
]);
|
|
647
|
+
|
|
648
|
+
function inputType(element: Element): string {
|
|
649
|
+
return (prop<string>(element, "type") ?? element.getAttribute("type") ?? "text").toLowerCase();
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Returns the function that types one character into the element, which
|
|
654
|
+
* answers whether the element took it.
|
|
655
|
+
*/
|
|
656
|
+
function textInserter(element: Element): (text: string) => boolean {
|
|
657
|
+
if (element.tagName !== "INPUT" && element.tagName !== "TEXTAREA") {
|
|
658
|
+
return (text) => {
|
|
659
|
+
if (!declaresEditable(element) && prop<boolean>(element, "isContentEditable") !== true) {
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
662
|
+
element.textContent = `${element.textContent ?? ""}${text}`;
|
|
663
|
+
return true;
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
const type = element.tagName === "INPUT" ? inputType(element) : "textarea";
|
|
667
|
+
if (UNTYPED_INPUT_TYPES.has(type)) {
|
|
668
|
+
return () => false;
|
|
669
|
+
}
|
|
670
|
+
const read = () => prop<string>(element, "value") ?? "";
|
|
671
|
+
// Types without a selection (email, number, date, ...) report a null
|
|
672
|
+
// `selectionStart` and throw from `setSelectionRange`. They also blank a value
|
|
673
|
+
// that is not complete yet, so the typed text is held here and assigned whole.
|
|
674
|
+
let held = REPLACED_INPUT_TYPES.has(type) ? "" : read();
|
|
675
|
+
let seen = read();
|
|
676
|
+
return (text) => {
|
|
677
|
+
const current = read();
|
|
678
|
+
const start = prop<number | null>(element, "selectionStart");
|
|
679
|
+
if (typeof start !== "number") {
|
|
680
|
+
// The page rewrote the field between keys; continue from what it wrote.
|
|
681
|
+
if (current !== seen) {
|
|
682
|
+
held = current;
|
|
683
|
+
}
|
|
684
|
+
held += text;
|
|
685
|
+
Object.assign(element, { value: held });
|
|
686
|
+
seen = read();
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
516
689
|
const end = prop<number | null>(element, "selectionEnd") ?? start;
|
|
517
|
-
|
|
518
|
-
(element as FormControl).value = next;
|
|
690
|
+
Object.assign(element, { value: `${current.slice(0, start)}${text}${current.slice(end)}` });
|
|
519
691
|
const cursor = start + text.length;
|
|
520
|
-
prop<(this: Element, start: number, end: number) => void>(
|
|
692
|
+
prop<(this: Element, start: number, end: number) => void>(element, "setSelectionRange")?.call(
|
|
521
693
|
element,
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
element.textContent = `${element.textContent ?? ""}${text}`;
|
|
528
|
-
}
|
|
694
|
+
cursor,
|
|
695
|
+
cursor,
|
|
696
|
+
);
|
|
697
|
+
return true;
|
|
698
|
+
};
|
|
529
699
|
}
|
|
530
700
|
|
|
531
701
|
function typeInto(
|
|
@@ -534,28 +704,41 @@ function typeInto(
|
|
|
534
704
|
text: string,
|
|
535
705
|
): ShimResult<Record<string, never>> {
|
|
536
706
|
const node = resolveRef(world.document, element);
|
|
537
|
-
if (!node)
|
|
707
|
+
if (!node) {
|
|
708
|
+
return unresolved(element);
|
|
709
|
+
}
|
|
710
|
+
if (node.tagName === "INPUT" && inputType(node) === "file") {
|
|
711
|
+
return unsupported("type cannot set an input type=file; use set_file_input");
|
|
712
|
+
}
|
|
538
713
|
focus(node);
|
|
714
|
+
const insert = textInserter(node);
|
|
715
|
+
let changed = false;
|
|
539
716
|
for (const char of text) {
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
717
|
+
// A cancelled keydown suppresses the text, as it does for a real key.
|
|
718
|
+
if (dispatchKey(world, node, "keydown", char, []) && insert(char)) {
|
|
719
|
+
changed = true;
|
|
720
|
+
dispatchInput(world, node, char);
|
|
721
|
+
}
|
|
543
722
|
dispatchKey(world, node, "keyup", char, []);
|
|
544
723
|
}
|
|
545
|
-
|
|
724
|
+
if (changed) {
|
|
725
|
+
node.dispatchEvent(new world.Event("change", { bubbles: true }));
|
|
726
|
+
}
|
|
546
727
|
return ok();
|
|
547
728
|
}
|
|
548
729
|
|
|
549
730
|
function press(
|
|
550
731
|
world: ShimWorld,
|
|
551
732
|
key: string,
|
|
552
|
-
modifiers: string
|
|
733
|
+
modifiers: Array<string> = [],
|
|
553
734
|
element?: string | null,
|
|
554
735
|
): ShimResult<Record<string, never>> {
|
|
555
736
|
let node: Element | null;
|
|
556
737
|
if (element) {
|
|
557
738
|
node = resolveRef(world.document, element);
|
|
558
|
-
if (!node)
|
|
739
|
+
if (!node) {
|
|
740
|
+
return unresolved(element);
|
|
741
|
+
}
|
|
559
742
|
} else {
|
|
560
743
|
node = world.document.activeElement ?? world.document.body ?? world.document.documentElement;
|
|
561
744
|
}
|
|
@@ -573,7 +756,9 @@ function scroll(
|
|
|
573
756
|
): ShimResult<Record<string, never>> {
|
|
574
757
|
if (element) {
|
|
575
758
|
const node = resolveRef(world.document, element);
|
|
576
|
-
if (!node)
|
|
759
|
+
if (!node) {
|
|
760
|
+
return unresolved(element);
|
|
761
|
+
}
|
|
577
762
|
node.scrollTop += deltaY;
|
|
578
763
|
node.scrollLeft += deltaX;
|
|
579
764
|
node.dispatchEvent(new world.Event("scroll", { bubbles: true }));
|
|
@@ -592,9 +777,13 @@ function drag(
|
|
|
592
777
|
targetRef: string,
|
|
593
778
|
): ShimResult<Record<string, never>> {
|
|
594
779
|
const source = resolveRef(world.document, sourceRef);
|
|
595
|
-
if (!source)
|
|
780
|
+
if (!source) {
|
|
781
|
+
return unresolved(sourceRef);
|
|
782
|
+
}
|
|
596
783
|
const target = resolveRef(world.document, targetRef);
|
|
597
|
-
if (!target)
|
|
784
|
+
if (!target) {
|
|
785
|
+
return unresolved(targetRef);
|
|
786
|
+
}
|
|
598
787
|
const from = centerOf(source);
|
|
599
788
|
const to = centerOf(target);
|
|
600
789
|
// Two-point untrusted path: pointer/mouse at the source center, then the
|
|
@@ -603,10 +792,10 @@ function drag(
|
|
|
603
792
|
dispatchMouse(world, source, "mousedown", from);
|
|
604
793
|
dispatchMouse(world, target, "pointermove", to);
|
|
605
794
|
dispatchMouse(world, target, "mousemove", to);
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
795
|
+
// Only the two methods are read, so a page without DataTransfer gets a stand-in with just those.
|
|
796
|
+
const transfer: Pick<DataTransfer, "setData" | "getData"> = world.DataTransfer
|
|
797
|
+
? new world.DataTransfer()
|
|
798
|
+
: { setData() {}, getData: () => "" };
|
|
610
799
|
const dragEvent = (type: string, current: Element, point: { x: number; y: number }) => {
|
|
611
800
|
const event = new world.MouseEvent(type, {
|
|
612
801
|
bubbles: true,
|
|
@@ -632,7 +821,9 @@ function decodeBase64(content: string): Uint8Array<ArrayBuffer> | null {
|
|
|
632
821
|
try {
|
|
633
822
|
const binary = atob(content);
|
|
634
823
|
const bytes = new Uint8Array(binary.length);
|
|
635
|
-
for (let i = 0; i < binary.length; i += 1)
|
|
824
|
+
for (let i = 0; i < binary.length; i += 1) {
|
|
825
|
+
bytes[i] = binary.charCodeAt(i);
|
|
826
|
+
}
|
|
636
827
|
return bytes;
|
|
637
828
|
} catch {
|
|
638
829
|
return null;
|
|
@@ -644,13 +835,19 @@ function acceptMatches(fileName: string, mediaType: string, accept: string): boo
|
|
|
644
835
|
.split(",")
|
|
645
836
|
.map((token) => token.trim())
|
|
646
837
|
.filter((token) => token.length > 0);
|
|
647
|
-
if (tokens.length === 0)
|
|
838
|
+
if (tokens.length === 0) {
|
|
839
|
+
return true;
|
|
840
|
+
}
|
|
648
841
|
const name = fileName.toLowerCase();
|
|
649
842
|
const type = mediaType.toLowerCase();
|
|
650
843
|
return tokens.some((token) => {
|
|
651
844
|
const needle = token.toLowerCase();
|
|
652
|
-
if (needle.startsWith("."))
|
|
653
|
-
|
|
845
|
+
if (needle.startsWith(".")) {
|
|
846
|
+
return name.endsWith(needle);
|
|
847
|
+
}
|
|
848
|
+
if (needle.endsWith("/*")) {
|
|
849
|
+
return type.startsWith(needle.slice(0, -1));
|
|
850
|
+
}
|
|
654
851
|
return type === needle;
|
|
655
852
|
});
|
|
656
853
|
}
|
|
@@ -658,12 +855,13 @@ function acceptMatches(fileName: string, mediaType: string, accept: string): boo
|
|
|
658
855
|
function setFileInput(
|
|
659
856
|
world: ShimWorld,
|
|
660
857
|
element: string,
|
|
661
|
-
files: FileInputFile
|
|
858
|
+
files: Array<FileInputFile>,
|
|
662
859
|
): ShimResult<Record<string, never>> {
|
|
663
860
|
const node = resolveRef(world.document, element);
|
|
664
|
-
if (!node)
|
|
665
|
-
|
|
666
|
-
|
|
861
|
+
if (!node) {
|
|
862
|
+
return unresolved(element);
|
|
863
|
+
}
|
|
864
|
+
if (node.tagName !== "INPUT" || inputType(node) !== "file") {
|
|
667
865
|
return unsupported("set_file_input requires an input type=file");
|
|
668
866
|
}
|
|
669
867
|
if (!Array.isArray(files) || files.length === 0) {
|
|
@@ -673,16 +871,17 @@ function setFileInput(
|
|
|
673
871
|
if (files.length > 1 && !multiple) {
|
|
674
872
|
return unsupported("set_file_input received multiple files for an input without multiple");
|
|
675
873
|
}
|
|
676
|
-
|
|
677
|
-
const DataTransferCtor =
|
|
678
|
-
world.DataTransfer ?? (typeof DataTransfer === "function" ? DataTransfer : undefined);
|
|
874
|
+
// Constructed in the page's own realm, or the input refuses the FileList.
|
|
875
|
+
const { File: FileCtor, DataTransfer: DataTransferCtor } = world;
|
|
679
876
|
if (!FileCtor || !DataTransferCtor) {
|
|
680
877
|
return unsupported("set_file_input needs File and DataTransfer in the page");
|
|
681
878
|
}
|
|
682
879
|
const accept = node.getAttribute("accept") ?? "";
|
|
683
880
|
const transfer = new DataTransferCtor();
|
|
684
881
|
for (const file of files) {
|
|
685
|
-
if (!file.name)
|
|
882
|
+
if (!file.name) {
|
|
883
|
+
return unsupported("set_file_input file name must be non-empty");
|
|
884
|
+
}
|
|
686
885
|
const bytes = decodeBase64(file.contentBase64);
|
|
687
886
|
if (!bytes) {
|
|
688
887
|
return unsupported(`set_file_input file ${JSON.stringify(file.name)} is not valid base64`);
|
|
@@ -691,10 +890,12 @@ function setFileInput(
|
|
|
691
890
|
if (!acceptMatches(file.name, mediaType, accept)) {
|
|
692
891
|
return unsupported(`set_file_input file ${JSON.stringify(file.name)} does not match accept`);
|
|
693
892
|
}
|
|
694
|
-
transfer.items.add(
|
|
893
|
+
transfer.items.add(
|
|
894
|
+
new FileCtor([bytes], file.name, mediaType ? { type: mediaType } : undefined),
|
|
895
|
+
);
|
|
695
896
|
}
|
|
696
897
|
try {
|
|
697
|
-
(node
|
|
898
|
+
Object.assign(node, { files: transfer.files });
|
|
698
899
|
} catch (error) {
|
|
699
900
|
return unsupported(
|
|
700
901
|
`the page refused FileList assignment from a constructed DataTransfer: ${String(error)}`,
|
|
@@ -730,11 +931,30 @@ function waitFor(world: ShimWorld, predicate: WaitPredicate): ShimResult<{ holds
|
|
|
730
931
|
}
|
|
731
932
|
}
|
|
732
933
|
|
|
934
|
+
/**
|
|
935
|
+
* Text for a value the page logged or threw. `String` throws for a
|
|
936
|
+
* prototype-less object or a throwing `toString`, and that must not escape into
|
|
937
|
+
* the page's own `console` call.
|
|
938
|
+
*/
|
|
939
|
+
function describeValue(value: unknown): string {
|
|
940
|
+
try {
|
|
941
|
+
return String(value);
|
|
942
|
+
} catch {
|
|
943
|
+
return "[unprintable]";
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
733
947
|
function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
|
|
734
|
-
const existing = world[SHIM_GLOBAL]
|
|
735
|
-
if (existing)
|
|
948
|
+
const existing = world[SHIM_GLOBAL];
|
|
949
|
+
if (existing) {
|
|
950
|
+
return existing.readEvents.bind(existing);
|
|
951
|
+
}
|
|
736
952
|
|
|
737
|
-
const
|
|
953
|
+
const epoch =
|
|
954
|
+
typeof crypto !== "undefined" && typeof crypto.randomUUID === "function"
|
|
955
|
+
? crypto.randomUUID()
|
|
956
|
+
: `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
957
|
+
const ring: Array<PageEvent> = [];
|
|
738
958
|
let nextSeq = 1;
|
|
739
959
|
let dropped = 0;
|
|
740
960
|
|
|
@@ -750,12 +970,12 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
|
|
|
750
970
|
|
|
751
971
|
const wrap = (level: "log" | "info" | "warn" | "error" | "debug") => {
|
|
752
972
|
const original = world.console[level]?.bind(world.console);
|
|
753
|
-
world.console[level] = (...args: unknown
|
|
973
|
+
world.console[level] = (...args: Array<unknown>) => {
|
|
754
974
|
push({
|
|
755
975
|
seq: nextSeq,
|
|
756
976
|
kind: "console",
|
|
757
977
|
level,
|
|
758
|
-
text: args.map(
|
|
978
|
+
text: args.map(describeValue).join(" "),
|
|
759
979
|
});
|
|
760
980
|
original?.(...args);
|
|
761
981
|
};
|
|
@@ -770,18 +990,20 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
|
|
|
770
990
|
const message =
|
|
771
991
|
"message" in event && typeof event.message === "string"
|
|
772
992
|
? event.message
|
|
773
|
-
:
|
|
993
|
+
: describeValue(event);
|
|
774
994
|
push({ seq: nextSeq, kind: "error", message });
|
|
775
995
|
});
|
|
776
996
|
world.addEventListener("unhandledrejection", (event) => {
|
|
777
|
-
const reason =
|
|
778
|
-
"reason" in event ? String((event as PromiseRejectionEvent).reason) : "unhandledrejection";
|
|
997
|
+
const reason = "reason" in event ? describeValue(event.reason) : "unhandledrejection";
|
|
779
998
|
push({ seq: nextSeq, kind: "error", message: reason });
|
|
780
999
|
});
|
|
781
1000
|
|
|
782
1001
|
const notifyNavigation = () => {
|
|
783
1002
|
push({ seq: nextSeq, kind: "navigation", url: world.location.href });
|
|
784
1003
|
};
|
|
1004
|
+
// Record the initial document load so full-document navigations and reloads
|
|
1005
|
+
// are observable through `longhorn://agent-control/navigation` as seq 1.
|
|
1006
|
+
notifyNavigation();
|
|
785
1007
|
world.addEventListener("hashchange", notifyNavigation);
|
|
786
1008
|
world.addEventListener("popstate", notifyNavigation);
|
|
787
1009
|
const history = world.history;
|
|
@@ -796,21 +1018,27 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
|
|
|
796
1018
|
notifyNavigation();
|
|
797
1019
|
}) as History["replaceState"];
|
|
798
1020
|
|
|
799
|
-
return (sinceSeq = 0) =>
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
1021
|
+
return (sinceSeq = 0, sinceEpoch?: string | null) => {
|
|
1022
|
+
const effectiveSince =
|
|
1023
|
+
sinceEpoch !== undefined && sinceEpoch !== null && sinceEpoch !== epoch ? 0 : sinceSeq;
|
|
1024
|
+
return {
|
|
1025
|
+
epoch,
|
|
1026
|
+
events: ring.filter((event) => event.seq > effectiveSince),
|
|
1027
|
+
nextSeq,
|
|
1028
|
+
dropped,
|
|
1029
|
+
};
|
|
1030
|
+
};
|
|
804
1031
|
}
|
|
805
1032
|
|
|
806
|
-
function installOriginTracking(
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
> {
|
|
1033
|
+
function installOriginTracking(
|
|
1034
|
+
world: ShimWorld,
|
|
1035
|
+
): Pick<AgentControlApi, "markAgentOrigin" | "isAgentOriginated"> {
|
|
810
1036
|
let agentOriginSeq = 0;
|
|
811
1037
|
let humanOriginSeq = 0;
|
|
812
1038
|
const markHuman = (event: Event) => {
|
|
813
|
-
if (event.isTrusted)
|
|
1039
|
+
if (event.isTrusted) {
|
|
1040
|
+
humanOriginSeq = agentOriginSeq + 1;
|
|
1041
|
+
}
|
|
814
1042
|
};
|
|
815
1043
|
world.addEventListener("pointerdown", markHuman, true);
|
|
816
1044
|
world.addEventListener("keydown", markHuman, true);
|
|
@@ -824,8 +1052,10 @@ function installOriginTracking(world: ShimWorld): Pick<
|
|
|
824
1052
|
}
|
|
825
1053
|
|
|
826
1054
|
export function installAgentControlShim(world: ShimWorld): AgentControlApi {
|
|
827
|
-
const existing = world[SHIM_GLOBAL]
|
|
828
|
-
if (existing)
|
|
1055
|
+
const existing = world[SHIM_GLOBAL];
|
|
1056
|
+
if (existing) {
|
|
1057
|
+
return existing;
|
|
1058
|
+
}
|
|
829
1059
|
const readEvents = installEventRing(world);
|
|
830
1060
|
const origin = installOriginTracking(world);
|
|
831
1061
|
const api: AgentControlApi = {
|