[Bug]: Annotation Select picks the shadow host instead of elements inside shadow DOM
Before submitting
- [x] I searched existing issues and did not find a duplicate.
- [x] I included enough detail to reproduce or investigate the problem.
Area
apps/desktop
Steps to reproduce
-
Save this page as
index.htmland serve it with any static server (for examplenpx serve):<!doctype html> <html> <body> <shadow-demo></shadow-demo> <script> customElements.define( "shadow-demo", class extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }).innerHTML = ` <style>button { margin: 8px; padding: 12px; }</style> <button>First</button> <button>Second</button>`; } }, ); </script> </body> </html> -
Open it in the desktop preview browser.
-
Start the annotation tool, choose Select, and hover over / click the Second button.
Expected behavior
The hovered element inside the open shadow root (the Second button) is outlined and selected, like elements in the regular DOM.
Actual behavior
The whole shadow host (<shadow-demo>) is outlined and selected. The captured element context reports the host, e.g. selector html > body > shadow-demo.
Real-world impact: apps built from web components are effectively unpickable. In Home Assistant the entire UI lives under <home-assistant>, so every click selects the whole page (html > body > home-assistant).
Likely cause: pickFromPoint() in apps/desktop/src/preview/PickPreload.ts only queries document.elementsFromPoint(). Hit-testing on document is retargeted to the outermost shadow host and never descends into shadow roots. A possible direction is to continue with candidate.shadowRoot.elementsFromPoint(x, y) while the candidate has an open shadow root (closed roots, including the picker's own overlay, stay opaque), and to make the reported selector shadow-aware. Possibly related boundary issue for iframes: #3141.
Impact
Major degradation or frequent failure
Version or commit
Desktop nightly (auto-update channel nightly); the same code path is unchanged on main @ 805967a878
Environment
Linux Mint 22.3 (x86_64), T3 Code desktop AppImage
Workaround
Use the Region (or Draw) tool instead of Select on shadow-DOM pages.
[!NOTE] Grok responding on behalf of Julius.
Thanks for the clear repro. From reading the code on main @ 805967a, the cause you describe matches what's there. I haven't run the repro page myself.
Where it happens
PickPreload.ts:276-284:pickFromPoint()only walksdocument.elementsFromPoint()and returns the first non-overlay element. Document-level hit testing is retargeted to the outermost shadow host, and nothing descends intoshadowRoot. Hover (L1194-L1195) and click (L1225-L1226) both go through it, so the outline, label and selection all land on the host.- The selector comes from react-grab's
getElementContext()(L392-L414). Today it gets the host, which is consistent with thehtml > body > shadow-demoyou saw. From the minified react-grab 0.1.44 bundle, it looks like an inner element would get a selector that is only unique within its own shadow root, with no host prefix. So fixing the pick alone probably isn't enough: the selector sent to the agent (and copied into style changes atL1373-L1374) would need the host chain. - The screenshot crop uses
getBoundingClientRect()(L1380), which is in viewport coordinates, so it should work unchanged for inner elements. - The Region tool's element scan has the same gap:
document.querySelectorAll("body *")(L1150) doesn't enter shadow roots either. That's likely why it falls back to a plain region on shadow-DOM pages.
Possible fix (untested)
- In
pickFromPoint(), after picking a candidate, check whether it has an openshadowRoot. If so, runshadowRoot.elementsFromPoint(x, y), keep only results whosegetRootNode()is that root, and repeat while there's a deeper open root. If the inner query finds nothing, keep the host. Closed roots returnshadowRoot === null, so they stay opaque. That includes the picker's own overlay, which is a closed root on a host markeddata-t3code-annotation-ui(L520-L524), so the existingisAnnotationNodeskip should still apply. A similar descent pattern already exists inRecordingInput.ts:52. - In
captureElement(), build a shadow-aware selector by walkinggetRootNode().hostoutward and joining the selector for each scope, for examplehome-assistant >>> ha-panel >>> button.>>>isn't validquerySelectorsyntax, so it should be treated as an agent-readable path, or carried in a separate field, rather than something to re-query directly. - Optionally, make the Region scan descend open roots too, so the tools behave the same way.
PickPreload.ts is the only element-pick path I found (it has been at this path since 3d9e8dea7). Iframe boundaries are a separate, related gap tracked in #3141. I didn't find any duplicates or open PRs for this.