Skip to content

fix: capturing the correct event names in set_attributes #11783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-dogs-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: capture the correct event names when spreading attributes
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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>');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts">
const props = {};
let changed = $state('');
</script>

<div {...props} ona={() => (changed = 'a')} onb={() => (changed = 'b')}>
{changed}
</div>
Loading