Skip to content

Commit ff1ae81

Browse files
authored
better changeset, use owner_document, comment
1 parent 4779f15 commit ff1ae81

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

.changeset/fresh-impalas-bow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"svelte": patch
33
---
44

5-
fix: trigger again when window listener already handled
5+
fix: prevent window listeners from triggering events twice

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,7 @@ export function delegate(events) {
13831383
* @returns {void}
13841384
*/
13851385
function handle_event_propagation(handler_element, event) {
1386+
const owner_document = handler_element.ownerDocument;
13861387
const event_name = event.type;
13871388
const path = event.composedPath?.() || [];
13881389
let current_target = /** @type {null | Element} */ (path[0] || event.target);
@@ -1402,9 +1403,11 @@ function handle_event_propagation(handler_element, event) {
14021403
const handled_at = event.__root;
14031404
if (handled_at) {
14041405
const at_idx = path.indexOf(handled_at);
1405-
// @ts-ignore
1406-
if (at_idx !== -1 && (handler_element === document || handler_element === window)) {
1407-
// This is the fallback document/window listener but the event was already handled
1406+
if (
1407+
at_idx !== -1 &&
1408+
(handler_element === owner_document || handler_element === /** @type {any} */ (window))
1409+
) {
1410+
// This is the fallback document listener or a window listener, but the event was already handled
14081411
// -> ignore, but set handle_at to document/window so that we're resetting the event
14091412
// chain in case someone manually dispatches the same event object again.
14101413
// @ts-expect-error
@@ -1434,8 +1437,7 @@ function handle_event_propagation(handler_element, event) {
14341437
define_property(event, 'currentTarget', {
14351438
configurable: true,
14361439
get() {
1437-
// TODO: ensure correct document?
1438-
return current_target || document;
1440+
return current_target || owner_document;
14391441
}
14401442
});
14411443

0 commit comments

Comments
 (0)