Skip to content

Commit fc6666b

Browse files
authored
fix: prevent window listeners from triggering events twice (#10611)
fixes #10271 The prior fix in #10307 wasn't quite right, because JSDom has a difference between window and event.composedPath which sounds like a bug, and a workaround in that PR made it so the test did pass when the bug still occurred. That's also why there isn't a test here, because we can't properly test this using JSDom.
1 parent 45e3857 commit fc6666b

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

.changeset/fresh-impalas-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: prevent window listeners from triggering events twice

packages/svelte/src/internal/client/render.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,7 @@ export function delegate(events) {
14011401
* @returns {void}
14021402
*/
14031403
function handle_event_propagation(handler_element, event) {
1404+
const owner_document = handler_element.ownerDocument;
14041405
const event_name = event.type;
14051406
const path = event.composedPath?.() || [];
14061407
let current_target = /** @type {null | Element} */ (path[0] || event.target);
@@ -1420,12 +1421,15 @@ function handle_event_propagation(handler_element, event) {
14201421
const handled_at = event.__root;
14211422
if (handled_at) {
14221423
const at_idx = path.indexOf(handled_at);
1423-
if (at_idx !== -1 && handler_element === document) {
1424-
// This is the fallback document listener but the event was already handled
1425-
// -> ignore, but set handle_at to document so that we're resetting the event
1424+
if (
1425+
at_idx !== -1 &&
1426+
(handler_element === document || handler_element === /** @type {any} */ (window))
1427+
) {
1428+
// This is the fallback document listener or a window listener, but the event was already handled
1429+
// -> ignore, but set handle_at to document/window so that we're resetting the event
14261430
// chain in case someone manually dispatches the same event object again.
14271431
// @ts-expect-error
1428-
event.__root = document;
1432+
event.__root = handler_element;
14291433
return;
14301434
}
14311435
// We're deliberately not skipping if the index is higher, because
@@ -1451,8 +1455,7 @@ function handle_event_propagation(handler_element, event) {
14511455
define_property(event, 'currentTarget', {
14521456
configurable: true,
14531457
get() {
1454-
// TODO: ensure correct document?
1455-
return current_target || document;
1458+
return current_target || owner_document;
14561459
}
14571460
});
14581461

0 commit comments

Comments
 (0)