Skip to content
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

feat: {@html} should handle null and undefined as empty string #13072

Closed
wants to merge 3 commits into from
Closed
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/eighty-mails-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: render undefined html as the empty string
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dom/blocks/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function html(node, get_value, svg, mathml, skip_warning) {
return;
}

var html = value + '';
var html = (value == null) ? '' : value + '';
if (svg) html = `<svg>${html}</svg>`;
else if (mathml) html = `<math>${html}</math>`;

Expand Down
5 changes: 3 additions & 2 deletions packages/svelte/src/internal/server/blocks/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { hash } from '../../../utils.js';
* @param {string} value
*/
export function html(value) {
var open = DEV ? `<!--${hash(String(value ?? ''))}-->` : '<!---->';
return `${open}${value}<!---->`;
var html = String(value ?? '');
var open = DEV ? `<!--${hash(html)}-->` : '<!---->';
return open + html + '<!---->';
}
4 changes: 2 additions & 2 deletions packages/svelte/tests/runtime-legacy/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ async function run_test_variant(
config.withoutNormalizeHtml === 'only-strip-comments' ? false : undefined,
withoutNormalizeHtml: !!config.withoutNormalizeHtml
});
} else if (config.html) {
} else if (config.html !== undefined) {
assert_html_equal_with_options(target.innerHTML, config.html, {
preserveComments:
config.withoutNormalizeHtml === 'only-strip-comments' ? false : undefined,
Expand Down Expand Up @@ -369,7 +369,7 @@ async function run_test_variant(
assert.fail('Expected a runtime error');
}

if (config.html) {
if (config.html !== undefined) {
flushSync();
assert_html_equal_with_options(target.innerHTML, config.html, {
preserveComments:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: ''
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{@html undefined}
Loading