Skip to content

fix(mock-doc): retarget composed events when crossing shadow boundaries - #7008

Closed
deleonio wants to merge 5 commits into
stenciljs:mainfrom
deleonio:fix/4618-shadow-dom-event-bubbling
Closed

deleonio wants to merge 5 commits into
stenciljs:mainfrom
deleonio:fix/4618-shadow-dom-event-bubbling

Conversation

@deleonio

@deleonio deleonio commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What is the current behavior?

When a composed event bubbles out of a shadow tree in mock-doc, listeners outside that tree see the element the event was dispatched on as event.target. In a real browser the target is retargeted to the shadow host at every boundary crossing, so spec tests can't assert the target the way a browser delivers it. That's #7012.

#4618 is a different question: does the event reach the wrapper at all? It does, since #6301. I ran both test cases from #4618 against current main and they pass. This PR adds a spec-page regression test for that, so #4618 can be closed.

GitHub Issue Number: #7012, refs #4618

What is the new behavior?

getNextEventTarget now reports whether the next bubbling step crosses a shadow boundary, and triggerEventListener retargets event.target at that point. I audited my own first version of this PR and found two gaps; both are closed in the second commit.

Why the proxy resolution is part of this PR: createElement for unregistered custom elements returns a proxy wrapper, so after retargeting, event.target held the wrapped mock element while user code compared against the proxy. A test using the tags from #4618 (my-wrapper-component, my-component) would have failed its identity assertion, and I did not want to leave a fix in that only works for native elements. The proxy pairing now lives in its own small module (proxy-element.ts), because importing the custom element registry from event.ts would create an import cycle. The retargeting resolves the proxy before assigning the target, so event.target === wrapperHost holds for custom elements too.

Why there is a test through newSpecPage: #4618 reproduces with newSpecPage, my first version only tested mock-doc directly. Mock-doc is the engine underneath, but the acceptance of #4618 is about Stencil's testing wrappers, so the second commit adds a spec-page test with two nested shadow components (@Event({ composed: true }) on the inner one, listener on the wrapper host). It covers both parts: the spy fires, and the target seen from outside is the wrapper host. One thing I learned doing this: spec-page tests run against the compiled artifacts, not src/mock-doc, so the first run of that test quietly exercised the old build. After rebuilding, everything matched.

For a composed event dispatched on a button inside two nested shadow roots, listeners on the wrapper host and on document now see the wrapper host as target, listeners inside the wrapper's shadow root see the inner component's host, and listeners on the button and in its own shadow root still see the button. That matches what a browser does.

Documentation

No user-facing documentation changes needed, this aligns mock-doc with existing browser behavior.

Does this introduce a breaking change?

  • Yes
  • No

No API changes, but spec tests observe a different event.target now. A test that expects the inner element as target on a listener outside the shadow tree will fail after this change. The new value is the one a browser delivers, so I'd treat it as a fix. If you'd rather call it out in the changelog, that's fine with me.

Testing

Mock-doc tests cover composed bubbling out of nested shadow roots, non-composed events staying inside, retargeting for native elements and for custom elements (proxy identity), and listeners inside the dispatching shadow tree still seeing the original target. Spec-page tests cover the same scenario through newSpecPage with registered components. The mock-doc and testing suites pass (666 tests), and the full jest unit suite is green (4200 passed, 17 skipped).

Event bubbling across nested shadow roots already worked, but the
event's `target` was never retargeted when a composed event crossed a
shadow boundary. Listeners outside the shadow tree saw the element the
event was dispatched on instead of the shadow host, which does not
match browser behavior and made it impossible to assert bubbling
behavior in spec tests (see stenciljs#4618).

getNextEventTarget now reports whether the next step crosses a shadow
boundary, and triggerEventListener retargets `event.target` to the
shadow host at that point. Adds regression tests covering nested
shadow roots and target retargeting.
@deleonio
deleonio requested a review from a team as a code owner November 12, 2026 23:55
Follow-up to the retargeting change: unregistered custom elements come
back from createElement as proxy wrappers, so the retargeted event
target held the wrapped mock element while user code compared against
the proxy. The proxy pairing now lives in its own module (avoids an
import cycle between event.ts and custom-element-registry.ts) and the
retargeting resolves the proxy before assigning `event.target`.

Adds regression tests at both levels: mock-doc with custom element
tags, and through `newSpecPage` with two nested shadow components,
which is the test setup described in stenciljs#4618.
@johnjenkins

Copy link
Copy Markdown
Contributor

Thanks for this @deleonio ... although retargeting is missing on main, I'm gonna close this one out I think.

This PR is mostly about test parity, especially the proxy bookkeeping in proxy-element.ts.

mock-doc is SSR-first: we keep it as a minimal DOM for server rendering, not a browser emulator for spec tests (I know it's traditionally been that way - but with integrated testing being removed) - this is the kind of work we don't want in mock-doc going forward.

If you want test behaviour that matches the browser, use @stencil/vitest with jsdom, happy-dom or browser mode. That's worth doing soon anyway, because Stencil's built-in testing is removed in v5 - v4 -> v5 general migration guide | vitest migration guide.

If your concern is SSR, we'd take a much narrower PR:

Change only event.ts, so ev.target is set to the host when an event crosses a shadow boundary.
Leave out proxy-element.ts and the registry changes.
Add a renderToString test where the rendered output depends on ev.target from a composed event emitted during a lifecycle hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants