Skip to content

Improve CommandEvent and ToggleEvent source retargeting #11345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

josepharhar
Copy link
Contributor

@josepharhar josepharhar commented May 29, 2025

This change prevents CommandEvent.source from leaking nodes across shadow boundaries. More context here:
#11255 (comment)

Event.currentTarget and Event.target already have retargeting capabilities built into them to prevent leaking nodes across shadow boundaries. By retargeting against those, we can make sure that Event.source does not leak across shadow boundaries.

(See WHATWG Working Mode: Changes for more details.)


/interaction.html ( diff )

This change prevents CommandEvent.source from leaking nodes across
shadow boundaries. More context here:
whatwg#11255 (comment)
Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Editorially LGTM, but I'd appreciate someone more familiar with the intent checking the logic.

Copy link
Contributor

@keithamus keithamus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good, it would certainly solve the issue. I'm a little curious if this can be simplified to "if target is null or currentTarget is null, return null, otherwise retarget source against currentTarget". I'm not sure of the performance cost of re-targeting twice.

@josepharhar
Copy link
Contributor Author

I think this looks good, it would certainly solve the issue. I'm a little curious if this can be simplified to "if target is null or currentTarget is null, return null, otherwise retarget source against currentTarget". I'm not sure of the performance cost of re-targeting twice.

I think there are cases where currentTarget is null but target is not null after event dispatch, so we should continue considering target in addition to currentTarget. See the "clearTargets" variable here and how it only removes the target from the event in some situations (this prevents leaking nodes): https://dom.spec.whatwg.org/#concept-event-dispatch

@josepharhar
Copy link
Contributor Author

I just pushed a change to move the retargeting logic into a separate algorithm so it can also be called for the ToggleEvent.source getter

@josepharhar josepharhar changed the title Improve CommandEvent.source retargeting Improve CommandEvent and ToggleEvent source retargeting Jun 4, 2025
data-x="dom-Event-currentTarget">currentTarget</code>.</p></li>

<li><p>If <var>retargetAgainst</var> is null, then set <var>retargetAgainst</var> to
<var>event</var>'s <span data-x="concept-event-target">target</span>.</p></li>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this setup. We want events which have been dispatched in light DOM to have their .source retargeted? What is the use case for that. Events dispatched in shadow DOM would get just null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want events which have been dispatched in light DOM to have their .source retargeted?

If the event has been dispatched in light DOM, then retargeting won't change the resulting source element that we return; we are effectively not retargeting events that are dispatched in light DOM.

Events dispatched in shadow DOM would get just null.

If the event was dispatched in shadow DOM, then source will become null after event dispatch is done, just like target and relatedTarget.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the event has been dispatched in light dom and source is in shadow DOM, retargeting will happen.

But what are we trying to achieve with this rather unusual setup? Why do we need to fall back to 'target'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the event has been dispatched in light dom and source is in shadow DOM, retargeting will happen.

Ah yes, that is true. In this case, I'm assuming that you still don't want source to be leaked somewhere since it is in a shadow root, so we should do retargeting.

But what are we trying to achieve with this rather unusual setup? Why do we need to fall back to 'target'?

Target is better than currentTarget because after event dispatch target is likely still set to something we can retarget against which won't leak any nodes, whereas currentTarget just becomes null.

Now that I think about it, maybe we can just always retarget against target instead of currentTarget

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the usage of currentTarget and now it just uses target. It shouldn't change the behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that also what happens for relatedTarget? Ideally source works the same way as relatedTarget.

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

Successfully merging this pull request may close these issues.

5 participants