Skip to content

fix: ensure set_text applies coercion to objects before diff #13542

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 4 commits into from
Oct 9, 2024
Merged
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/wet-hats-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure set_text applies coercion to objects before diff
8 changes: 5 additions & 3 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
@@ -48,11 +48,13 @@ export function set_should_intro(value) {
* @returns {void}
*/
export function set_text(text, value) {
// For objects, we apply string coercion (which might make things like $state array references in the template reactive) before diffing
var str = value == null ? '' : typeof value === 'object' ? value + '' : value;
// @ts-expect-error
if (value !== (text.__t ??= text.nodeValue)) {
if (str !== (text.__t ??= text.nodeValue)) {
// @ts-expect-error
text.__t = value;
text.nodeValue = value == null ? '' : value + '';
text.__t = str;
text.nodeValue = str == null ? '' : str + '';
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
test({ assert, target }) {
const btn = target.querySelector('button');
btn?.click();
flushSync();
assert.htmlEqual(target.innerHTML, `<button>add</button><span>1,2,3,4</span>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
let array = $state([1, 2, 3]);
function addToArray() {
array.push(array.length + 1);
}
</script>

<button onclick={addToArray}>add</button>

<span>{array}</span>