Repository navigation
Conversation
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.
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.
"retargeted" is not in the project dictionary; "retargeting" is fine.
|
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. |
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
mainand 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?
getNextEventTargetnow reports whether the next bubbling step crosses a shadow boundary, andtriggerEventListenerretargetsevent.targetat 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:
createElementfor unregistered custom elements returns a proxy wrapper, so after retargeting,event.targetheld 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 fromevent.tswould create an import cycle. The retargeting resolves the proxy before assigning the target, soevent.target === wrapperHostholds for custom elements too.Why there is a test through
newSpecPage: #4618 reproduces withnewSpecPage, 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, notsrc/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
documentnow 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?
No API changes, but spec tests observe a different
event.targetnow. 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
newSpecPagewith registered components. The mock-doc and testing suites pass (666 tests), and the full jest unit suite is green (4200 passed, 17 skipped).