fix: capture the correct event names when spreading attributes (#11783)

fixes #11777
We need to scope the `key` variable because it may be captured in a closure
pull/11795/head
FoHoOV 7 months ago committed by GitHub
parent ee9d5ef850
commit 4fef0eb08f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: capture the correct event names when spreading attributes

@ -163,7 +163,8 @@ export function set_attributes(element, prev, next, lowercase_attributes, css_ha
/** @type {Array<[string, any, () => void]>} */
var events = [];
for (key in next) {
// since key is captured we use const
for (const key in next) {
// let instead of var because referenced in a closure
let value = next[key];
if (value === prev?.[key]) continue;

@ -0,0 +1,16 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
test({ assert, target }) {
const div = target.querySelector('div');
div?.dispatchEvent(new Event('b'));
flushSync();
assert.htmlEqual(target.innerHTML, '<div>b</div>');
div?.dispatchEvent(new Event('a'));
flushSync();
assert.htmlEqual(target.innerHTML, '<div>a</div>');
}
});

@ -0,0 +1,8 @@
<script lang="ts">
const props = {};
let changed = $state('');
</script>
<div {...props} ona={() => (changed = 'a')} onb={() => (changed = 'b')}>
{changed}
</div>
Loading…
Cancel
Save