webcomponents / polyfills

Web Components Polyfills
BSD 3-Clause "New" or "Revised" License
1.14k stars 165 forks source link

[shadydom] currentTarget incorrect for unpatched `focus` listener in `noPatch` mode #503

Closed kevinpschaaf closed 2 years ago

kevinpschaaf commented 2 years ago

Description

When focus and blur are dispatched, a global capturing event listener sees the event and manually dispatches it through the tree to listeners added with wrapped addEventListener calls. The manual dispatching patches currentTarget to return retargeted values (along with eventPhase, since we are simulating the bubbling phase via a capturing listener). When all addEventListener calls are wrapped this works fine.

However, because the capturing listener does not stop propagation, the event will also natively fire to any native listeners added with an unwrapped addEventListener call (note that Polymer does not wrap addEventListener for listeners added via templates when in noPatch mode, as a performance optimization/correctness tradeoff). Because the manual dispatching code leaves the patched currentTarget accessor installed, it can cause the subsequent native listeners to see the same event, but with patched accessors that return invalid values (like null), rather than just the unpatched values.

Example

https://lit.dev/playground/?mods=gists#gist=d0bcd06f7a01426a52a9b1af0251c0d2

Steps to reproduce

Focus input in demo above

Expected behavior

The logged currentTarget is the input.

Actual behavior

The logged currentTarget is null.