MCPcopy
hub / github.com/sveltejs/svelte / handle_events

Function handle_events

packages/svelte/src/compiler/migrate/index.js:1727–1814  ·  view source on GitHub ↗

* @param {AST.RegularElement | AST.SvelteElement | AST.SvelteWindow | AST.SvelteDocument | AST.SvelteBody} element * @param {State} state

(element, state)

Source from the content-addressed store, hash-verified

1725 * @param {State} state
1726 */
1727function handle_events(element, state) {
1728 /** @type {Map<string, AST.OnDirective[]>} */
1729 const handlers = new Map();
1730 for (const attribute of element.attributes) {
1731 if (attribute.type !== 'OnDirective') continue;
1732
1733 let name = `on${attribute.name}`;
1734 if (attribute.modifiers.includes('capture')) {
1735 name += 'capture';
1736 }
1737
1738 const nodes = handlers.get(name) || [];
1739 nodes.push(attribute);
1740 handlers.set(name, nodes);
1741 }
1742
1743 for (const [name, nodes] of handlers) {
1744 const handlers = [];
1745
1746 let first = null;
1747
1748 for (const node of nodes) {
1749 /** @type {string} */
1750 let body;
1751
1752 if (node.expression) {
1753 body = state.str.original.substring(
1754 /** @type {number} */ (node.expression.start),
1755 /** @type {number} */ (node.expression.end)
1756 );
1757 } else {
1758 body = `${state.names.bubble}('${node.name}')`;
1759 state.legacy_imports.add('createBubbler');
1760 state.script_insertions.add(
1761 `const ${state.names.bubble} = ${state.names.createBubbler}();`
1762 );
1763 }
1764
1765 const has_passive = node.modifiers.includes('passive');
1766 const has_nonpassive = node.modifiers.includes('nonpassive');
1767
1768 const modifiers = modifier_order.filter((modifier) => node.modifiers.includes(modifier));
1769
1770 for (const modifier of modifiers) {
1771 state.legacy_imports.add(modifier);
1772 body = `${state.names[modifier]}(${body})`;
1773 }
1774
1775 if (has_passive || has_nonpassive) {
1776 const action = has_passive ? 'passive' : 'nonpassive';
1777 state.legacy_imports.add(action);
1778
1779 state.str.overwrite(
1780 node.start,
1781 node.end,
1782 `use:${state.names[action]}={['${node.name}', () => ${body}]}`
1783 );
1784 } else {

Callers 5

RegularElementFunction · 0.85
SvelteElementFunction · 0.85
SvelteWindowFunction · 0.85
SvelteBodyFunction · 0.85
SvelteDocumentFunction · 0.85

Calls 4

pushMethod · 0.80
setMethod · 0.65
getMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected