diff --git a/.changeset/itchy-cooks-yawn.md b/.changeset/itchy-cooks-yawn.md new file mode 100644 index 0000000000..2b23ca5d93 --- /dev/null +++ b/.changeset/itchy-cooks-yawn.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: remove spreaded event handlers when they become nullish diff --git a/packages/svelte/src/internal/client/dom/elements/attributes.js b/packages/svelte/src/internal/client/dom/elements/attributes.js index d927af543f..ced4c4cf25 100644 --- a/packages/svelte/src/internal/client/dom/elements/attributes.js +++ b/packages/svelte/src/internal/client/dom/elements/attributes.js @@ -281,6 +281,9 @@ export function set_attributes( element[`__${event_name}`] = value; delegate([event_name]); } + } else if (delegated) { + // @ts-ignore + element[`__${event_name}`] = undefined; } } else if (key === 'style' && value != null) { element.style.cssText = value + ''; diff --git a/packages/svelte/tests/runtime-runes/samples/remove-spreaded-handlers/_config.js b/packages/svelte/tests/runtime-runes/samples/remove-spreaded-handlers/_config.js new file mode 100644 index 0000000000..9cff16d9f5 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/remove-spreaded-handlers/_config.js @@ -0,0 +1,32 @@ +import { ok, test } from '../../test'; +import { flushSync } from 'svelte'; + +export default test({ + async test({ assert, target, instance }) { + const p = target.querySelector('p'); + const btn = target.querySelector('button'); + const input = target.querySelector('input'); + ok(p); + + flushSync(() => { + btn?.click(); + }); + assert.equal(p.innerHTML, '1'); + + flushSync(() => { + input?.click(); + }); + flushSync(() => { + btn?.click(); + }); + assert.equal(p.innerHTML, '1'); + + flushSync(() => { + input?.click(); + }); + flushSync(() => { + btn?.click(); + }); + assert.equal(p.innerHTML, '2'); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/remove-spreaded-handlers/main.svelte b/packages/svelte/tests/runtime-runes/samples/remove-spreaded-handlers/main.svelte new file mode 100644 index 0000000000..4ca6e61339 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/remove-spreaded-handlers/main.svelte @@ -0,0 +1,12 @@ + + +

+ {count} +

+ +