mirror of
https://github.com/Tencent/BrowserSkill.git
synced 2026-09-10 13:01:23 +08:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7b2c6cccd | ||
|
|
1f6faa7a0a | ||
|
|
27dba3c57f | ||
|
|
e8137b42cc |
No files matched your search
@@ -29,6 +29,24 @@ function fakeApi() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("ChromiumCdp", () => {
|
describe("ChromiumCdp", () => {
|
||||||
|
it("changes attachment identity only across real debugger attachments", async () => {
|
||||||
|
const { api, onEvent, onDetach } = fakeApi();
|
||||||
|
const cdp = new ChromiumCdp(api);
|
||||||
|
expect(cdp.getAttachmentId(4)).toBeUndefined();
|
||||||
|
await cdp.ensureAttached(4);
|
||||||
|
const first = cdp.getAttachmentId(4);
|
||||||
|
expect(first).toBeTruthy();
|
||||||
|
await cdp.ensureAttached(4);
|
||||||
|
onEvent.fire({ tabId: 4 }, "Target.attachedToTarget", { sessionId: "child" });
|
||||||
|
expect(cdp.getAttachmentId(4)).toBe(first);
|
||||||
|
onDetach.fire({ tabId: 4 }, "target_closed");
|
||||||
|
expect(cdp.getAttachmentId(4)).toBeUndefined();
|
||||||
|
await cdp.ensureAttached(4);
|
||||||
|
expect(cdp.getAttachmentId(4)).not.toBe(first);
|
||||||
|
await cdp.detach(4);
|
||||||
|
expect(cdp.getAttachmentId(4)).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
it("discovers multiple iframe targets and recursively routes nested OOPIF commands", async () => {
|
it("discovers multiple iframe targets and recursively routes nested OOPIF commands", async () => {
|
||||||
const { api, onEvent } = fakeApi();
|
const { api, onEvent } = fakeApi();
|
||||||
(api.sendCommand as ReturnType<typeof vi.fn>).mockImplementation(
|
(api.sendCommand as ReturnType<typeof vi.fn>).mockImplementation(
|
||||||
|
|||||||
@@ -2,6 +2,24 @@ import { describe, expect, it } from "vitest";
|
|||||||
import { buildFrameGraph, type CdpFrameTreeNode, type CdpFrameTreeSource } from "../frame-graph";
|
import { buildFrameGraph, type CdpFrameTreeNode, type CdpFrameTreeSource } from "../frame-graph";
|
||||||
|
|
||||||
describe("buildFrameGraph", () => {
|
describe("buildFrameGraph", () => {
|
||||||
|
it("preserves the owning target's loader identity", () => {
|
||||||
|
const graph = buildFrameGraph([
|
||||||
|
{
|
||||||
|
target: { tabId: 4 },
|
||||||
|
tree: {
|
||||||
|
frame: { id: "root", loaderId: "root-loader" },
|
||||||
|
childFrames: [{ frame: { id: "child", loaderId: "stale-placeholder" } }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
target: { tabId: 4, sessionId: "child-session" },
|
||||||
|
tree: { frame: { id: "child", loaderId: "child-loader" } },
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expect(graph?.frames.find((frame) => frame.frameId === "root")?.loaderId).toBe("root-loader");
|
||||||
|
expect(graph?.frames.find((frame) => frame.frameId === "child")?.loaderId).toBe("child-loader");
|
||||||
|
});
|
||||||
|
|
||||||
it("keeps sibling frames distinct and routes nested OOPIFs to their child sessions", () => {
|
it("keeps sibling frames distinct and routes nested OOPIFs to their child sessions", () => {
|
||||||
const graph = buildFrameGraph([
|
const graph = buildFrameGraph([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -164,6 +164,7 @@ async function settleBeforeDeadline(promises: Promise<void>[], deadline: number)
|
|||||||
export class ChromiumCdp {
|
export class ChromiumCdp {
|
||||||
private readonly api: CdpDebuggerApi;
|
private readonly api: CdpDebuggerApi;
|
||||||
private readonly attachedTabs = new Set<number>();
|
private readonly attachedTabs = new Set<number>();
|
||||||
|
private readonly attachmentIds = new Map<number, string>();
|
||||||
private readonly attachInFlight = new Map<number, Promise<void>>();
|
private readonly attachInFlight = new Map<number, Promise<void>>();
|
||||||
private readonly detachInFlight = new Map<number, Promise<void>>();
|
private readonly detachInFlight = new Map<number, Promise<void>>();
|
||||||
private readonly tabOwners = new Map<number, Set<string>>();
|
private readonly tabOwners = new Map<number, Set<string>>();
|
||||||
@@ -198,6 +199,11 @@ export class ChromiumCdp {
|
|||||||
this.bindFrameTargetHandler();
|
this.bindFrameTargetHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Identity of this actual debugger attachment, independent of frame topology. */
|
||||||
|
getAttachmentId(tabId: number): string | undefined {
|
||||||
|
return this.attachmentIds.get(tabId);
|
||||||
|
}
|
||||||
|
|
||||||
/** Attach to `tabId` if we haven't already in this driver. */
|
/** Attach to `tabId` if we haven't already in this driver. */
|
||||||
async ensureAttached(tabId: number): Promise<void> {
|
async ensureAttached(tabId: number): Promise<void> {
|
||||||
// Returning a tab clears the cache before Chrome finishes detaching.
|
// Returning a tab clears the cache before Chrome finishes detaching.
|
||||||
@@ -217,6 +223,7 @@ export class ChromiumCdp {
|
|||||||
await this.enableConsoleDomains(tabId);
|
await this.enableConsoleDomains(tabId);
|
||||||
await this.enableNetworkDomainBestEffort(tabId);
|
await this.enableNetworkDomainBestEffort(tabId);
|
||||||
this.attachedTabs.add(tabId);
|
this.attachedTabs.add(tabId);
|
||||||
|
this.attachmentIds.set(tabId, crypto.randomUUID());
|
||||||
await this.enableFrameDiscovery({ tabId }).catch((err) => {
|
await this.enableFrameDiscovery({ tabId }).catch((err) => {
|
||||||
console.debug("[bsk cdp] frame discovery unavailable", { tabId, err });
|
console.debug("[bsk cdp] frame discovery unavailable", { tabId, err });
|
||||||
});
|
});
|
||||||
@@ -444,6 +451,7 @@ export class ChromiumCdp {
|
|||||||
this.attachInFlight.delete(tabId);
|
this.attachInFlight.delete(tabId);
|
||||||
if (!this.attachedTabs.has(tabId)) return;
|
if (!this.attachedTabs.has(tabId)) return;
|
||||||
this.attachedTabs.delete(tabId);
|
this.attachedTabs.delete(tabId);
|
||||||
|
this.attachmentIds.delete(tabId);
|
||||||
this.clearDialogState(tabId);
|
this.clearDialogState(tabId);
|
||||||
this.clearConsoleState(tabId);
|
this.clearConsoleState(tabId);
|
||||||
this.clearNetworkState(tabId);
|
this.clearNetworkState(tabId);
|
||||||
@@ -502,6 +510,7 @@ export class ChromiumCdp {
|
|||||||
this.attachInFlight.clear();
|
this.attachInFlight.clear();
|
||||||
this.tabOwners.clear();
|
this.tabOwners.clear();
|
||||||
this.attachedTabs.clear();
|
this.attachedTabs.clear();
|
||||||
|
this.attachmentIds.clear();
|
||||||
this.dialogBuffers.clear();
|
this.dialogBuffers.clear();
|
||||||
this.dialogSequences.clear();
|
this.dialogSequences.clear();
|
||||||
this.consoleBuffers.clear();
|
this.consoleBuffers.clear();
|
||||||
@@ -848,6 +857,7 @@ export class ChromiumCdp {
|
|||||||
const listener = (source: chrome.debugger.Debuggee, _reason: string) => {
|
const listener = (source: chrome.debugger.Debuggee, _reason: string) => {
|
||||||
if (typeof source.tabId === "number") {
|
if (typeof source.tabId === "number") {
|
||||||
this.attachedTabs.delete(source.tabId);
|
this.attachedTabs.delete(source.tabId);
|
||||||
|
this.attachmentIds.delete(source.tabId);
|
||||||
this.attachInFlight.delete(source.tabId);
|
this.attachInFlight.delete(source.tabId);
|
||||||
this.tabOwners.delete(source.tabId);
|
this.tabOwners.delete(source.tabId);
|
||||||
this.clearDialogState(source.tabId);
|
this.clearDialogState(source.tabId);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ export interface CdpFrame {
|
|||||||
parentFrameId?: string;
|
parentFrameId?: string;
|
||||||
ownerBackendNodeId?: number;
|
ownerBackendNodeId?: number;
|
||||||
url?: string;
|
url?: string;
|
||||||
|
loaderId?: string;
|
||||||
target: CdpTarget;
|
target: CdpTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,6 +22,7 @@ export interface CdpFrameTreeNode {
|
|||||||
id: string;
|
id: string;
|
||||||
parentId?: string;
|
parentId?: string;
|
||||||
url?: string;
|
url?: string;
|
||||||
|
loaderId?: string;
|
||||||
};
|
};
|
||||||
childFrames?: CdpFrameTreeNode[];
|
childFrames?: CdpFrameTreeNode[];
|
||||||
}
|
}
|
||||||
@@ -48,6 +50,7 @@ function mergeFrame(
|
|||||||
frameId: node.frame.id,
|
frameId: node.frame.id,
|
||||||
...(parentFrameId ? { parentFrameId } : {}),
|
...(parentFrameId ? { parentFrameId } : {}),
|
||||||
...(url ? { url } : {}),
|
...(url ? { url } : {}),
|
||||||
|
...(node.frame.loaderId ? { loaderId: node.frame.loaderId } : {}),
|
||||||
target,
|
target,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { resolveFrameProjection, resolveNodeGeometry } from "../frame-geometry";
|
import { resolveNodeGeometry } from "../frame-geometry";
|
||||||
import {
|
import {
|
||||||
clipPolygon,
|
clipPolygon,
|
||||||
polygonArea,
|
polygonArea,
|
||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
rectPolygon,
|
rectPolygon,
|
||||||
regionBounds,
|
regionBounds,
|
||||||
} from "../geometry";
|
} from "../geometry";
|
||||||
|
import { GeometryContext } from "../geometry/frame-context";
|
||||||
import type { CdpRunner } from "../shared";
|
import type { CdpRunner } from "../shared";
|
||||||
|
|
||||||
describe("frame geometry projection", () => {
|
describe("frame geometry projection", () => {
|
||||||
@@ -82,28 +83,24 @@ describe("frame geometry projection", () => {
|
|||||||
throw new Error(`unexpected child command ${method}`);
|
throw new Error(`unexpected child command ${method}`);
|
||||||
}) as CdpRunner["sendToTarget"],
|
}) as CdpRunner["sendToTarget"],
|
||||||
};
|
};
|
||||||
const projection = await resolveFrameProjection(
|
const projection = await new GeometryContext(cdp, 4, {
|
||||||
cdp,
|
rootFrameId: "main",
|
||||||
{
|
frames: [
|
||||||
rootFrameId: "main",
|
{ frameId: "main", target: { tabId: 4 } },
|
||||||
frames: [
|
{
|
||||||
{ frameId: "main", target: { tabId: 4 } },
|
frameId: "same-process-parent",
|
||||||
{
|
parentFrameId: "main",
|
||||||
frameId: "same-process-parent",
|
ownerBackendNodeId: 10,
|
||||||
parentFrameId: "main",
|
target: { tabId: 4 },
|
||||||
ownerBackendNodeId: 10,
|
},
|
||||||
target: { tabId: 4 },
|
{
|
||||||
},
|
frameId: "oopif",
|
||||||
{
|
parentFrameId: "same-process-parent",
|
||||||
frameId: "oopif",
|
ownerBackendNodeId: 20,
|
||||||
parentFrameId: "same-process-parent",
|
target: { tabId: 4, sessionId: "oopif-session" },
|
||||||
ownerBackendNodeId: 20,
|
},
|
||||||
target: { tabId: 4, sessionId: "oopif-session" },
|
],
|
||||||
},
|
}).targetProjection("oopif");
|
||||||
],
|
|
||||||
},
|
|
||||||
"oopif",
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(projection?.edges).toEqual([
|
expect(projection?.edges).toEqual([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,284 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import type { CdpFrameGraph } from "@/browser-driver/frame-graph";
|
||||||
|
import { resolveNodeGeometry } from "../frame-geometry";
|
||||||
|
import { rectPolygon } from "../geometry";
|
||||||
|
import {
|
||||||
|
projectSnapshotRect,
|
||||||
|
screenshotPageRect,
|
||||||
|
snapshotViewportRect,
|
||||||
|
} from "../geometry/coordinate-types";
|
||||||
|
import { GeometryContext } from "../geometry/frame-context";
|
||||||
|
import type { CdpRunner } from "../shared";
|
||||||
|
|
||||||
|
const target = { tabId: 4 };
|
||||||
|
const graph: CdpFrameGraph = {
|
||||||
|
rootFrameId: "main",
|
||||||
|
frames: [
|
||||||
|
{ frameId: "main", target },
|
||||||
|
{ frameId: "child", parentFrameId: "main", ownerBackendNodeId: 10, target },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
function driver() {
|
||||||
|
const send = vi.fn(async (_tab: number, method: string): Promise<object> => {
|
||||||
|
if (method === "Page.getLayoutMetrics")
|
||||||
|
return { cssLayoutViewport: { clientWidth: 1000, clientHeight: 1000 } };
|
||||||
|
if (method === "DOM.getBoxModel")
|
||||||
|
return { model: { content: [52.5, 612.5, 427.5, 612.5, 427.5, 862.5, 52.5, 862.5] } };
|
||||||
|
if (method === "DOM.resolveNode") return { object: { objectId: "owner" } };
|
||||||
|
if (method === "Runtime.callFunctionOn")
|
||||||
|
return { result: { value: { width: 300, height: 200 } } };
|
||||||
|
if (method === "Runtime.releaseObject") return {};
|
||||||
|
throw new Error(method);
|
||||||
|
});
|
||||||
|
return { send: send as CdpRunner["send"], getFrameGraph: vi.fn(async () => graph), calls: send };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("measurement geometry context", () => {
|
||||||
|
it("shares in-flight frame measurements, but never shares them with the next context", async () => {
|
||||||
|
const cdp = driver();
|
||||||
|
const context = new GeometryContext(cdp, 4);
|
||||||
|
const results = await Promise.all(
|
||||||
|
Array.from({ length: 20 }, () => context.targetProjection("child")),
|
||||||
|
);
|
||||||
|
expect(results.every((value) => value === results[0])).toBe(true);
|
||||||
|
expect(cdp.getFrameGraph).toHaveBeenCalledTimes(1);
|
||||||
|
expect(cdp.calls.mock.calls.map(([, method]) => method)).toEqual([
|
||||||
|
"Page.getLayoutMetrics",
|
||||||
|
"DOM.getBoxModel",
|
||||||
|
]);
|
||||||
|
await new GeometryContext(cdp, 4).targetProjection("child");
|
||||||
|
expect(cdp.getFrameGraph).toHaveBeenCalledTimes(2);
|
||||||
|
expect(cdp.calls.mock.calls).toHaveLength(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("projects frame-local snapshot CSS bounds through the scaled owner content quad", async () => {
|
||||||
|
const cdp = driver();
|
||||||
|
const context = new GeometryContext(cdp, 4);
|
||||||
|
const source = { target, frameId: "child" };
|
||||||
|
const projection = await context.snapshotProjection(source, 10, [], {
|
||||||
|
width: 1000,
|
||||||
|
height: 1000,
|
||||||
|
});
|
||||||
|
const input = snapshotViewportRect([17, 23, 120, 40], source, { x: 0, y: 0 });
|
||||||
|
expect(projectSnapshotRect(input!, projection!)).toEqual({
|
||||||
|
x: 73.75,
|
||||||
|
y: 641.25,
|
||||||
|
width: 150,
|
||||||
|
height: 50,
|
||||||
|
});
|
||||||
|
const scrolled = snapshotViewportRect([17, 23, 120, 40], source, { x: 0, y: 10 });
|
||||||
|
expect(projectSnapshotRect(scrolled!, projection!)?.y).toBe(628.75);
|
||||||
|
await context.snapshotProjection(source, 10, [], { width: 1000, height: 1000 });
|
||||||
|
expect(cdp.calls.mock.calls.filter(([, method]) => method === "DOM.resolveNode")).toHaveLength(
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
cdp.calls.mock.calls.filter(([, method]) => method === "Runtime.releaseObject"),
|
||||||
|
).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects owner mismatches and invalid numeric bounds", () => {
|
||||||
|
const input = snapshotViewportRect(
|
||||||
|
[30, 50, 120, 40],
|
||||||
|
{ target, frameId: "main" },
|
||||||
|
{ x: 10, y: 20 },
|
||||||
|
)!;
|
||||||
|
const projection = {
|
||||||
|
source: { target, frameId: "main" },
|
||||||
|
geometry: { sourceClips: [], edges: [], topViewport: { width: 800, height: 600 } },
|
||||||
|
};
|
||||||
|
expect(projectSnapshotRect(input, projection)).toEqual({
|
||||||
|
x: 20,
|
||||||
|
y: 30,
|
||||||
|
width: 120,
|
||||||
|
height: 40,
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
projectSnapshotRect(input, { ...projection, source: { target, frameId: "other" } }),
|
||||||
|
).toBeNull();
|
||||||
|
expect(
|
||||||
|
projectSnapshotRect(input, {
|
||||||
|
...projection,
|
||||||
|
source: { target: { tabId: 5 }, frameId: "main" },
|
||||||
|
}),
|
||||||
|
).toBeNull();
|
||||||
|
expect(snapshotViewportRect([0, 0, NaN, 40], { target }, { x: 0, y: 0 })).toBeNull();
|
||||||
|
expect(snapshotViewportRect([0, 0, 10, 40], { target }, { x: Infinity, y: 0 })).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not turn a border quad or a failed owner read into a content projection", async () => {
|
||||||
|
const cdp: CdpRunner = {
|
||||||
|
send: vi.fn(async () => ({
|
||||||
|
model: { border: [0, 0, 100, 0, 100, 100, 0, 100] },
|
||||||
|
})) as CdpRunner["send"],
|
||||||
|
};
|
||||||
|
expect(
|
||||||
|
await new GeometryContext(cdp, 4).snapshotProjection({ target, frameId: "child" }, 10, [], {
|
||||||
|
width: 100,
|
||||||
|
height: 100,
|
||||||
|
}),
|
||||||
|
).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps ancestor clips in target coordinates instead of applying nested offsets twice", async () => {
|
||||||
|
const cdp = driver();
|
||||||
|
const projection = await new GeometryContext(cdp, 4).snapshotProjection(
|
||||||
|
{ target, frameId: "nested" },
|
||||||
|
10,
|
||||||
|
[rectPolygon({ x: 60, y: 620, w: 100, h: 100 })],
|
||||||
|
{ width: 1000, height: 1000 },
|
||||||
|
);
|
||||||
|
const input = snapshotViewportRect(
|
||||||
|
[0, 0, 300, 200],
|
||||||
|
{ target, frameId: "nested" },
|
||||||
|
{ x: 0, y: 0 },
|
||||||
|
);
|
||||||
|
expect(projectSnapshotRect(input!, projection!)).toEqual({
|
||||||
|
x: 60,
|
||||||
|
y: 620,
|
||||||
|
width: 100,
|
||||||
|
height: 100,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects malformed frame ancestry without looping", async () => {
|
||||||
|
const cdp = driver();
|
||||||
|
const cycle: CdpFrameGraph = {
|
||||||
|
rootFrameId: "a",
|
||||||
|
frames: [
|
||||||
|
{ frameId: "a", parentFrameId: "b", target },
|
||||||
|
{ frameId: "b", parentFrameId: "a", target },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
expect(await new GeometryContext(cdp, 4, cycle).targetProjection("a")).toBeNull();
|
||||||
|
expect(cdp.calls).not.toHaveBeenCalled();
|
||||||
|
const orphan: CdpFrameGraph = {
|
||||||
|
rootFrameId: "a",
|
||||||
|
frames: [{ frameId: "a", parentFrameId: "missing", target }],
|
||||||
|
};
|
||||||
|
expect(await new GeometryContext(cdp, 4, orphan).targetProjection("a")).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects a live node whose frame belongs to another target before any DOM input", async () => {
|
||||||
|
const cdp = driver();
|
||||||
|
expect(
|
||||||
|
await resolveNodeGeometry(
|
||||||
|
cdp,
|
||||||
|
4,
|
||||||
|
{ target: { tabId: 4, sessionId: "wrong" }, frameId: "child", backendNodeId: 20 },
|
||||||
|
{ scrollIntoView: true },
|
||||||
|
),
|
||||||
|
).toMatchObject({ code: "cdp_failed" });
|
||||||
|
expect(cdp.calls).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("bounds concurrent measurements and does not dispatch queued reads after cancellation", async () => {
|
||||||
|
let active = 0;
|
||||||
|
let peak = 0;
|
||||||
|
const release: Array<() => void> = [];
|
||||||
|
const send = vi.fn(async () => {
|
||||||
|
active++;
|
||||||
|
peak = Math.max(peak, active);
|
||||||
|
await new Promise<void>((resolve) => release.push(resolve));
|
||||||
|
active--;
|
||||||
|
return { cssLayoutViewport: { clientWidth: 100, clientHeight: 100 } };
|
||||||
|
});
|
||||||
|
const controller = new AbortController();
|
||||||
|
const context = new GeometryContext(
|
||||||
|
{ send: send as CdpRunner["send"] },
|
||||||
|
4,
|
||||||
|
undefined,
|
||||||
|
controller.signal,
|
||||||
|
);
|
||||||
|
const tasks = Array.from({ length: 8 }, (_, index) =>
|
||||||
|
context.viewport({ tabId: 4, sessionId: String(index) }),
|
||||||
|
);
|
||||||
|
const settled = Promise.allSettled(tasks);
|
||||||
|
expect(send).toHaveBeenCalledTimes(4);
|
||||||
|
controller.abort();
|
||||||
|
for (const done of release) done();
|
||||||
|
const results = await settled;
|
||||||
|
expect(peak).toBe(4);
|
||||||
|
expect(send).toHaveBeenCalledTimes(4);
|
||||||
|
expect(results.filter((item) => item.status === "rejected")).toHaveLength(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("releases an owner object even when cancellation arrives before its viewport read", async () => {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const cdp = driver();
|
||||||
|
const original = cdp.calls.getMockImplementation()!;
|
||||||
|
cdp.calls.mockImplementation(async (tab, method) => {
|
||||||
|
const result = await original(tab, method);
|
||||||
|
if (method === "DOM.resolveNode") controller.abort();
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
const context = new GeometryContext(cdp, 4, undefined, controller.signal);
|
||||||
|
await expect(
|
||||||
|
context.snapshotProjection({ target, frameId: "child" }, 10, [], {
|
||||||
|
width: 1000,
|
||||||
|
height: 1000,
|
||||||
|
}),
|
||||||
|
).rejects.toMatchObject({ name: "AbortError" });
|
||||||
|
expect(cdp.calls.mock.calls.map(([, method]) => method)).toEqual([
|
||||||
|
"DOM.getBoxModel",
|
||||||
|
"DOM.resolveNode",
|
||||||
|
"Runtime.releaseObject",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reads live owner geometry only after scrolling", async () => {
|
||||||
|
const cdp = driver();
|
||||||
|
const original = cdp.calls.getMockImplementation()!;
|
||||||
|
let scrolled = false;
|
||||||
|
cdp.calls.mockImplementation(async (tab, method) => {
|
||||||
|
if (method === "DOM.scrollIntoViewIfNeeded") {
|
||||||
|
scrolled = true;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
if (method === "DOM.getContentQuads") {
|
||||||
|
expect(scrolled).toBe(true);
|
||||||
|
return { quads: [[60, 620, 100, 620, 100, 640, 60, 640]] };
|
||||||
|
}
|
||||||
|
if (method === "DOM.getBoxModel") expect(scrolled).toBe(true);
|
||||||
|
return original(tab, method);
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
await resolveNodeGeometry(
|
||||||
|
cdp,
|
||||||
|
4,
|
||||||
|
{ target, frameId: "child", backendNodeId: 20 },
|
||||||
|
{ scrollIntoView: true },
|
||||||
|
),
|
||||||
|
).toMatchObject({ topBounds: { x: 60, y: 620, width: 40, height: 20 } });
|
||||||
|
expect(cdp.getFrameGraph).toHaveBeenCalledTimes(1);
|
||||||
|
expect(cdp.calls.mock.calls.filter(([, method]) => method === "DOM.getBoxModel")).toHaveLength(
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("adapts viewport CSS bounds to page DIP with scroll and browser zoom, never raster DPR", () => {
|
||||||
|
const viewport = { width: 1600, height: 1000, scrollX: 10, scrollY: 100, cssToDip: 0.9 };
|
||||||
|
const clip = screenshotPageRect({ x: 20, y: 30, width: 120, height: 40 }, viewport);
|
||||||
|
expect(clip).toEqual({ space: "page-dip", rect: { x: 27, y: 117, width: 108, height: 36 } });
|
||||||
|
expect(
|
||||||
|
screenshotPageRect({ x: 20, y: 30, width: 120, height: 40 }, { ...viewport, cssToDip: NaN }),
|
||||||
|
).toBeNull();
|
||||||
|
expect(
|
||||||
|
screenshotPageRect({ x: 20, y: 30, width: 120, height: 40 }, { ...viewport, cssToDip: 0 }),
|
||||||
|
).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("retries a rejected measurement only in a new operation", async () => {
|
||||||
|
const cdp = driver();
|
||||||
|
cdp.calls.mockRejectedValueOnce(new Error("unavailable"));
|
||||||
|
const context = new GeometryContext(cdp, 4);
|
||||||
|
await expect(context.viewport(target)).rejects.toThrow("unavailable");
|
||||||
|
await expect(context.viewport(target)).rejects.toThrow("unavailable");
|
||||||
|
expect(cdp.calls).toHaveBeenCalledTimes(1);
|
||||||
|
expect(await new GeometryContext(cdp, 4).viewport(target)).toEqual({
|
||||||
|
width: 1000,
|
||||||
|
height: 1000,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -4,9 +4,9 @@ import { CAPTURE_SUPPRESS, type CaptureSuppressMessage } from "@/lib/capture-sup
|
|||||||
import { OVERLAY_HOST_MARKER_ATTR, OVERLAY_HOST_NAME } from "@/lib/overlay-bridge";
|
import { OVERLAY_HOST_MARKER_ATTR, OVERLAY_HOST_NAME } from "@/lib/overlay-bridge";
|
||||||
import { SessionManager } from "@/session-manager/manager";
|
import { SessionManager } from "@/session-manager/manager";
|
||||||
import type { CdpRunner } from "@/tools/shared";
|
import type { CdpRunner } from "@/tools/shared";
|
||||||
|
import type { BuildVomSceneOptions, VomFrameDocument } from "../observation";
|
||||||
import {
|
import {
|
||||||
buildFrameVomScene,
|
buildFrameVomScene,
|
||||||
buildVomScene,
|
|
||||||
type CdpAxNode,
|
type CdpAxNode,
|
||||||
captureVomObservation,
|
captureVomObservation,
|
||||||
handleGetHtml,
|
handleGetHtml,
|
||||||
@@ -17,7 +17,35 @@ import {
|
|||||||
type ScreenshotDeps,
|
type ScreenshotDeps,
|
||||||
stripDataUrlPrefix,
|
stripDataUrlPrefix,
|
||||||
} from "../observation";
|
} from "../observation";
|
||||||
import type { CapturedNode, CapturedViewModel } from "../vom/capture";
|
import type { CapturedNode, CapturedSceneInput } from "../vom/facts";
|
||||||
|
|
||||||
|
interface SceneFixture extends CapturedSceneInput {
|
||||||
|
documents?: VomFrameDocument[];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only supplies the common root fixture; child documents declare their own ownership.
|
||||||
|
function buildTestScene(
|
||||||
|
axNodes: CdpAxNode[],
|
||||||
|
captured: SceneFixture,
|
||||||
|
options: BuildVomSceneOptions = {},
|
||||||
|
) {
|
||||||
|
const rootFrameId = captured.rootFrameId ?? "root";
|
||||||
|
return buildFrameVomScene(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
frameId: rootFrameId,
|
||||||
|
contextScopeId: rootFrameId,
|
||||||
|
target: { tabId: 7 },
|
||||||
|
url: options.pageUrl,
|
||||||
|
domNodes: captured.nodes,
|
||||||
|
axNodes,
|
||||||
|
},
|
||||||
|
...(captured.documents ?? []),
|
||||||
|
],
|
||||||
|
captured,
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function fakeAgentWindow(ids: number[]) {
|
function fakeAgentWindow(ids: number[]) {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
@@ -266,7 +294,15 @@ describe("handleScreenshot", () => {
|
|||||||
expect(sent.find((c) => c.method === "Page.captureScreenshot")).toBeUndefined();
|
expect(sent.find((c) => c.method === "Page.captureScreenshot")).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("captures a clipped PNG when ref is given", async () => {
|
it.each([
|
||||||
|
{ zoom: 1, scroll: 0, expected: { x: 10, y: 20, width: 100, height: 40 } },
|
||||||
|
{ zoom: 0.9, scroll: 100, expected: { x: 9, y: 108, width: 90, height: 36 } },
|
||||||
|
{ zoom: 1.25, scroll: 100, expected: { x: 12.5, y: 150, width: 125, height: 50 } },
|
||||||
|
])("captures a ref in page DIP at zoom $zoom and scroll $scroll", async ({
|
||||||
|
zoom,
|
||||||
|
scroll,
|
||||||
|
expected,
|
||||||
|
}) => {
|
||||||
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
||||||
const ctx = await sm.start("aa11");
|
const ctx = await sm.start("aa11");
|
||||||
ctx.refStore.set("e5", 999, { tabId: 7 });
|
ctx.refStore.set("e5", 999, { tabId: 7 });
|
||||||
@@ -274,6 +310,10 @@ describe("handleScreenshot", () => {
|
|||||||
"DOM.scrollIntoViewIfNeeded": () => ({}),
|
"DOM.scrollIntoViewIfNeeded": () => ({}),
|
||||||
"DOM.getContentQuads": () => ({ quads: [[10, 20, 110, 20, 110, 60, 10, 60]] }),
|
"DOM.getContentQuads": () => ({ quads: [[10, 20, 110, 20, 110, 60, 10, 60]] }),
|
||||||
"Page.captureScreenshot": () => ({ data: TINY_PNG }),
|
"Page.captureScreenshot": () => ({ data: TINY_PNG }),
|
||||||
|
"Page.getLayoutMetrics": () => ({
|
||||||
|
cssLayoutViewport: { clientWidth: 1280, clientHeight: 720, pageX: 0, pageY: scroll },
|
||||||
|
cssVisualViewport: { zoom },
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
const captureVisibleTab = vi.fn();
|
const captureVisibleTab = vi.fn();
|
||||||
const res = await handleScreenshot(
|
const res = await handleScreenshot(
|
||||||
@@ -297,7 +337,8 @@ describe("handleScreenshot", () => {
|
|||||||
clip?: { x: number; y: number; width: number; height: number };
|
clip?: { x: number; y: number; width: number; height: number };
|
||||||
}
|
}
|
||||||
)?.clip;
|
)?.clip;
|
||||||
expect(clip).toMatchObject({ x: 10, y: 20, width: 100, height: 40 });
|
expect(clip).toMatchObject(expected);
|
||||||
|
expect(sent.filter((c) => c.method === "Page.getLayoutMetrics")).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns not_found for unknown ref", async () => {
|
it("returns not_found for unknown ref", async () => {
|
||||||
@@ -509,10 +550,10 @@ describe("handleScreenshot overlay suppression", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// buildVomScene
|
// Semantic scene fixtures
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
describe("buildVomScene", () => {
|
describe("buildFrameVomScene", () => {
|
||||||
it("merges multiple sibling and nested frames without mixing their targets", () => {
|
it("merges multiple sibling and nested frames without mixing their targets", () => {
|
||||||
const node = (
|
const node = (
|
||||||
backendNodeId: number,
|
backendNodeId: number,
|
||||||
@@ -550,25 +591,10 @@ describe("buildVomScene", () => {
|
|||||||
node(300, null, "nested", "body"),
|
node(300, null, "nested", "body"),
|
||||||
node(301, 300, "nested", "input", { placeholder: "Nested field" }),
|
node(301, 300, "nested", "input", { placeholder: "Nested field" }),
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1200, height: 800 },
|
viewport: { width: 1200, height: 800 },
|
||||||
nodes: mainNodes,
|
nodes: mainNodes,
|
||||||
iframeNodes: new Map([
|
|
||||||
[10, firstNodes],
|
|
||||||
[20, secondNodes],
|
|
||||||
[210, nestedNodes],
|
|
||||||
]),
|
|
||||||
frameNodes: new Map([
|
|
||||||
["main", mainNodes],
|
|
||||||
["first", firstNodes],
|
|
||||||
["second", secondNodes],
|
|
||||||
["nested", nestedNodes],
|
|
||||||
]),
|
|
||||||
frameOwnerBackendNodeIds: new Map([
|
|
||||||
["first", 10],
|
|
||||||
["second", 20],
|
|
||||||
["nested", 210],
|
|
||||||
]),
|
|
||||||
rootFrameId: "main",
|
rootFrameId: "main",
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
};
|
};
|
||||||
@@ -667,11 +693,10 @@ describe("buildVomScene", () => {
|
|||||||
position: "static",
|
position: "static",
|
||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
};
|
};
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 800, height: 600 },
|
viewport: { width: 800, height: 600 },
|
||||||
nodes: [],
|
nodes: [],
|
||||||
iframeNodes: new Map(),
|
|
||||||
frameNodes: new Map([["child", [childNode]]]),
|
|
||||||
rootFrameId: "main",
|
rootFrameId: "main",
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
};
|
};
|
||||||
@@ -733,14 +758,10 @@ describe("buildVomScene", () => {
|
|||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 800, height: 600 },
|
viewport: { width: 800, height: 600 },
|
||||||
nodes: mainNodes,
|
nodes: mainNodes,
|
||||||
iframeNodes: new Map(),
|
|
||||||
frameNodes: new Map([
|
|
||||||
["main", mainNodes],
|
|
||||||
["child", []],
|
|
||||||
]),
|
|
||||||
rootFrameId: "main",
|
rootFrameId: "main",
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
};
|
};
|
||||||
@@ -816,9 +837,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 200,
|
backendDOMNodeId: 200,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -844,11 +864,11 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const scene = buildVomScene(axNodes, captured);
|
const scene = buildTestScene(axNodes, captured);
|
||||||
expect(scene.viewport).toEqual({ width: 1000, height: 800 });
|
expect(scene.viewport).toEqual({ width: 1000, height: 800 });
|
||||||
expect(scene.nodes[0]).toEqual(
|
expect(scene.nodes[0]).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: 100,
|
backendNodeId: 100,
|
||||||
parentId: null,
|
parentId: null,
|
||||||
role: "RootWebArea",
|
role: "RootWebArea",
|
||||||
name: "Example",
|
name: "Example",
|
||||||
@@ -860,14 +880,14 @@ describe("buildVomScene", () => {
|
|||||||
);
|
);
|
||||||
expect(scene.nodes[1]).toEqual(
|
expect(scene.nodes[1]).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: 200,
|
backendNodeId: 200,
|
||||||
parentId: 100,
|
parentId: scene.nodes.find((node) => node.backendNodeId === 100)!.id,
|
||||||
role: "button",
|
role: "button",
|
||||||
name: "Submit",
|
name: "Submit",
|
||||||
tag: "button",
|
tag: "button",
|
||||||
rect: { x: 20, y: 20, w: 120, h: 40 },
|
rect: { x: 20, y: 20, w: 120, h: 40 },
|
||||||
domParentId: 100,
|
domParentId: scene.nodes.find((node) => node.backendNodeId === 100)!.id,
|
||||||
domAncestorIds: [100],
|
domAncestorIds: [scene.nodes.find((node) => node.backendNodeId === 100)!.id],
|
||||||
attrs: {},
|
attrs: {},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@@ -896,9 +916,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 300,
|
backendDOMNodeId: 300,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -934,10 +953,10 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const scene = buildVomScene(axNodes, captured, { pageUrl: "https://app.example.test/home" });
|
const scene = buildTestScene(axNodes, captured, { pageUrl: "https://app.example.test/home" });
|
||||||
|
|
||||||
expect(scene.nodes.find((node) => node.id === 200)?.href).toBe("docs.example.org");
|
expect(scene.nodes.find((node) => node.backendNodeId === 200)?.href).toBe("docs.example.org");
|
||||||
expect(scene.nodes.find((node) => node.id === 300)?.href).toBeUndefined();
|
expect(scene.nodes.find((node) => node.backendNodeId === 300)?.href).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses the nearest backend AX ancestor as parentId", () => {
|
it("uses the nearest backend AX ancestor as parentId", () => {
|
||||||
@@ -962,9 +981,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 30,
|
backendDOMNodeId: 30,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 400, height: 300 },
|
viewport: { width: 400, height: 300 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -990,10 +1008,12 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const scene = buildVomScene(axNodes, captured);
|
const scene = buildTestScene(axNodes, captured);
|
||||||
|
|
||||||
expect(scene.nodes.map((node) => node.id)).toEqual([10, 30]);
|
expect(scene.nodes.map((node) => node.backendNodeId)).toEqual([10, 30]);
|
||||||
expect(scene.nodes.find((node) => node.id === 30)?.parentId).toBe(10);
|
expect(scene.nodes.find((node) => node.backendNodeId === 30)?.parentId).toBe(
|
||||||
|
scene.nodes.find((node) => node.backendNodeId === 10)!.id,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("maps iframe sub-document controls to VomNodes", () => {
|
it("maps iframe sub-document controls to VomNodes", () => {
|
||||||
@@ -1011,7 +1031,7 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 20,
|
backendDOMNodeId: 20,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1035,10 +1055,15 @@ describe("buildVomScene", () => {
|
|||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
iframeNodes: new Map([
|
documents: [
|
||||||
[
|
{
|
||||||
20,
|
frameId: "child",
|
||||||
[
|
contextScopeId: "child",
|
||||||
|
parentFrameId: "root",
|
||||||
|
ownerBackendNodeId: 20,
|
||||||
|
target: { tabId: 7 },
|
||||||
|
axNodes: [],
|
||||||
|
domNodes: [
|
||||||
{
|
{
|
||||||
backendNodeId: 101,
|
backendNodeId: 101,
|
||||||
parentBackendNodeId: null,
|
parentBackendNodeId: null,
|
||||||
@@ -1060,26 +1085,25 @@ describe("buildVomScene", () => {
|
|||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
],
|
},
|
||||||
]),
|
],
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const iframeControls = buildVomScene(axNodes, captured).nodes.filter((node) =>
|
const scene = buildTestScene(axNodes, captured);
|
||||||
[101, 102].includes(node.id),
|
const iframeControls = scene.nodes.filter((node) => [101, 102].includes(node.backendNodeId!));
|
||||||
);
|
|
||||||
|
|
||||||
expect(iframeControls).toEqual([
|
expect(iframeControls).toEqual([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: 101,
|
backendNodeId: 101,
|
||||||
parentId: 20,
|
parentId: scene.nodes.find((node) => node.backendNodeId === 20)!.id,
|
||||||
role: "textbox",
|
role: "textbox",
|
||||||
name: "请输入手机号",
|
name: "请输入手机号",
|
||||||
sensitive: false,
|
sensitive: false,
|
||||||
}),
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: 102,
|
backendNodeId: 102,
|
||||||
parentId: 20,
|
parentId: scene.nodes.find((node) => node.backendNodeId === 20)!.id,
|
||||||
role: "textbox",
|
role: "textbox",
|
||||||
name: "密码",
|
name: "密码",
|
||||||
sensitive: true,
|
sensitive: true,
|
||||||
@@ -1100,11 +1124,9 @@ describe("buildVomScene", () => {
|
|||||||
parentId: "1",
|
parentId: "1",
|
||||||
role: { type: "role", value: "Iframe" },
|
role: { type: "role", value: "Iframe" },
|
||||||
backendDOMNodeId: 20,
|
backendDOMNodeId: 20,
|
||||||
childIds: ["3"],
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
nodeId: "3",
|
nodeId: "3",
|
||||||
parentId: "2",
|
|
||||||
role: { type: "role", value: "textbox" },
|
role: { type: "role", value: "textbox" },
|
||||||
name: { type: "x", value: "输入密码" },
|
name: { type: "x", value: "输入密码" },
|
||||||
value: { value: "iframe-secret" },
|
value: { value: "iframe-secret" },
|
||||||
@@ -1112,7 +1134,7 @@ describe("buildVomScene", () => {
|
|||||||
properties: [{ name: "inputType", value: { value: "password" } }],
|
properties: [{ name: "inputType", value: { value: "password" } }],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1136,10 +1158,15 @@ describe("buildVomScene", () => {
|
|||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
iframeNodes: new Map([
|
documents: [
|
||||||
[
|
{
|
||||||
20,
|
frameId: "child",
|
||||||
[
|
contextScopeId: "child",
|
||||||
|
parentFrameId: "root",
|
||||||
|
ownerBackendNodeId: 20,
|
||||||
|
target: { tabId: 7 },
|
||||||
|
axNodes: [axNodes[2]],
|
||||||
|
domNodes: [
|
||||||
{
|
{
|
||||||
backendNodeId: 102,
|
backendNodeId: 102,
|
||||||
parentBackendNodeId: null,
|
parentBackendNodeId: null,
|
||||||
@@ -1151,25 +1178,27 @@ describe("buildVomScene", () => {
|
|||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
],
|
},
|
||||||
]),
|
],
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const passwordNodes = buildVomScene(axNodes, captured).nodes.filter(
|
const passwordNodes = buildTestScene(axNodes.slice(0, 2), captured).nodes.filter(
|
||||||
(node) => node.role === "textbox" && node.name === "输入密码",
|
(node) => node.role === "textbox" && node.name === "输入密码",
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(passwordNodes).toHaveLength(1);
|
expect(passwordNodes).toHaveLength(1);
|
||||||
expect(passwordNodes[0]).toEqual(expect.objectContaining({ id: 202, sensitive: true }));
|
expect(passwordNodes[0]).toEqual(
|
||||||
|
expect.objectContaining({ backendNodeId: 202, sensitive: true }),
|
||||||
|
);
|
||||||
expect(passwordNodes[0].value).toBeUndefined();
|
expect(passwordNodes[0].value).toBeUndefined();
|
||||||
const rendered = renderVom(buildVomScene(axNodes, captured)).text;
|
const rendered = renderVom(buildTestScene(axNodes.slice(0, 2), captured)).text;
|
||||||
expect(rendered).toContain('textbox "输入密码" [filled] ="•••"');
|
expect(rendered).toContain('textbox "输入密码" [filled] ="•••"');
|
||||||
expect(rendered).not.toContain("iframe-secret");
|
expect(rendered).not.toContain("iframe-secret");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps unnamed iframe controls but skips unnamed iframe links", () => {
|
it("keeps unnamed iframe controls but skips unnamed iframe links", () => {
|
||||||
const scene = buildVomScene(
|
const scene = buildTestScene(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
nodeId: "1",
|
nodeId: "1",
|
||||||
@@ -1208,10 +1237,15 @@ describe("buildVomScene", () => {
|
|||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
iframeNodes: new Map([
|
documents: [
|
||||||
[
|
{
|
||||||
20,
|
frameId: "child",
|
||||||
[
|
contextScopeId: "child",
|
||||||
|
parentFrameId: "root",
|
||||||
|
ownerBackendNodeId: 20,
|
||||||
|
target: { tabId: 7 },
|
||||||
|
axNodes: [],
|
||||||
|
domNodes: [
|
||||||
{
|
{
|
||||||
backendNodeId: 201,
|
backendNodeId: 201,
|
||||||
parentBackendNodeId: null,
|
parentBackendNodeId: null,
|
||||||
@@ -1233,25 +1267,25 @@ describe("buildVomScene", () => {
|
|||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
],
|
},
|
||||||
]),
|
],
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(scene.nodes.find((node) => node.id === 201)).toEqual(
|
expect(scene.nodes.find((node) => node.backendNodeId === 201)).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: 201,
|
backendNodeId: 201,
|
||||||
parentId: 20,
|
parentId: scene.nodes.find((node) => node.backendNodeId === 20)!.id,
|
||||||
role: "button",
|
role: "button",
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
expect(scene.nodes.find((node) => node.id === 201)).not.toHaveProperty("name");
|
expect(scene.nodes.find((node) => node.backendNodeId === 201)).not.toHaveProperty("name");
|
||||||
expect(scene.nodes.find((node) => node.id === 202)).toBeUndefined();
|
expect(scene.nodes.find((node) => node.backendNodeId === 202)).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("preserves captured-only iframe anchors for iframe sub-document controls", () => {
|
it("preserves captured-only iframe anchors for iframe sub-document controls", () => {
|
||||||
const scene = buildVomScene(
|
const scene = buildTestScene(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
nodeId: "1",
|
nodeId: "1",
|
||||||
@@ -1284,10 +1318,15 @@ describe("buildVomScene", () => {
|
|||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
iframeNodes: new Map<number, CapturedNode[]>([
|
documents: [
|
||||||
[
|
{
|
||||||
20,
|
frameId: "child",
|
||||||
[
|
contextScopeId: "child",
|
||||||
|
parentFrameId: "root",
|
||||||
|
ownerBackendNodeId: 20,
|
||||||
|
target: { tabId: 7 },
|
||||||
|
axNodes: [],
|
||||||
|
domNodes: [
|
||||||
{
|
{
|
||||||
backendNodeId: 201,
|
backendNodeId: 201,
|
||||||
parentBackendNodeId: null,
|
parentBackendNodeId: null,
|
||||||
@@ -1309,16 +1348,16 @@ describe("buildVomScene", () => {
|
|||||||
pointerEvents: "auto",
|
pointerEvents: "auto",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
],
|
},
|
||||||
]),
|
],
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(scene.nodes.find((node) => node.id === 20)).toEqual(
|
expect(scene.nodes.find((node) => node.backendNodeId === 20)).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: 20,
|
backendNodeId: 20,
|
||||||
parentId: 10,
|
parentId: scene.nodes.find((node) => node.backendNodeId === 10)!.id,
|
||||||
role: "Iframe",
|
role: "Iframe",
|
||||||
name: "支付验证",
|
name: "支付验证",
|
||||||
}),
|
}),
|
||||||
@@ -1339,9 +1378,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 10,
|
backendDOMNodeId: 10,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 320, height: 240 },
|
viewport: { width: 320, height: 240 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1362,9 +1400,9 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(buildVomScene(axNodes, captured).nodes[0]).toEqual(
|
expect(buildTestScene(axNodes, captured).nodes[0]).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: 10,
|
backendNodeId: 10,
|
||||||
sensitive: true,
|
sensitive: true,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@@ -1379,9 +1417,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 10,
|
backendDOMNodeId: 10,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1398,9 +1435,9 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(buildVomScene(axNodes, captured).nodes[0]).toEqual(
|
expect(buildTestScene(axNodes, captured).nodes[0]).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: 10,
|
backendNodeId: 10,
|
||||||
role: "textbox",
|
role: "textbox",
|
||||||
name: "+86",
|
name: "+86",
|
||||||
placeholder: "输入手机号",
|
placeholder: "输入手机号",
|
||||||
@@ -1418,9 +1455,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 10,
|
backendDOMNodeId: 10,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1437,8 +1473,13 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(buildVomScene(axNodes, captured).nodes[0]).toEqual(
|
expect(buildTestScene(axNodes, captured).nodes[0]).toEqual(
|
||||||
expect.objectContaining({ id: 10, role: "textbox", name: "Email", value: "a@b.com" }),
|
expect.objectContaining({
|
||||||
|
backendNodeId: 10,
|
||||||
|
role: "textbox",
|
||||||
|
name: "Email",
|
||||||
|
value: "a@b.com",
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1457,9 +1498,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 20,
|
backendDOMNodeId: 20,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1497,9 +1537,9 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(buildVomScene(axNodes, captured).nodes.find((node) => node.id === 20)).toEqual(
|
expect(
|
||||||
expect.objectContaining({ role: "textbox", name: "验证码" }),
|
buildTestScene(axNodes, captured).nodes.find((node) => node.backendNodeId === 20),
|
||||||
);
|
).toEqual(expect.objectContaining({ role: "textbox", name: "验证码" }));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses preceding AX static text as a form control label", () => {
|
it("uses preceding AX static text as a form control label", () => {
|
||||||
@@ -1523,9 +1563,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 20,
|
backendDOMNodeId: 20,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1542,13 +1581,13 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(buildVomScene(axNodes, captured).nodes.find((node) => node.id === 20)).toEqual(
|
expect(
|
||||||
expect.objectContaining({ role: "textbox", name: "验证码", inputState: "empty" }),
|
buildTestScene(axNodes, captured).nodes.find((node) => node.backendNodeId === 20),
|
||||||
);
|
).toEqual(expect.objectContaining({ role: "textbox", name: "验证码", inputState: "empty" }));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("marks runtime default input values without treating them as ordinary filled input", () => {
|
it("marks runtime default input values without treating them as ordinary filled input", () => {
|
||||||
const scene = buildVomScene(
|
const scene = buildTestScene(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
nodeId: "1",
|
nodeId: "1",
|
||||||
@@ -1558,7 +1597,6 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
{
|
{
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1602,9 +1640,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 20,
|
backendDOMNodeId: 20,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1654,9 +1691,14 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const scene = buildVomScene(axNodes, captured);
|
const scene = buildTestScene(axNodes, captured);
|
||||||
expect(scene.nodes.find((n) => n.id === 20)).toEqual(
|
expect(scene.nodes.find((n) => n.backendNodeId === 20)).toEqual(
|
||||||
expect.objectContaining({ id: 20, role: "button", name: "close", cursor: "pointer" }),
|
expect.objectContaining({
|
||||||
|
backendNodeId: 20,
|
||||||
|
role: "button",
|
||||||
|
name: "close",
|
||||||
|
cursor: "pointer",
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
const rendered = renderVom(scene);
|
const rendered = renderVom(scene);
|
||||||
expect(rendered.text).toContain('@e1 button "close"');
|
expect(rendered.text).toContain('@e1 button "close"');
|
||||||
@@ -1690,9 +1732,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 30,
|
backendDOMNodeId: 30,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1731,9 +1772,9 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const scene = buildVomScene(axNodes, captured);
|
const scene = buildTestScene(axNodes, captured);
|
||||||
expect(scene.nodes.find((n) => n.id === 20)).toBeUndefined();
|
expect(scene.nodes.find((n) => n.backendNodeId === 20)).toBeUndefined();
|
||||||
expect(scene.nodes.find((n) => n.id === 30)?.role).toBe("link");
|
expect(scene.nodes.find((n) => n.backendNodeId === 30)?.role).toBe("link");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("promotes only the outermost clickable in a nested pointer chain", () => {
|
it("promotes only the outermost clickable in a nested pointer chain", () => {
|
||||||
@@ -1758,9 +1799,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 30,
|
backendDOMNodeId: 30,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1799,11 +1839,15 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const scene = buildVomScene(axNodes, captured);
|
const scene = buildTestScene(axNodes, captured);
|
||||||
expect(scene.nodes.find((n) => n.id === 20)).toEqual(
|
expect(scene.nodes.find((n) => n.backendNodeId === 20)).toEqual(
|
||||||
expect.objectContaining({ id: 20, role: "button", attrs: { "aria-label": "收藏" } }),
|
expect.objectContaining({
|
||||||
|
backendNodeId: 20,
|
||||||
|
role: "button",
|
||||||
|
attrs: { "aria-label": "收藏" },
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
expect(scene.nodes.find((n) => n.id === 30)).toBeUndefined();
|
expect(scene.nodes.find((n) => n.backendNodeId === 30)).toBeUndefined();
|
||||||
const rendered = renderVom(scene);
|
const rendered = renderVom(scene);
|
||||||
expect(rendered.text).toContain('@e1 button "收藏"');
|
expect(rendered.text).toContain('@e1 button "收藏"');
|
||||||
expect(rendered.refs.map(({ ref, backendNodeId }) => ({ ref, backendNodeId }))).toEqual([
|
expect(rendered.refs.map(({ ref, backendNodeId }) => ({ ref, backendNodeId }))).toEqual([
|
||||||
@@ -1827,9 +1871,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 20,
|
backendDOMNodeId: 20,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1877,10 +1920,10 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const scene = buildVomScene(axNodes, captured);
|
const scene = buildTestScene(axNodes, captured);
|
||||||
expect(scene.activeScopeBlocks).toEqual([
|
expect(scene.activeScopeBlocks).toEqual([
|
||||||
{
|
{
|
||||||
triggerId: 20,
|
triggerId: scene.nodes.find((node) => node.backendNodeId === 20)!.id,
|
||||||
label: "Reviews (12)",
|
label: "Reviews (12)",
|
||||||
lines: ["Jane - ear cups are small", "Bob - great sound"],
|
lines: ["Jane - ear cups are small", "Bob - great sound"],
|
||||||
},
|
},
|
||||||
@@ -1904,11 +1947,10 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 20,
|
backendDOMNodeId: 20,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const scene = buildVomScene(
|
const scene = buildTestScene(
|
||||||
axNodes,
|
axNodes,
|
||||||
{
|
{
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -1941,7 +1983,11 @@ describe("buildVomScene", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
expect(scene.surfaces).toEqual([
|
expect(scene.surfaces).toEqual([
|
||||||
{ triggerId: 20, triggerAction: "hover", subItems: ["Shoes", "Bags"] },
|
{
|
||||||
|
triggerId: scene.nodes.find((node) => node.backendNodeId === 20)!.id,
|
||||||
|
triggerAction: "hover",
|
||||||
|
subItems: ["Shoes", "Bags"],
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
expect(renderVom(scene).text).toContain('@e1 button "Products" [hover first: Shoes | Bags]');
|
expect(renderVom(scene).text).toContain('@e1 button "Products" [hover first: Shoes | Bags]');
|
||||||
});
|
});
|
||||||
@@ -1962,11 +2008,10 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 21,
|
backendDOMNodeId: 21,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const scene = buildVomScene(
|
const scene = buildTestScene(
|
||||||
axNodes,
|
axNodes,
|
||||||
{
|
{
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -2027,11 +2072,10 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 21,
|
backendDOMNodeId: 21,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const scene = buildVomScene(
|
const scene = buildTestScene(
|
||||||
axNodes,
|
axNodes,
|
||||||
{
|
{
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -2076,7 +2120,11 @@ describe("buildVomScene", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
expect(scene.surfaces).toEqual([
|
expect(scene.surfaces).toEqual([
|
||||||
{ triggerId: 21, triggerAction: "hover", subItems: ["My profile"] },
|
{
|
||||||
|
triggerId: scene.nodes.find((node) => node.backendNodeId === 21)!.id,
|
||||||
|
triggerAction: "hover",
|
||||||
|
subItems: ["My profile"],
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -2096,11 +2144,10 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 21,
|
backendDOMNodeId: 21,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const scene = buildVomScene(
|
const scene = buildTestScene(
|
||||||
axNodes,
|
axNodes,
|
||||||
{
|
{
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -2151,7 +2198,11 @@ describe("buildVomScene", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
expect(scene.surfaces).toEqual([
|
expect(scene.surfaces).toEqual([
|
||||||
{ triggerId: 20, triggerAction: "hover", subItems: ["My profile", "Sign out"] },
|
{
|
||||||
|
triggerId: scene.nodes.find((node) => node.backendNodeId === 20)!.id,
|
||||||
|
triggerAction: "hover",
|
||||||
|
subItems: ["My profile", "Sign out"],
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
expect(renderVom(scene).text).toContain(
|
expect(renderVom(scene).text).toContain(
|
||||||
'@e1 button "image" [hover first: My profile | Sign out]',
|
'@e1 button "image" [hover first: My profile | Sign out]',
|
||||||
@@ -2174,11 +2225,10 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 42,
|
backendDOMNodeId: 42,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const scene = buildVomScene(
|
const scene = buildTestScene(
|
||||||
axNodes,
|
axNodes,
|
||||||
{
|
{
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -2248,9 +2298,8 @@ describe("buildVomScene", () => {
|
|||||||
properties: [{ name: "inputType", value: { value: "credit-card" } }],
|
properties: [{ name: "inputType", value: { value: "credit-card" } }],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const scene = buildVomScene(axNodes, {
|
const scene = buildTestScene(axNodes, {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -2297,7 +2346,7 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(scene.nodes.find((node) => node.id === 20)).toEqual(
|
expect(scene.nodes.find((node) => node.backendNodeId === 20)).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
name: "Products [expanded]",
|
name: "Products [expanded]",
|
||||||
attrs: expect.objectContaining({
|
attrs: expect.objectContaining({
|
||||||
@@ -2307,13 +2356,17 @@ describe("buildVomScene", () => {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
expect(scene.activeScopeBlocks).toEqual([
|
expect(scene.activeScopeBlocks).toEqual([
|
||||||
{ triggerId: 20, label: "Products [expanded]", lines: ["Shoes Bags"] },
|
{
|
||||||
|
triggerId: scene.nodes.find((node) => node.backendNodeId === 20)!.id,
|
||||||
|
label: "Products [expanded]",
|
||||||
|
lines: ["Shoes Bags"],
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
expect(scene.nodes.find((node) => node.id === 30)?.sensitive).toBe(true);
|
expect(scene.nodes.find((node) => node.backendNodeId === 30)?.sensitive).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("aggregates AX virtual text into unnamed structural nodes", () => {
|
it("aggregates AX virtual text into unnamed structural nodes", () => {
|
||||||
const scene = buildVomScene(
|
const scene = buildTestScene(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
nodeId: "1",
|
nodeId: "1",
|
||||||
@@ -2337,7 +2390,6 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
{
|
{
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -2364,13 +2416,12 @@ describe("buildVomScene", () => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(scene.nodes.find((node) => node.id === 20)?.name).toBe("Inline only text");
|
expect(scene.nodes.find((node) => node.backendNodeId === 20)?.name).toBe("Inline only text");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("marks captured dialog elements as modal without AX role or aria-modal", () => {
|
it("marks captured dialog elements as modal without AX role or aria-modal", () => {
|
||||||
const scene = buildVomScene([], {
|
const scene = buildTestScene([], {
|
||||||
viewport: { width: 640, height: 480 },
|
viewport: { width: 640, height: 480 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
excludedBackendNodeIds: new Set(),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -2388,7 +2439,7 @@ describe("buildVomScene", () => {
|
|||||||
|
|
||||||
expect(scene.nodes[0]).toEqual(
|
expect(scene.nodes[0]).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: 50,
|
backendNodeId: 50,
|
||||||
modal: true,
|
modal: true,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@@ -2417,9 +2468,8 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 202,
|
backendDOMNodeId: 202,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const captured: CapturedViewModel = {
|
const captured: SceneFixture = {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set([200, 201, 202]),
|
excludedBackendNodeIds: new Set([200, 201, 202]),
|
||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
@@ -2435,9 +2485,9 @@ describe("buildVomScene", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const scene = buildVomScene(axNodes, captured);
|
const scene = buildTestScene(axNodes, captured);
|
||||||
expect(scene.nodes.find((n) => n.id === 202)).toBeUndefined();
|
expect(scene.nodes.find((n) => n.backendNodeId === 202)).toBeUndefined();
|
||||||
expect(scene.nodes.find((n) => n.id === 2)).toEqual(
|
expect(scene.nodes.find((n) => n.backendNodeId === 2)).toEqual(
|
||||||
expect.objectContaining({ role: "button", name: "Real Button" }),
|
expect.objectContaining({ role: "button", name: "Real Button" }),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -2465,13 +2515,12 @@ describe("buildVomScene", () => {
|
|||||||
backendDOMNodeId: 203,
|
backendDOMNodeId: 203,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const scene = buildVomScene(axNodes, {
|
const scene = buildTestScene(axNodes, {
|
||||||
viewport: { width: 1000, height: 800 },
|
viewport: { width: 1000, height: 800 },
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set([200]),
|
excludedBackendNodeIds: new Set([200]),
|
||||||
nodes: [],
|
nodes: [],
|
||||||
});
|
});
|
||||||
expect(scene.nodes.find((n) => n.id === 203)).toBeUndefined();
|
expect(scene.nodes.find((n) => n.backendNodeId === 203)).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -2576,6 +2625,7 @@ describe("handleSnapshot", () => {
|
|||||||
strings,
|
strings,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0],
|
parentIndex: [-1, 0],
|
||||||
nodeName: [i("body"), i("button")],
|
nodeName: [i("body"), i("button")],
|
||||||
@@ -2667,6 +2717,7 @@ describe("handleSnapshot", () => {
|
|||||||
strings,
|
strings,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0],
|
parentIndex: [-1, 0],
|
||||||
nodeName: [i("body"), i("button")],
|
nodeName: [i("body"), i("button")],
|
||||||
@@ -2761,6 +2812,7 @@ describe("handleSnapshot", () => {
|
|||||||
strings,
|
strings,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0],
|
parentIndex: [-1, 0],
|
||||||
nodeName: [i("body"), i("button")],
|
nodeName: [i("body"), i("button")],
|
||||||
@@ -3025,6 +3077,7 @@ describe("handleSnapshot", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 2],
|
parentIndex: [-1, 0, 1, 2],
|
||||||
nodeName: [i("html"), i("body"), i("div"), i("input")],
|
nodeName: [i("html"), i("body"), i("div"), i("input")],
|
||||||
@@ -3082,6 +3135,7 @@ describe("handleSnapshot", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 1, 1, 1],
|
parentIndex: [-1, 0, 1, 1, 1, 1],
|
||||||
nodeName: [i("html"), i("body"), i("input"), i("input"), i("input"), i("input")],
|
nodeName: [i("html"), i("body"), i("input"), i("input"), i("input"), i("input")],
|
||||||
@@ -3215,12 +3269,15 @@ describe("handleSnapshot", () => {
|
|||||||
const send = vi.fn(async (_tabId: number, method: string) => {
|
const send = vi.fn(async (_tabId: number, method: string) => {
|
||||||
if (method === "Page.getLayoutMetrics") return VP_METRICS;
|
if (method === "Page.getLayoutMetrics") return VP_METRICS;
|
||||||
if (method === "DOMSnapshot.enable" || method === "Accessibility.enable") return {};
|
if (method === "DOMSnapshot.enable" || method === "Accessibility.enable") return {};
|
||||||
if (method === "DOMSnapshot.captureSnapshot") return snapshot;
|
if (method === "DOMSnapshot.captureSnapshot")
|
||||||
|
return { ...snapshot, documents: [snapshot.documents[0]] };
|
||||||
if (method === "Accessibility.getFullAXTree") return { nodes: mainAx };
|
if (method === "Accessibility.getFullAXTree") return { nodes: mainAx };
|
||||||
throw new Error(`unexpected root CDP method ${method}`);
|
throw new Error(`unexpected root CDP method ${method}`);
|
||||||
});
|
});
|
||||||
const sendToTarget = vi.fn(async (_target, method: string) => {
|
const sendToTarget = vi.fn(async (_target, method: string) => {
|
||||||
if (method === "Accessibility.enable") return {};
|
if (method === "Accessibility.enable" || method === "DOMSnapshot.enable") return {};
|
||||||
|
if (method === "DOMSnapshot.captureSnapshot")
|
||||||
|
return { ...snapshot, documents: [snapshot.documents[1]] };
|
||||||
if (method === "Accessibility.getFullAXTree") return { nodes: childAx };
|
if (method === "Accessibility.getFullAXTree") return { nodes: childAx };
|
||||||
throw new Error(`unexpected child CDP method ${method}`);
|
throw new Error(`unexpected child CDP method ${method}`);
|
||||||
});
|
});
|
||||||
@@ -3321,6 +3378,7 @@ describe("handleSnapshot", () => {
|
|||||||
strings,
|
strings,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 1, 3],
|
parentIndex: [-1, 0, 1, 1, 3],
|
||||||
nodeName: [index("html"), index("body"), index("button"), index("div"), index("#text")],
|
nodeName: [index("html"), index("body"), index("button"), index("div"), index("#text")],
|
||||||
@@ -3608,6 +3666,7 @@ describe("handleSnapshot", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
scrollOffsetX: 0,
|
scrollOffsetX: 0,
|
||||||
scrollOffsetY: 0,
|
scrollOffsetY: 0,
|
||||||
nodes: {
|
nodes: {
|
||||||
@@ -3634,6 +3693,7 @@ describe("handleSnapshot", () => {
|
|||||||
},
|
},
|
||||||
// documents[1]: cross-origin login form
|
// documents[1]: cross-origin login form
|
||||||
{
|
{
|
||||||
|
frameId: "child",
|
||||||
scrollOffsetX: 0,
|
scrollOffsetX: 0,
|
||||||
scrollOffsetY: 0,
|
scrollOffsetY: 0,
|
||||||
nodes: {
|
nodes: {
|
||||||
@@ -3812,6 +3872,7 @@ describe("handleSnapshot", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 0, 3, 4, 5],
|
parentIndex: [-1, 0, 1, 0, 3, 4, 5],
|
||||||
nodeName: [
|
nodeName: [
|
||||||
|
|||||||
@@ -1,24 +1,21 @@
|
|||||||
import { type CdpFrame, type CdpFrameGraph, type CdpTarget } from "@/browser-driver/frame-graph";
|
import { type CdpTarget, cdpTargetKey } from "@/browser-driver/frame-graph";
|
||||||
import type { RpcError } from "@/transport/types";
|
import type { RpcError } from "@/transport/types";
|
||||||
import { nodeContentRegion, scrollNodeIntoView } from "./element-geometry";
|
import { nodeContentRegion, scrollNodeIntoView } from "./element-geometry";
|
||||||
import {
|
import {
|
||||||
clipPolygon,
|
clipPolygon,
|
||||||
type GeometryProjection,
|
|
||||||
type Point,
|
type Point,
|
||||||
type Polygon,
|
type Polygon,
|
||||||
type ProjectiveEdge,
|
|
||||||
parseCdpQuad,
|
|
||||||
polygonArea,
|
polygonArea,
|
||||||
polygonCentroid,
|
polygonCentroid,
|
||||||
projectRegionToViewport,
|
projectRegionToViewport,
|
||||||
type Quad,
|
|
||||||
type Region,
|
type Region,
|
||||||
rectPolygon,
|
rectPolygon,
|
||||||
regionBounds,
|
regionBounds,
|
||||||
type Size,
|
|
||||||
type ViewportRect,
|
type ViewportRect,
|
||||||
} from "./geometry";
|
} from "./geometry";
|
||||||
import { type CdpRunner, cdpRunnerForTarget, isRpcError, sendToCdpTarget } from "./shared";
|
import type { CssViewport } from "./geometry/coordinate-types";
|
||||||
|
import { cssViewport, GeometryContext } from "./geometry/frame-context";
|
||||||
|
import { type CdpRunner, cdpRunnerForTarget, isRpcError } from "./shared";
|
||||||
|
|
||||||
export interface NodeAddress {
|
export interface NodeAddress {
|
||||||
target: CdpTarget;
|
target: CdpTarget;
|
||||||
@@ -27,6 +24,8 @@ export interface NodeAddress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface ResolvedNodeGeometry {
|
export interface ResolvedNodeGeometry {
|
||||||
|
/** The same top-level measurement used by projection, for screenshot coordinate adaptation. */
|
||||||
|
topViewport: CssViewport;
|
||||||
topVisibleRegions: Region;
|
topVisibleRegions: Region;
|
||||||
topBounds: ViewportRect;
|
topBounds: ViewportRect;
|
||||||
/** Point in the top-level tab viewport, used by root-target input events. */
|
/** Point in the top-level tab viewport, used by root-target input events. */
|
||||||
@@ -39,145 +38,16 @@ function geometryError(message: string): RpcError {
|
|||||||
return { code: "cdp_failed", message };
|
return { code: "cdp_failed", message };
|
||||||
}
|
}
|
||||||
|
|
||||||
function frameMap(graph: CdpFrameGraph): Map<string, CdpFrame> {
|
|
||||||
return new Map(graph.frames.map((frame) => [frame.frameId, frame]));
|
|
||||||
}
|
|
||||||
|
|
||||||
function targetRootFrame(byId: Map<string, CdpFrame>, frame: CdpFrame): CdpFrame | null {
|
|
||||||
const seen = new Set<string>();
|
|
||||||
let current = frame;
|
|
||||||
while (current.parentFrameId) {
|
|
||||||
if (seen.has(current.frameId)) return null;
|
|
||||||
seen.add(current.frameId);
|
|
||||||
const parent = byId.get(current.parentFrameId);
|
|
||||||
if (!parent || parent.target.sessionId !== current.target.sessionId) break;
|
|
||||||
current = parent;
|
|
||||||
}
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
||||||
function frameAncestry(graph: CdpFrameGraph, frameId: string): CdpFrame[] | null {
|
|
||||||
const byId = frameMap(graph);
|
|
||||||
const path: CdpFrame[] = [];
|
|
||||||
const seen = new Set<string>();
|
|
||||||
let current = byId.get(frameId);
|
|
||||||
if (!current) return null;
|
|
||||||
while (current.parentFrameId) {
|
|
||||||
if (seen.has(current.frameId)) return null;
|
|
||||||
seen.add(current.frameId);
|
|
||||||
path.push(current);
|
|
||||||
const parent = byId.get(current.parentFrameId);
|
|
||||||
if (!parent) return null;
|
|
||||||
current = parent;
|
|
||||||
}
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function targetViewport(cdp: CdpRunner, target: CdpTarget): Promise<Size | null> {
|
|
||||||
const metrics = await sendToCdpTarget<{
|
|
||||||
cssLayoutViewport?: { clientWidth?: number; clientHeight?: number };
|
|
||||||
layoutViewport?: { clientWidth?: number; clientHeight?: number };
|
|
||||||
}>(cdp, target, "Page.getLayoutMetrics", {});
|
|
||||||
const viewport = metrics.cssLayoutViewport ?? metrics.layoutViewport ?? {};
|
|
||||||
const width = viewport.clientWidth ?? 0;
|
|
||||||
const height = viewport.clientHeight ?? 0;
|
|
||||||
return width > 0 && height > 0 ? { width, height } : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function ownerContentQuad(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
parent: CdpFrame,
|
|
||||||
ownerBackendNodeId: number,
|
|
||||||
): Promise<Quad | null> {
|
|
||||||
const result = await sendToCdpTarget<{ model?: { content?: number[] } }>(
|
|
||||||
cdp,
|
|
||||||
parent.target,
|
|
||||||
"DOM.getBoxModel",
|
|
||||||
{ backendNodeId: ownerBackendNodeId },
|
|
||||||
);
|
|
||||||
return parseCdpQuad(result.model?.content);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function sameTargetFrameClips(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
byId: Map<string, CdpFrame>,
|
|
||||||
frame: CdpFrame,
|
|
||||||
targetRoot: CdpFrame,
|
|
||||||
): Promise<Polygon[] | null> {
|
|
||||||
const clips: Polygon[] = [];
|
|
||||||
let current = frame;
|
|
||||||
const seen = new Set<string>();
|
|
||||||
while (current.frameId !== targetRoot.frameId) {
|
|
||||||
if (seen.has(current.frameId) || !current.parentFrameId) return null;
|
|
||||||
seen.add(current.frameId);
|
|
||||||
const parent = byId.get(current.parentFrameId);
|
|
||||||
if (!parent || current.ownerBackendNodeId === undefined) return null;
|
|
||||||
const clip = await ownerContentQuad(cdp, parent, current.ownerBackendNodeId);
|
|
||||||
if (!clip) return null;
|
|
||||||
clips.push(clip);
|
|
||||||
current = parent;
|
|
||||||
}
|
|
||||||
return clips;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function resolveFrameProjection(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
graph: CdpFrameGraph,
|
|
||||||
frameId: string,
|
|
||||||
): Promise<GeometryProjection | null> {
|
|
||||||
const byId = frameMap(graph);
|
|
||||||
const frame = byId.get(frameId);
|
|
||||||
if (!frame) return null;
|
|
||||||
let targetRoot = targetRootFrame(byId, frame);
|
|
||||||
if (!targetRoot) return null;
|
|
||||||
const sourceViewport = await targetViewport(cdp, targetRoot.target);
|
|
||||||
if (!sourceViewport) return null;
|
|
||||||
const sourceClips = await sameTargetFrameClips(cdp, byId, frame, targetRoot);
|
|
||||||
if (!sourceClips) return null;
|
|
||||||
|
|
||||||
const edges: ProjectiveEdge[] = [];
|
|
||||||
while (targetRoot.parentFrameId) {
|
|
||||||
const parent = byId.get(targetRoot.parentFrameId);
|
|
||||||
if (!parent || targetRoot.ownerBackendNodeId === undefined) return null;
|
|
||||||
const destinationQuad = await ownerContentQuad(cdp, parent, targetRoot.ownerBackendNodeId);
|
|
||||||
if (!destinationQuad) return null;
|
|
||||||
const source =
|
|
||||||
edges.length === 0 ? sourceViewport : await targetViewport(cdp, targetRoot.target);
|
|
||||||
if (!source) return null;
|
|
||||||
const parentTargetRoot = targetRootFrame(byId, parent);
|
|
||||||
if (!parentTargetRoot) return null;
|
|
||||||
const destinationClips = await sameTargetFrameClips(cdp, byId, parent, parentTargetRoot);
|
|
||||||
if (!destinationClips) return null;
|
|
||||||
edges.push({ sourceViewport: source, destinationQuad, destinationClips });
|
|
||||||
targetRoot = parentTargetRoot;
|
|
||||||
}
|
|
||||||
|
|
||||||
const topViewport =
|
|
||||||
edges.length === 0 ? sourceViewport : await targetViewport(cdp, targetRoot.target);
|
|
||||||
return topViewport ? { sourceClips, edges, topViewport } : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadFrameGraph(cdp: CdpRunner, tabId: number): Promise<CdpFrameGraph | null> {
|
|
||||||
if (!cdp.getFrameGraph) return null;
|
|
||||||
try {
|
|
||||||
return await cdp.getFrameGraph(tabId);
|
|
||||||
} catch (error) {
|
|
||||||
console.debug("[bsk frame-geometry] frame graph resolution failed", error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function scrollFrameOwners(
|
async function scrollFrameOwners(
|
||||||
cdp: CdpRunner,
|
cdp: CdpRunner,
|
||||||
tabId: number,
|
tabId: number,
|
||||||
graph: CdpFrameGraph,
|
context: GeometryContext,
|
||||||
frameId: string,
|
frameId: string,
|
||||||
): Promise<RpcError | null> {
|
): Promise<RpcError | null> {
|
||||||
const byId = frameMap(graph);
|
const ancestry = await context.ancestry(frameId);
|
||||||
const ancestry = frameAncestry(graph, frameId);
|
|
||||||
if (!ancestry) return geometryError(`could not resolve frame ancestry for ${frameId}`);
|
if (!ancestry) return geometryError(`could not resolve frame ancestry for ${frameId}`);
|
||||||
for (const child of [...ancestry].reverse()) {
|
for (const child of [...ancestry].reverse()) {
|
||||||
const parent = child.parentFrameId ? byId.get(child.parentFrameId) : undefined;
|
const parent = child.parentFrameId ? await context.frame(child.parentFrameId) : undefined;
|
||||||
if (!parent || child.ownerBackendNodeId === undefined) {
|
if (!parent || child.ownerBackendNodeId === undefined) {
|
||||||
return geometryError(`could not resolve frame owner for ${child.frameId}`);
|
return geometryError(`could not resolve frame owner for ${child.frameId}`);
|
||||||
}
|
}
|
||||||
@@ -191,17 +61,20 @@ async function scrollFrameOwners(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function scrollElementWithFrameGraph(
|
async function scrollElementWithContext(
|
||||||
cdp: CdpRunner,
|
cdp: CdpRunner,
|
||||||
tabId: number,
|
tabId: number,
|
||||||
target: CdpTarget,
|
target: CdpTarget,
|
||||||
backendNodeId: number,
|
backendNodeId: number,
|
||||||
frameId: string | undefined,
|
frameId: string | undefined,
|
||||||
graph: CdpFrameGraph | null,
|
context: GeometryContext,
|
||||||
): Promise<RpcError | null> {
|
): Promise<RpcError | null> {
|
||||||
if (frameId) {
|
if (frameId) {
|
||||||
if (!graph) return geometryError(`could not resolve frame graph for ${frameId}`);
|
const frame = await context.frame(frameId);
|
||||||
const error = await scrollFrameOwners(cdp, tabId, graph, frameId);
|
if (!frame || cdpTargetKey(frame.target) !== cdpTargetKey(target)) {
|
||||||
|
return geometryError("node frame does not belong to its target");
|
||||||
|
}
|
||||||
|
const error = await scrollFrameOwners(cdp, tabId, context, frameId);
|
||||||
if (error) return error;
|
if (error) return error;
|
||||||
} else if (target.sessionId) {
|
} else if (target.sessionId) {
|
||||||
return geometryError("an OOPIF node address requires frameId");
|
return geometryError("an OOPIF node address requires frameId");
|
||||||
@@ -216,8 +89,15 @@ export async function scrollElementAndFramesIntoView(
|
|||||||
backendNodeId: number,
|
backendNodeId: number,
|
||||||
frameId?: string,
|
frameId?: string,
|
||||||
): Promise<RpcError | null> {
|
): Promise<RpcError | null> {
|
||||||
const graph = frameId ? await loadFrameGraph(cdp, tabId) : null;
|
if (target.tabId !== tabId) return geometryError("node target belongs to another tab");
|
||||||
return scrollElementWithFrameGraph(cdp, tabId, target, backendNodeId, frameId, graph);
|
return scrollElementWithContext(
|
||||||
|
cdp,
|
||||||
|
tabId,
|
||||||
|
target,
|
||||||
|
backendNodeId,
|
||||||
|
frameId,
|
||||||
|
new GeometryContext(cdp, tabId),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function largestRegion(regions: Region): Polygon | null {
|
function largestRegion(regions: Region): Polygon | null {
|
||||||
@@ -240,23 +120,32 @@ export async function resolveNodeGeometry(
|
|||||||
if (address.target.sessionId && !address.frameId) {
|
if (address.target.sessionId && !address.frameId) {
|
||||||
return geometryError("an OOPIF node address requires frameId");
|
return geometryError("an OOPIF node address requires frameId");
|
||||||
}
|
}
|
||||||
const graph = address.frameId ? await loadFrameGraph(cdp, tabId) : null;
|
if (address.target.tabId !== tabId) return geometryError("node target belongs to another tab");
|
||||||
|
const context = new GeometryContext(cdp, tabId);
|
||||||
|
const graph = address.frameId ? await context.graph() : null;
|
||||||
|
if (address.frameId && graph) {
|
||||||
|
const frame = await context.frame(address.frameId);
|
||||||
|
if (!frame || cdpTargetKey(frame.target) !== cdpTargetKey(address.target)) {
|
||||||
|
return geometryError("node frame does not belong to its target");
|
||||||
|
}
|
||||||
|
}
|
||||||
if (address.frameId && !graph) {
|
if (address.frameId && !graph) {
|
||||||
return geometryError(`could not resolve frame graph for ${address.frameId}`);
|
return geometryError(`could not resolve frame graph for ${address.frameId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.scrollIntoView) {
|
if (options.scrollIntoView) {
|
||||||
const scrollError = await scrollElementWithFrameGraph(
|
const scrollError = await scrollElementWithContext(
|
||||||
cdp,
|
cdp,
|
||||||
tabId,
|
tabId,
|
||||||
address.target,
|
address.target,
|
||||||
address.backendNodeId,
|
address.backendNodeId,
|
||||||
address.frameId,
|
address.frameId,
|
||||||
graph,
|
context,
|
||||||
);
|
);
|
||||||
if (scrollError) return scrollError;
|
if (scrollError) return scrollError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Only topology was read above. Begin live measurements after scrolling.
|
||||||
const localRegion = await nodeContentRegion(
|
const localRegion = await nodeContentRegion(
|
||||||
cdpRunnerForTarget(cdp, address.target),
|
cdpRunnerForTarget(cdp, address.target),
|
||||||
tabId,
|
tabId,
|
||||||
@@ -267,7 +156,7 @@ export async function resolveNodeGeometry(
|
|||||||
let topVisibleRegions: Region;
|
let topVisibleRegions: Region;
|
||||||
let targetActionPoint: Point | null = null;
|
let targetActionPoint: Point | null = null;
|
||||||
if (address.frameId && graph) {
|
if (address.frameId && graph) {
|
||||||
const projection = await resolveFrameProjection(cdp, graph, address.frameId);
|
const projection = await context.targetProjection(address.frameId);
|
||||||
if (!projection)
|
if (!projection)
|
||||||
return geometryError(`could not resolve frame geometry for ${address.frameId}`);
|
return geometryError(`could not resolve frame geometry for ${address.frameId}`);
|
||||||
topVisibleRegions = projectRegionToViewport(localRegion, projection);
|
topVisibleRegions = projectRegionToViewport(localRegion, projection);
|
||||||
@@ -290,7 +179,7 @@ export async function resolveNodeGeometry(
|
|||||||
targetActionPoint = localActionRegion ? polygonCentroid(localActionRegion) : null;
|
targetActionPoint = localActionRegion ? polygonCentroid(localActionRegion) : null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const viewport = await targetViewport(cdp, address.target);
|
const viewport = await context.viewport(address.target);
|
||||||
if (!viewport) return geometryError("could not resolve top viewport geometry");
|
if (!viewport) return geometryError("could not resolve top viewport geometry");
|
||||||
topVisibleRegions = localRegion
|
topVisibleRegions = localRegion
|
||||||
.map((polygon) =>
|
.map((polygon) =>
|
||||||
@@ -309,7 +198,13 @@ export async function resolveNodeGeometry(
|
|||||||
return { code: "permission_denied", message: "element not visible in its target" };
|
return { code: "permission_denied", message: "element not visible in its target" };
|
||||||
}
|
}
|
||||||
targetActionPoint ??= actionPoint;
|
targetActionPoint ??= actionPoint;
|
||||||
return { topVisibleRegions, topBounds, actionPoint, targetActionPoint };
|
return {
|
||||||
|
topVisibleRegions,
|
||||||
|
topBounds,
|
||||||
|
actionPoint,
|
||||||
|
targetActionPoint,
|
||||||
|
topViewport: cssViewport(await context.layoutMetrics({ tabId })),
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return geometryError(error instanceof Error ? error.message : String(error));
|
return geometryError(error instanceof Error ? error.message : String(error));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -245,22 +245,3 @@ export function projectRectToViewport(
|
|||||||
if (!rect) return null;
|
if (!rect) return null;
|
||||||
return regionBounds(projectRegionToViewport([rectPolygon(rect)], projection));
|
return regionBounds(projectRegionToViewport([rectPolygon(rect)], projection));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function childFrameProjection(
|
|
||||||
parent: GeometryProjection,
|
|
||||||
ownerRectInParent: { x: number; y: number; w: number; h: number },
|
|
||||||
): GeometryProjection {
|
|
||||||
const destinationQuad = rectPolygon(ownerRectInParent);
|
|
||||||
return {
|
|
||||||
sourceClips: [],
|
|
||||||
edges: [
|
|
||||||
{
|
|
||||||
sourceViewport: { width: ownerRectInParent.w, height: ownerRectInParent.h },
|
|
||||||
destinationQuad,
|
|
||||||
destinationClips: parent.sourceClips,
|
|
||||||
},
|
|
||||||
...parent.edges,
|
|
||||||
],
|
|
||||||
topViewport: parent.topViewport,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
import { type CdpTarget, cdpTargetKey } from "@/browser-driver/frame-graph";
|
||||||
|
import type { GeometryProjection, ViewportRect } from "../geometry";
|
||||||
|
import { projectRectToViewport } from "../geometry";
|
||||||
|
|
||||||
|
export interface CoordinateOwner {
|
||||||
|
target: CdpTarget;
|
||||||
|
frameId?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FrameViewportRect {
|
||||||
|
space: "frame-viewport-css";
|
||||||
|
owner: CoordinateOwner;
|
||||||
|
rect: ViewportRect;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SnapshotProjection {
|
||||||
|
source: CoordinateOwner;
|
||||||
|
geometry: GeometryProjection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** DOMSnapshot bounds are document-relative CSS pixels, independent of raster DPR. */
|
||||||
|
export function snapshotViewportRect(
|
||||||
|
bounds: number[],
|
||||||
|
owner: CoordinateOwner,
|
||||||
|
scroll: { x: number; y: number },
|
||||||
|
): FrameViewportRect | null {
|
||||||
|
if (bounds.length < 4 || !bounds.slice(0, 4).every(Number.isFinite)) return null;
|
||||||
|
const [x, y, width, height] = bounds;
|
||||||
|
if (width <= 0 || height <= 0 || !Number.isFinite(scroll.x) || !Number.isFinite(scroll.y)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
space: "frame-viewport-css",
|
||||||
|
owner,
|
||||||
|
rect: { x: x - scroll.x, y: y - scroll.y, width, height },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function projectSnapshotRect(
|
||||||
|
input: FrameViewportRect,
|
||||||
|
projection: SnapshotProjection,
|
||||||
|
): ViewportRect | null {
|
||||||
|
if (
|
||||||
|
input.owner.frameId !== projection.source.frameId ||
|
||||||
|
cdpTargetKey(input.owner.target) !== cdpTargetKey(projection.source.target)
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const { x, y, width: w, height: h } = input.rect;
|
||||||
|
return projectRectToViewport({ x, y, w, h }, projection.geometry);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** CSS viewport metadata; browser zoom is distinct from raster devicePixelRatio. */
|
||||||
|
export interface CssViewport {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
scrollX: number;
|
||||||
|
scrollY: number;
|
||||||
|
cssToDip: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Page.captureScreenshot takes page-relative DIP, not viewport-relative CSS pixels. */
|
||||||
|
export function screenshotPageRect(
|
||||||
|
rect: ViewportRect,
|
||||||
|
viewport: CssViewport,
|
||||||
|
): { space: "page-dip"; rect: ViewportRect } | null {
|
||||||
|
const { cssToDip, scrollX, scrollY } = viewport;
|
||||||
|
if (
|
||||||
|
![rect.x, rect.y, rect.width, rect.height, cssToDip, scrollX, scrollY].every(Number.isFinite) ||
|
||||||
|
cssToDip <= 0 ||
|
||||||
|
rect.width <= 0 ||
|
||||||
|
rect.height <= 0
|
||||||
|
)
|
||||||
|
return null;
|
||||||
|
return {
|
||||||
|
space: "page-dip",
|
||||||
|
rect: {
|
||||||
|
x: (rect.x + scrollX) * cssToDip,
|
||||||
|
y: (rect.y + scrollY) * cssToDip,
|
||||||
|
width: rect.width * cssToDip,
|
||||||
|
height: rect.height * cssToDip,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,297 @@
|
|||||||
|
import {
|
||||||
|
type CdpFrame,
|
||||||
|
type CdpFrameGraph,
|
||||||
|
type CdpTarget,
|
||||||
|
cdpTargetKey,
|
||||||
|
} from "@/browser-driver/frame-graph";
|
||||||
|
import {
|
||||||
|
type GeometryProjection,
|
||||||
|
type Polygon,
|
||||||
|
type ProjectiveEdge,
|
||||||
|
parseCdpQuad,
|
||||||
|
type Quad,
|
||||||
|
type Size,
|
||||||
|
} from "../geometry";
|
||||||
|
import { type CdpRunner, sendToCdpTarget } from "../shared";
|
||||||
|
import type { CoordinateOwner, CssViewport, SnapshotProjection } from "./coordinate-types";
|
||||||
|
|
||||||
|
export interface LayoutMetrics {
|
||||||
|
cssVisualViewport?: { zoom?: number };
|
||||||
|
visualViewport?: { zoom?: number };
|
||||||
|
cssLayoutViewport?: {
|
||||||
|
clientWidth?: number;
|
||||||
|
clientHeight?: number;
|
||||||
|
pageX?: number;
|
||||||
|
pageY?: number;
|
||||||
|
};
|
||||||
|
layoutViewport?: { clientWidth?: number; clientHeight?: number; pageX?: number; pageY?: number };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The legacy field is a compatibility fallback, never a source of raster DPR. */
|
||||||
|
export function cssViewport(metrics: LayoutMetrics): CssViewport {
|
||||||
|
const value = metrics.cssLayoutViewport ?? metrics.layoutViewport;
|
||||||
|
return {
|
||||||
|
width: value?.clientWidth ?? 0,
|
||||||
|
height: value?.clientHeight ?? 0,
|
||||||
|
scrollX: value?.pageX ?? 0,
|
||||||
|
scrollY: value?.pageY ?? 0,
|
||||||
|
cssToDip: metrics.cssVisualViewport?.zoom ?? metrics.visualViewport?.zoom ?? 1,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One read-only measurement phase. Discard after scrolling or any later operation. */
|
||||||
|
export class GeometryContext {
|
||||||
|
private readonly metrics = new Map<string, Promise<LayoutMetrics>>();
|
||||||
|
private readonly owners = new Map<string, Promise<Quad | null>>();
|
||||||
|
private readonly projections = new Map<string, Promise<GeometryProjection | null>>();
|
||||||
|
private readonly snapshotOwners = new Map<string, Promise<{ quad: Quad; size: Size } | null>>();
|
||||||
|
private active = 0;
|
||||||
|
private readonly waiting: Array<() => void> = [];
|
||||||
|
private graphPromise?: Promise<CdpFrameGraph | null>;
|
||||||
|
private framesPromise?: Promise<Map<string, CdpFrame>>;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly cdp: CdpRunner,
|
||||||
|
private readonly tabId: number,
|
||||||
|
graph?: CdpFrameGraph,
|
||||||
|
private readonly signal?: AbortSignal,
|
||||||
|
) {
|
||||||
|
if (graph) this.graphPromise = Promise.resolve(graph);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async request<T>(
|
||||||
|
target: CdpTarget,
|
||||||
|
method: string,
|
||||||
|
params: object,
|
||||||
|
cleanup = false,
|
||||||
|
): Promise<T> {
|
||||||
|
if (!cleanup && this.signal?.aborted) throw new DOMException("geometry aborted", "AbortError");
|
||||||
|
if (this.active >= 4) await new Promise<void>((resolve) => this.waiting.push(resolve));
|
||||||
|
else this.active++;
|
||||||
|
try {
|
||||||
|
if (!cleanup && this.signal?.aborted)
|
||||||
|
throw new DOMException("geometry aborted", "AbortError");
|
||||||
|
return await sendToCdpTarget<T>(this.cdp, target, method, params);
|
||||||
|
} finally {
|
||||||
|
const next = this.waiting.shift();
|
||||||
|
if (next) next();
|
||||||
|
else this.active--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
graph(): Promise<CdpFrameGraph | null> {
|
||||||
|
this.graphPromise ??=
|
||||||
|
this.cdp.getFrameGraph?.(this.tabId).catch(() => null) ?? Promise.resolve(null);
|
||||||
|
return this.graphPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
private frames(): Promise<Map<string, CdpFrame>> {
|
||||||
|
this.framesPromise ??= this.graph().then(
|
||||||
|
(graph) => new Map(graph?.frames.map((frame) => [frame.frameId, frame]) ?? []),
|
||||||
|
);
|
||||||
|
return this.framesPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
async frame(frameId: string): Promise<CdpFrame | undefined> {
|
||||||
|
return (await this.frames()).get(frameId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Topology only: safe to resolve before the caller scrolls frame owners. */
|
||||||
|
async ancestry(frameId: string): Promise<CdpFrame[] | null> {
|
||||||
|
const frames = await this.frames();
|
||||||
|
const path: CdpFrame[] = [];
|
||||||
|
const seen = new Set<string>();
|
||||||
|
let current = frames.get(frameId);
|
||||||
|
if (!current) return null;
|
||||||
|
while (current.parentFrameId) {
|
||||||
|
if (seen.has(current.frameId)) return null;
|
||||||
|
seen.add(current.frameId);
|
||||||
|
path.push(current);
|
||||||
|
const parent = frames.get(current.parentFrameId);
|
||||||
|
if (!parent) return null;
|
||||||
|
current = parent;
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
layoutMetrics(target: CdpTarget): Promise<LayoutMetrics> {
|
||||||
|
const key = cdpTargetKey(target);
|
||||||
|
let promise = this.metrics.get(key);
|
||||||
|
if (!promise) {
|
||||||
|
promise = this.request<LayoutMetrics>(target, "Page.getLayoutMetrics", {});
|
||||||
|
this.metrics.set(key, promise);
|
||||||
|
}
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
async viewport(target: CdpTarget): Promise<Size | null> {
|
||||||
|
const { width, height } = cssViewport(await this.layoutMetrics(target));
|
||||||
|
return Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0
|
||||||
|
? { width, height }
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ownerContent(target: CdpTarget, backendNodeId: number): Promise<Quad | null> {
|
||||||
|
const key = `${cdpTargetKey(target)}:${backendNodeId}`;
|
||||||
|
let promise = this.owners.get(key);
|
||||||
|
if (!promise) {
|
||||||
|
promise = this.request<{ model?: { content?: number[] } }>(target, "DOM.getBoxModel", {
|
||||||
|
backendNodeId,
|
||||||
|
}).then((reply) => parseCdpQuad(reply.model?.content));
|
||||||
|
this.owners.set(key, promise);
|
||||||
|
}
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Same-target owner quads are already target-relative, including nested owners. */
|
||||||
|
async snapshotProjection(
|
||||||
|
source: CoordinateOwner,
|
||||||
|
ownerBackendNodeId: number,
|
||||||
|
ancestorClips: Polygon[],
|
||||||
|
viewport: Size,
|
||||||
|
): Promise<SnapshotProjection | null> {
|
||||||
|
const key = `${cdpTargetKey(source.target)}:${ownerBackendNodeId}`;
|
||||||
|
let promise = this.snapshotOwners.get(key);
|
||||||
|
if (!promise) {
|
||||||
|
promise = this.snapshotOwner(source.target, ownerBackendNodeId);
|
||||||
|
this.snapshotOwners.set(key, promise);
|
||||||
|
}
|
||||||
|
const owner = await promise;
|
||||||
|
return owner
|
||||||
|
? {
|
||||||
|
source,
|
||||||
|
geometry: {
|
||||||
|
sourceClips: [],
|
||||||
|
edges: [
|
||||||
|
{
|
||||||
|
sourceViewport: owner.size,
|
||||||
|
destinationQuad: owner.quad,
|
||||||
|
destinationClips: ancestorClips,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
topViewport: viewport,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async snapshotOwner(
|
||||||
|
target: CdpTarget,
|
||||||
|
backendNodeId: number,
|
||||||
|
): Promise<{ quad: Quad; size: Size } | null> {
|
||||||
|
const quad = await this.ownerContent(target, backendNodeId);
|
||||||
|
if (!quad) return null;
|
||||||
|
const resolved = await this.request<{ object?: { objectId?: string } }>(
|
||||||
|
target,
|
||||||
|
"DOM.resolveNode",
|
||||||
|
{ backendNodeId },
|
||||||
|
);
|
||||||
|
const objectId = resolved.object?.objectId;
|
||||||
|
if (!objectId) return null;
|
||||||
|
try {
|
||||||
|
const reply = await this.request<{ result?: { value?: Size }; exceptionDetails?: unknown }>(
|
||||||
|
target,
|
||||||
|
"Runtime.callFunctionOn",
|
||||||
|
{
|
||||||
|
objectId,
|
||||||
|
functionDeclaration: `function() {
|
||||||
|
if (!this.isConnected) return null;
|
||||||
|
const style = getComputedStyle(this);
|
||||||
|
return {
|
||||||
|
width: this.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight),
|
||||||
|
height: this.clientHeight - parseFloat(style.paddingTop) - parseFloat(style.paddingBottom)
|
||||||
|
};
|
||||||
|
}`,
|
||||||
|
returnByValue: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const size = reply.result?.value;
|
||||||
|
return !reply.exceptionDetails &&
|
||||||
|
size &&
|
||||||
|
Number.isFinite(size.width) &&
|
||||||
|
Number.isFinite(size.height) &&
|
||||||
|
size.width > 0 &&
|
||||||
|
size.height > 0
|
||||||
|
? { quad, size }
|
||||||
|
: null;
|
||||||
|
} finally {
|
||||||
|
// Cleanup must still run if cancellation arrived after resolving the object.
|
||||||
|
await this.request(target, "Runtime.releaseObject", { objectId }, true).catch(() => {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private targetRoot(frames: Map<string, CdpFrame>, frame: CdpFrame): CdpFrame | null {
|
||||||
|
const seen = new Set<string>();
|
||||||
|
let current = frame;
|
||||||
|
while (current.parentFrameId) {
|
||||||
|
if (seen.has(current.frameId)) return null;
|
||||||
|
seen.add(current.frameId);
|
||||||
|
const parent = frames.get(current.parentFrameId);
|
||||||
|
if (!parent) return null;
|
||||||
|
if (cdpTargetKey(parent.target) !== cdpTargetKey(current.target)) break;
|
||||||
|
current = parent;
|
||||||
|
}
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async clips(
|
||||||
|
frames: Map<string, CdpFrame>,
|
||||||
|
frame: CdpFrame,
|
||||||
|
root: CdpFrame,
|
||||||
|
): Promise<Polygon[] | null> {
|
||||||
|
const clips: Polygon[] = [];
|
||||||
|
let current = frame;
|
||||||
|
const seen = new Set<string>();
|
||||||
|
while (current.frameId !== root.frameId) {
|
||||||
|
if (seen.has(current.frameId) || !current.parentFrameId) return null;
|
||||||
|
seen.add(current.frameId);
|
||||||
|
const parent = frames.get(current.parentFrameId);
|
||||||
|
if (!parent || current.ownerBackendNodeId === undefined) return null;
|
||||||
|
const quad = await this.ownerContent(parent.target, current.ownerBackendNodeId);
|
||||||
|
if (!quad) return null;
|
||||||
|
clips.push(quad);
|
||||||
|
current = parent;
|
||||||
|
}
|
||||||
|
return clips;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Input is already in the frame's CDP target viewport, not its local viewport. */
|
||||||
|
targetProjection(frameId: string): Promise<GeometryProjection | null> {
|
||||||
|
let promise = this.projections.get(frameId);
|
||||||
|
if (!promise) {
|
||||||
|
promise = this.buildTargetProjection(frameId);
|
||||||
|
this.projections.set(frameId, promise);
|
||||||
|
}
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async buildTargetProjection(frameId: string): Promise<GeometryProjection | null> {
|
||||||
|
const frames = await this.frames();
|
||||||
|
const frame = frames.get(frameId);
|
||||||
|
if (!frame) return null;
|
||||||
|
let root = this.targetRoot(frames, frame);
|
||||||
|
if (!root) return null;
|
||||||
|
const sourceViewport = await this.viewport(root.target);
|
||||||
|
if (!sourceViewport) return null;
|
||||||
|
const sourceClips = await this.clips(frames, frame, root);
|
||||||
|
if (!sourceClips) return null;
|
||||||
|
const edges: ProjectiveEdge[] = [];
|
||||||
|
const seen = new Set<string>();
|
||||||
|
while (root.parentFrameId) {
|
||||||
|
if (seen.has(root.frameId)) return null;
|
||||||
|
seen.add(root.frameId);
|
||||||
|
const parent = frames.get(root.parentFrameId);
|
||||||
|
if (!parent || root.ownerBackendNodeId === undefined) return null;
|
||||||
|
const destinationQuad = await this.ownerContent(parent.target, root.ownerBackendNodeId);
|
||||||
|
const source = await this.viewport(root.target);
|
||||||
|
const parentRoot = this.targetRoot(frames, parent);
|
||||||
|
if (!destinationQuad || !source || !parentRoot) return null;
|
||||||
|
const destinationClips = await this.clips(frames, parent, parentRoot);
|
||||||
|
if (!destinationClips) return null;
|
||||||
|
edges.push({ sourceViewport: source, destinationQuad, destinationClips });
|
||||||
|
root = parentRoot;
|
||||||
|
}
|
||||||
|
const topViewport = await this.viewport(root.target);
|
||||||
|
return topViewport ? { sourceClips, edges, topViewport } : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import type { CapturedSceneInput } from "./vom/facts";
|
||||||
// Observation handlers — `tool.snapshot`, `tool.get_html`, `tool.screenshot`,
|
// Observation handlers — `tool.snapshot`, `tool.get_html`, `tool.screenshot`,
|
||||||
// and semantic `tool.observe` (design §7). Each handler resolves the target
|
// and semantic `tool.observe` (design §7). Each handler resolves the target
|
||||||
// tab (defaulting to the Agent Window's active tab when omitted) and
|
// tab (defaulting to the Agent Window's active tab when omitted) and
|
||||||
@@ -34,6 +35,7 @@ import type {
|
|||||||
import { attachDialogs, markDialogCursor } from "./dialogs";
|
import { attachDialogs, markDialogCursor } from "./dialogs";
|
||||||
import { rpcError } from "./errors";
|
import { rpcError } from "./errors";
|
||||||
import { resolveNodeGeometry } from "./frame-geometry";
|
import { resolveNodeGeometry } from "./frame-geometry";
|
||||||
|
import { screenshotPageRect } from "./geometry/coordinate-types";
|
||||||
import {
|
import {
|
||||||
type ChromeTabsApi,
|
type ChromeTabsApi,
|
||||||
enforceToolTargetScope,
|
enforceToolTargetScope,
|
||||||
@@ -47,15 +49,9 @@ import {
|
|||||||
type ToolEffect,
|
type ToolEffect,
|
||||||
} from "./shared";
|
} from "./shared";
|
||||||
import { resolveSnapshotRef } from "./snapshot-ref";
|
import { resolveSnapshotRef } from "./snapshot-ref";
|
||||||
import {
|
import { type CapturedNode, type CapturedSurfaceProbe, probeHoverSurfaces } from "./vom/capture";
|
||||||
type CapturedNode,
|
import { captureObservationFacts, semanticCapture } from "./vom/capture-coordinator";
|
||||||
type CapturedSurfaceProbe,
|
import type { FrameDocument as CapturedFrameDocument } from "./vom/frame-document";
|
||||||
type CapturedViewModel,
|
|
||||||
captureViewModel,
|
|
||||||
collectOverlayExcludedBackendIds,
|
|
||||||
probeHoverSurfaces,
|
|
||||||
} from "./vom/capture";
|
|
||||||
import { type CapturedFrameDocument, captureFrameData } from "./vom/frame-capture";
|
|
||||||
import { withOverlayBypass } from "./vom/hover-perception";
|
import { withOverlayBypass } from "./vom/hover-perception";
|
||||||
import { probeTooltipNames } from "./vom/name-enrichment";
|
import { probeTooltipNames } from "./vom/name-enrichment";
|
||||||
import {
|
import {
|
||||||
@@ -197,15 +193,14 @@ async function captureElementScreenshot(
|
|||||||
if (isRpcError(geometry)) return geometry;
|
if (isRpcError(geometry)) return geometry;
|
||||||
if (signal?.aborted) return cancelled("screenshot");
|
if (signal?.aborted) return cancelled("screenshot");
|
||||||
const rect = geometry.topBounds;
|
const rect = geometry.topBounds;
|
||||||
|
const clip = screenshotPageRect(rect, geometry.topViewport);
|
||||||
|
if (!clip) return { code: "cdp_failed", message: "invalid screenshot coordinate space" };
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const shot = await cdp.send<{ data?: string }>(tabId, "Page.captureScreenshot", {
|
const shot = await cdp.send<{ data?: string }>(tabId, "Page.captureScreenshot", {
|
||||||
format: "png",
|
format: "png",
|
||||||
clip: {
|
clip: {
|
||||||
x: rect.x,
|
...clip.rect,
|
||||||
y: rect.y,
|
|
||||||
width: rect.width,
|
|
||||||
height: rect.height,
|
|
||||||
scale: 1,
|
scale: 1,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -530,7 +525,7 @@ function buildActiveScopeBlocks(nodes: VomNode[], signals: VomNodeDomSignals): A
|
|||||||
|
|
||||||
function buildConditionalSurfaces(
|
function buildConditionalSurfaces(
|
||||||
nodes: VomNode[],
|
nodes: VomNode[],
|
||||||
captured: CapturedViewModel,
|
captured: CapturedSceneInput,
|
||||||
probes: CapturedSurfaceProbe[],
|
probes: CapturedSurfaceProbe[],
|
||||||
): CondSurface[] {
|
): CondSurface[] {
|
||||||
if (probes.length === 0) return [];
|
if (probes.length === 0) return [];
|
||||||
@@ -660,161 +655,9 @@ export interface BuildVomSceneOptions {
|
|||||||
|
|
||||||
export type VomFrameDocument = CapturedFrameDocument<CdpAxNode>;
|
export type VomFrameDocument = CapturedFrameDocument<CdpAxNode>;
|
||||||
|
|
||||||
function legacyFrameDocuments(
|
|
||||||
axNodes: CdpAxNode[],
|
|
||||||
captured: CapturedViewModel,
|
|
||||||
pageUrl?: string,
|
|
||||||
rootTarget: VomFrameDocument["target"] = { tabId: 0 },
|
|
||||||
): VomFrameDocument[] {
|
|
||||||
const rootFrameId = captured.rootFrameId ?? "root";
|
|
||||||
const documents: VomFrameDocument[] = [
|
|
||||||
{
|
|
||||||
frameId: rootFrameId,
|
|
||||||
contextScopeId: rootFrameId,
|
|
||||||
target: rootTarget,
|
|
||||||
...(pageUrl ? { url: pageUrl } : {}),
|
|
||||||
axNodes: [],
|
|
||||||
domNodes: captured.nodes.map((node) => ({ ...node, frameId: node.frameId ?? rootFrameId })),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
const pending = [...captured.iframeNodes.entries()];
|
|
||||||
let progress = true;
|
|
||||||
let nextSyntheticFrame = 1;
|
|
||||||
while (pending.length > 0 && progress) {
|
|
||||||
progress = false;
|
|
||||||
for (let index = pending.length - 1; index >= 0; index -= 1) {
|
|
||||||
const [ownerBackendNodeId, domNodes] = pending[index];
|
|
||||||
const parent = documents.find((document) =>
|
|
||||||
document.domNodes.some((node) => node.backendNodeId === ownerBackendNodeId),
|
|
||||||
);
|
|
||||||
if (!parent) continue;
|
|
||||||
const frameId =
|
|
||||||
domNodes.find((node) => node.frameId)?.frameId ?? `legacy-frame-${nextSyntheticFrame++}`;
|
|
||||||
documents.push({
|
|
||||||
frameId,
|
|
||||||
parentFrameId: parent.frameId,
|
|
||||||
ownerBackendNodeId,
|
|
||||||
contextScopeId: frameId,
|
|
||||||
target: parent.target,
|
|
||||||
axNodes: [],
|
|
||||||
domNodes: domNodes.map((node) => ({ ...node, frameId: node.frameId ?? frameId })),
|
|
||||||
});
|
|
||||||
pending.splice(index, 1);
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const documentByFrameId = new Map(documents.map((document) => [document.frameId, document]));
|
|
||||||
const backendFrame = new Map<number, string>();
|
|
||||||
const childFrameByOwner = new Map<number, string>();
|
|
||||||
for (const document of documents) {
|
|
||||||
if (document.ownerBackendNodeId !== undefined) {
|
|
||||||
childFrameByOwner.set(document.ownerBackendNodeId, document.frameId);
|
|
||||||
}
|
|
||||||
for (const node of document.domNodes) backendFrame.set(node.backendNodeId, document.frameId);
|
|
||||||
}
|
|
||||||
const axById = new Map(axNodes.map((node) => [node.nodeId, node]));
|
|
||||||
const ownership = new Map<string, string>();
|
|
||||||
const resolving = new Set<string>();
|
|
||||||
const frameForAx = (node: CdpAxNode): string => {
|
|
||||||
const cached = ownership.get(node.nodeId);
|
|
||||||
if (cached) return cached;
|
|
||||||
let frameId: string | undefined;
|
|
||||||
if (node.frameId && documentByFrameId.has(node.frameId)) frameId = node.frameId;
|
|
||||||
if (!frameId && typeof node.backendDOMNodeId === "number") {
|
|
||||||
frameId = backendFrame.get(node.backendDOMNodeId);
|
|
||||||
}
|
|
||||||
if (!frameId && node.parentId && !resolving.has(node.nodeId)) {
|
|
||||||
const parent = axById.get(node.parentId);
|
|
||||||
if (parent) {
|
|
||||||
frameId =
|
|
||||||
typeof parent.backendDOMNodeId === "number"
|
|
||||||
? childFrameByOwner.get(parent.backendDOMNodeId)
|
|
||||||
: undefined;
|
|
||||||
if (!frameId) {
|
|
||||||
resolving.add(node.nodeId);
|
|
||||||
frameId = frameForAx(parent);
|
|
||||||
resolving.delete(node.nodeId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
frameId ??= rootFrameId;
|
|
||||||
ownership.set(node.nodeId, frameId);
|
|
||||||
return frameId;
|
|
||||||
};
|
|
||||||
for (const node of axNodes) frameForAx(node);
|
|
||||||
for (const node of axNodes) {
|
|
||||||
const frameId = ownership.get(node.nodeId) ?? rootFrameId;
|
|
||||||
const document = documentByFrameId.get(frameId) ?? documents[0];
|
|
||||||
document.axNodes.push({
|
|
||||||
...node,
|
|
||||||
frameId,
|
|
||||||
...(node.parentId && ownership.get(node.parentId) === frameId
|
|
||||||
? { parentId: node.parentId }
|
|
||||||
: { parentId: undefined }),
|
|
||||||
...(node.childIds
|
|
||||||
? { childIds: node.childIds.filter((childId) => ownership.get(childId) === frameId) }
|
|
||||||
: {}),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return documents;
|
|
||||||
}
|
|
||||||
|
|
||||||
function withLegacyBackendIds(scene: VomScene): VomScene {
|
|
||||||
const used = new Set<number>();
|
|
||||||
const idMap = new Map<number, number>();
|
|
||||||
let nextVirtualId = -1;
|
|
||||||
for (const node of scene.nodes) {
|
|
||||||
const preferred = node.backendNodeId;
|
|
||||||
const id = preferred !== undefined && !used.has(preferred) ? preferred : nextVirtualId--;
|
|
||||||
used.add(id);
|
|
||||||
idMap.set(node.id, id);
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
...scene,
|
|
||||||
nodes: scene.nodes.map((node) => ({
|
|
||||||
...node,
|
|
||||||
id: idMap.get(node.id) as number,
|
|
||||||
parentId: node.parentId === null ? null : (idMap.get(node.parentId) ?? null),
|
|
||||||
...(node.domParentId !== undefined
|
|
||||||
? { domParentId: node.domParentId === null ? null : (idMap.get(node.domParentId) ?? null) }
|
|
||||||
: {}),
|
|
||||||
...(node.domAncestorIds
|
|
||||||
? { domAncestorIds: node.domAncestorIds.flatMap((id) => idMap.get(id) ?? []) }
|
|
||||||
: {}),
|
|
||||||
})),
|
|
||||||
...(scene.surfaces
|
|
||||||
? {
|
|
||||||
surfaces: scene.surfaces.map((surface) => ({
|
|
||||||
...surface,
|
|
||||||
triggerId: idMap.get(surface.triggerId) ?? surface.triggerId,
|
|
||||||
})),
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
...(scene.activeScopeBlocks
|
|
||||||
? {
|
|
||||||
activeScopeBlocks: scene.activeScopeBlocks.map((block) => ({
|
|
||||||
...block,
|
|
||||||
triggerId: idMap.get(block.triggerId) ?? block.triggerId,
|
|
||||||
})),
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildVomScene(
|
|
||||||
axNodes: CdpAxNode[],
|
|
||||||
captured: CapturedViewModel,
|
|
||||||
options: BuildVomSceneOptions = {},
|
|
||||||
): VomScene {
|
|
||||||
return withLegacyBackendIds(
|
|
||||||
buildFrameVomScene(legacyFrameDocuments(axNodes, captured, options.pageUrl), captured, options),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildFrameVomScene(
|
export function buildFrameVomScene(
|
||||||
documents: VomFrameDocument[],
|
documents: VomFrameDocument[],
|
||||||
captured: CapturedViewModel,
|
captured: CapturedSceneInput,
|
||||||
options: BuildVomSceneOptions = {},
|
options: BuildVomSceneOptions = {},
|
||||||
): VomScene {
|
): VomScene {
|
||||||
const scene = buildSemanticVomScene({
|
const scene = buildSemanticVomScene({
|
||||||
@@ -830,7 +673,7 @@ export function buildFrameVomScene(
|
|||||||
function attachCapturedSceneAnnotations(
|
function attachCapturedSceneAnnotations(
|
||||||
scene: VomScene,
|
scene: VomScene,
|
||||||
documents: VomFrameDocument[],
|
documents: VomFrameDocument[],
|
||||||
captured: CapturedViewModel,
|
captured: CapturedSceneInput,
|
||||||
surfaceProbes: CapturedSurfaceProbe[],
|
surfaceProbes: CapturedSurfaceProbe[],
|
||||||
): VomScene {
|
): VomScene {
|
||||||
const rootDocument = documents.find((document) => document.frameId === scene.rootFrameId);
|
const rootDocument = documents.find((document) => document.frameId === scene.rootFrameId);
|
||||||
@@ -980,51 +823,6 @@ export async function handleGetHtml(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function emptyCapturedViewModel(viewport = { width: 0, height: 0 }): CapturedViewModel {
|
|
||||||
return { viewport, nodes: [], iframeNodes: new Map(), excludedBackendNodeIds: new Set() };
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LayoutMetricsViewportReply {
|
|
||||||
cssLayoutViewport?: { clientWidth?: number; clientHeight?: number };
|
|
||||||
layoutViewport?: { clientWidth?: number; clientHeight?: number };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fallbackCapturedViewModel(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
tabId: number,
|
|
||||||
signal?: AbortSignal,
|
|
||||||
): Promise<CapturedViewModel> {
|
|
||||||
throwIfAborted(signal, "observation");
|
|
||||||
let viewport = { width: 0, height: 0 };
|
|
||||||
try {
|
|
||||||
const metrics = await cdp.send<LayoutMetricsViewportReply>(tabId, "Page.getLayoutMetrics", {});
|
|
||||||
throwIfAborted(signal, "observation");
|
|
||||||
const source = metrics.cssLayoutViewport ?? metrics.layoutViewport ?? {};
|
|
||||||
viewport = {
|
|
||||||
width: source.clientWidth ?? 0,
|
|
||||||
height: source.clientHeight ?? 0,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
if (isAbortError(error)) throw error;
|
|
||||||
}
|
|
||||||
const excludedBackendNodeIds = await collectOverlayExcludedBackendIds(cdp, tabId, signal);
|
|
||||||
throwIfAborted(signal, "observation");
|
|
||||||
return { ...emptyCapturedViewModel(viewport), excludedBackendNodeIds };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function captureForVom(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
tabId: number,
|
|
||||||
options: CaptureVomObservationOptions,
|
|
||||||
): Promise<CapturedViewModel> {
|
|
||||||
try {
|
|
||||||
return await captureViewModel(cdp, tabId, { signal: options.signal });
|
|
||||||
} catch (error) {
|
|
||||||
if (isAbortError(error)) throw error;
|
|
||||||
return fallbackCapturedViewModel(cdp, tabId, options.signal);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface HoverProbeOutcome {
|
export interface HoverProbeOutcome {
|
||||||
/** Whether any active hover was dispatched during this observation. */
|
/** Whether any active hover was dispatched during this observation. */
|
||||||
performed: boolean;
|
performed: boolean;
|
||||||
@@ -1057,7 +855,7 @@ const NO_HOVER_PROBES: HoverProbeOutcome = {
|
|||||||
async function runHoverProbes(
|
async function runHoverProbes(
|
||||||
cdp: CdpRunner,
|
cdp: CdpRunner,
|
||||||
tabId: number,
|
tabId: number,
|
||||||
captured: CapturedViewModel,
|
captured: CapturedSceneInput,
|
||||||
documents: VomFrameDocument[],
|
documents: VomFrameDocument[],
|
||||||
staticSemantics: ReturnType<typeof resolveSemanticGraph>,
|
staticSemantics: ReturnType<typeof resolveSemanticGraph>,
|
||||||
options: CaptureVomObservationOptions,
|
options: CaptureVomObservationOptions,
|
||||||
@@ -1096,14 +894,9 @@ export async function captureVomObservation(
|
|||||||
throwIfAborted(options.signal, "observation");
|
throwIfAborted(options.signal, "observation");
|
||||||
await cdp.ensureAttachedToUrl?.(tabId, url);
|
await cdp.ensureAttachedToUrl?.(tabId, url);
|
||||||
throwIfAborted(options.signal, "observation");
|
throwIfAborted(options.signal, "observation");
|
||||||
const captured = await captureForVom(cdp, tabId, options);
|
const facts = await captureObservationFacts<CdpAxNode>(cdp, tabId, options.signal);
|
||||||
|
const { captured, documents: normalizedDocuments } = semanticCapture(facts);
|
||||||
throwIfAborted(options.signal, "observation");
|
throwIfAborted(options.signal, "observation");
|
||||||
const documents = await captureFrameData<CdpAxNode>(cdp, tabId, captured, options.signal);
|
|
||||||
throwIfAborted(options.signal, "observation");
|
|
||||||
const normalizedDocuments =
|
|
||||||
documents.length === 1 && captured.iframeNodes.size > 0
|
|
||||||
? legacyFrameDocuments(documents[0].axNodes, captured, url, documents[0].target)
|
|
||||||
: documents;
|
|
||||||
const semanticGraph = buildSemanticGraph({
|
const semanticGraph = buildSemanticGraph({
|
||||||
documents: normalizedDocuments,
|
documents: normalizedDocuments,
|
||||||
viewport: captured.viewport,
|
viewport: captured.viewport,
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ export interface CdpRunner {
|
|||||||
sendToTarget?<T = unknown>(target: CdpTarget, method: string, params?: object): Promise<T>;
|
sendToTarget?<T = unknown>(target: CdpTarget, method: string, params?: object): Promise<T>;
|
||||||
detach?(tabId: number): Promise<void>;
|
detach?(tabId: number): Promise<void>;
|
||||||
getFrameGraph?(tabId: number): Promise<CdpFrameGraph>;
|
getFrameGraph?(tabId: number): Promise<CdpFrameGraph>;
|
||||||
|
getAttachmentId?(tabId: number): string | undefined;
|
||||||
ensureAttachedToUrl?(tabId: number, expectedUrl: string | undefined): Promise<void>;
|
ensureAttachedToUrl?(tabId: number, expectedUrl: string | undefined): Promise<void>;
|
||||||
trackSessionTab?(sessionId: string, tabId: number): void;
|
trackSessionTab?(sessionId: string, tabId: number): void;
|
||||||
releaseSessionTab?(sessionId: string, tabId: number): Promise<void>;
|
releaseSessionTab?(sessionId: string, tabId: number): Promise<void>;
|
||||||
|
|||||||
@@ -0,0 +1,471 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import type { CdpFrame, CdpTarget } from "@/browser-driver/frame-graph";
|
||||||
|
import { cdpTargetKey } from "@/browser-driver/frame-graph";
|
||||||
|
import { OVERLAY_HOST_MARKER_ATTR } from "@/lib/overlay-bridge";
|
||||||
|
import type { CdpRunner } from "../../shared";
|
||||||
|
import { captureObservationFacts, semanticCapture } from "../capture-coordinator";
|
||||||
|
import { buildSemanticGraph } from "../semantic-graph/build";
|
||||||
|
import { REQUESTED_STYLES } from "../snapshot";
|
||||||
|
|
||||||
|
function fixture(
|
||||||
|
options: {
|
||||||
|
frames?: CdpFrame[];
|
||||||
|
after?: Record<string, { loader?: string; element?: number; missing?: boolean }>;
|
||||||
|
fail?: string;
|
||||||
|
missingIdentity?: boolean;
|
||||||
|
omitDocument?: string;
|
||||||
|
attachmentChanged?: boolean;
|
||||||
|
overlay?: string;
|
||||||
|
} = {},
|
||||||
|
) {
|
||||||
|
const frames = options.frames ?? [
|
||||||
|
{ frameId: "main", loaderId: "main-loader", target: { tabId: 4 } },
|
||||||
|
{
|
||||||
|
frameId: "same",
|
||||||
|
parentFrameId: "main",
|
||||||
|
ownerBackendNodeId: 3,
|
||||||
|
loaderId: "same-loader",
|
||||||
|
target: { tabId: 4 },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
frameId: "nested",
|
||||||
|
parentFrameId: "same",
|
||||||
|
ownerBackendNodeId: 13,
|
||||||
|
loaderId: "nested-loader",
|
||||||
|
target: { tabId: 4 },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
frameId: "remote",
|
||||||
|
parentFrameId: "main",
|
||||||
|
ownerBackendNodeId: 4,
|
||||||
|
loaderId: "remote-loader",
|
||||||
|
target: { tabId: 4, sessionId: "remote" },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const elements = new Map(
|
||||||
|
frames.map((frame, i) => [frame.frameId, frame.target.sessionId ? 1 : i * 10 + 1]),
|
||||||
|
);
|
||||||
|
const contexts = new Map(frames.map((frame, i) => [i + 1, frame.frameId]));
|
||||||
|
const reads = new Map<string, number>();
|
||||||
|
const logs: Array<{ target: CdpTarget; method: string; params: Record<string, unknown> }> = [];
|
||||||
|
let snapshots = 0;
|
||||||
|
const targetCount = new Set(frames.map((frame) => cdpTargetKey(frame.target))).size;
|
||||||
|
const send = vi.fn(
|
||||||
|
async <T>(target: CdpTarget, method: string, params: object = {}): Promise<T> => {
|
||||||
|
logs.push({ target, method, params: params as Record<string, unknown> });
|
||||||
|
const args = params as Record<string, string | number>;
|
||||||
|
if (options.fail === `${target.sessionId ?? "main"}:${method}`)
|
||||||
|
throw new Error("fixture failure");
|
||||||
|
let result: unknown = {};
|
||||||
|
if (method === "Page.createIsolatedWorld")
|
||||||
|
result = {
|
||||||
|
executionContextId: frames.findIndex((frame) => frame.frameId === args.frameId) + 1,
|
||||||
|
};
|
||||||
|
if (method === "Runtime.evaluate")
|
||||||
|
result = { result: { objectId: contexts.get(Number(args.contextId)) } };
|
||||||
|
if (method === "DOM.describeNode") {
|
||||||
|
const id = String(args.objectId);
|
||||||
|
const count = reads.get(id) ?? 0;
|
||||||
|
reads.set(id, count + 1);
|
||||||
|
result = options.missingIdentity
|
||||||
|
? {}
|
||||||
|
: {
|
||||||
|
node: {
|
||||||
|
backendNodeId:
|
||||||
|
count > 0 ? (options.after?.[id]?.element ?? elements.get(id)) : elements.get(id),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (method === "Page.getFrameTree") {
|
||||||
|
const own = frames.filter(
|
||||||
|
(frame) =>
|
||||||
|
cdpTargetKey(frame.target) === cdpTargetKey(target) &&
|
||||||
|
!options.after?.[frame.frameId]?.missing,
|
||||||
|
);
|
||||||
|
const trees = new Map(
|
||||||
|
own.map((frame) => [
|
||||||
|
frame.frameId,
|
||||||
|
{
|
||||||
|
frame: {
|
||||||
|
id: frame.frameId,
|
||||||
|
loaderId: options.after?.[frame.frameId]?.loader ?? frame.loaderId,
|
||||||
|
},
|
||||||
|
childFrames: [] as unknown[],
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
for (const frame of own)
|
||||||
|
if (frame.parentFrameId && trees.has(frame.parentFrameId))
|
||||||
|
trees.get(frame.parentFrameId)!.childFrames.push(trees.get(frame.frameId));
|
||||||
|
result = { frameTree: trees.get(own[0]?.frameId) };
|
||||||
|
}
|
||||||
|
if (method === "Page.getLayoutMetrics")
|
||||||
|
result = { cssLayoutViewport: { clientWidth: 1000, clientHeight: 800 } };
|
||||||
|
if (method === "DOMSnapshot.captureSnapshot") {
|
||||||
|
snapshots++;
|
||||||
|
expect((params as { computedStyles: unknown }).computedStyles).toEqual(REQUESTED_STYLES);
|
||||||
|
result = {
|
||||||
|
strings: [
|
||||||
|
"#document",
|
||||||
|
"html",
|
||||||
|
"button",
|
||||||
|
OVERLAY_HOST_MARKER_ATTR,
|
||||||
|
"",
|
||||||
|
"visible",
|
||||||
|
"1",
|
||||||
|
"static",
|
||||||
|
"auto",
|
||||||
|
],
|
||||||
|
documents: frames
|
||||||
|
.filter(
|
||||||
|
(frame) =>
|
||||||
|
cdpTargetKey(frame.target) === cdpTargetKey(target) &&
|
||||||
|
frame.frameId !== options.omitDocument,
|
||||||
|
)
|
||||||
|
.map((frame) => {
|
||||||
|
const element = elements.get(frame.frameId)!;
|
||||||
|
return {
|
||||||
|
frameId: frame.frameId,
|
||||||
|
nodes: {
|
||||||
|
backendNodeId: [element - 1, element + 10000, element, element + 1],
|
||||||
|
nodeName: [0, 1, 1, 2],
|
||||||
|
nodeType: [9, 10, 1, 1],
|
||||||
|
parentIndex: [-1, 0, 0, 2],
|
||||||
|
attributes: [[], [], [], frame.frameId === options.overlay ? [3, 4] : []],
|
||||||
|
},
|
||||||
|
layout: {
|
||||||
|
nodeIndex: [2, 3],
|
||||||
|
bounds: [
|
||||||
|
[0, 0, 1000, 800],
|
||||||
|
[10, 20, 100, 40],
|
||||||
|
],
|
||||||
|
styles: [
|
||||||
|
[7, 8, 8, 5, 6],
|
||||||
|
[7, 8, 8, 5, 6],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (method === "Accessibility.getFullAXTree") {
|
||||||
|
const frameId = String(args.frameId);
|
||||||
|
result = {
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
nodeId: `${frameId}-button`,
|
||||||
|
frameId,
|
||||||
|
backendDOMNodeId: elements.get(frameId)! + 1,
|
||||||
|
role: { value: "button" },
|
||||||
|
name: { value: frameId },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return result as T;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const cdp: CdpRunner = {
|
||||||
|
getAttachmentId: () =>
|
||||||
|
options.attachmentChanged && snapshots === targetCount ? "new-attachment" : "attachment",
|
||||||
|
getFrameGraph: async () => ({ rootFrameId: frames[0].frameId, frames }),
|
||||||
|
send: (tabId, method, params) =>
|
||||||
|
(send as NonNullable<CdpRunner["sendToTarget"]>)({ tabId }, method, params),
|
||||||
|
sendToTarget: send as NonNullable<CdpRunner["sendToTarget"]>,
|
||||||
|
};
|
||||||
|
return { cdp, logs, elements };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("captureObservationFacts", () => {
|
||||||
|
it("collects each target once, validates every document and scopes equal backend IDs", async () => {
|
||||||
|
const { cdp, logs } = fixture();
|
||||||
|
const facts = await captureObservationFacts(cdp, 4);
|
||||||
|
expect(logs.filter((call) => call.method === "DOMSnapshot.captureSnapshot")).toHaveLength(2);
|
||||||
|
expect(logs.filter((call) => call.method === "Accessibility.enable")).toHaveLength(2);
|
||||||
|
expect(logs.filter((call) => call.method === "Accessibility.getFullAXTree")).toHaveLength(4);
|
||||||
|
expect(logs.filter((call) => call.method === "DOM.describeNode")).toHaveLength(8);
|
||||||
|
expect(logs.filter((call) => call.method === "Runtime.releaseObject")).toHaveLength(8);
|
||||||
|
expect(facts.documents.every((doc) => doc.identity)).toBe(true);
|
||||||
|
const main = facts.documents.find((doc) => doc.frame.frameId === "main")!;
|
||||||
|
const remote = facts.documents.find((doc) => doc.frame.frameId === "remote")!;
|
||||||
|
expect(main.index.nodes.get(2)).not.toBe(remote.index.nodes.get(2));
|
||||||
|
expect(main.index.children.get(1)).toEqual([2]);
|
||||||
|
expect(main.index.nodes.get(10001)?.nodeType).toBe(10);
|
||||||
|
expect(main.identity?.documentElementBackendNodeId).toBe(1);
|
||||||
|
expect(remote.axNodes[0].frameId).toBe("remote");
|
||||||
|
expect(main.index.nodes.get(2)).toBe(main.domNodes.find((node) => node.backendNodeId === 2));
|
||||||
|
expect(facts.finishedAt).toBeGreaterThanOrEqual(facts.startedAt);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ loader: "new-loader" },
|
||||||
|
{ element: 99 },
|
||||||
|
{ missing: true },
|
||||||
|
])("isolates changed child identity %j and dependent descendants", async (change) => {
|
||||||
|
const { cdp } = fixture({ after: { same: change } });
|
||||||
|
const facts = await captureObservationFacts(cdp, 4);
|
||||||
|
expect(facts.documents.map((doc) => doc.frame.frameId)).toEqual(["main", "remote"]);
|
||||||
|
expect(facts.issues).toContainEqual(
|
||||||
|
expect.objectContaining({ frameId: "same", reason: "document-changed" }),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ after: { main: { element: 99 } } },
|
||||||
|
{ attachmentChanged: true },
|
||||||
|
])("rejects a root replacement or reattachment %j", async (options) => {
|
||||||
|
await expect(captureObservationFacts(fixture(options).cdp, 4)).rejects.toThrow(
|
||||||
|
"document identity",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not retry failed or missing documents and retains valid AX-only semantics", async () => {
|
||||||
|
const { cdp, logs } = fixture({
|
||||||
|
fail: "remote:DOMSnapshot.captureSnapshot",
|
||||||
|
omitDocument: "same",
|
||||||
|
});
|
||||||
|
const facts = await captureObservationFacts(cdp, 4);
|
||||||
|
const remote = facts.documents.find((doc) => doc.frame.frameId === "remote")!;
|
||||||
|
expect(remote.domNodes).toHaveLength(0);
|
||||||
|
expect(remote.axNodes).toHaveLength(1);
|
||||||
|
expect(logs.filter((call) => call.method === "DOMSnapshot.captureSnapshot")).toHaveLength(2);
|
||||||
|
expect(facts.issues).toContainEqual(
|
||||||
|
expect.objectContaining({ frameId: "same", stage: "dom", reason: "capture-unavailable" }),
|
||||||
|
);
|
||||||
|
expect(facts.issues).toContainEqual(
|
||||||
|
expect.objectContaining({ target: { tabId: 4, sessionId: "remote" }, stage: "dom" }),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps missing identity explicit without manufacturing a verified document", async () => {
|
||||||
|
const facts = await captureObservationFacts(fixture({ missingIdentity: true }).cdp, 4);
|
||||||
|
expect(facts.documents.every((doc) => !doc.identity)).toBe(true);
|
||||||
|
expect(facts.issues.filter((issue) => issue.reason === "identity-unverified")).toHaveLength(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("excludes overlay AX in its own document without excluding equal IDs elsewhere", async () => {
|
||||||
|
const facts = await captureObservationFacts(fixture({ overlay: "remote" }).cdp, 4);
|
||||||
|
const { documents } = semanticCapture(facts);
|
||||||
|
const graph = buildSemanticGraph({
|
||||||
|
documents,
|
||||||
|
viewport: facts.viewport,
|
||||||
|
rootFrameId: facts.rootFrameId,
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
[...graph.nodes.values()].find(
|
||||||
|
(node) => node.frameId === "remote" && node.backendNodeId === 2,
|
||||||
|
)?.excluded,
|
||||||
|
).toBe(true);
|
||||||
|
expect(
|
||||||
|
[...graph.nodes.values()].find((node) => node.frameId === "main" && node.backendNodeId === 2)
|
||||||
|
?.excluded,
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("bounds collection across many targets and stops scheduling after cancellation", async () => {
|
||||||
|
const frames = Array.from({ length: 12 }, (_, i) => ({
|
||||||
|
frameId: `f${i}`,
|
||||||
|
target: { tabId: 4, ...(i ? { sessionId: `s${i}` } : {}) },
|
||||||
|
}));
|
||||||
|
const { cdp } = fixture({ frames });
|
||||||
|
let active = 0,
|
||||||
|
peak = 0;
|
||||||
|
const original = cdp.sendToTarget!;
|
||||||
|
const send: NonNullable<CdpRunner["sendToTarget"]> = async (target, method, params) => {
|
||||||
|
active++;
|
||||||
|
peak = Math.max(peak, active);
|
||||||
|
try {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1));
|
||||||
|
return await original(target, method, params);
|
||||||
|
} finally {
|
||||||
|
active--;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cdp.sendToTarget = send;
|
||||||
|
cdp.send = (tabId, method, params) => send({ tabId }, method, params);
|
||||||
|
await captureObservationFacts(cdp, 4);
|
||||||
|
// This fixture has no frame edges, so no concurrent owner requests.
|
||||||
|
expect(peak).toBeLessThanOrEqual(4);
|
||||||
|
expect(peak).toBeGreaterThan(1);
|
||||||
|
const controller = new AbortController();
|
||||||
|
controller.abort();
|
||||||
|
await expect(captureObservationFacts(cdp, 4, controller.signal)).rejects.toMatchObject({
|
||||||
|
name: "AbortError",
|
||||||
|
});
|
||||||
|
expect(active).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function childSnapshot(frameId: string, backendNodeId: number) {
|
||||||
|
const strings = [frameId, "body", "button", "static", "auto", "pointer"];
|
||||||
|
return {
|
||||||
|
strings,
|
||||||
|
documents: [
|
||||||
|
{
|
||||||
|
frameId,
|
||||||
|
nodes: {
|
||||||
|
parentIndex: [-1, 0],
|
||||||
|
nodeName: [1, 2],
|
||||||
|
backendNodeId: [backendNodeId - 1, backendNodeId],
|
||||||
|
attributes: [[], []],
|
||||||
|
},
|
||||||
|
layout: {
|
||||||
|
nodeIndex: [0, 1],
|
||||||
|
styles: [
|
||||||
|
[3, 4, 4],
|
||||||
|
[3, 4, 5],
|
||||||
|
],
|
||||||
|
bounds: [
|
||||||
|
[0, 0, 300, 200],
|
||||||
|
[10, 20, 100, 40],
|
||||||
|
],
|
||||||
|
paintOrders: [0, 1],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("OOPIF capture", () => {
|
||||||
|
it("captures and positions multiple OOPIF documents missing from the root snapshot", async () => {
|
||||||
|
const sendToTarget = vi.fn(async (target, method) => {
|
||||||
|
if (method === "Page.getLayoutMetrics") {
|
||||||
|
return { cssLayoutViewport: { clientWidth: 300, clientHeight: 200, pageX: 0, pageY: 0 } };
|
||||||
|
}
|
||||||
|
if (method === "DOMSnapshot.enable" || method === "Accessibility.enable") return {};
|
||||||
|
if (method === "DOMSnapshot.captureSnapshot") {
|
||||||
|
return target.sessionId === "left-session"
|
||||||
|
? childSnapshot("left", 101)
|
||||||
|
: childSnapshot("right", 201);
|
||||||
|
}
|
||||||
|
if (method === "Accessibility.getFullAXTree") return { nodes: [] };
|
||||||
|
throw new Error(`unexpected ${method}`);
|
||||||
|
});
|
||||||
|
const cdp: CdpRunner = {
|
||||||
|
send: vi.fn(async (_tabId, method, params) => {
|
||||||
|
if (method === "Accessibility.enable" || method === "DOMSnapshot.enable") return {};
|
||||||
|
if (method === "DOMSnapshot.captureSnapshot") return childSnapshot("main", 1);
|
||||||
|
if (method === "Accessibility.getFullAXTree") return { nodes: [] };
|
||||||
|
if (method === "DOM.getBoxModel") {
|
||||||
|
const backendNodeId = (params as { backendNodeId?: number })?.backendNodeId;
|
||||||
|
const x = backendNodeId === 10 ? 50 : 500;
|
||||||
|
return { model: { content: [x, 100, x + 300, 100, x + 300, 300, x, 300] } };
|
||||||
|
}
|
||||||
|
if (method === "Page.getLayoutMetrics") {
|
||||||
|
return { cssLayoutViewport: { clientWidth: 1000, clientHeight: 800 } };
|
||||||
|
}
|
||||||
|
throw new Error(`unexpected root ${method}`);
|
||||||
|
}) as CdpRunner["send"],
|
||||||
|
sendToTarget: sendToTarget as unknown as NonNullable<CdpRunner["sendToTarget"]>,
|
||||||
|
getFrameGraph: vi.fn(async () => ({
|
||||||
|
rootFrameId: "main",
|
||||||
|
frames: [
|
||||||
|
{ frameId: "main", target: { tabId: 4 } },
|
||||||
|
{
|
||||||
|
frameId: "left",
|
||||||
|
parentFrameId: "main",
|
||||||
|
ownerBackendNodeId: 10,
|
||||||
|
target: { tabId: 4, sessionId: "left-session" },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
frameId: "right",
|
||||||
|
parentFrameId: "main",
|
||||||
|
ownerBackendNodeId: 20,
|
||||||
|
target: { tabId: 4, sessionId: "right-session" },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
const { documents: trees } = semanticCapture(await captureObservationFacts(cdp, 4));
|
||||||
|
|
||||||
|
expect(trees.map((tree) => tree.frameId)).toEqual(["main", "left", "right"]);
|
||||||
|
expect(
|
||||||
|
trees
|
||||||
|
.find((doc) => doc.frameId === "left")
|
||||||
|
?.domNodes?.find((node) => node.backendNodeId === 101)?.rect,
|
||||||
|
).toEqual({ x: 60, y: 120, w: 100, h: 40 });
|
||||||
|
expect(
|
||||||
|
trees
|
||||||
|
.find((doc) => doc.frameId === "right")
|
||||||
|
?.domNodes?.find((node) => node.backendNodeId === 201)?.rect,
|
||||||
|
).toEqual({ x: 510, y: 120, w: 100, h: 40 });
|
||||||
|
expect(
|
||||||
|
new Map(
|
||||||
|
trees
|
||||||
|
.filter((doc) => doc.ownerBackendNodeId !== undefined)
|
||||||
|
.map((doc) => [doc.frameId, doc.ownerBackendNodeId]),
|
||||||
|
),
|
||||||
|
).toEqual(
|
||||||
|
new Map([
|
||||||
|
["left", 10],
|
||||||
|
["right", 20],
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
new Map(
|
||||||
|
trees.filter((doc) => doc.parentFrameId).map((doc) => [doc.frameId, doc.parentFrameId]),
|
||||||
|
),
|
||||||
|
).toEqual(
|
||||||
|
new Map([
|
||||||
|
["left", "main"],
|
||||||
|
["right", "main"],
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps OOPIF semantics when viewport projection is unavailable", async () => {
|
||||||
|
const cdp: CdpRunner = {
|
||||||
|
send: vi.fn(async (_tabId, method) => {
|
||||||
|
if (method === "Accessibility.enable" || method === "DOMSnapshot.enable") return {};
|
||||||
|
if (method === "DOMSnapshot.captureSnapshot") return childSnapshot("main", 1);
|
||||||
|
if (method === "Accessibility.getFullAXTree") return { nodes: [] };
|
||||||
|
if (method === "DOM.getBoxModel") throw new Error("owner geometry unavailable");
|
||||||
|
if (method === "Page.getLayoutMetrics") {
|
||||||
|
return { cssLayoutViewport: { clientWidth: 1000, clientHeight: 800 } };
|
||||||
|
}
|
||||||
|
throw new Error(`unexpected root ${method}`);
|
||||||
|
}) as CdpRunner["send"],
|
||||||
|
sendToTarget: vi.fn(async (_target, method) => {
|
||||||
|
if (method === "Page.getLayoutMetrics") throw new Error("viewport unavailable");
|
||||||
|
if (method === "DOMSnapshot.enable" || method === "Accessibility.enable") return {};
|
||||||
|
if (method === "DOMSnapshot.captureSnapshot") return childSnapshot("child", 101);
|
||||||
|
if (method === "Accessibility.getFullAXTree") {
|
||||||
|
return {
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
nodeId: "button",
|
||||||
|
backendDOMNodeId: 101,
|
||||||
|
role: { type: "role", value: "button" },
|
||||||
|
name: { type: "computedString", value: "Continue" },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
throw new Error(`unexpected ${method}`);
|
||||||
|
}) as unknown as NonNullable<CdpRunner["sendToTarget"]>,
|
||||||
|
getFrameGraph: vi.fn(async () => ({
|
||||||
|
rootFrameId: "main",
|
||||||
|
frames: [
|
||||||
|
{ frameId: "main", target: { tabId: 4 } },
|
||||||
|
{
|
||||||
|
frameId: "child",
|
||||||
|
parentFrameId: "main",
|
||||||
|
ownerBackendNodeId: 10,
|
||||||
|
target: { tabId: 4, sessionId: "child-session" },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
const { documents } = semanticCapture(await captureObservationFacts(cdp, 4));
|
||||||
|
const childDocument = documents.find((document) => document.frameId === "child");
|
||||||
|
|
||||||
|
expect(childDocument?.axNodes).toEqual([
|
||||||
|
expect.objectContaining({ backendDOMNodeId: 101, frameId: "child" }),
|
||||||
|
]);
|
||||||
|
expect(childDocument?.domNodes.find((node) => node.backendNodeId === 101)).toEqual(
|
||||||
|
expect.objectContaining({ rect: null, localRect: { x: 10, y: 20, w: 100, h: 40 } }),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { OVERLAY_HOST_MARKER_ATTR, OVERLAY_HOST_NAME } from "../../../lib/overlay-bridge";
|
import { OVERLAY_HOST_MARKER_ATTR, OVERLAY_HOST_NAME } from "../../../lib/overlay-bridge";
|
||||||
import { captureViewModel, collectOverlayExcludedBackendIds, probeHoverSurfaces } from "../capture";
|
import { collectOverlayExcludedBackendIds, probeHoverSurfaces } from "../capture";
|
||||||
|
import { captureObservationFacts, semanticCapture } from "../capture-coordinator";
|
||||||
|
|
||||||
// Minimal but format-accurate captureSnapshot reply: a body with one
|
// Minimal but format-accurate captureSnapshot reply: a body with one
|
||||||
// fixed full-screen overlay div carrying a password input.
|
// fixed full-screen overlay div carrying a password input.
|
||||||
@@ -26,6 +27,7 @@ function fakeSnapshotReply() {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 2],
|
parentIndex: [-1, 0, 1, 2],
|
||||||
nodeType: [1, 1, 1, 1],
|
nodeType: [1, 1, 1, 1],
|
||||||
@@ -73,6 +75,7 @@ function hoverTriggerSnapshotReply() {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1],
|
parentIndex: [-1, 0, 1],
|
||||||
nodeType: [1, 1, 1],
|
nodeType: [1, 1, 1],
|
||||||
@@ -117,6 +120,7 @@ function twoHoverTriggerSnapshotReply() {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 1],
|
parentIndex: [-1, 0, 1, 1],
|
||||||
nodeType: [1, 1, 1, 1],
|
nodeType: [1, 1, 1, 1],
|
||||||
@@ -170,6 +174,7 @@ function nestedHoverTriggerSnapshotReply() {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 2, 3],
|
parentIndex: [-1, 0, 1, 2, 3],
|
||||||
nodeType: [1, 1, 1, 1, 1],
|
nodeType: [1, 1, 1, 1, 1],
|
||||||
@@ -243,13 +248,11 @@ function makeCdp(snapshot: unknown) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("captureViewModel", () => {
|
describe("snapshot capture and hover", () => {
|
||||||
it("parses nodes, attrs, rects, paint order and styles", async () => {
|
it("parses nodes, attrs, rects, paint order and styles", async () => {
|
||||||
const { nodes, viewport, iframeNodes } = await captureViewModel(
|
const { nodes, viewport } = semanticCapture(
|
||||||
makeCdp(fakeSnapshotReply()),
|
await captureObservationFacts(makeCdp(fakeSnapshotReply()), 4),
|
||||||
4,
|
).captured;
|
||||||
);
|
|
||||||
expect(iframeNodes.size).toBe(0);
|
|
||||||
expect(viewport).toEqual({ width: 1000, height: 800 });
|
expect(viewport).toEqual({ width: 1000, height: 800 });
|
||||||
const div = nodes.find((n) => n.backendNodeId === 12);
|
const div = nodes.find((n) => n.backendNodeId === 12);
|
||||||
expect(div).toMatchObject({ tag: "div", position: "fixed", parentBackendNodeId: 11 });
|
expect(div).toMatchObject({ tag: "div", position: "fixed", parentBackendNodeId: 11 });
|
||||||
@@ -261,35 +264,11 @@ describe("captureViewModel", () => {
|
|||||||
|
|
||||||
it("never hovers the page while capturing", async () => {
|
it("never hovers the page while capturing", async () => {
|
||||||
const cdp = makeCdp(fakeSnapshotReply());
|
const cdp = makeCdp(fakeSnapshotReply());
|
||||||
await captureViewModel(cdp, 4);
|
semanticCapture(await captureObservationFacts(cdp, 4)).captured;
|
||||||
|
|
||||||
expect(cdp.send).not.toHaveBeenCalledWith(4, "Input.dispatchMouseEvent", expect.anything());
|
expect(cdp.send).not.toHaveBeenCalledWith(4, "Input.dispatchMouseEvent", expect.anything());
|
||||||
});
|
});
|
||||||
|
|
||||||
it("batch-enriches form controls without per-node object resolution", async () => {
|
|
||||||
const cdp = makeCdp(fakeSnapshotReply());
|
|
||||||
const result = await captureViewModel(cdp, 4);
|
|
||||||
const input = result.nodes.find((node) => node.backendNodeId === 13);
|
|
||||||
|
|
||||||
expect(input).toMatchObject({
|
|
||||||
formState: "filled",
|
|
||||||
formPlaceholder: "secret",
|
|
||||||
attrs: { type: "password" },
|
|
||||||
});
|
|
||||||
expect(input?.formValue).toBeUndefined();
|
|
||||||
expect(input?.formDefaultValue).toBeUndefined();
|
|
||||||
expect(cdp.send).toHaveBeenCalledWith(
|
|
||||||
4,
|
|
||||||
"Runtime.evaluate",
|
|
||||||
expect.objectContaining({
|
|
||||||
serializationOptions: expect.objectContaining({ serialization: "deep" }),
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
expect(cdp.send).toHaveBeenCalledWith(4, "Runtime.releaseObjectGroup", expect.anything());
|
|
||||||
expect(cdp.send).not.toHaveBeenCalledWith(4, "DOM.resolveNode", expect.anything());
|
|
||||||
expect(cdp.send).not.toHaveBeenCalledWith(4, "Runtime.callFunctionOn", expect.anything());
|
|
||||||
});
|
|
||||||
|
|
||||||
it("runs hover surface probes when explicitly enabled", async () => {
|
it("runs hover surface probes when explicitly enabled", async () => {
|
||||||
let hoverStateCalls = 0;
|
let hoverStateCalls = 0;
|
||||||
const cdp = {
|
const cdp = {
|
||||||
@@ -329,7 +308,7 @@ describe("captureViewModel", () => {
|
|||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
|
|
||||||
const captured = await captureViewModel(cdp, 4);
|
const captured = semanticCapture(await captureObservationFacts(cdp, 4)).captured;
|
||||||
const surfaceProbes = await probeHoverSurfaces(cdp, 4, captured.nodes);
|
const surfaceProbes = await probeHoverSurfaces(cdp, 4, captured.nodes);
|
||||||
|
|
||||||
expect(surfaceProbes).toEqual([
|
expect(surfaceProbes).toEqual([
|
||||||
@@ -389,7 +368,7 @@ describe("captureViewModel", () => {
|
|||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
|
|
||||||
const captured = await captureViewModel(cdp, 4);
|
const captured = semanticCapture(await captureObservationFacts(cdp, 4)).captured;
|
||||||
const surfaceProbes = await probeHoverSurfaces(cdp, 4, captured.nodes);
|
const surfaceProbes = await probeHoverSurfaces(cdp, 4, captured.nodes);
|
||||||
|
|
||||||
expect(surfaceProbes).toEqual([
|
expect(surfaceProbes).toEqual([
|
||||||
@@ -434,7 +413,7 @@ describe("captureViewModel", () => {
|
|||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
|
|
||||||
const captured = await captureViewModel(cdp, 4);
|
const captured = semanticCapture(await captureObservationFacts(cdp, 4)).captured;
|
||||||
await probeHoverSurfaces(cdp, 4, captured.nodes);
|
await probeHoverSurfaces(cdp, 4, captured.nodes);
|
||||||
|
|
||||||
expect(hoverMoves).toBe(1);
|
expect(hoverMoves).toBe(1);
|
||||||
@@ -466,6 +445,7 @@ describe("captureViewModel", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 0, 3, 4],
|
parentIndex: [-1, 0, 1, 0, 3, 4],
|
||||||
nodeName: [i("html"), i("body"), i("div"), i(OVERLAY_HOST_NAME), i("div"), i("button")],
|
nodeName: [i("html"), i("body"), i("div"), i(OVERLAY_HOST_NAME), i("div"), i("button")],
|
||||||
@@ -496,7 +476,7 @@ describe("captureViewModel", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const { nodes } = await captureViewModel(makeCdp(snapshot), 4);
|
const { nodes } = semanticCapture(await captureObservationFacts(makeCdp(snapshot), 4)).captured;
|
||||||
const ids = nodes.map((n) => n.backendNodeId);
|
const ids = nodes.map((n) => n.backendNodeId);
|
||||||
// Real page nodes survive.
|
// Real page nodes survive.
|
||||||
expect(ids).toContain(12);
|
expect(ids).toContain(12);
|
||||||
@@ -527,6 +507,7 @@ describe("captureViewModel", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 0, 3, 4],
|
parentIndex: [-1, 0, 1, 0, 3, 4],
|
||||||
nodeName: [i("html"), i("body"), i("div"), i(OVERLAY_HOST_NAME), i("div"), i("button")],
|
nodeName: [i("html"), i("body"), i("div"), i(OVERLAY_HOST_NAME), i("div"), i("button")],
|
||||||
@@ -557,7 +538,9 @@ describe("captureViewModel", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const { excludedBackendNodeIds } = await captureViewModel(makeCdp(snapshot), 4);
|
const { excludedBackendNodeIds } = semanticCapture(
|
||||||
|
await captureObservationFacts(makeCdp(snapshot), 4),
|
||||||
|
).captured;
|
||||||
expect(excludedBackendNodeIds).toEqual(new Set([13, 14, 15]));
|
expect(excludedBackendNodeIds).toEqual(new Set([13, 14, 15]));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -578,6 +561,7 @@ describe("captureViewModel", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1],
|
parentIndex: [-1, 0, 1],
|
||||||
nodeName: [i("html"), i("body"), i(OVERLAY_HOST_NAME)],
|
nodeName: [i("html"), i("body"), i(OVERLAY_HOST_NAME)],
|
||||||
@@ -600,7 +584,9 @@ describe("captureViewModel", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const { nodes, excludedBackendNodeIds } = await captureViewModel(makeCdp(snapshot), 4);
|
const { nodes, excludedBackendNodeIds } = semanticCapture(
|
||||||
|
await captureObservationFacts(makeCdp(snapshot), 4),
|
||||||
|
).captured;
|
||||||
expect(nodes.map((n) => n.backendNodeId)).not.toContain(13);
|
expect(nodes.map((n) => n.backendNodeId)).not.toContain(13);
|
||||||
expect(excludedBackendNodeIds).toEqual(new Set([13]));
|
expect(excludedBackendNodeIds).toEqual(new Set([13]));
|
||||||
});
|
});
|
||||||
@@ -623,6 +609,7 @@ describe("captureViewModel", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1],
|
parentIndex: [-1, 0, 1],
|
||||||
nodeName: [i("html"), i("body"), i("div")],
|
nodeName: [i("html"), i("body"), i("div")],
|
||||||
@@ -645,7 +632,9 @@ describe("captureViewModel", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const { nodes, excludedBackendNodeIds } = await captureViewModel(makeCdp(snapshot), 4);
|
const { nodes, excludedBackendNodeIds } = semanticCapture(
|
||||||
|
await captureObservationFacts(makeCdp(snapshot), 4),
|
||||||
|
).captured;
|
||||||
expect(nodes.map((n) => n.backendNodeId)).not.toContain(13);
|
expect(nodes.map((n) => n.backendNodeId)).not.toContain(13);
|
||||||
expect(excludedBackendNodeIds).toEqual(new Set([13]));
|
expect(excludedBackendNodeIds).toEqual(new Set([13]));
|
||||||
});
|
});
|
||||||
@@ -668,6 +657,7 @@ describe("captureViewModel", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1],
|
parentIndex: [-1, 0, 1],
|
||||||
nodeName: [i("html"), i("body"), i("div")],
|
nodeName: [i("html"), i("body"), i("div")],
|
||||||
@@ -690,7 +680,7 @@ describe("captureViewModel", () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
const { nodes } = await captureViewModel(makeCdp(snapshot), 4);
|
const { nodes } = semanticCapture(await captureObservationFacts(makeCdp(snapshot), 4)).captured;
|
||||||
expect(nodes.find((n) => n.backendNodeId === 12)?.cursor).toBe("pointer");
|
expect(nodes.find((n) => n.backendNodeId === 12)?.cursor).toBe("pointer");
|
||||||
expect(nodes.find((n) => n.backendNodeId === 11)?.cursor).toBe("auto");
|
expect(nodes.find((n) => n.backendNodeId === 11)?.cursor).toBe("auto");
|
||||||
});
|
});
|
||||||
@@ -702,6 +692,7 @@ describe("captureViewModel", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1, 1, 1],
|
parentIndex: [-1, 0, 1, 1, 1],
|
||||||
nodeName: [i("html"), i("body"), i("button"), i("button"), i("button")],
|
nodeName: [i("html"), i("body"), i("button"), i("button"), i("button")],
|
||||||
@@ -728,7 +719,7 @@ describe("captureViewModel", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const { nodes } = await captureViewModel(makeCdp(snapshot), 4);
|
const { nodes } = semanticCapture(await captureObservationFacts(makeCdp(snapshot), 4)).captured;
|
||||||
|
|
||||||
expect(nodes.find((node) => node.backendNodeId === 12)?.rendered).toBe(true);
|
expect(nodes.find((node) => node.backendNodeId === 12)?.rendered).toBe(true);
|
||||||
expect(nodes.find((node) => node.backendNodeId === 13)?.rendered).toBe(false);
|
expect(nodes.find((node) => node.backendNodeId === 13)?.rendered).toBe(false);
|
||||||
@@ -748,19 +739,20 @@ describe("captureViewModel", () => {
|
|||||||
throw new Error(method);
|
throw new Error(method);
|
||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
const { nodes } = await captureViewModel(cdp, 4);
|
const { nodes } = semanticCapture(await captureObservationFacts(cdp, 4)).captured;
|
||||||
const div = nodes.find((n) => n.backendNodeId === 12);
|
const div = nodes.find((n) => n.backendNodeId === 12);
|
||||||
expect(div?.localRect?.y).toBe(-200);
|
expect(div?.localRect?.y).toBe(-200);
|
||||||
expect(div?.rect).toMatchObject({ y: 0, h: 600 });
|
expect(div?.rect).toMatchObject({ y: 0, h: 600 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("normalizes device-pixel bounds by devicePixelRatio", async () => {
|
it("keeps CSS snapshot bounds independent of legacy metrics ratio", async () => {
|
||||||
const S = ["html", "body", "div", "position", "fixed", "static", "pointer-events", "auto"];
|
const S = ["html", "body", "div", "position", "fixed", "static", "pointer-events", "auto"];
|
||||||
const i = (s: string) => S.indexOf(s);
|
const i = (s: string) => S.indexOf(s);
|
||||||
const snapshot = {
|
const snapshot = {
|
||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1],
|
parentIndex: [-1, 0, 1],
|
||||||
nodeName: [i("html"), i("body"), i("div")],
|
nodeName: [i("html"), i("body"), i("div")],
|
||||||
@@ -795,13 +787,19 @@ describe("captureViewModel", () => {
|
|||||||
throw new Error(method);
|
throw new Error(method);
|
||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
const { nodes } = await captureViewModel(cdp, 4);
|
const { nodes } = semanticCapture(await captureObservationFacts(cdp, 4)).captured;
|
||||||
expect(nodes.find((n) => n.backendNodeId === 12)?.rect).toEqual({
|
expect(nodes.find((n) => n.backendNodeId === 12)?.rect).toEqual({
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
w: 1000,
|
w: 1000,
|
||||||
h: 800,
|
h: 800,
|
||||||
});
|
});
|
||||||
|
expect(nodes.find((n) => n.backendNodeId === 12)?.localRect).toEqual({
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
w: 2000,
|
||||||
|
h: 1600,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("extracts textContent from #text child nodes via nodeValue", async () => {
|
it("extracts textContent from #text child nodes via nodeValue", async () => {
|
||||||
@@ -825,6 +823,7 @@ describe("captureViewModel", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
scrollOffsetX: 0,
|
scrollOffsetX: 0,
|
||||||
scrollOffsetY: 0,
|
scrollOffsetY: 0,
|
||||||
nodes: {
|
nodes: {
|
||||||
@@ -863,7 +862,7 @@ describe("captureViewModel", () => {
|
|||||||
throw new Error(method);
|
throw new Error(method);
|
||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
const { nodes } = await captureViewModel(cdp, 4);
|
const { nodes } = semanticCapture(await captureObservationFacts(cdp, 4)).captured;
|
||||||
const btn = nodes.find((n) => n.backendNodeId === 12);
|
const btn = nodes.find((n) => n.backendNodeId === 12);
|
||||||
expect(btn?.textContent).toBe("登录");
|
expect(btn?.textContent).toBe("登录");
|
||||||
const link = nodes.find((n) => n.backendNodeId === 14);
|
const link = nodes.find((n) => n.backendNodeId === 14);
|
||||||
@@ -873,7 +872,7 @@ describe("captureViewModel", () => {
|
|||||||
expect(body?.textContent).toBeUndefined();
|
expect(body?.textContent).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("parses iframe sub-documents and returns iframeNodes keyed by iframe backendNodeId", async () => {
|
it("retains explicit iframe document ownership and projected nodes", async () => {
|
||||||
const S = [
|
const S = [
|
||||||
"html",
|
"html",
|
||||||
"body",
|
"body",
|
||||||
@@ -950,36 +949,33 @@ describe("captureViewModel", () => {
|
|||||||
cssLayoutViewport: { clientWidth: 1000, clientHeight: 800, pageX: 0, pageY: 0 },
|
cssLayoutViewport: { clientWidth: 1000, clientHeight: 800, pageX: 0, pageY: 0 },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (method === "DOM.getBoxModel")
|
||||||
|
return { model: { content: [100, 300, 900, 300, 900, 700, 100, 700] } };
|
||||||
|
if (method === "DOM.resolveNode") return { object: { objectId: "owner" } };
|
||||||
|
if (method === "Runtime.callFunctionOn")
|
||||||
|
return { result: { value: { width: 800, height: 400 } } };
|
||||||
|
if (method === "Runtime.releaseObject") return {};
|
||||||
throw new Error(method);
|
throw new Error(method);
|
||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
const {
|
const facts = await captureObservationFacts(cdp, 4);
|
||||||
nodes,
|
const root = facts.documents.find((doc) => doc.frame.frameId === "main-frame")!;
|
||||||
iframeNodes,
|
const child = facts.documents.find((doc) => doc.frame.frameId === "child-frame")!;
|
||||||
frameNodes,
|
expect(facts.documents).toHaveLength(2);
|
||||||
frameOwnerBackendNodeIds,
|
expect(root.domNodes.find((n) => n.backendNodeId === 13)?.tag).toBe("iframe");
|
||||||
frameParentIds,
|
const subNodes = child.domNodes;
|
||||||
rootFrameId,
|
|
||||||
} = await captureViewModel(cdp, 4);
|
|
||||||
// Main frame has 4 nodes; iframe is included
|
|
||||||
expect(nodes.find((n) => n.backendNodeId === 13)?.tag).toBe("iframe");
|
|
||||||
// iframeNodes keyed by the <iframe> element's backendNodeId=13
|
|
||||||
expect(iframeNodes.size).toBe(1);
|
|
||||||
expect(iframeNodes.has(13)).toBe(true);
|
|
||||||
const subNodes = iframeNodes.get(13)!;
|
|
||||||
const input = subNodes.find((n) => n.backendNodeId === 101);
|
const input = subNodes.find((n) => n.backendNodeId === 101);
|
||||||
expect(input?.tag).toBe("input");
|
expect(input?.tag).toBe("input");
|
||||||
expect(input?.attrs.type).toBe("text");
|
expect(input?.attrs.type).toBe("text");
|
||||||
expect(input?.ownerFrameBackendNodeId).toBe(13);
|
expect(input?.ownerFrameBackendNodeId).toBe(13);
|
||||||
expect(input?.localRect).toEqual({ x: 0, y: 0, w: 200, h: 40 });
|
expect(input?.localRect).toEqual({ x: 0, y: 0, w: 200, h: 40 });
|
||||||
expect(input?.rect).toEqual({ x: 100, y: 300, w: 200, h: 40 });
|
expect(input?.rect).toEqual({ x: 100, y: 300, w: 200, h: 40 });
|
||||||
expect(rootFrameId).toBe("main-frame");
|
expect(facts.rootFrameId).toBe("main-frame");
|
||||||
expect(frameNodes?.get("child-frame")).toBe(subNodes);
|
expect(child.frame.ownerBackendNodeId).toBe(13);
|
||||||
expect(frameOwnerBackendNodeIds?.get("child-frame")).toBe(13);
|
expect(child.frame.parentFrameId).toBe("main-frame");
|
||||||
expect(frameParentIds?.get("child-frame")).toBe("main-frame");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("captures iframe documents without requiring owner-frame geometry", async () => {
|
it("projects iframe documents independently of owner snapshot geometry", async () => {
|
||||||
const S = [
|
const S = [
|
||||||
"html",
|
"html",
|
||||||
"body",
|
"body",
|
||||||
@@ -997,6 +993,7 @@ describe("captureViewModel", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
scrollOffsetX: 0,
|
scrollOffsetX: 0,
|
||||||
scrollOffsetY: 0,
|
scrollOffsetY: 0,
|
||||||
nodes: {
|
nodes: {
|
||||||
@@ -1014,6 +1011,7 @@ describe("captureViewModel", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
frameId: "child",
|
||||||
scrollOffsetX: 0,
|
scrollOffsetX: 0,
|
||||||
scrollOffsetY: 0,
|
scrollOffsetY: 0,
|
||||||
nodes: {
|
nodes: {
|
||||||
@@ -1040,15 +1038,23 @@ describe("captureViewModel", () => {
|
|||||||
cssLayoutViewport: { clientWidth: 1000, clientHeight: 800, pageX: 0, pageY: 0 },
|
cssLayoutViewport: { clientWidth: 1000, clientHeight: 800, pageX: 0, pageY: 0 },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (method === "DOM.getBoxModel")
|
||||||
|
return { model: { content: [100, 300, 900, 300, 900, 700, 100, 700] } };
|
||||||
|
if (method === "DOM.resolveNode") return { object: { objectId: "owner" } };
|
||||||
|
if (method === "Runtime.callFunctionOn")
|
||||||
|
return { result: { value: { width: 800, height: 400 } } };
|
||||||
|
if (method === "Runtime.releaseObject") return {};
|
||||||
throw new Error(method);
|
throw new Error(method);
|
||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
|
|
||||||
const { iframeNodes } = await captureViewModel(cdp, 4);
|
const { documents } = await captureObservationFacts(cdp, 4);
|
||||||
const input = iframeNodes.get(13)?.find((n) => n.backendNodeId === 101);
|
const input = documents
|
||||||
|
.find((doc) => doc.frame.frameId === "child")
|
||||||
|
?.domNodes?.find((n) => n.backendNodeId === 101);
|
||||||
|
|
||||||
expect(input?.localRect).toEqual({ x: 20, y: 20, w: 120, h: 60 });
|
expect(input?.localRect).toEqual({ x: 20, y: 20, w: 120, h: 60 });
|
||||||
expect(input?.rect).toBeNull();
|
expect(input?.rect).toEqual({ x: 120, y: 320, w: 120, h: 60 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("recursively normalizes nested iframe sub-documents", async () => {
|
it("recursively normalizes nested iframe sub-documents", async () => {
|
||||||
@@ -1069,6 +1075,7 @@ describe("captureViewModel", () => {
|
|||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1],
|
parentIndex: [-1, 0, 1],
|
||||||
nodeName: [i("html"), i("body"), i("iframe")],
|
nodeName: [i("html"), i("body"), i("iframe")],
|
||||||
@@ -1090,6 +1097,7 @@ describe("captureViewModel", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
frameId: "child",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1],
|
parentIndex: [-1, 0, 1],
|
||||||
nodeName: [i("html"), i("body"), i("iframe")],
|
nodeName: [i("html"), i("body"), i("iframe")],
|
||||||
@@ -1111,6 +1119,7 @@ describe("captureViewModel", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
frameId: "nested",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0],
|
parentIndex: [-1, 0],
|
||||||
nodeName: [i("body"), i("input")],
|
nodeName: [i("body"), i("input")],
|
||||||
@@ -1127,21 +1136,52 @@ describe("captureViewModel", () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
const cdp = {
|
const cdp = {
|
||||||
send: vi.fn(async (_t: number, method: string) => {
|
send: vi.fn(
|
||||||
if (method === "DOMSnapshot.enable") return {};
|
async (
|
||||||
if (method === "DOMSnapshot.captureSnapshot") return snapshot;
|
_t: number,
|
||||||
if (method === "Page.getLayoutMetrics") {
|
method: string,
|
||||||
return {
|
params?: { backendNodeId?: number; objectId?: string },
|
||||||
cssLayoutViewport: { clientWidth: 1000, clientHeight: 800, pageX: 0, pageY: 0 },
|
) => {
|
||||||
};
|
if (method === "DOMSnapshot.enable") return {};
|
||||||
}
|
if (method === "DOMSnapshot.captureSnapshot") return snapshot;
|
||||||
throw new Error(method);
|
if (method === "Page.getLayoutMetrics") {
|
||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
return {
|
||||||
|
cssLayoutViewport: { clientWidth: 1000, clientHeight: 800, pageX: 0, pageY: 0 },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (method === "DOM.getBoxModel")
|
||||||
|
return {
|
||||||
|
model: {
|
||||||
|
content:
|
||||||
|
params?.backendNodeId === 13
|
||||||
|
? [10, 20, 310, 20, 310, 220, 10, 220]
|
||||||
|
: [15, 26, 115, 26, 115, 106, 15, 106],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (method === "DOM.resolveNode")
|
||||||
|
return { object: { objectId: String(params?.backendNodeId) } };
|
||||||
|
if (method === "Runtime.callFunctionOn")
|
||||||
|
return {
|
||||||
|
result: {
|
||||||
|
value:
|
||||||
|
params?.objectId === "13"
|
||||||
|
? { width: 300, height: 200 }
|
||||||
|
: { width: 100, height: 80 },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (method === "Runtime.releaseObject") return {};
|
||||||
|
throw new Error(method);
|
||||||
|
},
|
||||||
|
) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
|
|
||||||
const { iframeNodes } = await captureViewModel(cdp, 4);
|
const { documents } = await captureObservationFacts(cdp, 4);
|
||||||
const nestedIframe = iframeNodes.get(13)?.find((n) => n.backendNodeId === 23);
|
const nestedIframe = documents
|
||||||
const input = iframeNodes.get(23)?.find((n) => n.backendNodeId === 31);
|
.find((doc) => doc.frame.frameId === "child")
|
||||||
|
?.domNodes?.find((n) => n.backendNodeId === 23);
|
||||||
|
const input = documents
|
||||||
|
.find((doc) => doc.frame.frameId === "nested")
|
||||||
|
?.domNodes?.find((n) => n.backendNodeId === 31);
|
||||||
|
|
||||||
expect(nestedIframe?.localRect).toEqual({ x: 5, y: 6, w: 100, h: 80 });
|
expect(nestedIframe?.localRect).toEqual({ x: 5, y: 6, w: 100, h: 80 });
|
||||||
expect(nestedIframe?.rect).toEqual({ x: 15, y: 26, w: 100, h: 80 });
|
expect(nestedIframe?.rect).toEqual({ x: 15, y: 26, w: 100, h: 80 });
|
||||||
@@ -1150,13 +1190,14 @@ describe("captureViewModel", () => {
|
|||||||
expect(input?.rect).toEqual({ x: 16, y: 28, w: 40, h: 20 });
|
expect(input?.rect).toEqual({ x: 16, y: 28, w: 40, h: 20 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("normalizes bounds then subtracts CSS scroll at dpr>1", async () => {
|
it("subtracts CSS scroll once without dividing snapshot bounds by a metrics ratio", async () => {
|
||||||
const S = ["html", "body", "div", "position", "fixed", "static", "pointer-events", "auto"];
|
const S = ["html", "body", "div", "position", "fixed", "static", "pointer-events", "auto"];
|
||||||
const i = (s: string) => S.indexOf(s);
|
const i = (s: string) => S.indexOf(s);
|
||||||
const snapshot = {
|
const snapshot = {
|
||||||
strings: S,
|
strings: S,
|
||||||
documents: [
|
documents: [
|
||||||
{
|
{
|
||||||
|
frameId: "root",
|
||||||
nodes: {
|
nodes: {
|
||||||
parentIndex: [-1, 0, 1],
|
parentIndex: [-1, 0, 1],
|
||||||
nodeName: [i("html"), i("body"), i("div")],
|
nodeName: [i("html"), i("body"), i("div")],
|
||||||
@@ -1191,8 +1232,8 @@ describe("captureViewModel", () => {
|
|||||||
throw new Error(method);
|
throw new Error(method);
|
||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
const { nodes } = await captureViewModel(cdp, 4);
|
const { nodes } = semanticCapture(await captureObservationFacts(cdp, 4)).captured;
|
||||||
expect(nodes.find((n) => n.backendNodeId === 12)?.rect?.y).toBe(400 / 2 - 100);
|
expect(nodes.find((n) => n.backendNodeId === 12)?.rect?.y).toBe(400 - 100);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("collectOverlayExcludedBackendIds walks the pierced overlay host subtree", async () => {
|
it("collectOverlayExcludedBackendIds walks the pierced overlay host subtree", async () => {
|
||||||
|
|||||||
@@ -0,0 +1,156 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { OVERLAY_HOST_MARKER_ATTR } from "@/lib/overlay-bridge";
|
||||||
|
import { buildDocumentIndex, type DecodedNode } from "../facts";
|
||||||
|
import { decodeDocument, REQUESTED_STYLES } from "../snapshot";
|
||||||
|
|
||||||
|
function node(id: number, parent: number | null): DecodedNode {
|
||||||
|
return {
|
||||||
|
backendNodeId: id,
|
||||||
|
parentBackendNodeId: parent,
|
||||||
|
tag: "div",
|
||||||
|
attrs: {},
|
||||||
|
paintOrder: 0,
|
||||||
|
position: "static",
|
||||||
|
pointerEvents: "auto",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("document facts", () => {
|
||||||
|
it.each([
|
||||||
|
1_000, 10_000, 100_000,
|
||||||
|
])("indexes deep and wide inputs of %i without copying ancestors", async (count) => {
|
||||||
|
for (const wide of [false, true]) {
|
||||||
|
let reads = 0;
|
||||||
|
const input = Array.from({ length: count }, (_, i) => {
|
||||||
|
const item = node(i, i ? (wide ? 0 : i - 1) : null);
|
||||||
|
Object.defineProperty(item, "parentBackendNodeId", {
|
||||||
|
get: () => {
|
||||||
|
reads++;
|
||||||
|
return i ? (wide ? 0 : i - 1) : null;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return item;
|
||||||
|
}).reverse(); // parent need not precede child
|
||||||
|
const index = await buildDocumentIndex(input);
|
||||||
|
expect(index.nodes.size).toBe(count);
|
||||||
|
expect(index.ancestry.get(count - 1)).toEqual({ complete: true, overlay: false });
|
||||||
|
expect(index.children.get(0)?.length).toBe(wide ? count - 1 : 1);
|
||||||
|
expect(reads).toBeLessThanOrEqual(count * 8);
|
||||||
|
expect(index.nodes.get(count - 1)).toBe(input[0]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("marks cycles and orphans incomplete and propagates overlay through shadow roots", async () => {
|
||||||
|
const host = { ...node(1, null), attrs: { [OVERLAY_HOST_MARKER_ATTR]: "" } };
|
||||||
|
const input = [
|
||||||
|
node(3, 2),
|
||||||
|
{ ...node(2, 1), tag: "#document-fragment" },
|
||||||
|
host,
|
||||||
|
node(4, 99),
|
||||||
|
node(5, 6),
|
||||||
|
node(6, 5),
|
||||||
|
node(7, 6),
|
||||||
|
];
|
||||||
|
const index = await buildDocumentIndex(input);
|
||||||
|
expect([...index.excludedBackendNodeIds].sort()).toEqual([1, 2, 3]);
|
||||||
|
for (const id of [4, 5, 6, 7]) expect(index.ancestry.get(id)?.complete).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not propagate a descendant overlay backwards into a cycle", async () => {
|
||||||
|
const overlay = { ...node(3, 1), attrs: { [OVERLAY_HOST_MARKER_ATTR]: "" } };
|
||||||
|
const index = await buildDocumentIndex([overlay, node(1, 2), node(2, 1)]);
|
||||||
|
expect([...index.excludedBackendNodeIds]).toEqual([3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not treat a missing snapshot parent as a complete root", async () => {
|
||||||
|
const { nodes } = await decodeDocument(
|
||||||
|
{ nodes: { backendNodeId: [1], nodeName: [0], parentIndex: [99] } },
|
||||||
|
["div"],
|
||||||
|
);
|
||||||
|
const index = await buildDocumentIndex(nodes);
|
||||||
|
expect(index.ancestry.get(1)?.complete).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("retains raw box units and independent style/interaction evidence", async () => {
|
||||||
|
const strings = [
|
||||||
|
"html",
|
||||||
|
"canvas",
|
||||||
|
"aria-hidden",
|
||||||
|
"true",
|
||||||
|
"inert",
|
||||||
|
"",
|
||||||
|
"hidden",
|
||||||
|
"visible",
|
||||||
|
"0",
|
||||||
|
"auto",
|
||||||
|
"scroll",
|
||||||
|
"matrix(2,0,0,2,0,0)",
|
||||||
|
];
|
||||||
|
const styles = REQUESTED_STYLES.map((style) =>
|
||||||
|
style === "visibility"
|
||||||
|
? 7
|
||||||
|
: style === "opacity"
|
||||||
|
? 8
|
||||||
|
: style === "overflow-x"
|
||||||
|
? 10
|
||||||
|
: style === "transform"
|
||||||
|
? 11
|
||||||
|
: 9,
|
||||||
|
);
|
||||||
|
const { nodes } = await decodeDocument(
|
||||||
|
{
|
||||||
|
nodes: {
|
||||||
|
backendNodeId: [1, 2],
|
||||||
|
parentIndex: [-1, 0],
|
||||||
|
nodeName: [0, 1],
|
||||||
|
attributes: [[2, 3, 4, 5], []],
|
||||||
|
},
|
||||||
|
layout: {
|
||||||
|
nodeIndex: [0, 1],
|
||||||
|
bounds: [
|
||||||
|
[0, 0, 200, 100],
|
||||||
|
[10, 20, 120, 40],
|
||||||
|
],
|
||||||
|
clientRects: [
|
||||||
|
[5, 5, 90, 40],
|
||||||
|
[0, 0, 120, 40],
|
||||||
|
],
|
||||||
|
styles: [styles, styles],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
strings,
|
||||||
|
);
|
||||||
|
expect(nodes[0].attrs).toEqual({ "aria-hidden": "true", inert: "" });
|
||||||
|
expect(nodes[0].layout).toMatchObject({
|
||||||
|
boundsSpace: "snapshot-document-css",
|
||||||
|
clientSpace: "unscaled-client-offset-css",
|
||||||
|
clientRect: [5, 5, 90, 40],
|
||||||
|
styles: {
|
||||||
|
visibility: "visible",
|
||||||
|
opacity: "0",
|
||||||
|
"overflow-x": "scroll",
|
||||||
|
transform: "matrix(2,0,0,2,0,0)",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(nodes[0]).not.toHaveProperty("rect");
|
||||||
|
expect(nodes[0]).not.toHaveProperty("excluded");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("yields during large inputs so cancellation interrupts indexing", async () => {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const input = Array.from({ length: 100_000 }, (_, i) => node(i, i ? i - 1 : null));
|
||||||
|
let reads = 0;
|
||||||
|
for (const item of input)
|
||||||
|
Object.defineProperty(item, "attrs", {
|
||||||
|
get: () => {
|
||||||
|
reads++;
|
||||||
|
if (reads === 100) controller.abort();
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await expect(buildDocumentIndex(input, controller.signal)).rejects.toMatchObject({
|
||||||
|
name: "AbortError",
|
||||||
|
});
|
||||||
|
expect(reads).toBeLessThanOrEqual(356);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { type CdpRunner, cdpRunnerForTarget } from "../../shared";
|
import { type CdpRunner, cdpRunnerForTarget } from "../../shared";
|
||||||
import { captureViewModel } from "../capture";
|
import { captureObservationFacts } from "../capture-coordinator";
|
||||||
|
|
||||||
interface Control {
|
interface Control {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -106,17 +106,19 @@ describe("form state identity", () => {
|
|||||||
[11, state("alice@example.com")],
|
[11, state("alice@example.com")],
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
const captured = await captureViewModel(cdp, 7);
|
const captured = await captureObservationFacts(cdp, 7);
|
||||||
expect(captured.nodes.find((n) => n.backendNodeId === 10)).toMatchObject({
|
expect(captured.documents[0].domNodes.find((n) => n.backendNodeId === 10)).toMatchObject({
|
||||||
formValue: "",
|
formValue: "",
|
||||||
formState: "empty",
|
formState: "empty",
|
||||||
formPlaceholder: "snapshot placeholder",
|
formPlaceholder: "snapshot placeholder",
|
||||||
});
|
});
|
||||||
expect(captured.nodes.find((n) => n.backendNodeId === 11)).toMatchObject({
|
expect(captured.documents[0].domNodes.find((n) => n.backendNodeId === 11)).toMatchObject({
|
||||||
formValue: "alice@example.com",
|
formValue: "alice@example.com",
|
||||||
});
|
});
|
||||||
expect(captured.nodes.find((n) => n.backendNodeId === 12)).toMatchObject({ formValue: "上海" });
|
expect(captured.documents[0].domNodes.find((n) => n.backendNodeId === 12)).toMatchObject({
|
||||||
expect(captured.nodes.some((n) => n.backendNodeId === 99)).toBe(false);
|
formValue: "上海",
|
||||||
|
});
|
||||||
|
expect(captured.documents[0].domNodes.some((n) => n.backendNodeId === 99)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not shift state between frames when one frame is absent from the runtime batch", async () => {
|
it("does not shift state between frames when one frame is absent from the runtime batch", async () => {
|
||||||
@@ -133,8 +135,10 @@ describe("form state identity", () => {
|
|||||||
[21, state("second frame")],
|
[21, state("second frame")],
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
const captured = await captureViewModel(cdp, 7);
|
const captured = await captureObservationFacts(cdp, 7);
|
||||||
const controls = [...captured.iframeNodes.values()].flat();
|
const controls = captured.documents
|
||||||
|
.filter((doc) => doc.frame.parentFrameId)
|
||||||
|
.flatMap((doc) => doc.domNodes);
|
||||||
expect(controls.find((n) => n.backendNodeId === 11)?.formValue).toBe("inaccessible frame");
|
expect(controls.find((n) => n.backendNodeId === 11)?.formValue).toBe("inaccessible frame");
|
||||||
expect(controls.find((n) => n.backendNodeId === 21)?.formValue).toBe("second frame");
|
expect(controls.find((n) => n.backendNodeId === 21)?.formValue).toBe("second frame");
|
||||||
expect(controls.find((n) => n.backendNodeId === 31)?.formValue).toBe("third frame");
|
expect(controls.find((n) => n.backendNodeId === 31)?.formValue).toBe("third frame");
|
||||||
@@ -151,8 +155,13 @@ describe("form state identity", () => {
|
|||||||
});
|
});
|
||||||
const cdp = { send, sendToTarget } as unknown as CdpRunner;
|
const cdp = { send, sendToTarget } as unknown as CdpRunner;
|
||||||
for (const sessionId of ["left-frame", "right-frame"]) {
|
for (const sessionId of ["left-frame", "right-frame"]) {
|
||||||
const captured = await captureViewModel(cdpRunnerForTarget(cdp, { tabId: 7, sessionId }), 7);
|
const captured = await captureObservationFacts(
|
||||||
expect(captured.nodes.find((n) => n.backendNodeId === 11)?.formValue).toBe(sessionId);
|
cdpRunnerForTarget(cdp, { tabId: 7, sessionId }),
|
||||||
|
7,
|
||||||
|
);
|
||||||
|
expect(captured.documents[0].domNodes.find((n) => n.backendNodeId === 11)?.formValue).toBe(
|
||||||
|
sessionId,
|
||||||
|
);
|
||||||
expect(sendToTarget).toHaveBeenCalledWith(
|
expect(sendToTarget).toHaveBeenCalledWith(
|
||||||
{ tabId: 7, sessionId },
|
{ tabId: 7, sessionId },
|
||||||
"Runtime.releaseObjectGroup",
|
"Runtime.releaseObjectGroup",
|
||||||
@@ -178,8 +187,8 @@ describe("form state identity", () => {
|
|||||||
result.result.deepSerializedValue.value[0].value[1].value = "not json";
|
result.result.deepSerializedValue.value[0].value[1].value = "not json";
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
const captured = await captureViewModel(cdp, 7);
|
const captured = await captureObservationFacts(cdp, 7);
|
||||||
expect(captured.nodes.find((n) => n.backendNodeId === 11)).toMatchObject({
|
expect(captured.documents[0].domNodes.find((n) => n.backendNodeId === 11)).toMatchObject({
|
||||||
formValue: "snapshot value",
|
formValue: "snapshot value",
|
||||||
formPlaceholder: "snapshot placeholder",
|
formPlaceholder: "snapshot placeholder",
|
||||||
});
|
});
|
||||||
@@ -192,7 +201,7 @@ describe("form state identity", () => {
|
|||||||
controller.abort();
|
controller.abort();
|
||||||
return reply([[11, state("new")]]);
|
return reply([[11, state("new")]]);
|
||||||
});
|
});
|
||||||
await expect(captureViewModel(cdp, 7, { signal: controller.signal })).rejects.toMatchObject({
|
await expect(captureObservationFacts(cdp, 7, controller.signal)).rejects.toMatchObject({
|
||||||
name: "AbortError",
|
name: "AbortError",
|
||||||
});
|
});
|
||||||
expect(send).toHaveBeenCalledWith(7, "Runtime.releaseObjectGroup", expect.anything());
|
expect(send).toHaveBeenCalledWith(7, "Runtime.releaseObjectGroup", expect.anything());
|
||||||
@@ -216,9 +225,13 @@ describe("form state identity", () => {
|
|||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const captured = await captureViewModel(cdp, 7);
|
const captured = await captureObservationFacts(cdp, 7);
|
||||||
expect(captured.nodes.find((n) => n.backendNodeId === 11)?.formValue).toBe("snapshot value");
|
expect(captured.documents[0].domNodes.find((n) => n.backendNodeId === 11)?.formValue).toBe(
|
||||||
expect(captured.nodes.find((n) => n.backendNodeId === 12)?.formValue).toBe("live value");
|
"snapshot value",
|
||||||
|
);
|
||||||
|
expect(captured.documents[0].domNodes.find((n) => n.backendNodeId === 12)?.formValue).toBe(
|
||||||
|
"live value",
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
@@ -228,8 +241,8 @@ describe("form state identity", () => {
|
|||||||
const { cdp } = fakeCdp([[{ id: 11, value: "secret", type }]], () =>
|
const { cdp } = fakeCdp([[{ id: 11, value: "secret", type }]], () =>
|
||||||
reply([[11, { ...state("secret"), sensitive: true }]]),
|
reply([[11, { ...state("secret"), sensitive: true }]]),
|
||||||
);
|
);
|
||||||
const captured = await captureViewModel(cdp, 7);
|
const captured = await captureObservationFacts(cdp, 7);
|
||||||
const control = captured.nodes.find((n) => n.backendNodeId === 11);
|
const control = captured.documents[0].domNodes.find((n) => n.backendNodeId === 11);
|
||||||
expect(control?.formState).toBe("filled");
|
expect(control?.formState).toBe("filled");
|
||||||
expect(control?.formValue).toBeUndefined();
|
expect(control?.formValue).toBeUndefined();
|
||||||
expect(control?.formDefaultValue).toBeUndefined();
|
expect(control?.formDefaultValue).toBeUndefined();
|
||||||
@@ -257,9 +270,13 @@ describe("form state identity", () => {
|
|||||||
expect(entries).toHaveLength(250);
|
expect(entries).toHaveLength(250);
|
||||||
return reply(entries.map(([element, json]) => [element.backendNodeId, JSON.parse(json)]));
|
return reply(entries.map(([element, json]) => [element.backendNodeId, JSON.parse(json)]));
|
||||||
});
|
});
|
||||||
const captured = await captureViewModel(cdp, 7);
|
const captured = await captureObservationFacts(cdp, 7);
|
||||||
expect(captured.nodes.find((n) => n.backendNodeId === 250)?.formValue).toBe("live 250");
|
expect(captured.documents[0].domNodes.find((n) => n.backendNodeId === 250)?.formValue).toBe(
|
||||||
expect(captured.nodes.find((n) => n.backendNodeId === 251)?.formValue).toBe("old");
|
"live 250",
|
||||||
|
);
|
||||||
|
expect(captured.documents[0].domNodes.find((n) => n.backendNodeId === 251)?.formValue).toBe(
|
||||||
|
"old",
|
||||||
|
);
|
||||||
expect(send.mock.calls.filter(([, method]) => method === "Runtime.evaluate")).toHaveLength(1);
|
expect(send.mock.calls.filter(([, method]) => method === "Runtime.evaluate")).toHaveLength(1);
|
||||||
expect(
|
expect(
|
||||||
send.mock.calls.filter(([, method]) => method === "Runtime.releaseObjectGroup"),
|
send.mock.calls.filter(([, method]) => method === "Runtime.releaseObjectGroup"),
|
||||||
|
|||||||
@@ -1,198 +0,0 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
|
||||||
import type { CdpRunner } from "../../shared";
|
|
||||||
import type { CapturedNode, CapturedViewModel } from "../capture";
|
|
||||||
import { captureFrameData } from "../frame-capture";
|
|
||||||
|
|
||||||
function ownerNode(backendNodeId: number, x: number): CapturedNode {
|
|
||||||
return {
|
|
||||||
backendNodeId,
|
|
||||||
parentBackendNodeId: 1,
|
|
||||||
frameId: "main",
|
|
||||||
ownerFrameBackendNodeId: null,
|
|
||||||
tag: "iframe",
|
|
||||||
attrs: {},
|
|
||||||
rect: { x, y: 100, w: 300, h: 200 },
|
|
||||||
localRect: { x, y: 100, w: 300, h: 200 },
|
|
||||||
paintOrder: 1,
|
|
||||||
position: "static",
|
|
||||||
pointerEvents: "auto",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function childSnapshot(frameId: string, backendNodeId: number) {
|
|
||||||
const strings = [frameId, "body", "button", "static", "auto", "pointer"];
|
|
||||||
return {
|
|
||||||
strings,
|
|
||||||
documents: [
|
|
||||||
{
|
|
||||||
frameId,
|
|
||||||
nodes: {
|
|
||||||
parentIndex: [-1, 0],
|
|
||||||
nodeName: [1, 2],
|
|
||||||
backendNodeId: [backendNodeId - 1, backendNodeId],
|
|
||||||
attributes: [[], []],
|
|
||||||
},
|
|
||||||
layout: {
|
|
||||||
nodeIndex: [0, 1],
|
|
||||||
styles: [
|
|
||||||
[3, 4, 4],
|
|
||||||
[3, 4, 5],
|
|
||||||
],
|
|
||||||
bounds: [
|
|
||||||
[0, 0, 300, 200],
|
|
||||||
[10, 20, 100, 40],
|
|
||||||
],
|
|
||||||
paintOrders: [0, 1],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("captureFrameData", () => {
|
|
||||||
it("captures and positions multiple OOPIF documents missing from the root snapshot", async () => {
|
|
||||||
const leftOwner = ownerNode(10, 50);
|
|
||||||
const rightOwner = ownerNode(20, 500);
|
|
||||||
const captured: CapturedViewModel = {
|
|
||||||
nodes: [leftOwner, rightOwner],
|
|
||||||
viewport: { width: 1000, height: 800 },
|
|
||||||
iframeNodes: new Map(),
|
|
||||||
frameNodes: new Map([["main", [leftOwner, rightOwner]]]),
|
|
||||||
frameOwnerBackendNodeIds: new Map(),
|
|
||||||
rootFrameId: "main",
|
|
||||||
excludedBackendNodeIds: new Set(),
|
|
||||||
};
|
|
||||||
const sendToTarget = vi.fn(async (target, method) => {
|
|
||||||
if (method === "Page.getLayoutMetrics") {
|
|
||||||
return { cssLayoutViewport: { clientWidth: 300, clientHeight: 200, pageX: 0, pageY: 0 } };
|
|
||||||
}
|
|
||||||
if (method === "DOMSnapshot.enable" || method === "Accessibility.enable") return {};
|
|
||||||
if (method === "DOMSnapshot.captureSnapshot") {
|
|
||||||
return target.sessionId === "left-session"
|
|
||||||
? childSnapshot("left", 101)
|
|
||||||
: childSnapshot("right", 201);
|
|
||||||
}
|
|
||||||
if (method === "Accessibility.getFullAXTree") return { nodes: [] };
|
|
||||||
throw new Error(`unexpected ${method}`);
|
|
||||||
});
|
|
||||||
const cdp: CdpRunner = {
|
|
||||||
send: vi.fn(async (_tabId, method, params) => {
|
|
||||||
if (method === "Accessibility.enable") return {};
|
|
||||||
if (method === "Accessibility.getFullAXTree") return { nodes: [] };
|
|
||||||
if (method === "DOM.getBoxModel") {
|
|
||||||
const backendNodeId = (params as { backendNodeId?: number })?.backendNodeId;
|
|
||||||
const x = backendNodeId === 10 ? 50 : 500;
|
|
||||||
return { model: { content: [x, 100, x + 300, 100, x + 300, 300, x, 300] } };
|
|
||||||
}
|
|
||||||
if (method === "Page.getLayoutMetrics") {
|
|
||||||
return { cssLayoutViewport: { clientWidth: 1000, clientHeight: 800 } };
|
|
||||||
}
|
|
||||||
throw new Error(`unexpected root ${method}`);
|
|
||||||
}) as CdpRunner["send"],
|
|
||||||
sendToTarget: sendToTarget as unknown as NonNullable<CdpRunner["sendToTarget"]>,
|
|
||||||
getFrameGraph: vi.fn(async () => ({
|
|
||||||
rootFrameId: "main",
|
|
||||||
frames: [
|
|
||||||
{ frameId: "main", target: { tabId: 4 } },
|
|
||||||
{
|
|
||||||
frameId: "left",
|
|
||||||
parentFrameId: "main",
|
|
||||||
ownerBackendNodeId: 10,
|
|
||||||
target: { tabId: 4, sessionId: "left-session" },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
frameId: "right",
|
|
||||||
parentFrameId: "main",
|
|
||||||
ownerBackendNodeId: 20,
|
|
||||||
target: { tabId: 4, sessionId: "right-session" },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
|
|
||||||
const trees = await captureFrameData(cdp, 4, captured);
|
|
||||||
|
|
||||||
expect(trees.map((tree) => tree.frameId)).toEqual(["main", "left", "right"]);
|
|
||||||
expect(
|
|
||||||
captured.frameNodes?.get("left")?.find((node) => node.backendNodeId === 101)?.rect,
|
|
||||||
).toEqual({ x: 60, y: 120, w: 100, h: 40 });
|
|
||||||
expect(
|
|
||||||
captured.frameNodes?.get("right")?.find((node) => node.backendNodeId === 201)?.rect,
|
|
||||||
).toEqual({ x: 510, y: 120, w: 100, h: 40 });
|
|
||||||
expect(captured.frameOwnerBackendNodeIds).toEqual(
|
|
||||||
new Map([
|
|
||||||
["left", 10],
|
|
||||||
["right", 20],
|
|
||||||
]),
|
|
||||||
);
|
|
||||||
expect(captured.frameParentIds).toEqual(
|
|
||||||
new Map([
|
|
||||||
["left", "main"],
|
|
||||||
["right", "main"],
|
|
||||||
]),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("keeps OOPIF semantics when viewport projection is unavailable", async () => {
|
|
||||||
const owner = { ...ownerNode(10, 50), rect: null, localRect: null };
|
|
||||||
const captured: CapturedViewModel = {
|
|
||||||
nodes: [owner],
|
|
||||||
viewport: { width: 1000, height: 800 },
|
|
||||||
iframeNodes: new Map(),
|
|
||||||
frameNodes: new Map([["main", [owner]]]),
|
|
||||||
rootFrameId: "main",
|
|
||||||
excludedBackendNodeIds: new Set(),
|
|
||||||
};
|
|
||||||
const cdp: CdpRunner = {
|
|
||||||
send: vi.fn(async (_tabId, method) => {
|
|
||||||
if (method === "Accessibility.enable") return {};
|
|
||||||
if (method === "Accessibility.getFullAXTree") return { nodes: [] };
|
|
||||||
if (method === "DOM.getBoxModel") throw new Error("owner geometry unavailable");
|
|
||||||
if (method === "Page.getLayoutMetrics") {
|
|
||||||
return { cssLayoutViewport: { clientWidth: 1000, clientHeight: 800 } };
|
|
||||||
}
|
|
||||||
throw new Error(`unexpected root ${method}`);
|
|
||||||
}) as CdpRunner["send"],
|
|
||||||
sendToTarget: vi.fn(async (_target, method) => {
|
|
||||||
if (method === "Page.getLayoutMetrics") throw new Error("viewport unavailable");
|
|
||||||
if (method === "DOMSnapshot.enable" || method === "Accessibility.enable") return {};
|
|
||||||
if (method === "DOMSnapshot.captureSnapshot") return childSnapshot("child", 101);
|
|
||||||
if (method === "Accessibility.getFullAXTree") {
|
|
||||||
return {
|
|
||||||
nodes: [
|
|
||||||
{
|
|
||||||
nodeId: "button",
|
|
||||||
backendDOMNodeId: 101,
|
|
||||||
role: { type: "role", value: "button" },
|
|
||||||
name: { type: "computedString", value: "Continue" },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
throw new Error(`unexpected ${method}`);
|
|
||||||
}) as unknown as NonNullable<CdpRunner["sendToTarget"]>,
|
|
||||||
getFrameGraph: vi.fn(async () => ({
|
|
||||||
rootFrameId: "main",
|
|
||||||
frames: [
|
|
||||||
{ frameId: "main", target: { tabId: 4 } },
|
|
||||||
{
|
|
||||||
frameId: "child",
|
|
||||||
parentFrameId: "main",
|
|
||||||
ownerBackendNodeId: 10,
|
|
||||||
target: { tabId: 4, sessionId: "child-session" },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
|
|
||||||
const documents = await captureFrameData(cdp, 4, captured);
|
|
||||||
const childDocument = documents.find((document) => document.frameId === "child");
|
|
||||||
|
|
||||||
expect(childDocument?.axNodes).toEqual([
|
|
||||||
expect.objectContaining({ backendDOMNodeId: 101, frameId: "child" }),
|
|
||||||
]);
|
|
||||||
expect(childDocument?.domNodes.find((node) => node.backendNodeId === 101)).toEqual(
|
|
||||||
expect.objectContaining({ rect: null, localRect: { x: 10, y: 20, w: 100, h: 40 } }),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,7 +1,12 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import type { CdpFrameGraph } from "@/browser-driver/frame-graph";
|
import type { CdpFrameGraph } from "@/browser-driver/frame-graph";
|
||||||
import type { CapturedNode, CapturedViewModel } from "../capture";
|
import type { CapturedNode } from "../capture";
|
||||||
import { buildFrameDocuments, type FrameAxBatch, type FrameOwnedAxNode } from "../frame-document";
|
import {
|
||||||
|
buildFrameDocuments,
|
||||||
|
type FrameAxBatch,
|
||||||
|
type FrameDomInput,
|
||||||
|
type FrameOwnedAxNode,
|
||||||
|
} from "../frame-document";
|
||||||
|
|
||||||
function domNode(frameId: string, backendNodeId: number): CapturedNode {
|
function domNode(frameId: string, backendNodeId: number): CapturedNode {
|
||||||
return {
|
return {
|
||||||
@@ -17,19 +22,16 @@ function domNode(frameId: string, backendNodeId: number): CapturedNode {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function captured(frameNodes: Map<string, CapturedNode[]>): CapturedViewModel {
|
function captured(frameNodes: Map<string, CapturedNode[]>): FrameDomInput {
|
||||||
return {
|
return {
|
||||||
nodes: frameNodes.get("main") ?? [],
|
nodes: frameNodes.get("main") ?? [],
|
||||||
viewport: { width: 800, height: 600 },
|
|
||||||
iframeNodes: new Map(),
|
|
||||||
frameNodes,
|
frameNodes,
|
||||||
rootFrameId: "main",
|
rootFrameId: "main",
|
||||||
excludedBackendNodeIds: new Set(),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("buildFrameDocuments", () => {
|
describe("buildFrameDocuments", () => {
|
||||||
it("partitions mixed AX results by node ownership and cuts cross-frame parent edges", () => {
|
it("partitions mixed AX results by node ownership and cuts cross-frame parent edges", async () => {
|
||||||
const graph: CdpFrameGraph = {
|
const graph: CdpFrameGraph = {
|
||||||
rootFrameId: "main",
|
rootFrameId: "main",
|
||||||
frames: [
|
frames: [
|
||||||
@@ -54,7 +56,7 @@ describe("buildFrameDocuments", () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const documents = buildFrameDocuments(
|
const documents = await buildFrameDocuments(
|
||||||
graph,
|
graph,
|
||||||
batches,
|
batches,
|
||||||
captured(
|
captured(
|
||||||
@@ -72,7 +74,7 @@ describe("buildFrameDocuments", () => {
|
|||||||
expect(childNodes.find((node) => node.nodeId === "virtual")?.frameId).toBe("child");
|
expect(childNodes.find((node) => node.nodeId === "virtual")?.frameId).toBe("child");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("deduplicates overlapping same-target AX results using the strongest ownership", () => {
|
it("deduplicates overlapping same-target AX results using the strongest ownership", async () => {
|
||||||
const graph: CdpFrameGraph = {
|
const graph: CdpFrameGraph = {
|
||||||
rootFrameId: "main",
|
rootFrameId: "main",
|
||||||
frames: [
|
frames: [
|
||||||
@@ -92,7 +94,7 @@ describe("buildFrameDocuments", () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const documents = buildFrameDocuments(
|
const documents = await buildFrameDocuments(
|
||||||
graph,
|
graph,
|
||||||
batches,
|
batches,
|
||||||
captured(
|
captured(
|
||||||
@@ -107,7 +109,7 @@ describe("buildFrameDocuments", () => {
|
|||||||
expect(documents.find((document) => document.frameId === "child")?.axNodes).toHaveLength(1);
|
expect(documents.find((document) => document.frameId === "child")?.axNodes).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses target-scoped backend ownership when OOPIF backend ids collide", () => {
|
it("uses target-scoped backend ownership when OOPIF backend ids collide", async () => {
|
||||||
const graph: CdpFrameGraph = {
|
const graph: CdpFrameGraph = {
|
||||||
rootFrameId: "main",
|
rootFrameId: "main",
|
||||||
frames: [
|
frames: [
|
||||||
@@ -129,7 +131,7 @@ describe("buildFrameDocuments", () => {
|
|||||||
nodes: [{ nodeId: `${frame.frameId}-node`, backendDOMNodeId: 7 }],
|
nodes: [{ nodeId: `${frame.frameId}-node`, backendDOMNodeId: 7 }],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const documents = buildFrameDocuments(
|
const documents = await buildFrameDocuments(
|
||||||
graph,
|
graph,
|
||||||
batches,
|
batches,
|
||||||
captured(
|
captured(
|
||||||
@@ -149,7 +151,7 @@ describe("buildFrameDocuments", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses captured frame parent identity instead of guessing from backend ids", () => {
|
it("uses captured frame parent identity instead of guessing from backend ids", async () => {
|
||||||
const frameNodes = new Map([
|
const frameNodes = new Map([
|
||||||
["main", [domNode("main", 1)]],
|
["main", [domNode("main", 1)]],
|
||||||
["left", [domNode("left", 7)]],
|
["left", [domNode("left", 7)]],
|
||||||
@@ -164,11 +166,42 @@ describe("buildFrameDocuments", () => {
|
|||||||
nodes: [],
|
nodes: [],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const documents = buildFrameDocuments(null, frames, capture);
|
const documents = await buildFrameDocuments(null, frames, capture);
|
||||||
|
|
||||||
expect(documents.find((document) => document.frameId === "child")).toMatchObject({
|
expect(documents.find((document) => document.frameId === "child")).toMatchObject({
|
||||||
parentFrameId: "left",
|
parentFrameId: "left",
|
||||||
ownerBackendNodeId: 7,
|
ownerBackendNodeId: 7,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it("rejects explicit AX frame ownership in a different target", async () => {
|
||||||
|
const frames = [
|
||||||
|
{ frameId: "main", target: { tabId: 4 } },
|
||||||
|
{ frameId: "remote", target: { tabId: 4, sessionId: "remote" } },
|
||||||
|
];
|
||||||
|
const unresolved = vi.fn();
|
||||||
|
const documents = await buildFrameDocuments(
|
||||||
|
{ rootFrameId: "main", frames },
|
||||||
|
[{ frame: frames[0], nodes: [{ nodeId: "wrong", frameId: "remote" }] }],
|
||||||
|
captured(new Map()),
|
||||||
|
undefined,
|
||||||
|
unresolved,
|
||||||
|
);
|
||||||
|
expect(documents.every((doc) => doc.axNodes.length === 0)).toBe(true);
|
||||||
|
expect(unresolved).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves a reverse-ordered deep AX parent chain without recursion", async () => {
|
||||||
|
const frame = { frameId: "main", target: { tabId: 4 } };
|
||||||
|
const nodes: FrameOwnedAxNode[] = Array.from({ length: 10000 }, (_, i) => ({
|
||||||
|
nodeId: String(i),
|
||||||
|
...(i ? { parentId: String(i - 1) } : { frameId: "main" }),
|
||||||
|
})).reverse();
|
||||||
|
const documents = await buildFrameDocuments(
|
||||||
|
{ rootFrameId: "main", frames: [frame] },
|
||||||
|
[{ frame, nodes }],
|
||||||
|
captured(new Map()),
|
||||||
|
);
|
||||||
|
expect(documents[0].axNodes).toHaveLength(10000);
|
||||||
|
expect(documents[0].axNodes[0].frameId).toBe("main");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -0,0 +1,382 @@
|
|||||||
|
import {
|
||||||
|
buildFrameGraph,
|
||||||
|
type CdpFrame,
|
||||||
|
type CdpFrameGraph,
|
||||||
|
type CdpFrameTreeNode,
|
||||||
|
type CdpTarget,
|
||||||
|
cdpTargetKey,
|
||||||
|
} from "@/browser-driver/frame-graph";
|
||||||
|
import { cssViewport, GeometryContext } from "../geometry/frame-context";
|
||||||
|
import { type CdpRunner, cdpRunnerForTarget, sendToCdpTarget } from "../shared";
|
||||||
|
import { collectOverlayExcludedBackendIds } from "./capture";
|
||||||
|
import { readDocumentIdentity, sameDocument } from "./document-identity";
|
||||||
|
import type { CapturedSceneInput } from "./facts";
|
||||||
|
import {
|
||||||
|
buildDocumentIndex,
|
||||||
|
type CaptureIssue,
|
||||||
|
type DocumentIdentity,
|
||||||
|
isCaptureAbort,
|
||||||
|
type ObservationFacts,
|
||||||
|
throwCaptureAborted,
|
||||||
|
} from "./facts";
|
||||||
|
import { enrichFormControlStates } from "./form-capture";
|
||||||
|
import {
|
||||||
|
buildFrameDocuments,
|
||||||
|
type FrameAxBatch,
|
||||||
|
type FrameDocument,
|
||||||
|
type FrameOwnedAxNode,
|
||||||
|
} from "./frame-document";
|
||||||
|
import { type NormalizedDocument, normalizeSnapshot } from "./normalize";
|
||||||
|
import { REQUESTED_STYLES, type SnapshotReply, snapshotFrameId } from "./snapshot";
|
||||||
|
|
||||||
|
interface TargetBatch<T extends FrameOwnedAxNode> {
|
||||||
|
target: CdpTarget;
|
||||||
|
frames: CdpFrame[];
|
||||||
|
before: Map<string, DocumentIdentity>;
|
||||||
|
documents: NormalizedDocument[];
|
||||||
|
ax: FrameAxBatch<T>[];
|
||||||
|
fallbackExcluded: Set<number>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A fixed worker pool scoped to one capture; each worker issues at most one
|
||||||
|
* collection request at a time. Geometry has its own measurement-phase bound. */
|
||||||
|
async function collectTargets<T>(
|
||||||
|
items: readonly T[],
|
||||||
|
collect: (item: T) => Promise<void>,
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<void> {
|
||||||
|
let cursor = 0;
|
||||||
|
await Promise.all(
|
||||||
|
Array.from({ length: Math.min(4, items.length) }, async () => {
|
||||||
|
while (cursor < items.length) {
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
const item = items[cursor++];
|
||||||
|
await collect(item);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** When topology discovery is unavailable, snapshot documents still carry
|
||||||
|
* explicit target/frame provenance. Only explicit contentDocumentIndex edges
|
||||||
|
* establish ancestry; a foreign frame is never renamed to fit a request. */
|
||||||
|
function snapshotFrames(snapshot: SnapshotReply, target: CdpTarget): CdpFrame[] {
|
||||||
|
const raw = snapshot.documents ?? [];
|
||||||
|
const strings = snapshot.strings ?? [];
|
||||||
|
const frames = raw.map((doc) => {
|
||||||
|
const frameId = snapshotFrameId(doc, strings);
|
||||||
|
return frameId ? ({ frameId, target } as CdpFrame) : undefined;
|
||||||
|
});
|
||||||
|
for (let i = 0; i < raw.length; i++) {
|
||||||
|
const edges = raw[i].nodes?.contentDocumentIndex;
|
||||||
|
const parent = frames[i];
|
||||||
|
if (!parent || !edges) continue;
|
||||||
|
for (let e = 0; e < edges.index.length; e++) {
|
||||||
|
const child = frames[edges.value[e]];
|
||||||
|
const owner = raw[i].nodes?.backendNodeId?.[edges.index[e]];
|
||||||
|
if (child && owner !== undefined && child !== parent) {
|
||||||
|
child.parentFrameId = parent.frameId;
|
||||||
|
child.ownerBackendNodeId = owner;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return frames.filter((frame): frame is CdpFrame => !!frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function captureObservationFacts<T extends FrameOwnedAxNode>(
|
||||||
|
cdp: CdpRunner,
|
||||||
|
tabId: number,
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<ObservationFacts<T>> {
|
||||||
|
const startedAt = Date.now();
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
let graph: CdpFrameGraph | undefined;
|
||||||
|
try {
|
||||||
|
graph = await cdp.getFrameGraph?.(tabId);
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
}
|
||||||
|
const geometry = new GeometryContext(cdp, tabId, graph, signal);
|
||||||
|
const issues: CaptureIssue[] = [];
|
||||||
|
const groups = new Map<string, TargetBatch<T>>();
|
||||||
|
for (const frame of graph?.frames ?? []) {
|
||||||
|
const key = cdpTargetKey(frame.target);
|
||||||
|
let group = groups.get(key);
|
||||||
|
if (!group) {
|
||||||
|
group = {
|
||||||
|
target: frame.target,
|
||||||
|
frames: [],
|
||||||
|
before: new Map(),
|
||||||
|
documents: [],
|
||||||
|
ax: [],
|
||||||
|
fallbackExcluded: new Set(),
|
||||||
|
};
|
||||||
|
groups.set(key, group);
|
||||||
|
}
|
||||||
|
group.frames.push(frame);
|
||||||
|
}
|
||||||
|
if (!groups.size)
|
||||||
|
groups.set(cdpTargetKey({ tabId }), {
|
||||||
|
target: { tabId },
|
||||||
|
frames: [],
|
||||||
|
before: new Map(),
|
||||||
|
documents: [],
|
||||||
|
ax: [],
|
||||||
|
fallbackExcluded: new Set(),
|
||||||
|
});
|
||||||
|
const batches = [...groups.values()];
|
||||||
|
const unavailable = (batch: TargetBatch<T>, stage: CaptureIssue["stage"], frameId?: string) =>
|
||||||
|
issues.push({ target: batch.target, frameId, stage, reason: "capture-unavailable" });
|
||||||
|
let firstFailure: unknown;
|
||||||
|
await collectTargets(
|
||||||
|
batches,
|
||||||
|
async (batch) => {
|
||||||
|
const scoped = cdpRunnerForTarget(cdp, batch.target);
|
||||||
|
for (const frame of batch.frames) {
|
||||||
|
const identity = await readDocumentIdentity(cdp, frame, signal);
|
||||||
|
if (identity) batch.before.set(frame.frameId, identity);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
await scoped.send(tabId, "DOMSnapshot.enable", {});
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
const snapshot = await scoped.send<SnapshotReply>(tabId, "DOMSnapshot.captureSnapshot", {
|
||||||
|
computedStyles: REQUESTED_STYLES,
|
||||||
|
includePaintOrder: true,
|
||||||
|
includeDOMRects: true,
|
||||||
|
});
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
if (!graph) batch.frames = snapshotFrames(snapshot, batch.target);
|
||||||
|
batch.documents = await normalizeSnapshot(
|
||||||
|
snapshot,
|
||||||
|
batch.target,
|
||||||
|
batch.frames,
|
||||||
|
geometry,
|
||||||
|
issues,
|
||||||
|
signal,
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
!(await enrichFormControlStates(
|
||||||
|
scoped,
|
||||||
|
tabId,
|
||||||
|
batch.documents.map((doc) => doc.domNodes),
|
||||||
|
signal,
|
||||||
|
))
|
||||||
|
)
|
||||||
|
unavailable(batch, "forms");
|
||||||
|
const present = new Set(batch.documents.map((doc) => doc.frame.frameId));
|
||||||
|
for (const frame of batch.frames)
|
||||||
|
if (!present.has(frame.frameId)) unavailable(batch, "dom", frame.frameId);
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
firstFailure ??= error;
|
||||||
|
unavailable(batch, "dom");
|
||||||
|
batch.fallbackExcluded = await collectOverlayExcludedBackendIds(scoped, tabId, signal);
|
||||||
|
}
|
||||||
|
if (!batch.frames.length && !graph)
|
||||||
|
batch.frames = [{ frameId: "root", target: batch.target }];
|
||||||
|
try {
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
await scoped.send(tabId, "Accessibility.enable", {});
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
firstFailure ??= error;
|
||||||
|
unavailable(batch, "ax");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const frame of batch.frames) {
|
||||||
|
try {
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
const result = await scoped.send<{ nodes?: T[] }>(
|
||||||
|
tabId,
|
||||||
|
"Accessibility.getFullAXTree",
|
||||||
|
frame.frameId === "root" ? {} : { frameId: frame.frameId },
|
||||||
|
);
|
||||||
|
batch.ax.push({ frame, nodes: result.nodes ?? [] });
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
firstFailure ??= error;
|
||||||
|
unavailable(batch, "ax", frame.frameId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
signal,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Validate after every target's static data and geometry have been collected.
|
||||||
|
const identities = new Map<string, DocumentIdentity>();
|
||||||
|
const invalid = new Set<string>();
|
||||||
|
await collectTargets(
|
||||||
|
batches,
|
||||||
|
async (batch) => {
|
||||||
|
let fresh: Map<string, CdpFrame> | undefined;
|
||||||
|
if (batch.before.size) {
|
||||||
|
try {
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
const reply = await sendToCdpTarget<{ frameTree?: CdpFrameTreeNode }>(
|
||||||
|
cdp,
|
||||||
|
batch.target,
|
||||||
|
"Page.getFrameTree",
|
||||||
|
{},
|
||||||
|
);
|
||||||
|
const tree = reply.frameTree
|
||||||
|
? buildFrameGraph([{ target: batch.target, tree: reply.frameTree }])
|
||||||
|
: null;
|
||||||
|
fresh = new Map(tree?.frames.map((frame) => [frame.frameId, frame]));
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const docs = new Map(batch.documents.map((doc) => [doc.frame.frameId, doc]));
|
||||||
|
for (const frame of batch.frames) {
|
||||||
|
const before = batch.before.get(frame.frameId);
|
||||||
|
const currentFrame = fresh?.get(frame.frameId);
|
||||||
|
const after = currentFrame
|
||||||
|
? await readDocumentIdentity(cdp, currentFrame, signal)
|
||||||
|
: undefined;
|
||||||
|
const doc = docs.get(frame.frameId);
|
||||||
|
const snapshotMismatch =
|
||||||
|
before &&
|
||||||
|
doc?.documentElementBackendNodeId !== undefined &&
|
||||||
|
before.documentElementBackendNodeId !== doc.documentElementBackendNodeId;
|
||||||
|
if (
|
||||||
|
before &&
|
||||||
|
(snapshotMismatch ||
|
||||||
|
(fresh && !currentFrame) ||
|
||||||
|
(after && !sameDocument(before, after)) ||
|
||||||
|
cdp.getAttachmentId?.(tabId) !== before.attachmentId)
|
||||||
|
) {
|
||||||
|
invalid.add(frame.frameId);
|
||||||
|
issues.push({
|
||||||
|
target: frame.target,
|
||||||
|
frameId: frame.frameId,
|
||||||
|
stage: "identity",
|
||||||
|
reason: "document-changed",
|
||||||
|
});
|
||||||
|
} else if (before && !after) {
|
||||||
|
invalid.add(frame.frameId);
|
||||||
|
issues.push({
|
||||||
|
target: frame.target,
|
||||||
|
frameId: frame.frameId,
|
||||||
|
stage: "identity",
|
||||||
|
reason: "identity-unverified",
|
||||||
|
});
|
||||||
|
} else if (
|
||||||
|
before &&
|
||||||
|
after &&
|
||||||
|
sameDocument(before, after) &&
|
||||||
|
(!doc || doc.documentElementBackendNodeId === before.documentElementBackendNodeId)
|
||||||
|
)
|
||||||
|
identities.set(frame.frameId, after);
|
||||||
|
else
|
||||||
|
issues.push({
|
||||||
|
target: frame.target,
|
||||||
|
frameId: frame.frameId,
|
||||||
|
stage: "identity",
|
||||||
|
reason: "identity-unverified",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
signal,
|
||||||
|
);
|
||||||
|
const allFrames = batches.flatMap((batch) => batch.frames);
|
||||||
|
const rootFrameId = graph?.rootFrameId ?? allFrames[0]?.frameId ?? "root";
|
||||||
|
if (invalid.has(rootFrameId))
|
||||||
|
throw new Error(
|
||||||
|
"observation document identity changed or could not be verified; observe again",
|
||||||
|
);
|
||||||
|
// A descendant cannot retain geometry measured through an invalid ancestor.
|
||||||
|
const childFrames = new Map<string, string[]>();
|
||||||
|
for (const frame of allFrames)
|
||||||
|
if (frame.parentFrameId) {
|
||||||
|
const children = childFrames.get(frame.parentFrameId);
|
||||||
|
if (children) children.push(frame.frameId);
|
||||||
|
else childFrames.set(frame.parentFrameId, [frame.frameId]);
|
||||||
|
}
|
||||||
|
const queue = [...invalid];
|
||||||
|
for (let i = 0; i < queue.length; i++)
|
||||||
|
for (const child of childFrames.get(queue[i]) ?? []) {
|
||||||
|
if (!invalid.has(child)) {
|
||||||
|
invalid.add(child);
|
||||||
|
queue.push(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const frames = allFrames.filter((frame) => !invalid.has(frame.frameId));
|
||||||
|
const decoded = new Map(
|
||||||
|
batches
|
||||||
|
.flatMap((batch) => batch.documents)
|
||||||
|
.filter((doc) => !invalid.has(doc.frame.frameId))
|
||||||
|
.map((doc) => [doc.frame.frameId, doc]),
|
||||||
|
);
|
||||||
|
const ax = batches
|
||||||
|
.flatMap((batch) => batch.ax)
|
||||||
|
.filter((batch) => !invalid.has(batch.frame.frameId));
|
||||||
|
const documents = await buildFrameDocuments(
|
||||||
|
{ rootFrameId, frames },
|
||||||
|
ax,
|
||||||
|
{
|
||||||
|
nodes: [],
|
||||||
|
rootFrameId,
|
||||||
|
frameNodes: new Map([...decoded].map(([id, doc]) => [id, doc.domNodes])),
|
||||||
|
},
|
||||||
|
signal,
|
||||||
|
(frame) =>
|
||||||
|
issues.push({
|
||||||
|
target: frame.target,
|
||||||
|
frameId: frame.frameId,
|
||||||
|
stage: "ownership",
|
||||||
|
reason: "frame-ownership-unresolved",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
let viewport = { width: 0, height: 0 };
|
||||||
|
try {
|
||||||
|
const measured = cssViewport(await geometry.layoutMetrics({ tabId }));
|
||||||
|
viewport = { width: measured.width, height: measured.height };
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
}
|
||||||
|
const facts: ObservationFacts<T>["documents"][number][] = [];
|
||||||
|
for (const document of documents) {
|
||||||
|
const doc = decoded.get(document.frameId);
|
||||||
|
const index = doc?.index ?? (await buildDocumentIndex([], signal));
|
||||||
|
const fallback = groups.get(cdpTargetKey(document.target))?.fallbackExcluded;
|
||||||
|
const { domNodes, axNodes, contextScopeId: _scope, ...frame } = document;
|
||||||
|
facts.push({
|
||||||
|
frame,
|
||||||
|
identity: identities.get(document.frameId),
|
||||||
|
index: fallback?.size ? { ...index, excludedBackendNodeIds: fallback } : index,
|
||||||
|
domNodes,
|
||||||
|
axNodes,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
if (firstFailure && facts.every((doc) => !doc.domNodes.length && !doc.axNodes.length))
|
||||||
|
throw firstFailure;
|
||||||
|
return { rootFrameId, viewport, documents: facts, issues, startedAt, finishedAt: Date.now() };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Existing semantic consumers get a narrow view; no raw snapshot or identity
|
||||||
|
* metadata can leak through recording's allowlisted output DTO. */
|
||||||
|
export function semanticCapture<T extends FrameOwnedAxNode>(
|
||||||
|
facts: ObservationFacts<T>,
|
||||||
|
): { captured: CapturedSceneInput; documents: FrameDocument<T>[] } {
|
||||||
|
const documents = facts.documents.map((doc) => ({
|
||||||
|
...doc.frame,
|
||||||
|
contextScopeId: doc.frame.frameId,
|
||||||
|
domNodes: doc.domNodes.filter(
|
||||||
|
(node) => !doc.index.excludedBackendNodeIds.has(node.backendNodeId),
|
||||||
|
),
|
||||||
|
axNodes: doc.axNodes,
|
||||||
|
excludedBackendNodeIds: doc.index.excludedBackendNodeIds,
|
||||||
|
}));
|
||||||
|
const root = documents.find((doc) => doc.frameId === facts.rootFrameId);
|
||||||
|
return {
|
||||||
|
documents,
|
||||||
|
captured: {
|
||||||
|
nodes: root?.domNodes ?? [],
|
||||||
|
viewport: facts.viewport,
|
||||||
|
rootFrameId: facts.rootFrameId,
|
||||||
|
excludedBackendNodeIds: root?.excludedBackendNodeIds ?? new Set(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,332 +1,21 @@
|
|||||||
// CDP capture adapter: DOMSnapshot.captureSnapshot + Page.getLayoutMetrics
|
// Browser-side hover probes and overlay exclusion fallback. Static collection
|
||||||
// → CapturedNode[] + Viewport. This is the ONLY VOM module that touches
|
// lives in capture-coordinator; snapshot decoding has no browser dependency.
|
||||||
// raw CDP. The captureSnapshot reply is columnar (parallel arrays + a
|
|
||||||
// shared string table); requested computed styles are decoded through
|
|
||||||
// STYLE_COL so capture and visibility policy share one explicit contract.
|
|
||||||
|
|
||||||
import type { Rect, Viewport } from "@browser-skill/vom";
|
|
||||||
import { evaluateHoverTrigger } from "@/lib/hover-trigger-policy";
|
import { evaluateHoverTrigger } from "@/lib/hover-trigger-policy";
|
||||||
import { isOverlayHostNode, OVERLAY_HOST_SELECTOR } from "../../lib/overlay-bridge";
|
import { OVERLAY_HOST_SELECTOR } from "../../lib/overlay-bridge";
|
||||||
import { childFrameProjection, type GeometryProjection, projectRectToViewport } from "../geometry";
|
|
||||||
import type { CdpRunner } from "../shared";
|
import type { CdpRunner } from "../shared";
|
||||||
|
import type { CapturedNode, CapturedSurfaceProbe } from "./facts";
|
||||||
|
|
||||||
|
export type {
|
||||||
|
CapturedNode,
|
||||||
|
CapturedSurfaceProbe,
|
||||||
|
} from "./facts";
|
||||||
|
|
||||||
|
import { isCaptureAbort as isAbortError, throwCaptureAborted as throwIfAborted } from "./facts";
|
||||||
import { clearHover, ProbeBudget, waitForHover } from "./hover-perception";
|
import { clearHover, ProbeBudget, waitForHover } from "./hover-perception";
|
||||||
|
|
||||||
const REQUESTED_STYLES = ["position", "pointer-events", "cursor", "visibility", "opacity"] as const;
|
|
||||||
const STYLE_COL = Object.fromEntries(
|
|
||||||
REQUESTED_STYLES.map((name, index) => [name, index]),
|
|
||||||
) as Record<(typeof REQUESTED_STYLES)[number], number>;
|
|
||||||
|
|
||||||
export interface CapturedNode {
|
|
||||||
backendNodeId: number;
|
|
||||||
parentBackendNodeId: number | null;
|
|
||||||
frameId?: string;
|
|
||||||
/** Owning iframe backend node id; `null` for the top-level document. */
|
|
||||||
ownerFrameBackendNodeId?: number | null;
|
|
||||||
tag: string;
|
|
||||||
attrs: Record<string, string>;
|
|
||||||
/** Top-level viewport-relative CSS px, clipped to the owning frame viewport. */
|
|
||||||
rect: Rect | null;
|
|
||||||
/** Frame-local viewport-relative CSS px before top-level projection. */
|
|
||||||
localRect?: Rect | null;
|
|
||||||
paintOrder: number;
|
|
||||||
position: string;
|
|
||||||
pointerEvents: string;
|
|
||||||
/**
|
|
||||||
* computed `cursor`. `cursor: pointer` is the strongest CDP-free signal
|
|
||||||
* that a non-semantic element (a `<div>`/`<span>` with a click handler)
|
|
||||||
* is actually an interactive control — used by the adapter to surface
|
|
||||||
* custom buttons/checkboxes the AX tree drops as `generic`. Optional like
|
|
||||||
* `textContent`: the live parser always sets it, hand-built fixtures may not.
|
|
||||||
*/
|
|
||||||
cursor?: string;
|
|
||||||
/**
|
|
||||||
* Whether the live DOM snapshot provides a painted, non-hidden box for this
|
|
||||||
* node. Semantic resolution uses this only for DOM fallback nodes; AX-backed
|
|
||||||
* nodes remain authoritative even when they are outside the viewport.
|
|
||||||
*/
|
|
||||||
rendered?: boolean;
|
|
||||||
textContent?: string;
|
|
||||||
formState?: "empty" | "filled" | "default";
|
|
||||||
formValue?: string;
|
|
||||||
formDefaultValue?: string;
|
|
||||||
formPlaceholder?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type CapturedIframeNodes = Map<number, CapturedNode[]>;
|
|
||||||
|
|
||||||
export interface CapturedSurfaceProbe {
|
|
||||||
triggerBackendNodeId: number;
|
|
||||||
triggerPoint?: { x: number; y: number };
|
|
||||||
triggerAction: "hover" | "focus" | string;
|
|
||||||
subItems: string[];
|
|
||||||
confidence?: "high" | "medium" | "low";
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface CapturedViewModel {
|
|
||||||
nodes: CapturedNode[];
|
|
||||||
viewport: Viewport;
|
|
||||||
iframeNodes: CapturedIframeNodes;
|
|
||||||
frameNodes?: Map<string, CapturedNode[]>;
|
|
||||||
frameOwnerBackendNodeIds?: Map<string, number>;
|
|
||||||
/** Explicit DOMSnapshot frame ancestry; never inferred from backend node ids. */
|
|
||||||
frameParentIds?: Map<string, string>;
|
|
||||||
rootFrameId?: string;
|
|
||||||
/** Backend node ids belonging to the agent overlay host + its shadow subtree. */
|
|
||||||
excludedBackendNodeIds: Set<number>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface CaptureViewModelOptions {
|
|
||||||
signal?: AbortSignal;
|
|
||||||
}
|
|
||||||
|
|
||||||
function captureAbortError(): Error {
|
|
||||||
const error = new Error("observation aborted");
|
|
||||||
error.name = "AbortError";
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isAbortError(error: unknown): boolean {
|
|
||||||
return (
|
|
||||||
typeof error === "object" &&
|
|
||||||
error !== null &&
|
|
||||||
(error as { name?: string }).name === "AbortError"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function throwIfAborted(signal: AbortSignal | undefined): void {
|
|
||||||
if (signal?.aborted) throw captureAbortError();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Sparse array format Chrome uses for infrequently-set per-node fields. */
|
|
||||||
interface SparseArray {
|
|
||||||
index: number[];
|
|
||||||
value: number[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface RareBooleanData {
|
|
||||||
index: number[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SnapshotDocument {
|
|
||||||
frameId?: string | number;
|
|
||||||
scrollOffsetX?: number;
|
|
||||||
scrollOffsetY?: number;
|
|
||||||
nodes?: {
|
|
||||||
parentIndex?: number[];
|
|
||||||
nodeName?: number[];
|
|
||||||
backendNodeId?: number[];
|
|
||||||
attributes?: number[][];
|
|
||||||
/**
|
|
||||||
* Per-node text value (index into strings), set for `#text` / CDATA nodes.
|
|
||||||
* Element nodes carry -1. Same length as `backendNodeId`.
|
|
||||||
*/
|
|
||||||
nodeValue?: number[];
|
|
||||||
/** Maps node array index → index into `documents[]` for frame content. */
|
|
||||||
contentDocumentIndex?: SparseArray;
|
|
||||||
inputValue?: SparseArray;
|
|
||||||
textValue?: SparseArray;
|
|
||||||
inputChecked?: RareBooleanData;
|
|
||||||
optionSelected?: RareBooleanData;
|
|
||||||
};
|
|
||||||
layout?: {
|
|
||||||
nodeIndex?: number[];
|
|
||||||
styles?: number[][];
|
|
||||||
bounds?: number[][];
|
|
||||||
paintOrders?: number[];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function snapshotFrameId(document: SnapshotDocument, strings: string[]): string | undefined {
|
|
||||||
if (typeof document.frameId === "string") return document.frameId || undefined;
|
|
||||||
if (typeof document.frameId === "number") return str(strings, document.frameId) || undefined;
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SnapshotReply {
|
|
||||||
strings?: string[];
|
|
||||||
documents?: SnapshotDocument[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LayoutMetricsReply {
|
|
||||||
cssLayoutViewport?: {
|
|
||||||
clientWidth?: number;
|
|
||||||
clientHeight?: number;
|
|
||||||
pageX?: number;
|
|
||||||
pageY?: number;
|
|
||||||
};
|
|
||||||
layoutViewport?: { clientWidth?: number; clientHeight?: number; pageX?: number; pageY?: number };
|
|
||||||
}
|
|
||||||
|
|
||||||
function isFormControlTag(tag: string): boolean {
|
|
||||||
return tag === "input" || tag === "textarea" || tag === "select";
|
|
||||||
}
|
|
||||||
|
|
||||||
function isSensitiveFormControl(node: Pick<CapturedNode, "tag" | "attrs">): boolean {
|
|
||||||
return node.tag === "input" && (node.attrs.type ?? "").toLowerCase() === "password";
|
|
||||||
}
|
|
||||||
|
|
||||||
function snapshotFormState(
|
|
||||||
value: string | undefined,
|
|
||||||
defaultValue: string,
|
|
||||||
hasDefaultValue: boolean,
|
|
||||||
sensitive: boolean,
|
|
||||||
): CapturedNode["formState"] {
|
|
||||||
if (sensitive) {
|
|
||||||
if (value === undefined && !hasDefaultValue) return undefined;
|
|
||||||
return (value ?? defaultValue) === "" ? "empty" : "filled";
|
|
||||||
}
|
|
||||||
if (value === undefined) return undefined;
|
|
||||||
if (value === "") return "empty";
|
|
||||||
return value === defaultValue ? "default" : "filled";
|
|
||||||
}
|
|
||||||
|
|
||||||
const MAX_FORM_ENRICH_CONTROLS = 250;
|
|
||||||
|
|
||||||
interface CapturedFormState {
|
|
||||||
value?: string;
|
|
||||||
defaultValue?: string;
|
|
||||||
placeholder?: string;
|
|
||||||
state?: "empty" | "filled" | "default";
|
|
||||||
sensitive?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface DeepSerializedValue {
|
|
||||||
type: string;
|
|
||||||
value?: unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
function formStateBatchExpression(maxControls: number): string {
|
|
||||||
return `(() => {
|
|
||||||
const maxControls = ${JSON.stringify(maxControls)};
|
|
||||||
let remaining = maxControls;
|
|
||||||
const controlSelector = "input,textarea,select";
|
|
||||||
const controlState = (el) => {
|
|
||||||
const tag = el.tagName.toLowerCase();
|
|
||||||
const type = tag === "input" ? String(el.type || "text").toLowerCase() : tag;
|
|
||||||
const sensitive = type === "password" || type === "credit-card";
|
|
||||||
const rawValue = typeof el.value === "string" ? el.value : "";
|
|
||||||
const defaultValue = typeof el.defaultValue === "string" ? el.defaultValue : "";
|
|
||||||
const placeholder = typeof el.placeholder === "string" ? el.placeholder : "";
|
|
||||||
const state = rawValue === "" ? "empty" : rawValue === defaultValue ? "default" : "filled";
|
|
||||||
return {
|
|
||||||
state,
|
|
||||||
sensitive,
|
|
||||||
placeholder,
|
|
||||||
...(sensitive ? {} : { value: rawValue, defaultValue }),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
const controls = [];
|
|
||||||
const collect = (doc) => {
|
|
||||||
for (const el of Array.from(doc.querySelectorAll(controlSelector))) {
|
|
||||||
if (remaining <= 0) break;
|
|
||||||
// Deep serialization supplies the node's backend id. Keep the
|
|
||||||
// state as JSON so decoding needs no general-purpose V8 deserializer.
|
|
||||||
controls.push([el, JSON.stringify(controlState(el))]);
|
|
||||||
remaining -= 1;
|
|
||||||
}
|
|
||||||
for (const frame of Array.from(doc.querySelectorAll("iframe"))) {
|
|
||||||
if (remaining <= 0) break;
|
|
||||||
let childDoc = null;
|
|
||||||
try { childDoc = frame.contentDocument; } catch { childDoc = null; }
|
|
||||||
if (childDoc) collect(childDoc);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
collect(document);
|
|
||||||
return controls;
|
|
||||||
})()`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function formStatesByBackendId(result: RuntimeEvaluateReply): Map<number, CapturedFormState> {
|
|
||||||
const states = new Map<number, CapturedFormState>();
|
|
||||||
const serialized = result.result?.deepSerializedValue;
|
|
||||||
if (serialized?.type !== "array" || !Array.isArray(serialized.value)) return states;
|
|
||||||
for (const entry of serialized.value as DeepSerializedValue[]) {
|
|
||||||
if (entry?.type !== "array" || !Array.isArray(entry.value)) continue;
|
|
||||||
const [element, json] = entry.value as DeepSerializedValue[];
|
|
||||||
if (element?.type !== "node" || json?.type !== "string" || typeof json.value !== "string") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const backendNodeId = (element.value as { backendNodeId?: number } | undefined)?.backendNodeId;
|
|
||||||
if (typeof backendNodeId !== "number" || !Number.isSafeInteger(backendNodeId)) continue;
|
|
||||||
try {
|
|
||||||
const state = JSON.parse(json.value) as CapturedFormState | null;
|
|
||||||
if (
|
|
||||||
!state ||
|
|
||||||
!["empty", "filled", "default"].includes(state.state ?? "") ||
|
|
||||||
typeof state.sensitive !== "boolean" ||
|
|
||||||
typeof state.placeholder !== "string" ||
|
|
||||||
(!state.sensitive &&
|
|
||||||
(typeof state.value !== "string" || typeof state.defaultValue !== "string"))
|
|
||||||
) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
states.set(backendNodeId, state);
|
|
||||||
} catch {
|
|
||||||
// A malformed entry must not overwrite the snapshot's own state.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return states;
|
|
||||||
}
|
|
||||||
|
|
||||||
function applyFormStates(nodes: CapturedNode[], states: Map<number, CapturedFormState>): void {
|
|
||||||
for (const node of nodes) {
|
|
||||||
if (!isFormControlTag(node.tag)) continue;
|
|
||||||
const state = states.get(node.backendNodeId);
|
|
||||||
if (!state) continue;
|
|
||||||
node.formState = state.state;
|
|
||||||
node.formPlaceholder = state.placeholder ?? "";
|
|
||||||
if (state.sensitive || isSensitiveFormControl(node)) {
|
|
||||||
delete node.formValue;
|
|
||||||
delete node.formDefaultValue;
|
|
||||||
delete node.attrs.value;
|
|
||||||
} else {
|
|
||||||
node.formDefaultValue = state.defaultValue ?? "";
|
|
||||||
if (state.value !== undefined) node.formValue = state.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function enrichFormControlStates(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
tabId: number,
|
|
||||||
frameNodeGroups: CapturedNode[][],
|
|
||||||
signal?: AbortSignal,
|
|
||||||
): Promise<void> {
|
|
||||||
const hasControls = frameNodeGroups.some((nodes) =>
|
|
||||||
nodes.some((node) => isFormControlTag(node.tag)),
|
|
||||||
);
|
|
||||||
if (!hasControls) return;
|
|
||||||
const objectGroup = `bsk-vom-forms-${crypto.randomUUID()}`;
|
|
||||||
try {
|
|
||||||
throwIfAborted(signal);
|
|
||||||
const result = await cdp.send<RuntimeEvaluateReply>(tabId, "Runtime.evaluate", {
|
|
||||||
expression: formStateBatchExpression(MAX_FORM_ENRICH_CONTROLS),
|
|
||||||
objectGroup,
|
|
||||||
serializationOptions: {
|
|
||||||
serialization: "deep",
|
|
||||||
maxDepth: 3,
|
|
||||||
additionalParameters: { maxNodeDepth: 0, includeShadowTree: "none" },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
throwIfAborted(signal);
|
|
||||||
// Backend ids are scoped to this capture's CDP target. OOPIF targets
|
|
||||||
// use their own captureViewModel call and never share this lookup.
|
|
||||||
const states = formStatesByBackendId(result);
|
|
||||||
for (const nodes of frameNodeGroups) {
|
|
||||||
applyFormStates(nodes, states);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
if (isAbortError(error)) throw error;
|
|
||||||
// Best-effort enrichment. DOMSnapshot/AX data still carries the nodes.
|
|
||||||
} finally {
|
|
||||||
await cdp.send(tabId, "Runtime.releaseObjectGroup", { objectGroup }).catch(() => undefined);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface RuntimeEvaluateReply {
|
interface RuntimeEvaluateReply {
|
||||||
result?: {
|
result?: {
|
||||||
value?: unknown;
|
value?: unknown;
|
||||||
deepSerializedValue?: DeepSerializedValue;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,44 +34,6 @@ interface CdpDomNode {
|
|||||||
shadowRoots?: CdpDomNode[];
|
shadowRoots?: CdpDomNode[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ParseDocumentResult {
|
|
||||||
nodes: CapturedNode[];
|
|
||||||
excludedBackendNodeIds: Set<number>;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface FrameContext {
|
|
||||||
frameId?: string;
|
|
||||||
ownerFrameBackendNodeId: number | null;
|
|
||||||
projection: GeometryProjection | null;
|
|
||||||
scrollX: number;
|
|
||||||
scrollY: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
function str(strings: string[], idx: number | undefined): string {
|
|
||||||
if (idx === undefined || idx < 0) return "";
|
|
||||||
return strings[idx] ?? "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function devicePixelRatio(metrics: LayoutMetricsReply): number {
|
|
||||||
const layoutW = metrics.layoutViewport?.clientWidth ?? 0;
|
|
||||||
const cssW = metrics.cssLayoutViewport?.clientWidth ?? 0;
|
|
||||||
if (!layoutW || !cssW) return 1;
|
|
||||||
const dpr = layoutW / cssW;
|
|
||||||
if (!Number.isFinite(dpr) || dpr <= 0) return 1;
|
|
||||||
return dpr >= 1 ? dpr : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function sparseIndexMap(sparse: SparseArray | undefined): Map<number, number> {
|
|
||||||
const out = new Map<number, number>();
|
|
||||||
if (!sparse?.index || !sparse.value) return out;
|
|
||||||
for (let i = 0; i < sparse.index.length; i++) {
|
|
||||||
const nodeIndex = sparse.index[i];
|
|
||||||
const docIndex = sparse.value[i];
|
|
||||||
if (nodeIndex !== undefined && docIndex !== undefined) out.set(nodeIndex, docIndex);
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
function collectBackendIdsFromDomNode(node: CdpDomNode | undefined, out: Set<number>): void {
|
function collectBackendIdsFromDomNode(node: CdpDomNode | undefined, out: Set<number>): void {
|
||||||
if (!node) return;
|
if (!node) return;
|
||||||
if (typeof node.backendNodeId === "number") {
|
if (typeof node.backendNodeId === "number") {
|
||||||
@@ -791,358 +442,3 @@ export async function collectOverlayExcludedBackendIds(
|
|||||||
}
|
}
|
||||||
return excluded;
|
return excluded;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse one DOMSnapshot document into CapturedNode[].
|
|
||||||
*
|
|
||||||
* @param doc - the raw DOMSnapshot document object
|
|
||||||
* @param strings - the shared string table for the whole snapshot
|
|
||||||
* @param dpr - device-pixel-ratio from Page.getLayoutMetrics
|
|
||||||
* @param context - frame coordinate context; output rects are top viewport-relative
|
|
||||||
*/
|
|
||||||
function parseDocumentNodes(
|
|
||||||
doc: SnapshotDocument,
|
|
||||||
strings: string[],
|
|
||||||
dpr: number,
|
|
||||||
context: FrameContext,
|
|
||||||
): ParseDocumentResult {
|
|
||||||
const dn = doc.nodes;
|
|
||||||
const dl = doc.layout;
|
|
||||||
if (!dn?.backendNodeId) {
|
|
||||||
return { nodes: [], excludedBackendNodeIds: new Set() };
|
|
||||||
}
|
|
||||||
|
|
||||||
const count = dn.backendNodeId.length;
|
|
||||||
const layoutByNode = new Map<number, number>();
|
|
||||||
(dl?.nodeIndex ?? []).forEach((nodeIdx, layoutIdx) => layoutByNode.set(nodeIdx, layoutIdx));
|
|
||||||
|
|
||||||
// The agent's own overlay (WXT shadow host, marked with OVERLAY_HOST_MARKER_ATTR)
|
|
||||||
// is injected into the page and its open shadow root
|
|
||||||
// is inlined by DOMSnapshot as descendants of the host. Its fixed
|
|
||||||
// full-viewport click-blocker would otherwise dominate occlusion detection
|
|
||||||
// and hide the real page, so mark the host + its whole subtree excluded.
|
|
||||||
// parentIndex always references an earlier node, so one forward pass suffices.
|
|
||||||
const excluded = new Array<boolean>(count).fill(false);
|
|
||||||
const excludedBackendNodeIds = new Set<number>();
|
|
||||||
for (let n = 0; n < count; n++) {
|
|
||||||
const parentIdx = dn.parentIndex?.[n] ?? -1;
|
|
||||||
const inherited = parentIdx >= 0 && excluded[parentIdx];
|
|
||||||
let isOverlayHost = inherited;
|
|
||||||
if (!isOverlayHost) {
|
|
||||||
const pairs = dn.attributes?.[n] ?? [];
|
|
||||||
const attrNames: string[] = [];
|
|
||||||
for (let a = 0; a + 1 < pairs.length; a += 2) {
|
|
||||||
attrNames.push(str(strings, pairs[a]));
|
|
||||||
}
|
|
||||||
isOverlayHost = isOverlayHostNode(str(strings, dn.nodeName?.[n]), attrNames);
|
|
||||||
}
|
|
||||||
if (!isOverlayHost) continue;
|
|
||||||
excluded[n] = true;
|
|
||||||
const backendNodeId = dn.backendNodeId[n];
|
|
||||||
if (backendNodeId !== undefined) {
|
|
||||||
excludedBackendNodeIds.add(backendNodeId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const posCol = STYLE_COL.position;
|
|
||||||
const peCol = STYLE_COL["pointer-events"];
|
|
||||||
const cursorCol = STYLE_COL.cursor;
|
|
||||||
const visibilityCol = STYLE_COL.visibility;
|
|
||||||
const opacityCol = STYLE_COL.opacity;
|
|
||||||
const inputValues = sparseIndexMap(dn.inputValue);
|
|
||||||
const textValues = sparseIndexMap(dn.textValue);
|
|
||||||
const checkedInputs = new Set(dn.inputChecked?.index ?? []);
|
|
||||||
const selectedOptions = new Set(dn.optionSelected?.index ?? []);
|
|
||||||
|
|
||||||
// Collect visible text from #text child nodes so element CapturedNodes
|
|
||||||
// carry a textContent value usable as a button/link label fallback.
|
|
||||||
// nodeValue is a parallel array: string index for text nodes, -1 otherwise.
|
|
||||||
const nodeTextContent = new Map<number, string>();
|
|
||||||
if (dn.nodeValue) {
|
|
||||||
for (let n = 0; n < count; n++) {
|
|
||||||
const nvIdx = dn.nodeValue[n] ?? -1;
|
|
||||||
if (nvIdx < 0) continue;
|
|
||||||
const text = str(strings, nvIdx).trim();
|
|
||||||
if (!text) continue;
|
|
||||||
const parentIdx = dn.parentIndex?.[n] ?? -1;
|
|
||||||
if (parentIdx >= 0) {
|
|
||||||
const existing = nodeTextContent.get(parentIdx);
|
|
||||||
nodeTextContent.set(parentIdx, existing ? `${existing} ${text}` : text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build CapturedNodes.
|
|
||||||
const nodes: CapturedNode[] = [];
|
|
||||||
for (let n = 0; n < count; n++) {
|
|
||||||
if (excluded[n]) continue;
|
|
||||||
const backendNodeId = dn.backendNodeId[n];
|
|
||||||
const parentIdx = dn.parentIndex?.[n] ?? -1;
|
|
||||||
const parentBackendNodeId = parentIdx >= 0 ? (dn.backendNodeId[parentIdx] ?? null) : null;
|
|
||||||
const tag = str(strings, dn.nodeName?.[n]).toLowerCase();
|
|
||||||
|
|
||||||
const attrs: Record<string, string> = {};
|
|
||||||
const pairs = dn.attributes?.[n] ?? [];
|
|
||||||
for (let a = 0; a + 1 < pairs.length; a += 2) {
|
|
||||||
attrs[str(strings, pairs[a]).toLowerCase()] = str(strings, pairs[a + 1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
let rect: Rect | null = null;
|
|
||||||
let localRect: Rect | null = null;
|
|
||||||
let paintOrder = 0;
|
|
||||||
let position = "static";
|
|
||||||
let pointerEvents = "auto";
|
|
||||||
let cursor = "auto";
|
|
||||||
let rendered = false;
|
|
||||||
const li = layoutByNode.get(n);
|
|
||||||
if (li !== undefined) {
|
|
||||||
const b = dl?.bounds?.[li];
|
|
||||||
if (b && b.length >= 4 && b[2] > 0 && b[3] > 0) {
|
|
||||||
localRect = {
|
|
||||||
x: b[0] / dpr - context.scrollX,
|
|
||||||
y: b[1] / dpr - context.scrollY,
|
|
||||||
w: b[2] / dpr,
|
|
||||||
h: b[3] / dpr,
|
|
||||||
};
|
|
||||||
const bounds = context.projection
|
|
||||||
? projectRectToViewport(localRect, context.projection)
|
|
||||||
: null;
|
|
||||||
rect = bounds ? { x: bounds.x, y: bounds.y, w: bounds.width, h: bounds.height } : null;
|
|
||||||
}
|
|
||||||
paintOrder = dl?.paintOrders?.[li] ?? 0;
|
|
||||||
const styleRow = dl?.styles?.[li] ?? [];
|
|
||||||
position = str(strings, styleRow[posCol]) || "static";
|
|
||||||
pointerEvents = str(strings, styleRow[peCol]) || "auto";
|
|
||||||
cursor = str(strings, styleRow[cursorCol]) || "auto";
|
|
||||||
const visibility = str(strings, styleRow[visibilityCol]) || "visible";
|
|
||||||
const opacity = str(strings, styleRow[opacityCol]) || "1";
|
|
||||||
rendered =
|
|
||||||
localRect !== null &&
|
|
||||||
visibility !== "hidden" &&
|
|
||||||
visibility !== "collapse" &&
|
|
||||||
(Number.parseFloat(opacity) || 0) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip non-element nodes (#text, #cdata-section, etc.) — they carry no
|
|
||||||
// geometry and are never queried by callers.
|
|
||||||
if (tag.startsWith("#")) continue;
|
|
||||||
|
|
||||||
const textContent = nodeTextContent.get(n);
|
|
||||||
|
|
||||||
const rawFormValueIndex = tag === "textarea" ? textValues.get(n) : inputValues.get(n);
|
|
||||||
const rawFormValue =
|
|
||||||
rawFormValueIndex !== undefined ? str(strings, rawFormValueIndex) : undefined;
|
|
||||||
const sensitive = isSensitiveFormControl({ tag, attrs });
|
|
||||||
const formDefaultValue = attrs.value ?? "";
|
|
||||||
const formValue = sensitive ? undefined : rawFormValue;
|
|
||||||
const formState = snapshotFormState(
|
|
||||||
rawFormValue,
|
|
||||||
formDefaultValue,
|
|
||||||
Object.prototype.hasOwnProperty.call(attrs, "value"),
|
|
||||||
sensitive,
|
|
||||||
);
|
|
||||||
if (sensitive) delete attrs.value;
|
|
||||||
|
|
||||||
nodes.push({
|
|
||||||
backendNodeId,
|
|
||||||
parentBackendNodeId,
|
|
||||||
...(context.frameId ? { frameId: context.frameId } : {}),
|
|
||||||
ownerFrameBackendNodeId: context.ownerFrameBackendNodeId,
|
|
||||||
tag,
|
|
||||||
attrs,
|
|
||||||
rect,
|
|
||||||
localRect,
|
|
||||||
paintOrder,
|
|
||||||
position,
|
|
||||||
pointerEvents,
|
|
||||||
cursor,
|
|
||||||
rendered,
|
|
||||||
textContent,
|
|
||||||
...(formValue !== undefined ? { formValue } : {}),
|
|
||||||
...(tag === "input" || tag === "textarea"
|
|
||||||
? {
|
|
||||||
formPlaceholder: attrs.placeholder ?? "",
|
|
||||||
...(!sensitive ? { formDefaultValue } : {}),
|
|
||||||
...(formState ? { formState } : {}),
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
...(checkedInputs.has(n) ? { formValue: "true", formState: "filled" } : {}),
|
|
||||||
...(selectedOptions.has(n) ? { formValue: attrs.value ?? textContent ?? "" } : {}),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return { nodes, excludedBackendNodeIds };
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ParseFrameDocumentsResult {
|
|
||||||
iframeNodes: CapturedIframeNodes;
|
|
||||||
frameOwnerBackendNodeIds: Map<string, number>;
|
|
||||||
frameParentIds: Map<string, string>;
|
|
||||||
excludedBackendNodeIds: Set<number>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseChildFrameDocuments(
|
|
||||||
documents: SnapshotDocument[],
|
|
||||||
strings: string[],
|
|
||||||
dpr: number,
|
|
||||||
parentDocIndex: number,
|
|
||||||
parentNodes: CapturedNode[],
|
|
||||||
parentContext: FrameContext,
|
|
||||||
visited = new Set<number>(),
|
|
||||||
): ParseFrameDocumentsResult {
|
|
||||||
const iframeNodes: CapturedIframeNodes = new Map();
|
|
||||||
const frameOwnerBackendNodeIds = new Map<string, number>();
|
|
||||||
const frameParentIds = new Map<string, string>();
|
|
||||||
const excludedBackendNodeIds = new Set<number>();
|
|
||||||
const parentDoc = documents[parentDocIndex];
|
|
||||||
const cdi = sparseIndexMap(parentDoc?.nodes?.contentDocumentIndex);
|
|
||||||
if (cdi.size === 0) {
|
|
||||||
return { iframeNodes, frameOwnerBackendNodeIds, frameParentIds, excludedBackendNodeIds };
|
|
||||||
}
|
|
||||||
|
|
||||||
const parentBackendIds = parentDoc?.nodes?.backendNodeId ?? [];
|
|
||||||
const parentNodeByBackendId = new Map(parentNodes.map((node) => [node.backendNodeId, node]));
|
|
||||||
|
|
||||||
for (const [nodeArrayIdx, childDocIndex] of cdi) {
|
|
||||||
if (visited.has(childDocIndex)) continue;
|
|
||||||
const childDoc = documents[childDocIndex];
|
|
||||||
if (!childDoc) continue;
|
|
||||||
const iframeBackendId = parentBackendIds[nodeArrayIdx];
|
|
||||||
if (iframeBackendId === undefined) continue;
|
|
||||||
const iframeNode = parentNodeByBackendId.get(iframeBackendId);
|
|
||||||
const projection =
|
|
||||||
parentContext.projection && iframeNode?.localRect
|
|
||||||
? childFrameProjection(parentContext.projection, iframeNode.localRect)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
const childContext: FrameContext = {
|
|
||||||
frameId: snapshotFrameId(childDoc, strings),
|
|
||||||
ownerFrameBackendNodeId: iframeBackendId,
|
|
||||||
projection,
|
|
||||||
scrollX: childDoc.scrollOffsetX ?? 0,
|
|
||||||
scrollY: childDoc.scrollOffsetY ?? 0,
|
|
||||||
};
|
|
||||||
const nextVisited = new Set(visited);
|
|
||||||
nextVisited.add(childDocIndex);
|
|
||||||
const parsed = parseDocumentNodes(childDoc, strings, dpr, childContext);
|
|
||||||
iframeNodes.set(iframeBackendId, parsed.nodes);
|
|
||||||
if (childContext.frameId) {
|
|
||||||
frameOwnerBackendNodeIds.set(childContext.frameId, iframeBackendId);
|
|
||||||
if (parentContext.frameId) frameParentIds.set(childContext.frameId, parentContext.frameId);
|
|
||||||
}
|
|
||||||
for (const id of parsed.excludedBackendNodeIds) excludedBackendNodeIds.add(id);
|
|
||||||
|
|
||||||
const nested = parseChildFrameDocuments(
|
|
||||||
documents,
|
|
||||||
strings,
|
|
||||||
dpr,
|
|
||||||
childDocIndex,
|
|
||||||
parsed.nodes,
|
|
||||||
childContext,
|
|
||||||
nextVisited,
|
|
||||||
);
|
|
||||||
for (const [nestedFrameId, nestedNodes] of nested.iframeNodes) {
|
|
||||||
iframeNodes.set(nestedFrameId, nestedNodes);
|
|
||||||
}
|
|
||||||
for (const [frameId, ownerBackendNodeId] of nested.frameOwnerBackendNodeIds) {
|
|
||||||
frameOwnerBackendNodeIds.set(frameId, ownerBackendNodeId);
|
|
||||||
}
|
|
||||||
for (const [frameId, parentFrameId] of nested.frameParentIds) {
|
|
||||||
frameParentIds.set(frameId, parentFrameId);
|
|
||||||
}
|
|
||||||
for (const id of nested.excludedBackendNodeIds) excludedBackendNodeIds.add(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
return { iframeNodes, frameOwnerBackendNodeIds, frameParentIds, excludedBackendNodeIds };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function captureViewModel(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
tabId: number,
|
|
||||||
options: CaptureViewModelOptions = {},
|
|
||||||
): Promise<CapturedViewModel> {
|
|
||||||
throwIfAborted(options.signal);
|
|
||||||
let metrics: LayoutMetricsReply = {};
|
|
||||||
try {
|
|
||||||
metrics = await cdp.send<LayoutMetricsReply>(tabId, "Page.getLayoutMetrics", {});
|
|
||||||
} catch (error) {
|
|
||||||
if (isAbortError(error)) throw error;
|
|
||||||
console.debug("[bsk capture] layout metrics unavailable", error);
|
|
||||||
}
|
|
||||||
throwIfAborted(options.signal);
|
|
||||||
const dpr = devicePixelRatio(metrics);
|
|
||||||
const vpSrc = metrics.cssLayoutViewport ?? metrics.layoutViewport ?? {};
|
|
||||||
const viewport: Viewport = {
|
|
||||||
width: vpSrc.clientWidth ?? 0,
|
|
||||||
height: vpSrc.clientHeight ?? 0,
|
|
||||||
};
|
|
||||||
const scrollX = vpSrc.pageX ?? 0;
|
|
||||||
const scrollY = vpSrc.pageY ?? 0;
|
|
||||||
|
|
||||||
await cdp.send(tabId, "DOMSnapshot.enable", {});
|
|
||||||
throwIfAborted(options.signal);
|
|
||||||
const snap = await cdp.send<SnapshotReply>(tabId, "DOMSnapshot.captureSnapshot", {
|
|
||||||
computedStyles: REQUESTED_STYLES,
|
|
||||||
includePaintOrder: true,
|
|
||||||
includeDOMRects: true,
|
|
||||||
});
|
|
||||||
throwIfAborted(options.signal);
|
|
||||||
|
|
||||||
const strings = snap.strings ?? [];
|
|
||||||
const documents = snap.documents ?? [];
|
|
||||||
const doc0 = documents[0];
|
|
||||||
if (!doc0?.nodes?.backendNodeId) {
|
|
||||||
return {
|
|
||||||
nodes: [],
|
|
||||||
viewport,
|
|
||||||
iframeNodes: new Map(),
|
|
||||||
excludedBackendNodeIds: new Set(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const topContext: FrameContext = {
|
|
||||||
frameId: snapshotFrameId(doc0, strings),
|
|
||||||
ownerFrameBackendNodeId: null,
|
|
||||||
projection: {
|
|
||||||
sourceClips: [],
|
|
||||||
edges: [],
|
|
||||||
topViewport: viewport,
|
|
||||||
},
|
|
||||||
scrollX,
|
|
||||||
scrollY,
|
|
||||||
};
|
|
||||||
const mainParsed = parseDocumentNodes(doc0, strings, dpr, topContext);
|
|
||||||
const nodes = mainParsed.nodes;
|
|
||||||
const excludedBackendNodeIds = new Set(mainParsed.excludedBackendNodeIds);
|
|
||||||
|
|
||||||
const frameParsed = parseChildFrameDocuments(documents, strings, dpr, 0, nodes, topContext);
|
|
||||||
const iframeNodes = frameParsed.iframeNodes;
|
|
||||||
for (const id of frameParsed.excludedBackendNodeIds) {
|
|
||||||
excludedBackendNodeIds.add(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
await enrichFormControlStates(cdp, tabId, [nodes, ...iframeNodes.values()], options.signal);
|
|
||||||
throwIfAborted(options.signal);
|
|
||||||
|
|
||||||
const frameNodes = new Map<string, CapturedNode[]>();
|
|
||||||
if (topContext.frameId) frameNodes.set(topContext.frameId, nodes);
|
|
||||||
for (const iframeFrameNodes of iframeNodes.values()) {
|
|
||||||
const frameId = iframeFrameNodes.find((node) => node.frameId)?.frameId;
|
|
||||||
if (frameId) frameNodes.set(frameId, iframeFrameNodes);
|
|
||||||
}
|
|
||||||
for (const [frameId, ownerBackendNodeId] of frameParsed.frameOwnerBackendNodeIds) {
|
|
||||||
if (!frameNodes.has(frameId)) {
|
|
||||||
frameNodes.set(frameId, iframeNodes.get(ownerBackendNodeId) ?? []);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
nodes,
|
|
||||||
viewport,
|
|
||||||
iframeNodes,
|
|
||||||
frameNodes,
|
|
||||||
frameOwnerBackendNodeIds: frameParsed.frameOwnerBackendNodeIds,
|
|
||||||
frameParentIds: frameParsed.frameParentIds,
|
|
||||||
...(topContext.frameId ? { rootFrameId: topContext.frameId } : {}),
|
|
||||||
excludedBackendNodeIds,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import type { CdpFrame } from "@/browser-driver/frame-graph";
|
||||||
|
import type { CdpRunner } from "../shared";
|
||||||
|
import { sendToCdpTarget } from "../shared";
|
||||||
|
import { type DocumentIdentity, isCaptureAbort, throwCaptureAborted } from "./facts";
|
||||||
|
|
||||||
|
/** Fresh, frame-scoped identity read. An isolated world supplies the owning
|
||||||
|
* document without querying each node or installing a page observer. */
|
||||||
|
export async function readDocumentIdentity(
|
||||||
|
cdp: CdpRunner,
|
||||||
|
frame: CdpFrame,
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<DocumentIdentity | undefined> {
|
||||||
|
const attachmentId = cdp.getAttachmentId?.(frame.target.tabId);
|
||||||
|
if (!attachmentId || !frame.loaderId) return undefined;
|
||||||
|
let objectId: string | undefined;
|
||||||
|
const send = <T>(method: string, params: object) => {
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
return sendToCdpTarget<T>(cdp, frame.target, method, params);
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
const world = await send<{ executionContextId: number }>("Page.createIsolatedWorld", {
|
||||||
|
frameId: frame.frameId,
|
||||||
|
worldName: "bsk-document-identity",
|
||||||
|
});
|
||||||
|
const element = await send<{ result?: { objectId?: string } }>("Runtime.evaluate", {
|
||||||
|
expression: "document.documentElement",
|
||||||
|
contextId: world.executionContextId,
|
||||||
|
});
|
||||||
|
objectId = element.result?.objectId;
|
||||||
|
if (!objectId) return undefined;
|
||||||
|
const reply = await send<{ node?: { backendNodeId?: number } }>("DOM.describeNode", {
|
||||||
|
objectId,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
const backendNodeId = reply.node?.backendNodeId;
|
||||||
|
if (!backendNodeId || cdp.getAttachmentId?.(frame.target.tabId) !== attachmentId)
|
||||||
|
return undefined;
|
||||||
|
return {
|
||||||
|
attachmentId,
|
||||||
|
target: frame.target,
|
||||||
|
frameId: frame.frameId,
|
||||||
|
loaderId: frame.loaderId,
|
||||||
|
documentElementBackendNodeId: backendNodeId,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
return undefined;
|
||||||
|
} finally {
|
||||||
|
if (objectId)
|
||||||
|
await sendToCdpTarget(cdp, frame.target, "Runtime.releaseObject", { objectId }).catch(
|
||||||
|
() => {},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sameDocument(a: DocumentIdentity, b: DocumentIdentity): boolean {
|
||||||
|
return (
|
||||||
|
a.attachmentId === b.attachmentId &&
|
||||||
|
a.target.tabId === b.target.tabId &&
|
||||||
|
a.target.sessionId === b.target.sessionId &&
|
||||||
|
a.frameId === b.frameId &&
|
||||||
|
a.loaderId === b.loaderId &&
|
||||||
|
a.documentElementBackendNodeId === b.documentElementBackendNodeId
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,221 @@
|
|||||||
|
import type { Rect, Viewport } from "@browser-skill/vom";
|
||||||
|
import type { CdpFrame, CdpTarget } from "@/browser-driver/frame-graph";
|
||||||
|
import { isOverlayHostNode } from "@/lib/overlay-bridge";
|
||||||
|
import type { FrameOwnedAxNode } from "./frame-document";
|
||||||
|
|
||||||
|
export interface CapturedNode {
|
||||||
|
backendNodeId: number;
|
||||||
|
parentBackendNodeId: number | null;
|
||||||
|
frameId?: string;
|
||||||
|
/** Owning iframe backend node id; `null` for the top-level document. */
|
||||||
|
ownerFrameBackendNodeId?: number | null;
|
||||||
|
tag: string;
|
||||||
|
attrs: Record<string, string>;
|
||||||
|
/** Top-level viewport-relative CSS px, clipped to the owning frame viewport. */
|
||||||
|
rect: Rect | null;
|
||||||
|
/** Frame-local viewport-relative CSS px before top-level projection. */
|
||||||
|
localRect?: Rect | null;
|
||||||
|
paintOrder: number;
|
||||||
|
position: string;
|
||||||
|
pointerEvents: string;
|
||||||
|
/**
|
||||||
|
* computed `cursor`. `cursor: pointer` is the strongest CDP-free signal
|
||||||
|
* that a non-semantic element (a `<div>`/`<span>` with a click handler)
|
||||||
|
* is actually an interactive control — used by the adapter to surface
|
||||||
|
* custom buttons/checkboxes the AX tree drops as `generic`. Optional like
|
||||||
|
* `textContent`: the live parser always sets it, hand-built fixtures may not.
|
||||||
|
*/
|
||||||
|
cursor?: string;
|
||||||
|
/**
|
||||||
|
* Whether the live DOM snapshot provides a painted, non-hidden box for this
|
||||||
|
* node. Semantic resolution uses this only for DOM fallback nodes; AX-backed
|
||||||
|
* nodes remain authoritative even when they are outside the viewport.
|
||||||
|
*/
|
||||||
|
rendered?: boolean;
|
||||||
|
textContent?: string;
|
||||||
|
formState?: "empty" | "filled" | "default";
|
||||||
|
formValue?: string;
|
||||||
|
formDefaultValue?: string;
|
||||||
|
formPlaceholder?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CapturedSurfaceProbe {
|
||||||
|
triggerBackendNodeId: number;
|
||||||
|
triggerPoint?: { x: number; y: number };
|
||||||
|
triggerAction: "hover" | "focus" | string;
|
||||||
|
subItems: string[];
|
||||||
|
confidence?: "high" | "medium" | "low";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Narrow input shared by the existing semantic scene/hover consumers. */
|
||||||
|
export interface CapturedSceneInput {
|
||||||
|
nodes: CapturedNode[];
|
||||||
|
viewport: Viewport;
|
||||||
|
rootFrameId?: string;
|
||||||
|
excludedBackendNodeIds: ReadonlySet<number>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DocumentIdentity {
|
||||||
|
attachmentId: string;
|
||||||
|
target: CdpTarget;
|
||||||
|
frameId: string;
|
||||||
|
loaderId: string;
|
||||||
|
documentElementBackendNodeId: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Snapshot client/offset rectangles retain their protocol units. In particular,
|
||||||
|
* transformed bounds cannot be combined with unscaled client offsets. */
|
||||||
|
export interface SnapshotLayout {
|
||||||
|
readonly boundsSpace: "snapshot-document-css";
|
||||||
|
readonly clientSpace: "unscaled-client-offset-css";
|
||||||
|
bounds?: number[];
|
||||||
|
clientRect?: number[];
|
||||||
|
offsetRect?: number[];
|
||||||
|
scrollRect?: number[];
|
||||||
|
styles: Readonly<Record<string, string>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DecodedNode extends Omit<CapturedNode, "rect" | "localRect" | "rendered"> {
|
||||||
|
nodeType?: number;
|
||||||
|
parentMissing?: boolean;
|
||||||
|
layout?: SnapshotLayout;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AncestorState {
|
||||||
|
complete: boolean;
|
||||||
|
overlay: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NodeFacts extends CapturedNode {
|
||||||
|
nodeType?: number;
|
||||||
|
layout?: SnapshotLayout;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DocumentIndex<T extends DecodedNode = NodeFacts> {
|
||||||
|
readonly nodes: ReadonlyMap<number, T>;
|
||||||
|
readonly children: ReadonlyMap<number, readonly number[]>;
|
||||||
|
readonly ancestry: ReadonlyMap<number, AncestorState>;
|
||||||
|
readonly excludedBackendNodeIds: ReadonlySet<number>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DecodedDocument {
|
||||||
|
nodes: DecodedNode[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CaptureIssue {
|
||||||
|
target: CdpTarget;
|
||||||
|
frameId?: string;
|
||||||
|
stage: "dom" | "ax" | "identity" | "ownership" | "geometry" | "forms";
|
||||||
|
reason:
|
||||||
|
| "capture-unavailable"
|
||||||
|
| "document-changed"
|
||||||
|
| "identity-unverified"
|
||||||
|
| "frame-ownership-unresolved"
|
||||||
|
| "geometry-unavailable";
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DocumentFacts<T extends FrameOwnedAxNode> {
|
||||||
|
readonly frame: CdpFrame;
|
||||||
|
readonly identity?: DocumentIdentity;
|
||||||
|
readonly index: DocumentIndex;
|
||||||
|
readonly domNodes: CapturedNode[];
|
||||||
|
readonly axNodes: T[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ObservationFacts<T extends FrameOwnedAxNode> {
|
||||||
|
readonly rootFrameId: string;
|
||||||
|
readonly viewport: Viewport;
|
||||||
|
readonly documents: readonly DocumentFacts<T>[];
|
||||||
|
readonly issues: readonly CaptureIssue[];
|
||||||
|
readonly startedAt: number;
|
||||||
|
readonly finishedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function throwCaptureAborted(signal?: AbortSignal): void {
|
||||||
|
if (signal?.aborted) throw new DOMException("observation aborted", "AbortError");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isCaptureAbort(error: unknown): boolean {
|
||||||
|
return error instanceof Error && error.name === "AbortError";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Called only at bounded block boundaries, not once per node. */
|
||||||
|
export async function captureCheckpoint(signal?: AbortSignal): Promise<void> {
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
await new Promise<void>((resolve) => {
|
||||||
|
const channel = new MessageChannel();
|
||||||
|
channel.port1.onmessage = () => {
|
||||||
|
channel.port1.close();
|
||||||
|
channel.port2.close();
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
channel.port2.postMessage(null);
|
||||||
|
});
|
||||||
|
throwCaptureAborted(signal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** All snapshot nodes participate, including document and shadow roots. This
|
||||||
|
* preserves ancestry evidence even when the semantic adapter omits those nodes. */
|
||||||
|
export async function buildDocumentIndex<T extends DecodedNode>(
|
||||||
|
input: readonly T[],
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<DocumentIndex<T>> {
|
||||||
|
const nodes = new Map<number, T>();
|
||||||
|
const children = new Map<number, number[]>();
|
||||||
|
const ancestry = new Map<number, AncestorState>();
|
||||||
|
const excludedBackendNodeIds = new Set<number>();
|
||||||
|
for (let i = 0; i < input.length; i++) {
|
||||||
|
if (i % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
const node = input[i];
|
||||||
|
nodes.set(node.backendNodeId, node);
|
||||||
|
if (node.parentBackendNodeId !== null) {
|
||||||
|
const siblings = children.get(node.parentBackendNodeId);
|
||||||
|
if (siblings) siblings.push(node.backendNodeId);
|
||||||
|
else children.set(node.parentBackendNodeId, [node.backendNodeId]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let work = 0;
|
||||||
|
for (const node of input) {
|
||||||
|
if (work++ % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
if (ancestry.has(node.backendNodeId)) continue;
|
||||||
|
const path: DecodedNode[] = [];
|
||||||
|
const visiting = new Set<number>();
|
||||||
|
let current: DecodedNode | undefined = node;
|
||||||
|
let state: AncestorState = { complete: true, overlay: false };
|
||||||
|
while (current && !ancestry.has(current.backendNodeId)) {
|
||||||
|
if (work++ % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
if (visiting.has(current.backendNodeId)) {
|
||||||
|
// No member of a malformed cycle supplies complete ancestry evidence.
|
||||||
|
state = {
|
||||||
|
complete: false,
|
||||||
|
overlay: path
|
||||||
|
.slice(path.findIndex((item) => item.backendNodeId === current!.backendNodeId))
|
||||||
|
.some((item) => isOverlayHostNode(item.tag, Object.keys(item.attrs))),
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
visiting.add(current.backendNodeId);
|
||||||
|
path.push(current);
|
||||||
|
if (current.parentMissing) state = { complete: false, overlay: false };
|
||||||
|
if (current.parentBackendNodeId === null) {
|
||||||
|
current = undefined;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current = nodes.get(current.parentBackendNodeId);
|
||||||
|
if (!current) state = { complete: false, overlay: false };
|
||||||
|
}
|
||||||
|
if (current && ancestry.has(current.backendNodeId))
|
||||||
|
state = ancestry.get(current.backendNodeId)!;
|
||||||
|
for (let i = path.length - 1; i >= 0; i--) {
|
||||||
|
if (work++ % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
const item = path[i];
|
||||||
|
state = {
|
||||||
|
complete: state.complete,
|
||||||
|
overlay: state.overlay || isOverlayHostNode(item.tag, Object.keys(item.attrs)),
|
||||||
|
};
|
||||||
|
ancestry.set(item.backendNodeId, state);
|
||||||
|
if (state.overlay) excludedBackendNodeIds.add(item.backendNodeId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { nodes, children, ancestry, excludedBackendNodeIds };
|
||||||
|
}
|
||||||
@@ -0,0 +1,154 @@
|
|||||||
|
import type { CdpRunner } from "../shared";
|
||||||
|
import type { CapturedNode } from "./facts";
|
||||||
|
import { isCaptureAbort as isAbortError, throwCaptureAborted as throwIfAborted } from "./facts";
|
||||||
|
|
||||||
|
interface RuntimeEvaluateReply {
|
||||||
|
result?: { deepSerializedValue?: DeepSerializedValue };
|
||||||
|
}
|
||||||
|
const MAX_FORM_ENRICH_CONTROLS = 250;
|
||||||
|
|
||||||
|
interface CapturedFormState {
|
||||||
|
value?: string;
|
||||||
|
defaultValue?: string;
|
||||||
|
placeholder?: string;
|
||||||
|
state?: "empty" | "filled" | "default";
|
||||||
|
sensitive?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DeepSerializedValue {
|
||||||
|
type: string;
|
||||||
|
value?: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formStateBatchExpression(maxControls: number): string {
|
||||||
|
return `(() => {
|
||||||
|
const maxControls = ${JSON.stringify(maxControls)};
|
||||||
|
let remaining = maxControls;
|
||||||
|
const controlSelector = "input,textarea,select";
|
||||||
|
const controlState = (el) => {
|
||||||
|
const tag = el.tagName.toLowerCase();
|
||||||
|
const type = tag === "input" ? String(el.type || "text").toLowerCase() : tag;
|
||||||
|
const sensitive = type === "password" || type === "credit-card";
|
||||||
|
const rawValue = typeof el.value === "string" ? el.value : "";
|
||||||
|
const defaultValue = typeof el.defaultValue === "string" ? el.defaultValue : "";
|
||||||
|
const placeholder = typeof el.placeholder === "string" ? el.placeholder : "";
|
||||||
|
const state = rawValue === "" ? "empty" : rawValue === defaultValue ? "default" : "filled";
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
sensitive,
|
||||||
|
placeholder,
|
||||||
|
...(sensitive ? {} : { value: rawValue, defaultValue }),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
const controls = [];
|
||||||
|
const collect = (doc) => {
|
||||||
|
for (const el of Array.from(doc.querySelectorAll(controlSelector))) {
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
// Deep serialization supplies the node's backend id. Keep the
|
||||||
|
// state as JSON so decoding needs no general-purpose V8 deserializer.
|
||||||
|
controls.push([el, JSON.stringify(controlState(el))]);
|
||||||
|
remaining -= 1;
|
||||||
|
}
|
||||||
|
for (const frame of Array.from(doc.querySelectorAll("iframe"))) {
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
let childDoc = null;
|
||||||
|
try { childDoc = frame.contentDocument; } catch { childDoc = null; }
|
||||||
|
if (childDoc) collect(childDoc);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
collect(document);
|
||||||
|
return controls;
|
||||||
|
})()`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formStatesByBackendId(result: RuntimeEvaluateReply): Map<number, CapturedFormState> {
|
||||||
|
const states = new Map<number, CapturedFormState>();
|
||||||
|
const serialized = result.result?.deepSerializedValue;
|
||||||
|
if (serialized?.type !== "array" || !Array.isArray(serialized.value)) return states;
|
||||||
|
for (const entry of serialized.value as DeepSerializedValue[]) {
|
||||||
|
if (entry?.type !== "array" || !Array.isArray(entry.value)) continue;
|
||||||
|
const [element, json] = entry.value as DeepSerializedValue[];
|
||||||
|
if (element?.type !== "node" || json?.type !== "string" || typeof json.value !== "string") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const backendNodeId = (element.value as { backendNodeId?: number } | undefined)?.backendNodeId;
|
||||||
|
if (typeof backendNodeId !== "number" || !Number.isSafeInteger(backendNodeId)) continue;
|
||||||
|
try {
|
||||||
|
const state = JSON.parse(json.value) as CapturedFormState | null;
|
||||||
|
if (
|
||||||
|
!state ||
|
||||||
|
!["empty", "filled", "default"].includes(state.state ?? "") ||
|
||||||
|
typeof state.sensitive !== "boolean" ||
|
||||||
|
typeof state.placeholder !== "string" ||
|
||||||
|
(!state.sensitive &&
|
||||||
|
(typeof state.value !== "string" || typeof state.defaultValue !== "string"))
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
states.set(backendNodeId, state);
|
||||||
|
} catch {
|
||||||
|
// A malformed entry must not overwrite the snapshot's own state.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return states;
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyFormStates(nodes: CapturedNode[], states: Map<number, CapturedFormState>): void {
|
||||||
|
for (const node of nodes) {
|
||||||
|
if (!["input", "textarea", "select"].includes(node.tag)) continue;
|
||||||
|
const state = states.get(node.backendNodeId);
|
||||||
|
if (!state) continue;
|
||||||
|
node.formState = state.state;
|
||||||
|
node.formPlaceholder = state.placeholder ?? "";
|
||||||
|
if (
|
||||||
|
state.sensitive ||
|
||||||
|
(node.tag === "input" && (node.attrs.type ?? "").toLowerCase() === "password")
|
||||||
|
) {
|
||||||
|
delete node.formValue;
|
||||||
|
delete node.formDefaultValue;
|
||||||
|
delete node.attrs.value;
|
||||||
|
} else {
|
||||||
|
node.formDefaultValue = state.defaultValue ?? "";
|
||||||
|
if (state.value !== undefined) node.formValue = state.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function enrichFormControlStates(
|
||||||
|
cdp: CdpRunner,
|
||||||
|
tabId: number,
|
||||||
|
frameNodeGroups: CapturedNode[][],
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<boolean> {
|
||||||
|
const hasControls = frameNodeGroups.some((nodes) =>
|
||||||
|
nodes.some((node) => ["input", "textarea", "select"].includes(node.tag)),
|
||||||
|
);
|
||||||
|
if (!hasControls) return true;
|
||||||
|
const objectGroup = `bsk-vom-forms-${crypto.randomUUID()}`;
|
||||||
|
try {
|
||||||
|
throwIfAborted(signal);
|
||||||
|
const result = await cdp.send<RuntimeEvaluateReply>(tabId, "Runtime.evaluate", {
|
||||||
|
expression: formStateBatchExpression(MAX_FORM_ENRICH_CONTROLS),
|
||||||
|
objectGroup,
|
||||||
|
serializationOptions: {
|
||||||
|
serialization: "deep",
|
||||||
|
maxDepth: 3,
|
||||||
|
additionalParameters: { maxNodeDepth: 0, includeShadowTree: "none" },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
throwIfAborted(signal);
|
||||||
|
// Backend ids are scoped to this capture's CDP target. OOPIF targets
|
||||||
|
// have separate enrichment calls and never share this lookup.
|
||||||
|
const states = formStatesByBackendId(result);
|
||||||
|
for (const nodes of frameNodeGroups) {
|
||||||
|
applyFormStates(nodes, states);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
if (isAbortError(error)) throw error;
|
||||||
|
// Best-effort enrichment. DOMSnapshot/AX data still carries the nodes.
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
await cdp.send(tabId, "Runtime.releaseObjectGroup", { objectGroup }).catch(() => undefined);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,181 +0,0 @@
|
|||||||
import type { CdpFrame, CdpFrameGraph } from "@/browser-driver/frame-graph";
|
|
||||||
import { resolveFrameProjection } from "../frame-geometry";
|
|
||||||
import { type GeometryProjection, projectRectToViewport } from "../geometry";
|
|
||||||
import { type CdpRunner, cdpRunnerForTarget, sendToCdpTarget } from "../shared";
|
|
||||||
import { type CapturedNode, type CapturedViewModel, captureViewModel } from "./capture";
|
|
||||||
import {
|
|
||||||
buildFrameDocuments,
|
|
||||||
type FrameAxBatch,
|
|
||||||
type FrameDocument,
|
|
||||||
type FrameOwnedAxNode,
|
|
||||||
} from "./frame-document";
|
|
||||||
|
|
||||||
export type FrameAxNode = FrameOwnedAxNode;
|
|
||||||
export type CapturedFrameDocument<T extends FrameAxNode> = FrameDocument<T>;
|
|
||||||
|
|
||||||
function throwIfAborted(signal?: AbortSignal): void {
|
|
||||||
if (signal?.aborted) throw new DOMException("observation aborted", "AbortError");
|
|
||||||
}
|
|
||||||
|
|
||||||
function isAbortError(err: unknown): boolean {
|
|
||||||
return err instanceof Error && err.name === "AbortError";
|
|
||||||
}
|
|
||||||
|
|
||||||
async function discoverFrameGraph(cdp: CdpRunner, tabId: number): Promise<CdpFrameGraph | null> {
|
|
||||||
if (!cdp.getFrameGraph) return null;
|
|
||||||
try {
|
|
||||||
return await cdp.getFrameGraph(tabId);
|
|
||||||
} catch (err) {
|
|
||||||
console.debug("[bsk observation] frame graph capture failed", err);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function transformFrameNodes(
|
|
||||||
nodes: CapturedNode[],
|
|
||||||
projection: GeometryProjection | null,
|
|
||||||
): CapturedNode[] {
|
|
||||||
return nodes.map((node) => ({
|
|
||||||
...node,
|
|
||||||
rect: projection
|
|
||||||
? (() => {
|
|
||||||
const bounds = projectRectToViewport(node.rect, projection);
|
|
||||||
return bounds ? { x: bounds.x, y: bounds.y, w: bounds.width, h: bounds.height } : null;
|
|
||||||
})()
|
|
||||||
: null,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function captureMissingFrameDocuments(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
tabId: number,
|
|
||||||
graph: CdpFrameGraph,
|
|
||||||
captured: CapturedViewModel,
|
|
||||||
signal?: AbortSignal,
|
|
||||||
): Promise<void> {
|
|
||||||
if (!captured.frameNodes) captured.frameNodes = new Map();
|
|
||||||
if (!captured.frameOwnerBackendNodeIds) captured.frameOwnerBackendNodeIds = new Map();
|
|
||||||
if (!captured.frameParentIds) captured.frameParentIds = new Map();
|
|
||||||
|
|
||||||
for (const frame of graph.frames) {
|
|
||||||
if (!frame.target.sessionId || captured.frameNodes.has(frame.frameId)) continue;
|
|
||||||
try {
|
|
||||||
const child = await captureViewModel(cdpRunnerForTarget(cdp, frame.target), tabId, {
|
|
||||||
signal,
|
|
||||||
});
|
|
||||||
let projection: GeometryProjection | null = null;
|
|
||||||
try {
|
|
||||||
projection = await resolveFrameProjection(cdp, graph, frame.frameId);
|
|
||||||
} catch (err) {
|
|
||||||
if (isAbortError(err)) throw err;
|
|
||||||
console.debug("[bsk observation] child frame geometry projection failed", {
|
|
||||||
frameId: frame.frameId,
|
|
||||||
err,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const rootFrameId = child.rootFrameId ?? frame.frameId;
|
|
||||||
const childFrames = child.frameNodes ?? new Map<string, CapturedNode[]>();
|
|
||||||
if (!childFrames.has(rootFrameId)) childFrames.set(rootFrameId, child.nodes);
|
|
||||||
if (rootFrameId !== frame.frameId && !childFrames.has(frame.frameId)) {
|
|
||||||
childFrames.set(
|
|
||||||
frame.frameId,
|
|
||||||
child.nodes.map((node) => ({ ...node, frameId: frame.frameId })),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
for (const [childFrameId, nodes] of childFrames) {
|
|
||||||
captured.frameNodes.set(childFrameId, transformFrameNodes(nodes, projection));
|
|
||||||
}
|
|
||||||
if (frame.ownerBackendNodeId !== undefined) {
|
|
||||||
captured.iframeNodes.set(
|
|
||||||
frame.ownerBackendNodeId,
|
|
||||||
captured.frameNodes.get(frame.frameId) ?? [],
|
|
||||||
);
|
|
||||||
captured.frameOwnerBackendNodeIds.set(frame.frameId, frame.ownerBackendNodeId);
|
|
||||||
}
|
|
||||||
if (frame.parentFrameId) captured.frameParentIds.set(frame.frameId, frame.parentFrameId);
|
|
||||||
for (const [childFrameId, ownerBackendNodeId] of child.frameOwnerBackendNodeIds ?? []) {
|
|
||||||
captured.frameOwnerBackendNodeIds.set(childFrameId, ownerBackendNodeId);
|
|
||||||
}
|
|
||||||
for (const [childFrameId, parentFrameId] of child.frameParentIds ?? []) {
|
|
||||||
captured.frameParentIds.set(childFrameId, parentFrameId);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
if (isAbortError(err)) throw err;
|
|
||||||
console.debug("[bsk observation] child frame DOM capture failed", {
|
|
||||||
frameId: frame.frameId,
|
|
||||||
err,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function captureAxTrees<T extends FrameAxNode>(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
tabId: number,
|
|
||||||
discoveredFrames: CdpFrame[],
|
|
||||||
captured: CapturedViewModel,
|
|
||||||
signal?: AbortSignal,
|
|
||||||
): Promise<FrameAxBatch<T>[]> {
|
|
||||||
let frames = [...discoveredFrames];
|
|
||||||
const capturedFrameIds = [...(captured.frameNodes?.keys() ?? [])];
|
|
||||||
if (frames.length === 0) {
|
|
||||||
const rootFrameId = captured.rootFrameId ?? capturedFrameIds[0] ?? "root";
|
|
||||||
frames = [{ frameId: rootFrameId, target: { tabId } }];
|
|
||||||
}
|
|
||||||
for (const frameId of capturedFrameIds) {
|
|
||||||
if (!frames.some((frame) => frame.frameId === frameId)) {
|
|
||||||
frames.push({ frameId, target: { tabId } });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let firstFailure: unknown;
|
|
||||||
const trees = await Promise.all(
|
|
||||||
frames.map(async (frame): Promise<FrameAxBatch<T>> => {
|
|
||||||
try {
|
|
||||||
throwIfAborted(signal);
|
|
||||||
await sendToCdpTarget(cdp, frame.target, "Accessibility.enable", {});
|
|
||||||
const result = await sendToCdpTarget<{ nodes?: T[] }>(
|
|
||||||
cdp,
|
|
||||||
frame.target,
|
|
||||||
"Accessibility.getFullAXTree",
|
|
||||||
frame.frameId === "root" ? {} : { frameId: frame.frameId },
|
|
||||||
);
|
|
||||||
throwIfAborted(signal);
|
|
||||||
return {
|
|
||||||
frame,
|
|
||||||
nodes: result.nodes ?? [],
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
if (isAbortError(err)) throw err;
|
|
||||||
firstFailure ??= err;
|
|
||||||
console.debug("[bsk observation] frame AX capture failed", {
|
|
||||||
frameId: frame.frameId,
|
|
||||||
err,
|
|
||||||
});
|
|
||||||
return { frame, nodes: [] };
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
const capturedNodeCount = [...captured.iframeNodes.values()].reduce(
|
|
||||||
(count, nodes) => count + nodes.length,
|
|
||||||
captured.nodes.length,
|
|
||||||
);
|
|
||||||
if (firstFailure && capturedNodeCount === 0 && trees.every((tree) => tree.nodes.length === 0)) {
|
|
||||||
throw firstFailure;
|
|
||||||
}
|
|
||||||
return trees;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function captureFrameData<T extends FrameAxNode>(
|
|
||||||
cdp: CdpRunner,
|
|
||||||
tabId: number,
|
|
||||||
captured: CapturedViewModel,
|
|
||||||
signal?: AbortSignal,
|
|
||||||
): Promise<CapturedFrameDocument<T>[]> {
|
|
||||||
const graph = await discoverFrameGraph(cdp, tabId);
|
|
||||||
const frames = graph?.frames ?? [];
|
|
||||||
if (graph) await captureMissingFrameDocuments(cdp, tabId, graph, captured, signal);
|
|
||||||
throwIfAborted(signal);
|
|
||||||
const batches = await captureAxTrees<T>(cdp, tabId, frames, captured, signal);
|
|
||||||
return buildFrameDocuments(graph, batches, captured);
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,8 @@ import {
|
|||||||
type CdpTarget,
|
type CdpTarget,
|
||||||
cdpTargetKey,
|
cdpTargetKey,
|
||||||
} from "@/browser-driver/frame-graph";
|
} from "@/browser-driver/frame-graph";
|
||||||
import type { CapturedNode, CapturedViewModel } from "./capture";
|
import type { CapturedNode } from "./facts";
|
||||||
|
import { captureCheckpoint } from "./facts";
|
||||||
|
|
||||||
export interface FrameOwnedAxNode {
|
export interface FrameOwnedAxNode {
|
||||||
nodeId: string;
|
nodeId: string;
|
||||||
@@ -23,6 +24,16 @@ export interface FrameDocument<T extends FrameOwnedAxNode> extends CdpFrame {
|
|||||||
contextScopeId: string;
|
contextScopeId: string;
|
||||||
axNodes: T[];
|
axNodes: T[];
|
||||||
domNodes: CapturedNode[];
|
domNodes: CapturedNode[];
|
||||||
|
excludedBackendNodeIds?: ReadonlySet<number>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Explicit snapshot document data used to join DOM and AX ownership. */
|
||||||
|
export interface FrameDomInput {
|
||||||
|
nodes: CapturedNode[];
|
||||||
|
rootFrameId?: string;
|
||||||
|
frameNodes?: ReadonlyMap<string, CapturedNode[]>;
|
||||||
|
frameOwnerBackendNodeIds?: ReadonlyMap<string, number>;
|
||||||
|
frameParentIds?: ReadonlyMap<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Ownership {
|
interface Ownership {
|
||||||
@@ -47,17 +58,13 @@ function targetBackendKey(target: CdpTarget, backendNodeId: number): string {
|
|||||||
function frameList<T extends FrameOwnedAxNode>(
|
function frameList<T extends FrameOwnedAxNode>(
|
||||||
graph: CdpFrameGraph | null,
|
graph: CdpFrameGraph | null,
|
||||||
batches: FrameAxBatch<T>[],
|
batches: FrameAxBatch<T>[],
|
||||||
captured: CapturedViewModel,
|
captured: FrameDomInput,
|
||||||
): CdpFrame[] {
|
): CdpFrame[] {
|
||||||
const frames = new Map<string, CdpFrame>();
|
const frames = new Map<string, CdpFrame>();
|
||||||
for (const frame of graph?.frames ?? []) frames.set(frame.frameId, frame);
|
for (const frame of graph?.frames ?? []) frames.set(frame.frameId, frame);
|
||||||
for (const batch of batches) {
|
for (const batch of batches) {
|
||||||
if (!frames.has(batch.frame.frameId)) frames.set(batch.frame.frameId, batch.frame);
|
if (!frames.has(batch.frame.frameId)) frames.set(batch.frame.frameId, batch.frame);
|
||||||
}
|
}
|
||||||
for (const frameId of captured.frameNodes?.keys() ?? []) {
|
|
||||||
if (!frames.has(frameId))
|
|
||||||
frames.set(frameId, { frameId, target: { tabId: batches[0]?.frame.target.tabId ?? 0 } });
|
|
||||||
}
|
|
||||||
return [...frames.values()].map((frame) => {
|
return [...frames.values()].map((frame) => {
|
||||||
const ownerBackendNodeId =
|
const ownerBackendNodeId =
|
||||||
frame.ownerBackendNodeId ?? captured.frameOwnerBackendNodeIds?.get(frame.frameId);
|
frame.ownerBackendNodeId ?? captured.frameOwnerBackendNodeIds?.get(frame.frameId);
|
||||||
@@ -70,11 +77,14 @@ function frameList<T extends FrameOwnedAxNode>(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildFrameDocuments<T extends FrameOwnedAxNode>(
|
export async function buildFrameDocuments<T extends FrameOwnedAxNode>(
|
||||||
graph: CdpFrameGraph | null,
|
graph: CdpFrameGraph | null,
|
||||||
batches: FrameAxBatch<T>[],
|
batches: FrameAxBatch<T>[],
|
||||||
captured: CapturedViewModel,
|
captured: FrameDomInput,
|
||||||
): FrameDocument<T>[] {
|
signal?: AbortSignal,
|
||||||
|
unresolved?: (frame: CdpFrame) => void,
|
||||||
|
): Promise<FrameDocument<T>[]> {
|
||||||
|
let work = 0;
|
||||||
const frames = frameList(graph, batches, captured);
|
const frames = frameList(graph, batches, captured);
|
||||||
const frameById = new Map(frames.map((frame) => [frame.frameId, frame]));
|
const frameById = new Map(frames.map((frame) => [frame.frameId, frame]));
|
||||||
const rootFrameId = graph?.rootFrameId ?? captured.rootFrameId ?? frames[0]?.frameId;
|
const rootFrameId = graph?.rootFrameId ?? captured.rootFrameId ?? frames[0]?.frameId;
|
||||||
@@ -83,54 +93,74 @@ export function buildFrameDocuments<T extends FrameOwnedAxNode>(
|
|||||||
const backendOwner = new Map<string, string>();
|
const backendOwner = new Map<string, string>();
|
||||||
for (const frame of frames) {
|
for (const frame of frames) {
|
||||||
for (const node of domNodesForFrame(frame.frameId)) {
|
for (const node of domNodesForFrame(frame.frameId)) {
|
||||||
|
if (work++ % 256 === 0) await captureCheckpoint(signal);
|
||||||
backendOwner.set(targetBackendKey(frame.target, node.backendNodeId), frame.frameId);
|
backendOwner.set(targetBackendKey(frame.target, node.backendNodeId), frame.frameId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const candidates = new Map<string, OwnedCandidate<T>>();
|
const candidates = new Map<string, OwnedCandidate<T>>();
|
||||||
for (const batch of batches) {
|
for (const batch of batches) {
|
||||||
const nodeById = new Map(batch.nodes.map((node) => [node.nodeId, node]));
|
const nodeById = new Map<string, T>();
|
||||||
|
for (const node of batch.nodes) {
|
||||||
|
if (work++ % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
nodeById.set(node.nodeId, node);
|
||||||
|
}
|
||||||
|
let reported = false;
|
||||||
const ownershipByNodeId = new Map<string, Ownership>();
|
const ownershipByNodeId = new Map<string, Ownership>();
|
||||||
const resolving = new Set<string>();
|
const resolveOwnership = async (node: T): Promise<Ownership> => {
|
||||||
const resolveOwnership = (node: T): Ownership => {
|
const path: T[] = [];
|
||||||
const cached = ownershipByNodeId.get(node.nodeId);
|
const visiting = new Set<string>();
|
||||||
if (cached) return cached;
|
let current: T | undefined = node;
|
||||||
if (node.frameId && frameById.has(node.frameId)) {
|
let ownership: Ownership = { frameId: batch.frame.frameId, strength: 1 };
|
||||||
const ownership = { frameId: node.frameId, strength: 4 };
|
while (current) {
|
||||||
ownershipByNodeId.set(node.nodeId, ownership);
|
if (work++ % 256 === 0) await captureCheckpoint(signal);
|
||||||
return ownership;
|
const cached = ownershipByNodeId.get(current.nodeId);
|
||||||
}
|
if (cached) {
|
||||||
if (typeof node.backendDOMNodeId === "number") {
|
ownership = cached;
|
||||||
const frameId = backendOwner.get(
|
break;
|
||||||
targetBackendKey(batch.frame.target, node.backendDOMNodeId),
|
}
|
||||||
);
|
if (visiting.has(current.nodeId)) break;
|
||||||
|
visiting.add(current.nodeId);
|
||||||
|
if (current.frameId) {
|
||||||
|
const frame = frameById.get(current.frameId);
|
||||||
|
// An explicit contradiction must not fall through to a guessed owner.
|
||||||
|
if (!frame || cdpTargetKey(frame.target) !== cdpTargetKey(batch.frame.target)) {
|
||||||
|
if (!reported) {
|
||||||
|
unresolved?.(batch.frame);
|
||||||
|
reported = true;
|
||||||
|
}
|
||||||
|
ownership = { frameId: "", strength: 0 };
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ownership = { frameId: frame.frameId, strength: 4 };
|
||||||
|
ownershipByNodeId.set(current.nodeId, ownership);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const frameId =
|
||||||
|
typeof current.backendDOMNodeId === "number"
|
||||||
|
? backendOwner.get(targetBackendKey(batch.frame.target, current.backendDOMNodeId))
|
||||||
|
: undefined;
|
||||||
if (frameId) {
|
if (frameId) {
|
||||||
const ownership = { frameId, strength: 3 };
|
ownership = { frameId, strength: 3 };
|
||||||
ownershipByNodeId.set(node.nodeId, ownership);
|
ownershipByNodeId.set(current.nodeId, ownership);
|
||||||
return ownership;
|
break;
|
||||||
}
|
}
|
||||||
|
path.push(current);
|
||||||
|
current = current.parentId ? nodeById.get(current.parentId) : undefined;
|
||||||
}
|
}
|
||||||
if (node.parentId && !resolving.has(node.nodeId)) {
|
for (let i = path.length - 1; i >= 0; i--) {
|
||||||
const parent = nodeById.get(node.parentId);
|
if (work++ % 256 === 0) await captureCheckpoint(signal);
|
||||||
if (parent) {
|
ownership = { frameId: ownership.frameId, strength: Math.min(2, ownership.strength) };
|
||||||
resolving.add(node.nodeId);
|
ownershipByNodeId.set(path[i].nodeId, ownership);
|
||||||
const inherited = resolveOwnership(parent);
|
|
||||||
resolving.delete(node.nodeId);
|
|
||||||
const ownership = {
|
|
||||||
frameId: inherited.frameId,
|
|
||||||
strength: Math.min(2, inherited.strength),
|
|
||||||
};
|
|
||||||
ownershipByNodeId.set(node.nodeId, ownership);
|
|
||||||
return ownership;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const ownership = { frameId: batch.frame.frameId, strength: 1 };
|
if (!ownershipByNodeId.has(node.nodeId)) ownershipByNodeId.set(node.nodeId, ownership);
|
||||||
ownershipByNodeId.set(node.nodeId, ownership);
|
|
||||||
return ownership;
|
return ownership;
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const node of batch.nodes) {
|
for (const node of batch.nodes) {
|
||||||
const ownership = resolveOwnership(node);
|
if (work++ % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
const ownership = await resolveOwnership(node);
|
||||||
|
if (!ownership.frameId) continue;
|
||||||
const key = targetNodeKey(batch.frame.target, node.nodeId);
|
const key = targetNodeKey(batch.frame.target, node.nodeId);
|
||||||
const existing = candidates.get(key);
|
const existing = candidates.get(key);
|
||||||
if (!existing || ownership.strength > existing.ownership.strength) {
|
if (!existing || ownership.strength > existing.ownership.strength) {
|
||||||
@@ -144,6 +174,7 @@ export function buildFrameDocuments<T extends FrameOwnedAxNode>(
|
|||||||
);
|
);
|
||||||
const axNodesByFrame = new Map<string, T[]>();
|
const axNodesByFrame = new Map<string, T[]>();
|
||||||
for (const candidate of candidates.values()) {
|
for (const candidate of candidates.values()) {
|
||||||
|
if (work++ % 256 === 0) await captureCheckpoint(signal);
|
||||||
const { node, target, ownership } = candidate;
|
const { node, target, ownership } = candidate;
|
||||||
const parentFrameId = node.parentId
|
const parentFrameId = node.parentId
|
||||||
? ownershipByTargetNode.get(targetNodeKey(target, node.parentId))
|
? ownershipByTargetNode.get(targetNodeKey(target, node.parentId))
|
||||||
|
|||||||
@@ -0,0 +1,184 @@
|
|||||||
|
import type { Viewport } from "@browser-skill/vom";
|
||||||
|
import type { CdpFrame, CdpTarget } from "@/browser-driver/frame-graph";
|
||||||
|
import { type GeometryProjection, projectRectToViewport } from "../geometry";
|
||||||
|
import {
|
||||||
|
projectSnapshotRect,
|
||||||
|
type SnapshotProjection,
|
||||||
|
snapshotViewportRect,
|
||||||
|
} from "../geometry/coordinate-types";
|
||||||
|
import { cssViewport, GeometryContext } from "../geometry/frame-context";
|
||||||
|
import type { CapturedNode } from "./facts";
|
||||||
|
import {
|
||||||
|
buildDocumentIndex,
|
||||||
|
type CaptureIssue,
|
||||||
|
captureCheckpoint,
|
||||||
|
type DocumentIndex,
|
||||||
|
isCaptureAbort,
|
||||||
|
type NodeFacts,
|
||||||
|
} from "./facts";
|
||||||
|
import {
|
||||||
|
decodeDocument,
|
||||||
|
type SnapshotDocument,
|
||||||
|
type SnapshotReply,
|
||||||
|
snapshotFrameId,
|
||||||
|
} from "./snapshot";
|
||||||
|
|
||||||
|
export interface NormalizedDocument {
|
||||||
|
index: DocumentIndex;
|
||||||
|
frame: CdpFrame;
|
||||||
|
domNodes: CapturedNode[];
|
||||||
|
documentElementBackendNodeId?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One target's snapshot. Source ownership is checked before interpreting any
|
||||||
|
* coordinates. Snapshot and live quad coordinates are deliberately not conflated. */
|
||||||
|
export async function normalizeSnapshot(
|
||||||
|
snapshot: SnapshotReply,
|
||||||
|
target: CdpTarget,
|
||||||
|
frames: readonly CdpFrame[],
|
||||||
|
geometry: GeometryContext,
|
||||||
|
issues: CaptureIssue[],
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<NormalizedDocument[]> {
|
||||||
|
const strings = snapshot.strings ?? [];
|
||||||
|
const raw = snapshot.documents ?? [];
|
||||||
|
const frameById = new Map(frames.map((frame) => [frame.frameId, frame]));
|
||||||
|
const sources = new Map<string, SnapshotDocument>();
|
||||||
|
for (const doc of raw) {
|
||||||
|
const id = snapshotFrameId(doc, strings);
|
||||||
|
if (!id || !frameById.has(id) || sources.has(id)) {
|
||||||
|
issues.push({
|
||||||
|
target,
|
||||||
|
frameId: id,
|
||||||
|
stage: "ownership",
|
||||||
|
reason: "frame-ownership-unresolved",
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sources.set(id, doc);
|
||||||
|
}
|
||||||
|
let viewport: Viewport = { width: 0, height: 0 };
|
||||||
|
let scrollX = 0,
|
||||||
|
scrollY = 0;
|
||||||
|
try {
|
||||||
|
const measured = cssViewport(await geometry.layoutMetrics(target));
|
||||||
|
viewport = { width: measured.width, height: measured.height };
|
||||||
|
scrollX = measured.scrollX;
|
||||||
|
scrollY = measured.scrollY;
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
const children = new Map<string, CdpFrame[]>();
|
||||||
|
const pending: CdpFrame[] = [];
|
||||||
|
for (const frame of frames) {
|
||||||
|
if (!frame.parentFrameId || !frameById.has(frame.parentFrameId)) pending.push(frame);
|
||||||
|
else {
|
||||||
|
const siblings = children.get(frame.parentFrameId);
|
||||||
|
if (siblings) siblings.push(frame);
|
||||||
|
else children.set(frame.parentFrameId, [frame]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const projections = new Map<string, SnapshotProjection | null>();
|
||||||
|
const result: NormalizedDocument[] = [];
|
||||||
|
let targetProjection: GeometryProjection | null = null;
|
||||||
|
const targetRoot = pending[0];
|
||||||
|
if (target.sessionId && targetRoot) {
|
||||||
|
try {
|
||||||
|
targetProjection = await geometry.targetProjection(targetRoot.frameId);
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let cursor = 0; cursor < pending.length; cursor++) {
|
||||||
|
if (cursor % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
const frame = pending[cursor];
|
||||||
|
for (const child of children.get(frame.frameId) ?? []) pending.push(child);
|
||||||
|
const doc = sources.get(frame.frameId);
|
||||||
|
if (!doc) continue;
|
||||||
|
const source = { target, frameId: frame.frameId };
|
||||||
|
const parent = frame.parentFrameId ? frameById.get(frame.parentFrameId) : undefined;
|
||||||
|
let projection: SnapshotProjection | null = null;
|
||||||
|
if (!parent)
|
||||||
|
projection = { source, geometry: { sourceClips: [], edges: [], topViewport: viewport } };
|
||||||
|
else {
|
||||||
|
const parentProjection = projections.get(parent.frameId)?.geometry;
|
||||||
|
if (parentProjection && frame.ownerBackendNodeId !== undefined) {
|
||||||
|
const edge = parentProjection.edges[0];
|
||||||
|
const clips = edge
|
||||||
|
? [edge.destinationQuad, ...(edge.destinationClips ?? [])]
|
||||||
|
: parentProjection.sourceClips;
|
||||||
|
try {
|
||||||
|
projection = await geometry.snapshotProjection(
|
||||||
|
source,
|
||||||
|
frame.ownerBackendNodeId,
|
||||||
|
clips,
|
||||||
|
viewport,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
if (isCaptureAbort(error)) throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
projections.set(frame.frameId, projection);
|
||||||
|
if (!projection || (target.sessionId && !targetProjection))
|
||||||
|
issues.push({
|
||||||
|
target,
|
||||||
|
frameId: frame.frameId,
|
||||||
|
stage: "geometry",
|
||||||
|
reason: "geometry-unavailable",
|
||||||
|
});
|
||||||
|
const decoded = await decodeDocument(doc, strings, signal);
|
||||||
|
const nodes: NodeFacts[] = [];
|
||||||
|
for (let i = 0; i < decoded.nodes.length; i++) {
|
||||||
|
if (i % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
const node = decoded.nodes[i];
|
||||||
|
const input = snapshotViewportRect(node.layout?.bounds ?? [], source, {
|
||||||
|
x: doc.scrollOffsetX ?? (parent ? 0 : scrollX),
|
||||||
|
y: doc.scrollOffsetY ?? (parent ? 0 : scrollY),
|
||||||
|
});
|
||||||
|
const local = input?.rect;
|
||||||
|
let rect = input && projection ? projectSnapshotRect(input, projection) : null;
|
||||||
|
if (rect && target.sessionId)
|
||||||
|
rect = targetProjection
|
||||||
|
? projectRectToViewport(
|
||||||
|
{ x: rect.x, y: rect.y, w: rect.width, h: rect.height },
|
||||||
|
targetProjection,
|
||||||
|
)
|
||||||
|
: null;
|
||||||
|
const styles = node.layout?.styles;
|
||||||
|
const visibility = styles?.visibility || "visible";
|
||||||
|
const opacity = styles?.opacity || "1";
|
||||||
|
nodes.push({
|
||||||
|
...node,
|
||||||
|
frameId: frame.frameId,
|
||||||
|
ownerFrameBackendNodeId: frame.ownerBackendNodeId ?? null,
|
||||||
|
localRect: local ? { x: local.x, y: local.y, w: local.width, h: local.height } : null,
|
||||||
|
rect: rect ? { x: rect.x, y: rect.y, w: rect.width, h: rect.height } : null,
|
||||||
|
rendered:
|
||||||
|
!!local &&
|
||||||
|
visibility !== "hidden" &&
|
||||||
|
visibility !== "collapse" &&
|
||||||
|
(Number.parseFloat(opacity) || 0) > 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const index = await buildDocumentIndex(nodes, signal);
|
||||||
|
const domNodes = nodes.filter((node) => !node.tag.startsWith("#"));
|
||||||
|
result.push({
|
||||||
|
index,
|
||||||
|
frame,
|
||||||
|
domNodes,
|
||||||
|
documentElementBackendNodeId: nodes.find(
|
||||||
|
(node) =>
|
||||||
|
node.nodeType === 1 &&
|
||||||
|
node.parentBackendNodeId !== null &&
|
||||||
|
index.nodes.get(node.parentBackendNodeId)?.nodeType === 9,
|
||||||
|
)?.backendNodeId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for (const frameId of sources.keys()) {
|
||||||
|
if (!projections.has(frameId))
|
||||||
|
issues.push({ target, frameId, stage: "ownership", reason: "frame-ownership-unresolved" });
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -2,7 +2,10 @@ import type { Rect, RenderedRef, VomResult } from "@browser-skill/vom";
|
|||||||
import type { CdpTarget } from "@/browser-driver/frame-graph";
|
import type { CdpTarget } from "@/browser-driver/frame-graph";
|
||||||
import { readRecordingDocumentIdentity } from "@/shared/recording-document-identity";
|
import { readRecordingDocumentIdentity } from "@/shared/recording-document-identity";
|
||||||
import type { CapturedSurfaceProbe } from "./capture";
|
import type { CapturedSurfaceProbe } from "./capture";
|
||||||
import type { CapturedFrameDocument, FrameAxNode } from "./frame-capture";
|
import type {
|
||||||
|
FrameDocument as CapturedFrameDocument,
|
||||||
|
FrameOwnedAxNode as FrameAxNode,
|
||||||
|
} from "./frame-document";
|
||||||
|
|
||||||
/** Frame identity needed to resolve an `@eN` that lives in an iframe. */
|
/** Frame identity needed to resolve an `@eN` that lives in an iframe. */
|
||||||
export interface CaptureVomFrame {
|
export interface CaptureVomFrame {
|
||||||
|
|||||||
@@ -61,8 +61,9 @@ export function buildSemanticGraph(input: BuildSemanticGraphInput): SemanticGrap
|
|||||||
dom,
|
dom,
|
||||||
sourceOrder: index,
|
sourceOrder: index,
|
||||||
excluded:
|
excluded:
|
||||||
document.frameId === rootFrameId &&
|
document.excludedBackendNodeIds?.has(dom.backendNodeId) === true ||
|
||||||
input.excludedBackendNodeIds?.has(dom.backendNodeId) === true,
|
(document.frameId === rootFrameId &&
|
||||||
|
input.excludedBackendNodeIds?.has(dom.backendNodeId) === true),
|
||||||
};
|
};
|
||||||
nodes.set(id, node);
|
nodes.set(id, node);
|
||||||
frameNodeIds.push(id);
|
frameNodeIds.push(id);
|
||||||
@@ -90,9 +91,10 @@ export function buildSemanticGraph(input: BuildSemanticGraphInput): SemanticGrap
|
|||||||
ax,
|
ax,
|
||||||
sourceOrder: document.domNodes.length + index,
|
sourceOrder: document.domNodes.length + index,
|
||||||
excluded:
|
excluded:
|
||||||
document.frameId === rootFrameId &&
|
|
||||||
typeof ax.backendDOMNodeId === "number" &&
|
typeof ax.backendDOMNodeId === "number" &&
|
||||||
input.excludedBackendNodeIds?.has(ax.backendDOMNodeId) === true,
|
(document.excludedBackendNodeIds?.has(ax.backendDOMNodeId) === true ||
|
||||||
|
(document.frameId === rootFrameId &&
|
||||||
|
input.excludedBackendNodeIds?.has(ax.backendDOMNodeId) === true)),
|
||||||
});
|
});
|
||||||
frameNodeIds.push(id);
|
frameNodeIds.push(id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,230 @@
|
|||||||
|
import type { CapturedNode } from "./facts";
|
||||||
|
import { captureCheckpoint, type DecodedDocument, type DecodedNode } from "./facts";
|
||||||
|
export const REQUESTED_STYLES = [
|
||||||
|
"position",
|
||||||
|
"pointer-events",
|
||||||
|
"cursor",
|
||||||
|
"visibility",
|
||||||
|
"opacity",
|
||||||
|
"display",
|
||||||
|
"overflow-x",
|
||||||
|
"overflow-y",
|
||||||
|
"transform",
|
||||||
|
"zoom",
|
||||||
|
"clip-path",
|
||||||
|
"mask-image",
|
||||||
|
] as const;
|
||||||
|
const STYLE_COL = Object.fromEntries(
|
||||||
|
REQUESTED_STYLES.map((name, index) => [name, index]),
|
||||||
|
) as Record<(typeof REQUESTED_STYLES)[number], number>;
|
||||||
|
/** Sparse array format Chrome uses for infrequently-set per-node fields. */
|
||||||
|
interface SparseArray {
|
||||||
|
index: number[];
|
||||||
|
value: number[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RareBooleanData {
|
||||||
|
index: number[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SnapshotDocument {
|
||||||
|
frameId?: string | number;
|
||||||
|
scrollOffsetX?: number;
|
||||||
|
scrollOffsetY?: number;
|
||||||
|
nodes?: {
|
||||||
|
parentIndex?: number[];
|
||||||
|
nodeName?: number[];
|
||||||
|
nodeType?: number[];
|
||||||
|
backendNodeId?: number[];
|
||||||
|
attributes?: number[][];
|
||||||
|
/**
|
||||||
|
* Per-node text value (index into strings), set for `#text` / CDATA nodes.
|
||||||
|
* Element nodes carry -1. Same length as `backendNodeId`.
|
||||||
|
*/
|
||||||
|
nodeValue?: number[];
|
||||||
|
/** Maps node array index → index into `documents[]` for frame content. */
|
||||||
|
contentDocumentIndex?: SparseArray;
|
||||||
|
inputValue?: SparseArray;
|
||||||
|
textValue?: SparseArray;
|
||||||
|
inputChecked?: RareBooleanData;
|
||||||
|
optionSelected?: RareBooleanData;
|
||||||
|
};
|
||||||
|
layout?: {
|
||||||
|
nodeIndex?: number[];
|
||||||
|
styles?: number[][];
|
||||||
|
bounds?: number[][];
|
||||||
|
paintOrders?: number[];
|
||||||
|
clientRects?: number[][];
|
||||||
|
offsetRects?: number[][];
|
||||||
|
scrollRects?: number[][];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function snapshotFrameId(document: SnapshotDocument, strings: string[]): string | undefined {
|
||||||
|
if (typeof document.frameId === "string") return document.frameId || undefined;
|
||||||
|
if (typeof document.frameId === "number") return str(strings, document.frameId) || undefined;
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SnapshotReply {
|
||||||
|
strings?: string[];
|
||||||
|
documents?: SnapshotDocument[];
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSensitiveFormControl(node: Pick<CapturedNode, "tag" | "attrs">): boolean {
|
||||||
|
return node.tag === "input" && (node.attrs.type ?? "").toLowerCase() === "password";
|
||||||
|
}
|
||||||
|
|
||||||
|
function snapshotFormState(
|
||||||
|
value: string | undefined,
|
||||||
|
defaultValue: string,
|
||||||
|
hasDefaultValue: boolean,
|
||||||
|
sensitive: boolean,
|
||||||
|
): CapturedNode["formState"] {
|
||||||
|
if (sensitive) {
|
||||||
|
if (value === undefined && !hasDefaultValue) return undefined;
|
||||||
|
return (value ?? defaultValue) === "" ? "empty" : "filled";
|
||||||
|
}
|
||||||
|
if (value === undefined) return undefined;
|
||||||
|
if (value === "") return "empty";
|
||||||
|
return value === defaultValue ? "default" : "filled";
|
||||||
|
}
|
||||||
|
|
||||||
|
function str(strings: string[], idx: number | undefined): string {
|
||||||
|
if (idx === undefined || idx < 0) return "";
|
||||||
|
return strings[idx] ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function sparseIndexMap(sparse: SparseArray | undefined): Map<number, number> {
|
||||||
|
const out = new Map<number, number>();
|
||||||
|
if (!sparse?.index || !sparse.value) return out;
|
||||||
|
for (let i = 0; i < sparse.index.length; i++) {
|
||||||
|
const nodeIndex = sparse.index[i];
|
||||||
|
const docIndex = sparse.value[i];
|
||||||
|
if (nodeIndex !== undefined && docIndex !== undefined) out.set(nodeIndex, docIndex);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function decodeDocument(
|
||||||
|
doc: SnapshotDocument,
|
||||||
|
strings: string[],
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<DecodedDocument> {
|
||||||
|
const dn = doc.nodes;
|
||||||
|
const dl = doc.layout;
|
||||||
|
if (!dn?.backendNodeId) {
|
||||||
|
return { nodes: [] };
|
||||||
|
}
|
||||||
|
|
||||||
|
const count = dn.backendNodeId.length;
|
||||||
|
const layoutByNode = new Map<number, number>();
|
||||||
|
for (let i = 0; i < (dl?.nodeIndex?.length ?? 0); i++) {
|
||||||
|
if (i % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
layoutByNode.set(dl!.nodeIndex![i], i);
|
||||||
|
}
|
||||||
|
|
||||||
|
const inputValues = sparseIndexMap(dn.inputValue);
|
||||||
|
const textValues = sparseIndexMap(dn.textValue);
|
||||||
|
const checkedInputs = new Set(dn.inputChecked?.index ?? []);
|
||||||
|
const selectedOptions = new Set(dn.optionSelected?.index ?? []);
|
||||||
|
|
||||||
|
// Collect visible text from #text child nodes so element CapturedNodes
|
||||||
|
// carry a textContent value usable as a button/link label fallback.
|
||||||
|
// nodeValue is a parallel array: string index for text nodes, -1 otherwise.
|
||||||
|
const nodeTextContent = new Map<number, string[]>();
|
||||||
|
if (dn.nodeValue) {
|
||||||
|
for (let n = 0; n < count; n++) {
|
||||||
|
if (n % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
const nvIdx = dn.nodeValue[n] ?? -1;
|
||||||
|
if (nvIdx < 0) continue;
|
||||||
|
const text = str(strings, nvIdx).trim();
|
||||||
|
if (!text) continue;
|
||||||
|
const parentIdx = dn.parentIndex?.[n] ?? -1;
|
||||||
|
if (parentIdx >= 0) {
|
||||||
|
const existing = nodeTextContent.get(parentIdx);
|
||||||
|
if (existing) existing.push(text);
|
||||||
|
else nodeTextContent.set(parentIdx, [text]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode fields without assigning a coordinate projection or semantic policy.
|
||||||
|
const nodes: DecodedNode[] = [];
|
||||||
|
for (let n = 0; n < count; n++) {
|
||||||
|
if (n % 256 === 0) await captureCheckpoint(signal);
|
||||||
|
const backendNodeId = dn.backendNodeId[n];
|
||||||
|
const parentIdx = dn.parentIndex?.[n] ?? -1;
|
||||||
|
const parentBackendNodeId = parentIdx >= 0 ? (dn.backendNodeId[parentIdx] ?? null) : null;
|
||||||
|
const tag = str(strings, dn.nodeName?.[n]).toLowerCase();
|
||||||
|
|
||||||
|
const attrs: Record<string, string> = {};
|
||||||
|
const pairs = dn.attributes?.[n] ?? [];
|
||||||
|
for (let a = 0; a + 1 < pairs.length; a += 2) {
|
||||||
|
attrs[str(strings, pairs[a]).toLowerCase()] = str(strings, pairs[a + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const li = layoutByNode.get(n);
|
||||||
|
const styleRow = li === undefined ? [] : (dl?.styles?.[li] ?? []);
|
||||||
|
const styles: Record<string, string> = {};
|
||||||
|
for (const name of REQUESTED_STYLES) styles[name] = str(strings, styleRow[STYLE_COL[name]]);
|
||||||
|
const layout =
|
||||||
|
li === undefined
|
||||||
|
? undefined
|
||||||
|
: {
|
||||||
|
boundsSpace: "snapshot-document-css" as const,
|
||||||
|
clientSpace: "unscaled-client-offset-css" as const,
|
||||||
|
bounds: dl?.bounds?.[li],
|
||||||
|
clientRect: dl?.clientRects?.[li],
|
||||||
|
offsetRect: dl?.offsetRects?.[li],
|
||||||
|
scrollRect: dl?.scrollRects?.[li],
|
||||||
|
styles,
|
||||||
|
};
|
||||||
|
|
||||||
|
const textContent = nodeTextContent.get(n)?.join(" ");
|
||||||
|
|
||||||
|
const rawFormValueIndex = tag === "textarea" ? textValues.get(n) : inputValues.get(n);
|
||||||
|
const rawFormValue =
|
||||||
|
rawFormValueIndex !== undefined ? str(strings, rawFormValueIndex) : undefined;
|
||||||
|
const sensitive = isSensitiveFormControl({ tag, attrs });
|
||||||
|
const formDefaultValue = attrs.value ?? "";
|
||||||
|
const formValue = sensitive ? undefined : rawFormValue;
|
||||||
|
const formState = snapshotFormState(
|
||||||
|
rawFormValue,
|
||||||
|
formDefaultValue,
|
||||||
|
Object.prototype.hasOwnProperty.call(attrs, "value"),
|
||||||
|
sensitive,
|
||||||
|
);
|
||||||
|
if (sensitive) delete attrs.value;
|
||||||
|
|
||||||
|
nodes.push({
|
||||||
|
backendNodeId,
|
||||||
|
nodeType: dn.nodeType?.[n],
|
||||||
|
parentBackendNodeId,
|
||||||
|
...(parentIdx >= 0 && dn.backendNodeId[parentIdx] === undefined
|
||||||
|
? { parentMissing: true }
|
||||||
|
: {}),
|
||||||
|
|
||||||
|
tag,
|
||||||
|
attrs,
|
||||||
|
layout,
|
||||||
|
paintOrder: li === undefined ? 0 : (dl?.paintOrders?.[li] ?? 0),
|
||||||
|
position: styles.position || "static",
|
||||||
|
pointerEvents: styles["pointer-events"] || "auto",
|
||||||
|
cursor: styles.cursor || "auto",
|
||||||
|
|
||||||
|
textContent,
|
||||||
|
...(formValue !== undefined ? { formValue } : {}),
|
||||||
|
...(tag === "input" || tag === "textarea"
|
||||||
|
? {
|
||||||
|
formPlaceholder: attrs.placeholder ?? "",
|
||||||
|
...(!sensitive ? { formDefaultValue } : {}),
|
||||||
|
...(formState ? { formState } : {}),
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
...(checkedInputs.has(n) ? { formValue: "true", formState: "filled" } : {}),
|
||||||
|
...(selectedOptions.has(n) ? { formValue: attrs.value ?? textContent ?? "" } : {}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return { nodes };
|
||||||
|
}
|
||||||
Reference in new issue
Block a user