@burdenoff/ui-engine 2026.1127.2 → 2026.1127.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +74 -4
  2. package/dist/capture.js +3 -2
  3. package/dist/dom-runtime/actions.d.ts +65 -0
  4. package/dist/dom-runtime/iife.d.ts +1 -0
  5. package/dist/dom-runtime/index.d.ts +7 -0
  6. package/dist/dom-runtime/runtime.d.ts +79 -0
  7. package/dist/dom-runtime.d.ts +2 -0
  8. package/dist/dom-runtime.iife.js +1 -0
  9. package/dist/dom-runtime.js +30 -0
  10. package/dist/drivers/dom-driver.d.ts +44 -0
  11. package/dist/drivers/index.d.ts +7 -0
  12. package/dist/drivers/snapshot-driver.d.ts +59 -0
  13. package/dist/drivers.d.ts +2 -0
  14. package/dist/drivers.js +170 -0
  15. package/dist/{index-n5amgh31.js → index-2kgsgn1c.js} +1 -1
  16. package/dist/index-3f1j8386.js +334 -0
  17. package/dist/index-3jymbm5t.js +315 -0
  18. package/dist/index-5k26keah.js +815 -0
  19. package/dist/index-77pcz6n3.js +303 -0
  20. package/dist/index-895kscx3.js +741 -0
  21. package/dist/{index-phd8yhyg.js → index-bnvmt4wf.js} +7 -5
  22. package/dist/{index-c6pmt2rg.js → index-cs6acyjn.js} +6 -59
  23. package/dist/index-ct2xctrq.js +229 -0
  24. package/dist/index-h88y5qgv.js +400 -0
  25. package/dist/index-mgztm37r.js +57 -0
  26. package/dist/{index-tqffkt91.js → index-pm13mvvc.js} +26 -1009
  27. package/dist/index-vg8r40y7.js +220 -0
  28. package/dist/index-ycs2bz1a.js +589 -0
  29. package/dist/index-zzhysbkj.js +1731 -0
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.js +130 -42
  32. package/dist/interpret/compile.d.ts +58 -0
  33. package/dist/interpret/driver.d.ts +171 -0
  34. package/dist/interpret/errors.d.ts +194 -0
  35. package/dist/interpret/events.d.ts +73 -0
  36. package/dist/interpret/expr.d.ts +35 -0
  37. package/dist/interpret/index.d.ts +13 -0
  38. package/dist/interpret/run.d.ts +77 -0
  39. package/dist/interpret.d.ts +2 -0
  40. package/dist/interpret.js +84 -0
  41. package/dist/recording.js +1 -1
  42. package/dist/replay.js +2 -2
  43. package/dist/script/adapters/browser-action-contract.d.ts +5 -2
  44. package/dist/script/limits.d.ts +2 -0
  45. package/dist/script.js +44 -38
  46. package/dist/target/aria.d.ts +31 -0
  47. package/dist/target/dom.d.ts +35 -0
  48. package/dist/target/index.d.ts +13 -0
  49. package/dist/target/markers.d.ts +18 -0
  50. package/dist/target/methods.d.ts +52 -0
  51. package/dist/target/resolve.d.ts +63 -0
  52. package/dist/target/snapshot.d.ts +57 -0
  53. package/dist/target/text.d.ts +23 -0
  54. package/dist/target/types.d.ts +88 -0
  55. package/dist/target.d.ts +2 -0
  56. package/dist/target.js +96 -0
  57. package/dist/testing/fake-web-driver.d.ts +28 -0
  58. package/dist/testing/fixtures/ambiguous.d.ts +7 -0
  59. package/dist/testing/fixtures/iframe.d.ts +6 -0
  60. package/dist/testing/fixtures/index.d.ts +26 -0
  61. package/dist/testing/fixtures/invoices-v2.d.ts +8 -0
  62. package/dist/testing/fixtures/invoices-v3.d.ts +7 -0
  63. package/dist/testing/fixtures/invoices.d.ts +6 -0
  64. package/dist/testing/fixtures/login.d.ts +6 -0
  65. package/dist/testing/fixtures/shadow.d.ts +7 -0
  66. package/dist/testing/index.d.ts +7 -0
  67. package/dist/testing/mount.d.ts +18 -0
  68. package/dist/testing.d.ts +2 -0
  69. package/dist/testing.js +379 -0
  70. package/package.json +29 -5
@@ -0,0 +1,815 @@
1
+ import {
2
+ SENSITIVE_MARKERS2,
3
+ assignedSlotOf,
4
+ containerOf,
5
+ isMarkedSensitive,
6
+ MAX_COMPARED_TEXT2,
7
+ normaliseText2,
8
+ sameText2,
9
+ containsText2,
10
+ textSimilarity2,
11
+ clip2,
12
+ roleOf2,
13
+ isTypedContent2,
14
+ accessibleName2,
15
+ labelName2,
16
+ visibleText2,
17
+ matchesRoleAndName2,
18
+ isElementVisible2,
19
+ isElementEnabled2,
20
+ elementRect2,
21
+ elementsUnder2,
22
+ isSensitiveElement2
23
+ } from "./index-ycs2bz1a.js";
24
+ import {
25
+ REDACTED2,
26
+ redactCredentials2,
27
+ containsCredential,
28
+ decodeUriText,
29
+ containsCredentialInUrl
30
+ } from "./index-mgztm37r.js";
31
+
32
+ // src/target/types.ts
33
+ var WEB_TARGET_ERROR_CODES2 = ["ELEMENT_NOT_FOUND", "ELEMENT_AMBIGUOUS", "ELEMENT_NOT_INTERACTABLE"];
34
+ var MAX_CANDIDATES2 = 5;
35
+ var MAX_CANDIDATE_NAME2 = 200;
36
+
37
+ // src/target/methods.ts
38
+ var DEFAULT_FUZZY_THRESHOLD2 = 0.8;
39
+ var FUZZY_MARGIN2 = 0.05;
40
+ var ANCHOR_MARGIN_PX2 = 4;
41
+ var MAX_XPATH_RESULTS2 = 1000;
42
+ var EDGE_SLACK_PX = 2;
43
+ var INTERACTIVE_ROLES2 = new Set([
44
+ "button",
45
+ "checkbox",
46
+ "combobox",
47
+ "link",
48
+ "listbox",
49
+ "menuitem",
50
+ "menuitemcheckbox",
51
+ "menuitemradio",
52
+ "option",
53
+ "radio",
54
+ "searchbox",
55
+ "slider",
56
+ "spinbutton",
57
+ "switch",
58
+ "tab",
59
+ "textbox",
60
+ "treeitem"
61
+ ]);
62
+ var ATTRIBUTE_NAME = /^[A-Za-z_][\w:.-]{0,127}$/;
63
+ function documentOf(root) {
64
+ return root.nodeType === 9 ? root : root.ownerDocument;
65
+ }
66
+ function matchRole(method, context) {
67
+ const elements = elementsUnder2(context.root).filter((element) => matchesRoleAndName2(element, method.role, method.name, context.probe));
68
+ return { kind: "list", elements, ...method.nth !== undefined ? { nth: method.nth } : {} };
69
+ }
70
+ function matchCss(selector, context) {
71
+ try {
72
+ return { kind: "list", elements: Array.from(context.root.querySelectorAll(selector)) };
73
+ } catch {
74
+ return { kind: "invalid" };
75
+ }
76
+ }
77
+ function matchTestId(method, context) {
78
+ const attribute = method.attribute ?? "data-testid";
79
+ if (!ATTRIBUTE_NAME.test(attribute))
80
+ return { kind: "invalid" };
81
+ const elements = elementsUnder2(context.root).filter((element) => element.getAttribute(attribute) === method.value);
82
+ return { kind: "list", elements };
83
+ }
84
+ function isUnder(root, node) {
85
+ if (root.nodeType === 9)
86
+ return node.ownerDocument === root && node.getRootNode() === root;
87
+ return root === node || root.contains(node);
88
+ }
89
+ function matchXPath(method, context) {
90
+ const document = documentOf(context.root);
91
+ if (typeof document.evaluate !== "function")
92
+ return { kind: "unsupported" };
93
+ let result;
94
+ try {
95
+ result = document.evaluate(method.xpath, context.root, null, 7, null);
96
+ } catch {
97
+ return { kind: "invalid" };
98
+ }
99
+ const elements = [];
100
+ const length = Math.min(result.snapshotLength, MAX_XPATH_RESULTS2);
101
+ for (let index = 0;index < length; index += 1) {
102
+ const node = result.snapshotItem(index);
103
+ if (node && node.nodeType === 1 && isUnder(context.root, node))
104
+ elements.push(node);
105
+ }
106
+ return { kind: "list", elements, ...result.snapshotLength > MAX_XPATH_RESULTS2 ? { overflow: true } : {} };
107
+ }
108
+ function lengthAllows(name, wantedLength, threshold) {
109
+ const length = Math.min(normaliseText2(name).length, MAX_COMPARED_TEXT2);
110
+ const longest = Math.max(length, wantedLength);
111
+ return longest > 0 && Math.abs(length - wantedLength) <= (1 - threshold) * longest;
112
+ }
113
+ function matchFuzzy(method, context) {
114
+ const threshold = Math.min(1, Math.max(0.5, method.threshold ?? DEFAULT_FUZZY_THRESHOLD2));
115
+ const wantedRole = method.role?.trim().toLowerCase();
116
+ const wantedLength = Math.min(normaliseText2(method.name).length, MAX_COMPARED_TEXT2);
117
+ const scored = [];
118
+ for (const element of elementsUnder2(context.root)) {
119
+ const role = roleOf2(element);
120
+ if (wantedRole ? role !== wantedRole : role === "")
121
+ continue;
122
+ const name = accessibleName2(element, context.probe);
123
+ if (!lengthAllows(name, wantedLength, threshold))
124
+ continue;
125
+ const score = textSimilarity2(name, method.name);
126
+ if (score >= threshold)
127
+ scored.push({ element, score });
128
+ }
129
+ return { kind: "ranked", scored, margin: FUZZY_MARGIN2 };
130
+ }
131
+ function innermostByText(context, matches) {
132
+ const hits = elementsUnder2(context.root).filter((element) => matches(visibleText2(element, context.probe)));
133
+ const keep = context.keep ?? (() => true);
134
+ const innerHits = hits.filter(keep);
135
+ return hits.filter((element) => !innerHits.some((inner) => inner !== element && element.contains(inner)));
136
+ }
137
+ function matchText(method, context) {
138
+ const test = method.exact ? (text) => sameText2(text, method.text) : (text) => containsText2(text, method.text);
139
+ if (normaliseText2(method.text).length === 0)
140
+ return { kind: "list", elements: [] };
141
+ return {
142
+ kind: "list",
143
+ elements: innermostByText(context, test),
144
+ ...method.nth !== undefined ? { nth: method.nth } : {}
145
+ };
146
+ }
147
+ function labelText(text) {
148
+ return normaliseText2(text).replace(/[\s:*]+$/u, "");
149
+ }
150
+ function anchorElements(anchor, context) {
151
+ const found = anchor.kind === "role" ? elementsUnder2(context.root).filter((element) => matchesRoleAndName2(element, anchor.role, anchor.name, context.probe)) : innermostByText(context, (text) => labelText(text) === labelText(anchor.text) && labelText(text) !== "");
152
+ return found.filter((element) => isElementVisible2(element, context.probe));
153
+ }
154
+ function candidateElements(method, context, anchors) {
155
+ const wantedRole = method.role?.trim().toLowerCase();
156
+ return elementsUnder2(context.root).filter((element) => {
157
+ const role = roleOf2(element);
158
+ if (wantedRole ? role !== wantedRole : !INTERACTIVE_ROLES2.has(role))
159
+ return false;
160
+ return !anchors.some((anchor) => anchor === element || element.contains(anchor));
161
+ });
162
+ }
163
+ var HEADING_TAGS = new Set(["h1", "h2", "h3", "h4", "h5", "h6", "legend", "caption", "figcaption", "summary", "dt"]);
164
+ function isHeadingLike(element) {
165
+ return HEADING_TAGS.has(element.localName) || roleOf2(element) === "heading";
166
+ }
167
+ function headingOf(anchor) {
168
+ for (let current = anchor;current; current = current.parentElement) {
169
+ if (isHeadingLike(current))
170
+ return current;
171
+ }
172
+ return null;
173
+ }
174
+ function matchInside(method, context, anchors) {
175
+ const candidates = candidateElements(method, context, anchors);
176
+ const inside = new Set;
177
+ for (const anchor of anchors) {
178
+ const heading = headingOf(anchor);
179
+ const container = heading?.parentElement ?? anchor;
180
+ for (const candidate of candidates)
181
+ if (container.contains(candidate))
182
+ inside.add(candidate);
183
+ }
184
+ return { kind: "list", elements: candidates.filter((candidate) => inside.has(candidate)) };
185
+ }
186
+ function overlaps(aStart, aEnd, bStart, bEnd) {
187
+ return aStart < bEnd && bStart < aEnd;
188
+ }
189
+ function relationDistance2(relation, anchor, element) {
190
+ const aRight = anchor.x + anchor.width;
191
+ const aBottom = anchor.y + anchor.height;
192
+ const eRight = element.x + element.width;
193
+ const eBottom = element.y + element.height;
194
+ const sameRow = overlaps(anchor.y, aBottom, element.y, eBottom);
195
+ const sameColumn = overlaps(anchor.x, aRight, element.x, eRight);
196
+ switch (relation) {
197
+ case "rightOf":
198
+ return sameRow && element.x >= aRight - EDGE_SLACK_PX ? Math.max(0, element.x - aRight) : null;
199
+ case "leftOf":
200
+ return sameRow && eRight <= anchor.x + EDGE_SLACK_PX ? Math.max(0, anchor.x - eRight) : null;
201
+ case "below":
202
+ return sameColumn && element.y >= aBottom - EDGE_SLACK_PX ? Math.max(0, element.y - aBottom) : null;
203
+ case "above":
204
+ return sameColumn && eBottom <= anchor.y + EDGE_SLACK_PX ? Math.max(0, anchor.y - eBottom) : null;
205
+ case "sameRow":
206
+ return sameRow ? Math.abs(element.x + element.width / 2 - (anchor.x + anchor.width / 2)) : null;
207
+ default:
208
+ return null;
209
+ }
210
+ }
211
+ function hasArea(rect) {
212
+ return rect.width > 0 && rect.height > 0;
213
+ }
214
+ function matchAnchor(method, context) {
215
+ const anchors = anchorElements(method.anchor, context);
216
+ if (anchors.length === 0)
217
+ return { kind: "list", elements: [] };
218
+ if (method.relation === "inside")
219
+ return matchInside(method, context, anchors);
220
+ const anchorRects = anchors.map((anchor) => elementRect2(anchor, context.probe)).filter(hasArea);
221
+ if (anchorRects.length === 0)
222
+ return { kind: "unsupported" };
223
+ const scored = [];
224
+ for (const element of candidateElements(method, context, anchors)) {
225
+ const rect = elementRect2(element, context.probe);
226
+ if (!hasArea(rect))
227
+ continue;
228
+ let best = null;
229
+ for (const anchorRect of anchorRects) {
230
+ const distance = relationDistance2(method.relation, anchorRect, rect);
231
+ if (distance !== null && (best === null || distance < best))
232
+ best = distance;
233
+ }
234
+ if (best !== null)
235
+ scored.push({ element, score: -best });
236
+ }
237
+ return { kind: "ranked", scored, margin: ANCHOR_MARGIN_PX2 };
238
+ }
239
+ function matchMethod2(method, context) {
240
+ switch (method.kind) {
241
+ case "role":
242
+ return matchRole(method, context);
243
+ case "css":
244
+ return matchCss(method.selector, context);
245
+ case "testId":
246
+ return matchTestId(method, context);
247
+ case "xpath":
248
+ return matchXPath(method, context);
249
+ case "fuzzy":
250
+ return matchFuzzy(method, context);
251
+ case "anchor":
252
+ return matchAnchor(method, context);
253
+ case "text":
254
+ return matchText(method, context);
255
+ default:
256
+ return { kind: "unsupported" };
257
+ }
258
+ }
259
+
260
+ // src/target/resolve.ts
261
+ var ZERO = { x: 0, y: 0, width: 0, height: 0 };
262
+ function enterFrames2(root, chain, probe) {
263
+ let entry = { root, offset: ZERO, hidden: false, disabled: false };
264
+ for (const [index, selector] of (chain ?? []).entries()) {
265
+ let found;
266
+ try {
267
+ found = Array.from(entry.root.querySelectorAll(selector));
268
+ } catch {
269
+ return { index, reason: "invalid" };
270
+ }
271
+ if (found.length === 0)
272
+ return { index, reason: "notFound" };
273
+ if (found.length > 1)
274
+ return { index, reason: "ambiguous" };
275
+ const host = found[0];
276
+ const tag = host.localName;
277
+ if (tag === "iframe" || tag === "frame") {
278
+ const frame = host;
279
+ let inner = null;
280
+ try {
281
+ inner = frame.contentDocument;
282
+ } catch {
283
+ inner = null;
284
+ }
285
+ if (!inner)
286
+ return { index, reason: "inaccessible" };
287
+ const box = elementRect2(host, probe, entry.offset);
288
+ entry = {
289
+ root: inner,
290
+ offset: { x: box.x + frame.clientLeft, y: box.y + frame.clientTop, width: 0, height: 0 },
291
+ hidden: entry.hidden || !isElementVisible2(host, probe),
292
+ disabled: entry.disabled || !isElementEnabled2(host)
293
+ };
294
+ } else if (host.shadowRoot) {
295
+ entry = { root: host.shadowRoot, offset: entry.offset, hidden: entry.hidden, disabled: entry.disabled };
296
+ } else {
297
+ return { index, reason: "notAFrame" };
298
+ }
299
+ }
300
+ return entry;
301
+ }
302
+ function inState(element, state, entry, probe) {
303
+ if (state === "attached")
304
+ return true;
305
+ if (entry.hidden || !isElementVisible2(element, probe))
306
+ return false;
307
+ return state === "visible" || !entry.disabled && isElementEnabled2(element);
308
+ }
309
+ function describeCandidate2(element, method, methodIndex, offset, probe) {
310
+ return {
311
+ role: roleOf2(element),
312
+ name: isSensitiveElement2(element) ? "" : clip2(isTypedContent2(element) ? labelName2(element, probe) : accessibleName2(element, probe), MAX_CANDIDATE_NAME2),
313
+ bounds: elementRect2(element, probe, offset),
314
+ method: method.kind,
315
+ methodIndex
316
+ };
317
+ }
318
+ function pickFromList(all, kept, nth, overflow = false) {
319
+ if (kept.length === 0) {
320
+ return all.length > 0 ? { outcome: "notInteractable", elements: all, matches: 0 } : { outcome: "none", elements: [], matches: 0 };
321
+ }
322
+ if (overflow)
323
+ return { outcome: "ambiguous", elements: kept, matches: kept.length };
324
+ if (nth !== undefined) {
325
+ const element = kept[nth];
326
+ return element ? { outcome: "found", element, matches: kept.length } : { outcome: "none", elements: [], matches: kept.length };
327
+ }
328
+ return kept.length === 1 ? { outcome: "found", element: kept[0], matches: 1 } : { outcome: "ambiguous", elements: kept, matches: kept.length };
329
+ }
330
+ function pickRanked(all, kept, margin) {
331
+ if (kept.length === 0) {
332
+ return all.length > 0 ? { outcome: "notInteractable", elements: all.map((item) => item.element), matches: 0 } : { outcome: "none", elements: [], matches: 0 };
333
+ }
334
+ const sorted = [...kept].sort((a, b) => b.score - a.score);
335
+ const best = sorted[0];
336
+ const runnerUp = sorted[1];
337
+ if (!runnerUp || best.score - runnerUp.score >= margin) {
338
+ return { outcome: "found", element: best.element, matches: kept.length };
339
+ }
340
+ const tied = sorted.filter((item) => best.score - item.score < margin).map((item) => item.element);
341
+ return { outcome: "ambiguous", elements: tied, matches: tied.length };
342
+ }
343
+ function pick(match, keep) {
344
+ switch (match.kind) {
345
+ case "invalid":
346
+ case "unsupported":
347
+ return match.kind;
348
+ case "list":
349
+ return pickFromList(match.elements, match.elements.filter(keep), match.nth, match.overflow);
350
+ case "ranked":
351
+ return pickRanked(match.scored, match.scored.filter((item) => keep(item.element)), match.margin);
352
+ }
353
+ }
354
+ function failure(code, attempts, candidates = [], candidateCount = 0, frame) {
355
+ return { status: "failed", code, candidates, candidateCount, attempts, ...frame ? { frame } : {} };
356
+ }
357
+ function resolveWebTarget2(root, target, options = {}) {
358
+ const state = options.state ?? "interactable";
359
+ const entered = enterFrames2(root, target.frame, options);
360
+ if (!("root" in entered)) {
361
+ const attempts = target.methods.map((method, index) => ({
362
+ index,
363
+ method: method.kind,
364
+ outcome: "notTried",
365
+ matches: 0
366
+ }));
367
+ return failure("ELEMENT_NOT_FOUND", attempts, [], 0, entered);
368
+ }
369
+ const attempts = [];
370
+ const firstEnabled = target.methods.findIndex((method) => method.enabled !== false);
371
+ let ambiguous;
372
+ let blocked;
373
+ let found;
374
+ for (const [index, method] of target.methods.entries()) {
375
+ if (found) {
376
+ attempts.push({ index, method: method.kind, outcome: "notTried", matches: 0 });
377
+ continue;
378
+ }
379
+ if (method.enabled === false) {
380
+ attempts.push({ index, method: method.kind, outcome: "disabled", matches: 0 });
381
+ continue;
382
+ }
383
+ const keep = (element) => inState(element, state, entered, options);
384
+ const result = pick(matchMethod2(method, { root: entered.root, probe: options, keep }), keep);
385
+ if (result === "invalid" || result === "unsupported") {
386
+ attempts.push({ index, method: method.kind, outcome: result, matches: 0 });
387
+ continue;
388
+ }
389
+ attempts.push({ index, method: method.kind, outcome: result.outcome, matches: result.matches });
390
+ if (result.outcome === "found")
391
+ found = { element: result.element, method, index };
392
+ else if (result.outcome === "ambiguous" && !ambiguous) {
393
+ ambiguous = { elements: result.elements, method, index, count: result.matches };
394
+ } else if (result.outcome === "notInteractable" && !blocked)
395
+ blocked = { elements: result.elements, method, index };
396
+ }
397
+ if (found) {
398
+ return {
399
+ status: "found",
400
+ element: found.element,
401
+ method: found.method.kind,
402
+ methodIndex: found.index,
403
+ fallback: found.index !== firstEnabled,
404
+ candidate: describeCandidate2(found.element, found.method, found.index, entered.offset, options),
405
+ attempts
406
+ };
407
+ }
408
+ const listed = (report) => report.elements.slice(0, MAX_CANDIDATES2).map((element) => describeCandidate2(element, report.method, report.index, entered.offset, options));
409
+ if (ambiguous)
410
+ return failure("ELEMENT_AMBIGUOUS", attempts, listed(ambiguous), ambiguous.count);
411
+ if (blocked)
412
+ return failure("ELEMENT_NOT_INTERACTABLE", attempts, listed(blocked), blocked.elements.length);
413
+ return failure("ELEMENT_NOT_FOUND", attempts);
414
+ }
415
+ function resolveAllWebTargets2(root, target, options = {}) {
416
+ const state = options.state ?? "visible";
417
+ const entered = enterFrames2(root, target.frame, options);
418
+ if (!("root" in entered)) {
419
+ return failure("ELEMENT_NOT_FOUND", target.methods.map((method, index) => ({ index, method: method.kind, outcome: "notTried", matches: 0 })), [], 0, entered);
420
+ }
421
+ const attempts = [];
422
+ let found;
423
+ for (const [index, method] of target.methods.entries()) {
424
+ if (found || method.enabled === false) {
425
+ attempts.push({ index, method: method.kind, outcome: found ? "notTried" : "disabled", matches: 0 });
426
+ continue;
427
+ }
428
+ const keep = (element) => inState(element, state, entered, options);
429
+ const match = matchMethod2(method, { root: entered.root, probe: options, keep });
430
+ if (match.kind === "invalid" || match.kind === "unsupported") {
431
+ attempts.push({ index, method: method.kind, outcome: match.kind, matches: 0 });
432
+ continue;
433
+ }
434
+ const elements = match.kind === "list" ? match.elements.filter(keep) : [...match.scored].sort((a, b) => b.score - a.score).filter((item) => keep(item.element)).map((item) => item.element);
435
+ attempts.push({
436
+ index,
437
+ method: method.kind,
438
+ outcome: elements.length > 0 ? "found" : "none",
439
+ matches: elements.length
440
+ });
441
+ if (elements.length > 0)
442
+ found = { status: "found", elements, method: method.kind, methodIndex: index, attempts };
443
+ }
444
+ return found ?? failure("ELEMENT_NOT_FOUND", attempts);
445
+ }
446
+
447
+ // src/target/snapshot.ts
448
+ var UI_TREE_SNAPSHOT_VERSION2 = 1;
449
+ var DEFAULT_SNAPSHOT_MAX_NODES2 = 5000;
450
+ var MAX_SNAPSHOT_DEPTH2 = 64;
451
+ var MAX_SNAPSHOT_TEXT2 = 500;
452
+ var SKIPPED = new Set(["script", "style", "noscript", "template", "link", "meta", "base", "head", "object", "embed"]);
453
+ var VALUE_CONTENT = new Set(["textarea"]);
454
+ var KEPT_ATTRIBUTES = new Set([
455
+ "id",
456
+ "class",
457
+ "role",
458
+ "name",
459
+ "type",
460
+ "for",
461
+ "title",
462
+ "alt",
463
+ "placeholder",
464
+ "label",
465
+ "autocomplete",
466
+ "tabindex",
467
+ "disabled",
468
+ "readonly",
469
+ "hidden",
470
+ "inert",
471
+ "contenteditable",
472
+ "list",
473
+ "multiple",
474
+ "size",
475
+ "scope",
476
+ "slot",
477
+ "part",
478
+ "lang",
479
+ "dir",
480
+ "data-testid",
481
+ "data-test",
482
+ "data-test-id",
483
+ "data-qa",
484
+ "data-cy",
485
+ "data-automation-id",
486
+ ...SENSITIVE_MARKERS2
487
+ ]);
488
+ var DROPPED_ARIA = new Set(["aria-valuenow", "aria-valuetext", "aria-checked", "aria-selected", "aria-pressed"]);
489
+ var BUTTON_INPUTS = new Set(["button", "submit", "reset"]);
490
+ var RESET_SEGMENT = /reset|recover|verify|verification|magic|confirm|activate|invite|unsubscribe/i;
491
+ var TAG_NAME = /^[a-z][a-z0-9-]{0,63}$/;
492
+ var EMPTY_FRAME_DOCUMENT = "<!doctype html><html><head></head><body></body></html>";
493
+ var ATTRIBUTE_NAME2 = /^[a-z][a-z0-9_:.-]{0,127}$/;
494
+ function isEditingHost(element) {
495
+ const value = element.getAttribute("contenteditable");
496
+ return value !== null && value.trim().toLowerCase() !== "false";
497
+ }
498
+ function hasFieldValueAncestor(element) {
499
+ for (let current = containerOf(element);current; current = containerOf(current)) {
500
+ if (current.localName === "textarea" || isEditingHost(current))
501
+ return true;
502
+ }
503
+ return false;
504
+ }
505
+ function slottedTextHidden(text) {
506
+ if (!text.parentElement?.shadowRoot)
507
+ return false;
508
+ const slot = assignedSlotOf(text);
509
+ return slot !== null && (isSensitiveElement2(slot) || hasFieldValueAncestor(slot));
510
+ }
511
+ function cleanText(value) {
512
+ return clip2(redactCredentials2(value), MAX_SNAPSHOT_TEXT2);
513
+ }
514
+ function decodeSegment(segment) {
515
+ let current = segment;
516
+ for (let round = 0;round < 3; round += 1) {
517
+ const next = decodeUriText(current);
518
+ if (next === current)
519
+ break;
520
+ current = next;
521
+ }
522
+ return current;
523
+ }
524
+ function resetPrefix(decoded) {
525
+ const parts = decoded.split(/[/?#;]/);
526
+ const index = parts.findIndex((part) => RESET_SEGMENT.test(part));
527
+ const kept = parts.slice(0, index + 1).map((part) => containsCredential(part) ? REDACTED2 : part);
528
+ return kept.join("/");
529
+ }
530
+ function snapshotUrl2(raw) {
531
+ const value = raw.trim();
532
+ if (value === "")
533
+ return null;
534
+ const absolute = /^[a-z][a-z0-9+.-]*:/i.test(value) || value.startsWith("//");
535
+ let origin = "";
536
+ let path;
537
+ if (absolute) {
538
+ let url;
539
+ try {
540
+ url = new URL(value.startsWith("//") ? `https:${value}` : value);
541
+ } catch {
542
+ return null;
543
+ }
544
+ if (url.protocol !== "http:" && url.protocol !== "https:")
545
+ return null;
546
+ origin = value.startsWith("//") ? `//${url.host}` : url.origin;
547
+ path = url.pathname;
548
+ } else {
549
+ path = value.split(/[?#]/)[0] ?? "";
550
+ }
551
+ const segments = path.split("/");
552
+ const decoded = segments.map(decodeSegment);
553
+ const reset = decoded.findIndex((segment) => RESET_SEGMENT.test(segment));
554
+ const kept = reset >= 0 ? segments.slice(0, reset + 1) : segments;
555
+ const cleaned = kept.map((segment, index) => {
556
+ const plain = decoded[index] ?? segment;
557
+ if (index === reset)
558
+ return resetPrefix(plain);
559
+ return containsCredential(plain) || containsCredentialInUrl(segment) ? REDACTED2 : segment;
560
+ }).join("/");
561
+ return clip2(`${origin}${cleaned}`, MAX_SNAPSHOT_TEXT2);
562
+ }
563
+ var DESCRIPTIVE_ATTRIBUTES = new Set([
564
+ "alt",
565
+ "label",
566
+ "title",
567
+ "placeholder",
568
+ "value",
569
+ "aria-label",
570
+ "aria-description",
571
+ "aria-roledescription",
572
+ "aria-placeholder"
573
+ ]);
574
+ var STRUCTURAL_ARIA = new Set([
575
+ "aria-hidden",
576
+ "aria-disabled",
577
+ "aria-expanded",
578
+ "aria-haspopup",
579
+ "aria-controls",
580
+ "aria-owns",
581
+ "aria-labelledby",
582
+ "aria-describedby",
583
+ "aria-level",
584
+ "aria-current",
585
+ "aria-modal",
586
+ "aria-multiline",
587
+ "aria-multiselectable",
588
+ "aria-orientation",
589
+ "aria-readonly",
590
+ "aria-required",
591
+ "aria-invalid",
592
+ "aria-busy",
593
+ "aria-live",
594
+ "aria-atomic",
595
+ "aria-posinset",
596
+ "aria-setsize",
597
+ "aria-colcount",
598
+ "aria-colindex",
599
+ "aria-colspan",
600
+ "aria-rowcount",
601
+ "aria-rowindex",
602
+ "aria-rowspan",
603
+ "aria-sort"
604
+ ]);
605
+ function keptAttributes(element, marked) {
606
+ const kept = {};
607
+ const tag = element.localName;
608
+ for (const attribute of Array.from(element.attributes)) {
609
+ const name = attribute.name.toLowerCase();
610
+ if (name.startsWith("on") || !ATTRIBUTE_NAME2.test(name))
611
+ continue;
612
+ if (marked && (DESCRIPTIVE_ATTRIBUTES.has(name) || name === "href" || name.startsWith("aria-") && !STRUCTURAL_ARIA.has(name))) {
613
+ continue;
614
+ }
615
+ if (KEPT_ATTRIBUTES.has(name) || name.startsWith("aria-") && !DROPPED_ARIA.has(name)) {
616
+ kept[name] = cleanText(attribute.value);
617
+ } else if (name === "href" && (tag === "a" || tag === "area")) {
618
+ const url = snapshotUrl2(attribute.value);
619
+ if (url !== null)
620
+ kept[name] = url;
621
+ } else if (name === "value" && tag === "input" && BUTTON_INPUTS.has((element.getAttribute("type") ?? "").toLowerCase())) {
622
+ kept[name] = cleanText(attribute.value);
623
+ }
624
+ }
625
+ return Object.keys(kept).length > 0 ? kept : undefined;
626
+ }
627
+ function rectTuple(element, probe) {
628
+ const box = probe?.rectOf?.(element) ?? element.getBoundingClientRect();
629
+ const tuple = [
630
+ Math.round(box.x),
631
+ Math.round(box.y),
632
+ Math.round(box.width),
633
+ Math.round(box.height)
634
+ ];
635
+ return tuple[2] > 0 || tuple[3] > 0 ? tuple : undefined;
636
+ }
637
+ function walkChildren(parent, state, depth, textAllowed, marked) {
638
+ const children = [];
639
+ for (const child of Array.from(parent.childNodes)) {
640
+ if (state.count >= state.max) {
641
+ state.truncated = true;
642
+ break;
643
+ }
644
+ if (child.nodeType === 3) {
645
+ if (!textAllowed || slottedTextHidden(child))
646
+ continue;
647
+ const raw = child.textContent ?? "";
648
+ if (raw.length === 0)
649
+ continue;
650
+ const text = raw.trim() === "" ? " " : cleanText(raw);
651
+ state.count += 1;
652
+ children.push(text);
653
+ } else if (child.nodeType === 1) {
654
+ const node = walkElement(child, state, depth + 1, textAllowed, marked);
655
+ if (node)
656
+ children.push(node);
657
+ }
658
+ }
659
+ return children;
660
+ }
661
+ function walkElement(element, state, depth, textAllowed, parentMarked) {
662
+ const tag = element.localName.toLowerCase();
663
+ if (SKIPPED.has(tag) || !TAG_NAME.test(tag))
664
+ return null;
665
+ if (depth > MAX_SNAPSHOT_DEPTH2) {
666
+ state.truncated = true;
667
+ return null;
668
+ }
669
+ state.count += 1;
670
+ const node = { t: tag };
671
+ const inFieldValue = hasFieldValueAncestor(element);
672
+ const marked = parentMarked || inFieldValue || isMarkedSensitive(element);
673
+ const attributes = keptAttributes(element, marked);
674
+ if (attributes)
675
+ node.a = attributes;
676
+ if (isElementVisible2(element, state.probe)) {
677
+ const rect = rectTuple(element, state.probe);
678
+ if (rect)
679
+ node.r = rect;
680
+ } else {
681
+ node.h = 1;
682
+ }
683
+ const sensitive = isSensitiveElement2(element);
684
+ const childText = textAllowed && !inFieldValue && !sensitive && !VALUE_CONTENT.has(tag) && !isEditingHost(element);
685
+ const childMarked = marked || !childText;
686
+ if (tag === "svg") {
687
+ const title = Array.from(element.children).find((child) => child.localName === "title");
688
+ const titleNode = title ? walkElement(title, state, depth + 1, childText, childMarked) : null;
689
+ if (titleNode)
690
+ node.c = [titleNode];
691
+ return node;
692
+ }
693
+ if (tag === "iframe" || tag === "frame") {
694
+ let inner = null;
695
+ try {
696
+ inner = element.contentDocument;
697
+ } catch {
698
+ inner = null;
699
+ }
700
+ const innerRoot = inner?.documentElement;
701
+ if (innerRoot) {
702
+ const frame = walkElement(innerRoot, state, depth + 1, childText, childMarked);
703
+ if (frame)
704
+ node.f = frame;
705
+ }
706
+ return node;
707
+ }
708
+ const children = walkChildren(element, state, depth, childText, childMarked);
709
+ if (children.length > 0)
710
+ node.c = children;
711
+ const shadow = element.shadowRoot;
712
+ if (shadow) {
713
+ const shadowChildren = walkChildren(shadow, state, depth, childText, childMarked);
714
+ if (shadowChildren.length > 0)
715
+ node.s = shadowChildren;
716
+ }
717
+ return node;
718
+ }
719
+ function pageTitle(document) {
720
+ const element = document.querySelector("title");
721
+ if (element && isMarkedSensitive(element))
722
+ return "";
723
+ return cleanText(document.title ?? "");
724
+ }
725
+ function serializeTreeSnapshot2(document, options = {}) {
726
+ const state = {
727
+ count: 0,
728
+ max: Math.max(1, options.maxNodes ?? DEFAULT_SNAPSHOT_MAX_NODES2),
729
+ truncated: false,
730
+ probe: options
731
+ };
732
+ const root = walkElement(document.documentElement, state, 0, true, false) ?? { t: "html" };
733
+ const view = document.defaultView;
734
+ return {
735
+ snapshotVersion: UI_TREE_SNAPSHOT_VERSION2,
736
+ url: snapshotUrl2(document.URL) ?? "",
737
+ title: pageTitle(document),
738
+ viewport: { width: Math.round(view?.innerWidth ?? 0), height: Math.round(view?.innerHeight ?? 0) },
739
+ root,
740
+ nodeCount: state.count,
741
+ truncated: state.truncated
742
+ };
743
+ }
744
+ function appendChildren(document, parent, children, state) {
745
+ for (const child of children ?? []) {
746
+ if (typeof child === "string")
747
+ parent.appendChild(document.createTextNode(child));
748
+ else {
749
+ const element = buildElement(document, child, state);
750
+ if (element)
751
+ parent.appendChild(element);
752
+ }
753
+ }
754
+ }
755
+ function applyNode(document, element, node, state) {
756
+ for (const [name, value] of Object.entries(node.a ?? {})) {
757
+ if (!ATTRIBUTE_NAME2.test(name) || name.startsWith("on"))
758
+ continue;
759
+ if (name === "href" && snapshotUrl2(value) === null)
760
+ continue;
761
+ element.setAttribute(name, value);
762
+ }
763
+ if (node.r)
764
+ state.rects.set(element, { x: node.r[0], y: node.r[1], width: node.r[2], height: node.r[3] });
765
+ if (node.h)
766
+ state.hidden.add(element);
767
+ if (node.f && (node.t === "iframe" || node.t === "frame")) {
768
+ element.setAttribute("srcdoc", EMPTY_FRAME_DOCUMENT);
769
+ state.frames.push({ element, root: node.f });
770
+ }
771
+ appendChildren(document, element, node.c, state);
772
+ }
773
+ function buildElement(document, node, state) {
774
+ if (!TAG_NAME.test(node.t) || SKIPPED.has(node.t))
775
+ return null;
776
+ const element = document.createElement(node.t);
777
+ applyNode(document, element, node, state);
778
+ if (node.s && node.s.length > 0) {
779
+ let shadow = null;
780
+ try {
781
+ shadow = element.attachShadow({ mode: "open" });
782
+ } catch {
783
+ shadow = null;
784
+ }
785
+ appendChildren(document, shadow ?? element, node.s, state);
786
+ }
787
+ return element;
788
+ }
789
+ function hydrateInto(document, root, state) {
790
+ const html = document.documentElement;
791
+ while (html.firstChild)
792
+ html.removeChild(html.firstChild);
793
+ for (const attribute of Array.from(html.attributes))
794
+ html.removeAttribute(attribute.name);
795
+ applyNode(document, html, root, state);
796
+ const frames = state.frames.splice(0);
797
+ for (const frame of frames) {
798
+ const inner = frame.element.contentDocument;
799
+ if (inner?.documentElement)
800
+ hydrateInto(inner, frame.root, state);
801
+ }
802
+ }
803
+ function hydrateTreeSnapshot2(document, snapshot) {
804
+ const state = { rects: new WeakMap, hidden: new WeakSet, frames: [] };
805
+ hydrateInto(document, snapshot.root, state);
806
+ const zero = { x: 0, y: 0, width: 0, height: 0 };
807
+ return {
808
+ probe: {
809
+ rectOf: (element) => state.rects.get(element) ?? zero,
810
+ isVisible: (element) => !state.hidden.has(element) && element.isConnected
811
+ }
812
+ };
813
+ }
814
+
815
+ export { WEB_TARGET_ERROR_CODES2, MAX_CANDIDATES2, MAX_CANDIDATE_NAME2, DEFAULT_FUZZY_THRESHOLD2, FUZZY_MARGIN2, ANCHOR_MARGIN_PX2, MAX_XPATH_RESULTS2, INTERACTIVE_ROLES2, relationDistance2, matchMethod2, enterFrames2, describeCandidate2, resolveWebTarget2, resolveAllWebTargets2, UI_TREE_SNAPSHOT_VERSION2, DEFAULT_SNAPSHOT_MAX_NODES2, MAX_SNAPSHOT_DEPTH2, MAX_SNAPSHOT_TEXT2, snapshotUrl2, serializeTreeSnapshot2, hydrateTreeSnapshot2 };