Home / Function/ handle_events() — svelte Function Reference

handle_events() — svelte Function Reference

Architecture documentation for the handle_events() function in index.js from the svelte codebase.

Function javascript Compiler Transformer calls 1 called by 5

Entity Profile

Dependency Diagram

graph TD
  24f461f2_c717_3a18_d33f_adb48a448ef4["handle_events()"]
  cab41022_1b55_3b7a_06c6_b90763bbea47["index.js"]
  24f461f2_c717_3a18_d33f_adb48a448ef4 -->|defined in| cab41022_1b55_3b7a_06c6_b90763bbea47
  a08d16d0_ebd3_b975_18ce_1fcee3350ed6["template.RegularElement()"]
  a08d16d0_ebd3_b975_18ce_1fcee3350ed6 -->|calls| 24f461f2_c717_3a18_d33f_adb48a448ef4
  efe8c251_8c53_8933_be63_87752f930714["template.SvelteElement()"]
  efe8c251_8c53_8933_be63_87752f930714 -->|calls| 24f461f2_c717_3a18_d33f_adb48a448ef4
  071071e2_beab_e428_0089_b12ad1aace93["template.SvelteWindow()"]
  071071e2_beab_e428_0089_b12ad1aace93 -->|calls| 24f461f2_c717_3a18_d33f_adb48a448ef4
  6b13a3df_5e4c_0438_d7c9_32502a73df0d["template.SvelteBody()"]
  6b13a3df_5e4c_0438_d7c9_32502a73df0d -->|calls| 24f461f2_c717_3a18_d33f_adb48a448ef4
  0a48f2d1_1124_ea30_ce68_208e8f257a25["template.SvelteDocument()"]
  0a48f2d1_1124_ea30_ce68_208e8f257a25 -->|calls| 24f461f2_c717_3a18_d33f_adb48a448ef4
  627dc2f8_4dbc_5bb1_8f54_cee503e17098["get()"]
  24f461f2_c717_3a18_d33f_adb48a448ef4 -->|calls| 627dc2f8_4dbc_5bb1_8f54_cee503e17098
  style 24f461f2_c717_3a18_d33f_adb48a448ef4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/migrate/index.js lines 1725–1812

function handle_events(element, state) {
	/** @type {Map<string, AST.OnDirective[]>} */
	const handlers = new Map();
	for (const attribute of element.attributes) {
		if (attribute.type !== 'OnDirective') continue;

		let name = `on${attribute.name}`;
		if (attribute.modifiers.includes('capture')) {
			name += 'capture';
		}

		const nodes = handlers.get(name) || [];
		nodes.push(attribute);
		handlers.set(name, nodes);
	}

	for (const [name, nodes] of handlers) {
		const handlers = [];

		let first = null;

		for (const node of nodes) {
			/** @type {string} */
			let body;

			if (node.expression) {
				body = state.str.original.substring(
					/** @type {number} */ (node.expression.start),
					/** @type {number} */ (node.expression.end)
				);
			} else {
				body = `${state.names.bubble}('${node.name}')`;
				state.legacy_imports.add('createBubbler');
				state.script_insertions.add(
					`const ${state.names.bubble} = ${state.names.createBubbler}();`
				);
			}

			const has_passive = node.modifiers.includes('passive');
			const has_nonpassive = node.modifiers.includes('nonpassive');

			const modifiers = modifier_order.filter((modifier) => node.modifiers.includes(modifier));

			for (const modifier of modifiers) {
				state.legacy_imports.add(modifier);
				body = `${state.names[modifier]}(${body})`;
			}

			if (has_passive || has_nonpassive) {
				const action = has_passive ? 'passive' : 'nonpassive';
				state.legacy_imports.add(action);

				state.str.overwrite(
					node.start,
					node.end,
					`use:${state.names[action]}={['${node.name}', () => ${body}]}`
				);
			} else {
				if (first) {
					let start = node.start;
					let end = node.end;

					while (/[\s\n]/.test(state.str.original[start - 1])) start -= 1;
					state.str.remove(start, end);
				} else {
					first = node;
				}

				handlers.push(body);
			}
		}

		if (first) {
			/** @type {string} */
			let replacement;

			if (handlers.length > 1) {
				state.legacy_imports.add('handlers');
				replacement = `${name}={${state.names.handlers}(${handlers.join(', ')})}`;
			} else {
				const handler = handlers[0];

Domain

Subdomains

Calls

Frequently Asked Questions

What does handle_events() do?
handle_events() is a function in the svelte codebase, defined in packages/svelte/src/compiler/migrate/index.js.
Where is handle_events() defined?
handle_events() is defined in packages/svelte/src/compiler/migrate/index.js at line 1725.
What does handle_events() call?
handle_events() calls 1 function(s): get.
What calls handle_events()?
handle_events() is called by 5 function(s): template.RegularElement, template.SvelteBody, template.SvelteDocument, template.SvelteElement, template.SvelteWindow.

Analyze Your Own Codebase

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

Try Supermodel Free