webcomponents / polyfills

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

Fix focus/blur events to be usable without wrapped addEventListener #502

Closed kevinpschaaf closed 2 years ago

kevinpschaaf commented 2 years ago

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.

This change restores the original accessors for those fields when the manual dispatching is complete, so that native listeners see native fields on the event.

Fixes https://github.com/webcomponents/polyfills/issues/503.

bicknellr commented 2 years ago

This seems good to me. I think since we're having trouble with the test infra right now, an internal run on a CL that imports this branch would be a good substitute.

kevinpschaaf commented 2 years ago

Manually running tests on IE also pass:

image