From 29e9301d4820a90a46a6333df833c90b7233c159 Mon Sep 17 00:00:00 2001 From: fixfon <12512757+fixfon@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:55:35 +0300 Subject: [PATCH 1/2] fix(desktop): annotation Select picks elements inside open shadow roots document.elementsFromPoint retargets shadow content to its outermost host, so hovering or clicking inside an open shadow root outlined and selected the whole host. The pick now descends into each open shadow root, keeping only the root's own elements because ShadowRoot.elementsFromPoint also lists the host and everything outside it, including the picker overlay. Closed roots stay opaque. --- apps/desktop/src/preview/PickPreload.ts | 17 +-- apps/desktop/src/preview/PickTarget.test.ts | 132 ++++++++++++++++++++ apps/desktop/src/preview/PickTarget.ts | 26 ++++ 3 files changed, 168 insertions(+), 7 deletions(-) create mode 100644 apps/desktop/src/preview/PickTarget.test.ts create mode 100644 apps/desktop/src/preview/PickTarget.ts diff --git a/apps/desktop/src/preview/PickPreload.ts b/apps/desktop/src/preview/PickPreload.ts index e49d27d40ba9e..90811cc4403f4 100644 --- a/apps/desktop/src/preview/PickPreload.ts +++ b/apps/desktop/src/preview/PickPreload.ts @@ -17,6 +17,7 @@ import type { import { createAnnotationIdSource } from "./AnnotationIds.ts"; import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts"; import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts"; +import { pickElementAtPoint } from "./PickTarget.ts"; import { installRecordingCursor } from "./RecordingCursor.ts"; import { DEFAULT_RECORDING_INPUT_OPTIONS } from "./RecordingInput.ts"; import { @@ -266,13 +267,15 @@ function isAnnotationNode(element: Element): boolean { } function pickFromPoint(clientX: number, clientY: number): Element | null { - for (const candidate of document.elementsFromPoint(clientX, clientY)) { - if (!(candidate instanceof Element)) continue; - if (isAnnotationNode(candidate)) continue; - if (candidate === document.documentElement || candidate === document.body) continue; - return candidate; - } - return null; + return pickElementAtPoint( + document, + clientX, + clientY, + (element) => + isAnnotationNode(element) || + element === document.documentElement || + element === document.body, + ); } function describeRawElement(element: Element): string { diff --git a/apps/desktop/src/preview/PickTarget.test.ts b/apps/desktop/src/preview/PickTarget.test.ts new file mode 100644 index 0000000000000..150f8ab6f210e --- /dev/null +++ b/apps/desktop/src/preview/PickTarget.test.ts @@ -0,0 +1,132 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { pickElementAtPoint } from "./PickTarget.ts"; + +// Models the DOM contract the picker relies on: hit-testing covers the whole page, and each +// `elementsFromPoint` retargets the hits against the scope it is called on, like Blink does. +class FakeElement { + shadowRoot: FakeScope | null = null; + readonly label: string; + readonly scope: FakeScope; + + constructor(label: string, scope: FakeScope) { + this.label = label; + this.scope = scope; + scope.members.add(this); + } +} + +class FakeScope { + readonly members = new Set(); + readonly host: FakeElement | null; + private readonly page: FakePage; + + constructor(page: FakePage, host: FakeElement | null) { + this.page = page; + this.host = host; + } + + contains(element: FakeElement): boolean { + return this.members.has(element); + } + + elementsFromPoint(): FakeElement[] { + const elements: FakeElement[] = []; + for (const hit of this.page.hits) { + const element = this.retarget(hit); + if (elements.at(-1) !== element) elements.push(element); + } + if (elements.at(-1) !== this.page.html) elements.push(this.page.html); + return elements; + } + + private retarget(target: FakeElement): FakeElement { + let element = target; + while (element.scope.host && !element.scope.encloses(this)) element = element.scope.host; + return element; + } + + private encloses(scope: FakeScope): boolean { + for (let current: FakeScope | null = scope; current; current = current.host?.scope ?? null) { + if (current === this) return true; + } + return false; + } +} + +class FakePage { + hits: FakeElement[] = []; + readonly overlays = new Set(); + readonly document = new FakeScope(this, null); + readonly html = new FakeElement("html", this.document); + readonly body = new FakeElement("body", this.document); + + attachShadow(host: FakeElement, mode: "open" | "closed"): FakeScope { + const root = new FakeScope(this, host); + if (mode === "open") host.shadowRoot = root; + return root; + } + + pick(): FakeElement | null { + return pickElementAtPoint( + this.document, + 0, + 0, + (element) => this.overlays.has(element) || element === this.html || element === this.body, + ); + } +} + +describe("pickElementAtPoint", () => { + it("picks the element inside an open shadow root instead of its host", () => { + const page = new FakePage(); + const host = new FakeElement("shadow-demo", page.document); + const button = new FakeElement("button", page.attachShadow(host, "open")); + page.hits = [button, host, page.body, page.html]; + + expect(page.pick()?.label).toBe("button"); + }); + + it("descends through nested open shadow roots", () => { + const page = new FakePage(); + const outerHost = new FakeElement("outer-host", page.document); + const outerRoot = page.attachShadow(outerHost, "open"); + const innerHost = new FakeElement("inner-host", outerRoot); + const button = new FakeElement("button", page.attachShadow(innerHost, "open")); + page.hits = [button, innerHost, outerHost, page.body, page.html]; + + expect(page.pick()?.label).toBe("button"); + }); + + it("falls back to the host when the point misses its shadow content", () => { + const page = new FakePage(); + const host = new FakeElement("shadow-demo", page.document); + page.attachShadow(host, "open"); + page.hits = [host, page.body, page.html]; + + expect(page.pick()?.label).toBe("shadow-demo"); + }); + + it("keeps a closed shadow root opaque", () => { + const page = new FakePage(); + const outerHost = new FakeElement("outer-host", page.document); + const outerRoot = page.attachShadow(outerHost, "open"); + const closedHost = new FakeElement("closed-host", outerRoot); + const secret = new FakeElement("secret", page.attachShadow(closedHost, "closed")); + page.hits = [secret, closedHost, outerHost, page.body, page.html]; + + expect(page.pick()?.label).toBe("closed-host"); + }); + + it("picks the page element beneath the picker overlay, never the overlay", () => { + const page = new FakePage(); + const host = new FakeElement("shadow-demo", page.document); + const button = new FakeElement("button", page.attachShadow(host, "open")); + const overlay = new FakeElement("overlay", page.document); + page.overlays.add(overlay); + const toolbar = new FakeElement("toolbar", page.attachShadow(overlay, "closed")); + page.hits = [toolbar, button, host, page.body, page.html]; + + expect(page.pick()?.label).toBe("button"); + }); +}); diff --git a/apps/desktop/src/preview/PickTarget.ts b/apps/desktop/src/preview/PickTarget.ts new file mode 100644 index 0000000000000..bfd2508b18483 --- /dev/null +++ b/apps/desktop/src/preview/PickTarget.ts @@ -0,0 +1,26 @@ +interface PickScope { + elementsFromPoint(x: number, y: number): ReadonlyArray; + contains(node: NoInfer): boolean; +} + +/** + * `Document.elementsFromPoint` retargets shadow content to its outermost host, so the pick + * descends into each open shadow root. `ShadowRoot.elementsFromPoint` hit-tests the whole page + * and only retargets, so it also lists the host and everything outside it (including the picker + * overlay); only the root's own elements count. Closed roots expose no `shadowRoot` and stay + * opaque. + */ +export function pickElementAtPoint | null }>( + scope: PickScope, + x: number, + y: number, + isExcluded: (element: E) => boolean, +): E | null { + for (const candidate of scope.elementsFromPoint(x, y)) { + if (!scope.contains(candidate) || isExcluded(candidate)) continue; + const inner = + candidate.shadowRoot && pickElementAtPoint(candidate.shadowRoot, x, y, isExcluded); + return inner ?? candidate; + } + return null; +} From 72c247f5eeb71816b6c51debb8dc82db86a87a91 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 03:21:41 +0000 Subject: [PATCH 2/2] refactor(desktop): trim shadow-root pick comment and redundant test AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/preview/PickTarget.test.ts | 11 ----------- apps/desktop/src/preview/PickTarget.ts | 10 +++------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/apps/desktop/src/preview/PickTarget.test.ts b/apps/desktop/src/preview/PickTarget.test.ts index 150f8ab6f210e..cb245e9565a1c 100644 --- a/apps/desktop/src/preview/PickTarget.test.ts +++ b/apps/desktop/src/preview/PickTarget.test.ts @@ -87,17 +87,6 @@ describe("pickElementAtPoint", () => { expect(page.pick()?.label).toBe("button"); }); - it("descends through nested open shadow roots", () => { - const page = new FakePage(); - const outerHost = new FakeElement("outer-host", page.document); - const outerRoot = page.attachShadow(outerHost, "open"); - const innerHost = new FakeElement("inner-host", outerRoot); - const button = new FakeElement("button", page.attachShadow(innerHost, "open")); - page.hits = [button, innerHost, outerHost, page.body, page.html]; - - expect(page.pick()?.label).toBe("button"); - }); - it("falls back to the host when the point misses its shadow content", () => { const page = new FakePage(); const host = new FakeElement("shadow-demo", page.document); diff --git a/apps/desktop/src/preview/PickTarget.ts b/apps/desktop/src/preview/PickTarget.ts index bfd2508b18483..af689a07bedda 100644 --- a/apps/desktop/src/preview/PickTarget.ts +++ b/apps/desktop/src/preview/PickTarget.ts @@ -3,13 +3,9 @@ interface PickScope { contains(node: NoInfer): boolean; } -/** - * `Document.elementsFromPoint` retargets shadow content to its outermost host, so the pick - * descends into each open shadow root. `ShadowRoot.elementsFromPoint` hit-tests the whole page - * and only retargets, so it also lists the host and everything outside it (including the picker - * overlay); only the root's own elements count. Closed roots expose no `shadowRoot` and stay - * opaque. - */ +// Document hit-testing retargets shadow content to its host, so descend into open roots. +// ShadowRoot.elementsFromPoint still lists the host and the rest of the page (the overlay +// too), so only the root's own elements count. Closed roots expose no shadowRoot. export function pickElementAtPoint | null }>( scope: PickScope, x: number,