t3code icon indicating copy to clipboard operation
t3code copied to clipboard

[Bug]: Annotation Select picks the shadow host instead of elements inside shadow DOM

Open henrik092 opened this issue 1 month ago • 1 comments

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

  1. Save this page as index.html and serve it with any static server (for example npx 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>
    
  2. Open it in the desktop preview browser.

  3. 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.

henrik092 avatar Oct 08 '26 19:10 henrik092

[!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 walks document.elementsFromPoint() and returns the first non-overlay element. Document-level hit testing is retargeted to the outermost shadow host, and nothing descends into shadowRoot. 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 the html > body > shadow-demo you 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 at L1373-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)

  1. In pickFromPoint(), after picking a candidate, check whether it has an open shadowRoot. If so, run shadowRoot.elementsFromPoint(x, y), keep only results whose getRootNode() is that root, and repeat while there's a deeper open root. If the inner query finds nothing, keep the host. Closed roots return shadowRoot === null, so they stay opaque. That includes the picker's own overlay, which is a closed root on a host marked data-t3code-annotation-ui (L520-L524), so the existing isAnnotationNode skip should still apply. A similar descent pattern already exists in RecordingInput.ts:52.
  2. In captureElement(), build a shadow-aware selector by walking getRootNode().host outward and joining the selector for each scope, for example home-assistant >>> ha-panel >>> button. >>> isn't valid querySelector syntax, so it should be treated as an agent-readable path, or carried in a separate field, rather than something to re-query directly.
  3. 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.

juliusmarminge avatar Oct 08 '26 19:10 juliusmarminge