@ -1279,11 +1279,11 @@ export function delegate(events) {
}
/ * *
* @ param { Node } root _element
* @ param { Node } handle r_element
* @ param { Event } event
* @ returns { void }
* /
function handle _event _propagation ( root _element , event ) {
function handle _event _propagation ( handle r_element , event ) {
const event _name = event . type ;
const path = event . composedPath ? . ( ) || [ ] ;
let current _target = /** @type {null | Element} */ ( path [ 0 ] || event . target ) ;
@ -1298,22 +1298,37 @@ function handle_event_propagation(root_element, event) {
// We check __root to skip all nodes below it in case this is a
// parent of the __root node, which indicates that there's nested
// mounted apps. In this case we don't want to trigger events multiple times.
// We're deliberately not skipping if the index is the same or higher, because
// someone could create an event programmatically and emit it multiple times,
// in which case we want to handle the whole propagation chain properly each time.
let path _idx = 0 ;
// @ts-expect-error is added below
const handled _at = event . _ _root ;
if ( handled _at ) {
const at _idx = path . indexOf ( handled _at ) ;
if ( at _idx !== - 1 && root _element === document ) {
// This is the fallback document listener but the event was already handled -> ignore
if ( at _idx !== - 1 && handler _element === document ) {
// This is the fallback document listener but the event was already handled
// -> ignore, but set handle_at to document so that we're resetting the event
// chain in case someone manually dispatches the same event object again.
// @ts-expect-error
event . _ _root = document ;
return ;
}
// We're deliberately not skipping if the index is higher, because
// someone could create an event programmatically and emit it multiple times,
// in which case we want to handle the whole propagation chain properly each time.
// (this will only be a false negative if the event is dispatched multiple times and
// the fallback document listener isn't reached in between, but that's super rare)
const handler _idx = path . indexOf ( handler _element ) ;
if ( handler _idx === - 1 ) {
// handle_idx can theoretically be -1 (happened in some JSDOM testing scenarios with an event listener on the window object)
// so guard against that, too, and assume that everything was handled at this point.
return ;
}
if ( at _idx < path . indexOf ( root _element ) ) {
path _idx = at _idx ;
if ( at _idx <= handler _idx ) {
// +1 because at_idx is the element which was already handled, and there can only be one delegated event per element.
// Avoids on:click and onclick on the same event resulting in onclick being fired twice.
path _idx = at _idx + 1 ;
}
}
current _target = /** @type {Element} */ ( path [ path _idx ] || event . target ) ;
// Proxy currentTarget to correct target
define _property ( event , 'currentTarget' , {
@ -1339,16 +1354,20 @@ function handle_event_propagation(root_element, event) {
delegated . call ( current _target , event ) ;
}
}
if ( event . cancelBubble || parent _element === root _element ) {
if (
event . cancelBubble ||
parent _element === handler _element ||
current _target === handler _element
) {
break ;
}
current _target = parent _element ;
}
// @ts-expect-error is used above
event . _ _root = root _element ;
event . _ _root = handle r_element ;
// @ts-expect-error is used above
current _target = root _element ;
current _target = handle r_element ;
}
/ * *