From f67d03df5b49e7c9c2e65ec66739f30c9d3d403a Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Sun, 22 Feb 2026 14:40:49 +0100 Subject: [PATCH] fix: make string coercion consistent to `toString` (#17774) Closes #17758 ### Before submitting the PR, please make sure you do the following - [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs - [x] Prefix your PR title with `feat:`, `fix:`, `chore:`, or `docs:`. - [x] This message body should clearly illustrate what problems it solves. - [x] Ideally, include a test that fails without this PR but passes with it. - [x] If this PR changes code within `packages/svelte/src`, add a changeset (`npx changeset`). ### Tests and linting - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` --- .changeset/thirty-groups-rhyme.md | 5 +++++ packages/svelte/src/internal/client/render.js | 4 ++-- .../set-text-stable-coercion/_config.js | 6 +++++ .../set-text-stable-coercion/main.svelte | 22 +++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .changeset/thirty-groups-rhyme.md create mode 100644 packages/svelte/tests/runtime-runes/samples/set-text-stable-coercion/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/set-text-stable-coercion/main.svelte diff --git a/.changeset/thirty-groups-rhyme.md b/.changeset/thirty-groups-rhyme.md new file mode 100644 index 0000000000..5a6bde7677 --- /dev/null +++ b/.changeset/thirty-groups-rhyme.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: make string coercion consistent to `toString` diff --git a/packages/svelte/src/internal/client/render.js b/packages/svelte/src/internal/client/render.js index 4316403ba6..cb152ed9c1 100644 --- a/packages/svelte/src/internal/client/render.js +++ b/packages/svelte/src/internal/client/render.js @@ -45,12 +45,12 @@ export function set_should_intro(value) { */ 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; + var str = value == null ? '' : typeof value === 'object' ? `${value}` : value; // @ts-expect-error if (str !== (text.__t ??= text.nodeValue)) { // @ts-expect-error text.__t = str; - text.nodeValue = str + ''; + text.nodeValue = `${str}`; } } diff --git a/packages/svelte/tests/runtime-runes/samples/set-text-stable-coercion/_config.js b/packages/svelte/tests/runtime-runes/samples/set-text-stable-coercion/_config.js new file mode 100644 index 0000000000..01ef02510a --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/set-text-stable-coercion/_config.js @@ -0,0 +1,6 @@ +import { test } from '../../test'; + +export default test({ + html: 'toString

toString

[toString]

toString', + async test() {} +}); diff --git a/packages/svelte/tests/runtime-runes/samples/set-text-stable-coercion/main.svelte b/packages/svelte/tests/runtime-runes/samples/set-text-stable-coercion/main.svelte new file mode 100644 index 0000000000..601e7ce9fb --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/set-text-stable-coercion/main.svelte @@ -0,0 +1,22 @@ + + + +{value} + + +

{value}

+ + +

[{value}]

+ + +{#if 1} + {value} +{/if}