@nexum-hub/browser 0.0.3-next.16 → 0.0.3-next.18

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.
@@ -150,6 +150,14 @@ export async function framePage(ctx, payload, selectors, execute) {
150
150
  ctx.router.noteFrameParent(ctx, node.node.frameId, frameId);
151
151
  frameId = node.node.frameId;
152
152
  }
153
+ // A frame may be replaced between hit-testing and arming the input
154
+ // receipt. Never transfer the receipt to a different frame by selector.
155
+ if (payload.expectedFrameId)
156
+ requireValue(
157
+ payload.expectedFrameId === frameId,
158
+ "STALE_REF",
159
+ "The input frame changed after its native target was observed.",
160
+ );
153
161
  const input = {
154
162
  ...payload,
155
163
  framePath: [],
@@ -179,6 +187,7 @@ export async function framePage(ctx, payload, selectors, execute) {
179
187
  if (!result.ok)
180
188
  throw new BrowserActionError(result.error.code, result.error.message);
181
189
  if (input.command === "resolve" && result.value?.rect) {
190
+ const framePoint = { ...result.value.point };
182
191
  // scrollIntoView in the resolved descendant can scroll every native ancestor.
183
192
  // Coordinates read during traversal are no longer an observation of this input target.
184
193
  for (const owner of owners) {
@@ -243,6 +252,8 @@ export async function framePage(ctx, payload, selectors, execute) {
243
252
  }
244
253
  result.value.point = point;
245
254
  result.value.rect = mappedRect(result.value.rect, owners);
255
+ result.value.framePoint = framePoint;
256
+ result.value.frameId = frameId;
246
257
  }
247
258
  if (input.command === "elementInfo" && Array.isArray(result.value))
248
259
  for (const entry of result.value)
@@ -18,6 +18,10 @@ export async function pageCommand(input) {
18
18
  state.document !== document ||
19
19
  state.version !== input.pageVersion
20
20
  ) {
21
+ for (const receipt of state?.clickReceipts?.values?.() ?? []) {
22
+ document.removeEventListener(receipt.type, receipt.listener, true);
23
+ clearTimeout(receipt.timer);
24
+ }
21
25
  state = {
22
26
  document,
23
27
  version: input.pageVersion,
@@ -27,6 +31,7 @@ export async function pageCommand(input) {
27
31
  next: 0,
28
32
  refs: new Map(),
29
33
  reverse: new WeakMap(),
34
+ clickReceipts: new Map(),
30
35
  };
31
36
  Object.defineProperty(globalThis, slot, {
32
37
  value: state,
@@ -415,6 +420,71 @@ export async function pageCommand(input) {
415
420
  };
416
421
  let value;
417
422
  switch (input.command) {
423
+ case "inputReceipt": {
424
+ check(
425
+ typeof input.token === "string" &&
426
+ input.token.length > 0 &&
427
+ input.token.length <= 128,
428
+ );
429
+ const receipts = (state.clickReceipts ??= new Map());
430
+ const release = (receipt) => {
431
+ if (!receipt) return;
432
+ document.removeEventListener(receipt.type, receipt.listener, true);
433
+ clearTimeout(receipt.timer);
434
+ receipts.delete(input.token);
435
+ };
436
+ if (input.action === "arm") {
437
+ check(!receipts.has(input.token) && receipts.size < 32, "QUEUE_FULL");
438
+ check(
439
+ Number.isFinite(input.x) &&
440
+ Number.isFinite(input.y) &&
441
+ [0, 1, 2].includes(input.button) &&
442
+ Number.isFinite(input.expiresAt) &&
443
+ input.expiresAt > Date.now(),
444
+ );
445
+ const type =
446
+ input.button === 2
447
+ ? "contextmenu"
448
+ : input.button === 1
449
+ ? "auxclick"
450
+ : "click";
451
+ const receipt = {
452
+ type,
453
+ count: 0,
454
+ expiresAt: input.expiresAt,
455
+ x: input.x,
456
+ y: input.y,
457
+ button: input.button,
458
+ };
459
+ receipt.listener = (event) => {
460
+ if (
461
+ event.isTrusted &&
462
+ event.button === input.button &&
463
+ Math.abs(event.clientX - input.x) <= 6 &&
464
+ Math.abs(event.clientY - input.y) <= 6
465
+ )
466
+ receipt.count++;
467
+ };
468
+ receipt.timer = setTimeout(
469
+ () => release(receipt),
470
+ Math.min(30000, input.expiresAt - Date.now()),
471
+ );
472
+ document.addEventListener(type, receipt.listener, true);
473
+ receipts.set(input.token, receipt);
474
+ value = { armed: true, documentToken: state.token };
475
+ } else if (input.action === "peek" || input.action === "drop") {
476
+ check(
477
+ !input.expectedDocumentToken ||
478
+ input.expectedDocumentToken === state.token,
479
+ "STALE_REF",
480
+ "The receipt belongs to a replaced document.",
481
+ );
482
+ const receipt = receipts.get(input.token);
483
+ value = { observed: receipt?.count ?? 0 };
484
+ if (input.action === "drop") release(receipt);
485
+ } else check(false);
486
+ break;
487
+ }
418
488
  // Core-only native bridge: the caller retains the actual remote Node(s), never serializes them.
419
489
  case "nativeNodes":
420
490
  return input.multiple ? all() : unique();
@@ -610,14 +680,27 @@ export async function pageCommand(input) {
610
680
  const node = unique(false, true),
611
681
  geometry = node && actionGeometry(node, true);
612
682
  value = geometry
613
- ? { ...describe(node), ...geometry, tagName: node.tagName }
683
+ ? {
684
+ ...describe(node),
685
+ ...geometry,
686
+ tagName: node.tagName,
687
+ documentToken: state.token,
688
+ }
614
689
  : null;
615
690
  break;
616
691
  }
617
- case "fill":
618
- setValue(unique(true), input.value ?? input.text);
692
+ case "fill": {
693
+ const node = unique(true),
694
+ expected = input.value ?? input.text;
695
+ setValue(node, expected);
696
+ check(
697
+ (node.isContentEditable ? node.textContent : node.value) === expected,
698
+ "INVALID_TARGET",
699
+ "The input did not retain the requested value.",
700
+ );
619
701
  value = null;
620
702
  break;
703
+ }
621
704
  case "focus":
622
705
  unique(true).focus();
623
706
  value = null;
@@ -652,6 +735,13 @@ export async function pageCommand(input) {
652
735
  node.dispatchEvent(new Event("input", { bubbles: true }));
653
736
  node.dispatchEvent(new Event("change", { bubbles: true }));
654
737
  value = matches.map((o) => o.value);
738
+ const actual = [...node.selectedOptions].map((o) => o.value);
739
+ check(
740
+ actual.length === value.length &&
741
+ actual.every((item) => value.includes(item)),
742
+ "INVALID_TARGET",
743
+ "The select did not retain the requested option values.",
744
+ );
655
745
  break;
656
746
  }
657
747
  case "checked": {