From 9429dc8eeb27f2090a82443be59df8022d2923de Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:50:53 -0800 Subject: [PATCH] implement inlining for template text nodes --- .../phases/3-transform/client/visitors/template.js | 11 ++++------- .../hydration/samples/html-tag-hydration/_after.html | 2 +- .../tests/runtime-legacy/samples/nbsp-div/_config.js | 11 +++-------- .../_expected/client/index.svelte.js | 5 ++--- 4 files changed, 10 insertions(+), 19 deletions(-) diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js index 987cc6578c..0a8080b4ec 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js @@ -1558,8 +1558,6 @@ function process_children(nodes, parent, { visit, state }) { return; } - push_template_quasi(state, ' '); - const text_id = get_node_id(expression, state, 'text'); const contains_call_expression = sequence.some( (n) => n.type === 'ExpressionTag' && n.metadata.contains_call_expression @@ -1575,21 +1573,19 @@ function process_children(nodes, parent, { visit, state }) { ); if (contains_call_expression && !within_bound_contenteditable) { + push_template_quasi(state, ' '); state.update_effects.push(singular); } else if ( sequence.some((node) => node.type === 'ExpressionTag' && node.metadata.dynamic) && !within_bound_contenteditable ) { + push_template_quasi(state, ' '); state.update.push({ singular, grouped: b.stmt(b.call('$.text', text_id, assignment)) }); } else { - // TODO: uncomment this - // it causes a test to fail. need to understand if it's a valid test or if it should be adjusted or deleted - // see https://github.com/sveltejs/svelte/issues/6832 for more context on that test - // const { can_inline } = can_inline_all_nodes(sequence, state); - const can_inline = false; + const { can_inline } = can_inline_all_nodes(sequence, state); if (can_inline) { for (let i = 0; i < assignment.quasis.length; i++) { push_template_quasi(state, assignment.quasis[i].value.raw); @@ -1598,6 +1594,7 @@ function process_children(nodes, parent, { visit, state }) { } } } else { + push_template_quasi(state, ' '); state.init.push(init); } } diff --git a/packages/svelte/tests/hydration/samples/html-tag-hydration/_after.html b/packages/svelte/tests/hydration/samples/html-tag-hydration/_after.html index b85905ec0b..733389da25 100644 --- a/packages/svelte/tests/hydration/samples/html-tag-hydration/_after.html +++ b/packages/svelte/tests/hydration/samples/html-tag-hydration/_after.html @@ -1 +1 @@ -1 2 3 + 1 2 3 diff --git a/packages/svelte/tests/runtime-legacy/samples/nbsp-div/_config.js b/packages/svelte/tests/runtime-legacy/samples/nbsp-div/_config.js index 0ffcdb7904..8cbdd7d133 100644 --- a/packages/svelte/tests/runtime-legacy/samples/nbsp-div/_config.js +++ b/packages/svelte/tests/runtime-legacy/samples/nbsp-div/_config.js @@ -7,13 +7,8 @@ export default test({ test({ assert, target }) { const div_list = target.querySelectorAll('div'); - assert.equal(normalizeSpaces(div_list[0].textContent), ' hello'); - assert.equal(normalizeSpaces(div_list[1].textContent), ' hello '); - assert.equal(normalizeSpaces(div_list[2].textContent), ' hello hello'); + assert.equal(div_list[0].textContent, ' hello'); + assert.equal(div_list[1].textContent, ' hello  '); + assert.equal(div_list[2].textContent, ' hello   hello'); } }); - -/** @param {string | null} str */ -function normalizeSpaces(str) { - return str ? str.replaceAll(String.fromCharCode(160), ' ') : str; -} diff --git a/packages/svelte/tests/snapshot/samples/hoist-unmodified-var/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/hoist-unmodified-var/_expected/client/index.svelte.js index 7d640226f1..46e589c5c9 100644 --- a/packages/svelte/tests/snapshot/samples/hoist-unmodified-var/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/hoist-unmodified-var/_expected/client/index.svelte.js @@ -8,7 +8,7 @@ const d = 'd'; import * as $ from "svelte/internal"; const boolean = false; -var frag = $.template(`

`); +var frag = $.template(`

boolean is ${$.stringify(boolean)} and autocapitalize is w${$.stringify(o)}r${$.stringify(d)}s

`); export default function Hoist_unmodified_var($$anchor, $$props) { $.push($$props, true); @@ -24,7 +24,6 @@ export default function Hoist_unmodified_var($$anchor, $$props) { var text = $.child(p); - text.nodeValue = `boolean is ${$.stringify(boolean)} and autocapitalize is w${$.stringify(o)}r${$.stringify(d)}s`; $.close($$anchor, p); $.pop(); -} \ No newline at end of file +}