From b70d12c2e387121763e69fe483b2102b2f64ff83 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Sun, 1 Sep 2024 19:57:25 +0100 Subject: [PATCH] fix: render undefined html as the empty string (#13092) Fix #13069 --------- Co-authored-by: adiGuba Co-authored-by: Rich Harris --- .changeset/eighty-mails-develop.md | 5 +++++ packages/svelte/src/internal/client/dom/blocks/html.js | 2 +- packages/svelte/src/internal/server/blocks/html.js | 5 +++-- .../samples/binding-contenteditable-html-initial/_config.js | 2 +- .../tests/runtime-runes/samples/html-tag-empty/_config.js | 5 +++++ .../tests/runtime-runes/samples/html-tag-empty/main.svelte | 1 + 6 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 .changeset/eighty-mails-develop.md create mode 100644 packages/svelte/tests/runtime-runes/samples/html-tag-empty/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/html-tag-empty/main.svelte diff --git a/.changeset/eighty-mails-develop.md b/.changeset/eighty-mails-develop.md new file mode 100644 index 0000000000..94eee085f7 --- /dev/null +++ b/.changeset/eighty-mails-develop.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: render undefined html as the empty string diff --git a/packages/svelte/src/internal/client/dom/blocks/html.js b/packages/svelte/src/internal/client/dom/blocks/html.js index 1240352ea0..aa13336b29 100644 --- a/packages/svelte/src/internal/client/dom/blocks/html.js +++ b/packages/svelte/src/internal/client/dom/blocks/html.js @@ -50,7 +50,7 @@ export function html(node, get_value, svg, mathml, skip_warning) { var effect; block(() => { - if (value === (value = get_value())) { + if (value === (value = get_value() ?? '')) { if (hydrating) { hydrate_next(); } diff --git a/packages/svelte/src/internal/server/blocks/html.js b/packages/svelte/src/internal/server/blocks/html.js index 09efbc78fe..c8ef5d81b4 100644 --- a/packages/svelte/src/internal/server/blocks/html.js +++ b/packages/svelte/src/internal/server/blocks/html.js @@ -5,6 +5,7 @@ import { hash } from '../../../utils.js'; * @param {string} value */ export function html(value) { - var open = DEV ? `` : ''; - return `${open}${value}`; + var html = String(value ?? ''); + var open = DEV ? `` : ''; + return open + html + ''; } diff --git a/packages/svelte/tests/runtime-legacy/samples/binding-contenteditable-html-initial/_config.js b/packages/svelte/tests/runtime-legacy/samples/binding-contenteditable-html-initial/_config.js index 38cf0ac542..46984df0c2 100644 --- a/packages/svelte/tests/runtime-legacy/samples/binding-contenteditable-html-initial/_config.js +++ b/packages/svelte/tests/runtime-legacy/samples/binding-contenteditable-html-initial/_config.js @@ -15,7 +15,7 @@ export default test({ ssrHtml: ` world -

hello null

+

hello

`, async test({ assert, component, target, window }) { diff --git a/packages/svelte/tests/runtime-runes/samples/html-tag-empty/_config.js b/packages/svelte/tests/runtime-runes/samples/html-tag-empty/_config.js new file mode 100644 index 0000000000..653ccc2192 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/html-tag-empty/_config.js @@ -0,0 +1,5 @@ +import { test } from '../../test'; + +export default test({ + html: '' +}); diff --git a/packages/svelte/tests/runtime-runes/samples/html-tag-empty/main.svelte b/packages/svelte/tests/runtime-runes/samples/html-tag-empty/main.svelte new file mode 100644 index 0000000000..d03d2a46c2 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/html-tag-empty/main.svelte @@ -0,0 +1 @@ +{@html undefined}