Home / Function/ handle_event_propagation() — svelte Function Reference

handle_event_propagation() — svelte Function Reference

Architecture documentation for the handle_event_propagation() function in events.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  a730a532_9664_13a9_084d_1752b81559e1["handle_event_propagation()"]
  2c990bd1_acff_5910_3af2_ab75f655b31b["events.js"]
  a730a532_9664_13a9_084d_1752b81559e1 -->|defined in| 2c990bd1_acff_5910_3af2_ab75f655b31b
  a08b6cc5_af73_1be4_d02f_3113cf8a8305["get()"]
  a730a532_9664_13a9_084d_1752b81559e1 -->|calls| a08b6cc5_af73_1be4_d02f_3113cf8a8305
  311ef9f4_9b68_c178_c1db_3b8696f7d964["set_active_reaction()"]
  a730a532_9664_13a9_084d_1752b81559e1 -->|calls| 311ef9f4_9b68_c178_c1db_3b8696f7d964
  55623862_10b7_5361_e30b_34ec6941f1a7["set_active_effect()"]
  a730a532_9664_13a9_084d_1752b81559e1 -->|calls| 55623862_10b7_5361_e30b_34ec6941f1a7
  style a730a532_9664_13a9_084d_1752b81559e1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/client/dom/elements/events.js lines 156–293

export function handle_event_propagation(event) {
	var handler_element = this;
	var owner_document = /** @type {Node} */ (handler_element).ownerDocument;
	var event_name = event.type;
	var path = event.composedPath?.() || [];
	var current_target = /** @type {null | Element} */ (path[0] || event.target);

	last_propagated_event = event;

	// composedPath contains list of nodes the event has propagated through.
	// 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.
	var path_idx = 0;

	// the `last_propagated_event === event` check is redundant, but
	// without it the variable will be DCE'd and things will
	// fail mysteriously in Firefox
	// @ts-expect-error is added below
	var handled_at = last_propagated_event === event && event.__root;

	if (handled_at) {
		var at_idx = path.indexOf(handled_at);
		if (
			at_idx !== -1 &&
			(handler_element === document || handler_element === /** @type {any} */ (window))
		) {
			// This is the fallback document listener or a window listener, but the event was already handled
			// -> ignore, but set handle_at to document/window so that we're resetting the event
			// chain in case someone manually dispatches the same event object again.
			// @ts-expect-error
			event.__root = handler_element;
			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)
		var 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 <= handler_idx) {
			path_idx = at_idx;
		}
	}

	current_target = /** @type {Element} */ (path[path_idx] || event.target);
	// there can only be one delegated event per element, and we either already handled the current target,
	// or this is the very first target in the chain which has a non-delegated listener, in which case it's safe
	// to handle a possible delegated event on it later (through the root delegation listener for example).
	if (current_target === handler_element) return;

	// Proxy currentTarget to correct target
	define_property(event, 'currentTarget', {
		configurable: true,
		get() {
			return current_target || owner_document;
		}
	});

	// This started because of Chromium issue https://chromestatus.com/feature/5128696823545856,
	// where removal or moving of of the DOM can cause sync `blur` events to fire, which can cause logic
	// to run inside the current `active_reaction`, which isn't what we want at all. However, on reflection,
	// it's probably best that all event handled by Svelte have this behaviour, as we don't really want
	// an event handler to run in the context of another reaction or effect.
	var previous_reaction = active_reaction;
	var previous_effect = active_effect;
	set_active_reaction(null);
	set_active_effect(null);

	try {
		/**
		 * @type {unknown}
		 */
		var throw_error;

Domain

Subdomains

Frequently Asked Questions

What does handle_event_propagation() do?
handle_event_propagation() is a function in the svelte codebase, defined in packages/svelte/src/internal/client/dom/elements/events.js.
Where is handle_event_propagation() defined?
handle_event_propagation() is defined in packages/svelte/src/internal/client/dom/elements/events.js at line 156.
What does handle_event_propagation() call?
handle_event_propagation() calls 3 function(s): get, set_active_effect, set_active_reaction.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free