@@ -1383,6 +1383,7 @@ export function delegate(events) {
1383
1383
* @returns {void }
1384
1384
*/
1385
1385
function handle_event_propagation ( handler_element , event ) {
1386
+ const owner_document = handler_element . ownerDocument ;
1386
1387
const event_name = event . type ;
1387
1388
const path = event . composedPath ?. ( ) || [ ] ;
1388
1389
let current_target = /** @type {null | Element } */ ( path [ 0 ] || event . target ) ;
@@ -1402,9 +1403,11 @@ function handle_event_propagation(handler_element, event) {
1402
1403
const handled_at = event . __root ;
1403
1404
if ( handled_at ) {
1404
1405
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
1408
1411
// -> ignore, but set handle_at to document/window so that we're resetting the event
1409
1412
// chain in case someone manually dispatches the same event object again.
1410
1413
// @ts -expect-error
@@ -1434,8 +1437,7 @@ function handle_event_propagation(handler_element, event) {
1434
1437
define_property ( event , 'currentTarget' , {
1435
1438
configurable : true ,
1436
1439
get ( ) {
1437
- // TODO: ensure correct document?
1438
- return current_target || document ;
1440
+ return current_target || owner_document ;
1439
1441
}
1440
1442
} ) ;
1441
1443
0 commit comments