fix: handle event delegation in custom elements (#17057)

Fixes issue where handle_event_propagation breaks when delegated events
are arrays instead of functions, which can happen in custom elements
used across different Svelte versions.

Changes:
1. Compiler: Don't delegate events in custom element builds
2. Runtime: Handle both function and array delegates defensively

This prevents cross-version compatibility issues with custom elements.
pull/17497/head
benjosua 2 weeks ago
parent 2eb54f57ae
commit d6e70b661b

@ -60,7 +60,9 @@ export function Attribute(node, context) {
}
node.metadata.delegated =
parent?.type === 'RegularElement' && can_delegate_event(node.name.slice(2));
parent?.type === 'RegularElement' &&
can_delegate_event(node.name.slice(2)) &&
!context.state.analysis.custom_element;
}
}
}

@ -258,7 +258,13 @@ export function handle_event_propagation(event) {
// -> the target could not have been disabled because it emits the event in the first place
event.target === current_target)
) {
delegated.call(current_target, event);
if (Array.isArray(delegated)) {
for (const handler of delegated) {
handler.call(current_target, event);
}
} else if (typeof delegated === "function") {
delegated.call(current_target, event);
}
}
} catch (error) {
if (throw_error) {

Loading…
Cancel
Save