diff --git a/packages/svelte/src/internal/client/dom/operations.js b/packages/svelte/src/internal/client/dom/operations.js index de6233d205..0ef60f52c8 100644 --- a/packages/svelte/src/internal/client/dom/operations.js +++ b/packages/svelte/src/internal/client/dom/operations.js @@ -123,17 +123,14 @@ export function empty() { /*#__NO_SIDE_EFFECTS__*/ export function child(node) { const child = first_child_get.call(node); - if (hydrating) { - // Child can be null if we have an element with a single child, like `

{text}

`, where `text` is empty - if (child === null) { - const text = empty(); - node.appendChild(text); - return text; - } else { - return capture_fragment_from_node(child); - } + if (!hydrating) return child; + + // Child can be null if we have an element with a single child, like `

{text}

`, where `text` is empty + if (child === null) { + return node.appendChild(empty()); } - return child; + + return capture_fragment_from_node(child); } /** @@ -144,26 +141,26 @@ export function child(node) { */ /*#__NO_SIDE_EFFECTS__*/ export function child_frag(node, is_text) { - if (hydrating) { - const first_node = /** @type {import('#client').TemplateNode[]} */ (node)[0]; + if (!hydrating) { + return first_child_get.call(/** @type {Node} */ (node)); + } - // if an {expression} is empty during SSR, there might be no - // text node to hydrate — we must therefore create one - if (is_text && first_node?.nodeType !== 3) { - const text = empty(); - hydrate_nodes.unshift(text); - first_node?.before(text); - return text; - } + const first_node = /** @type {import('#client').TemplateNode[]} */ (node)[0]; - if (first_node !== null) { - return capture_fragment_from_node(first_node); - } + // if an {expression} is empty during SSR, there might be no + // text node to hydrate — we must therefore create one + if (is_text && first_node?.nodeType !== 3) { + const text = empty(); + hydrate_nodes.unshift(text); + first_node?.before(text); + return text; + } - return first_node; + if (first_node !== null) { + return capture_fragment_from_node(first_node); } - return first_child_get.call(/** @type {Node} */ (node)); + return first_node; } /** @@ -175,6 +172,7 @@ export function child_frag(node, is_text) { /*#__NO_SIDE_EFFECTS__*/ export function sibling(node, is_text = false) { const next_sibling = next_sibling_get.call(node); + if (hydrating) { // if a sibling {expression} is empty during SSR, there might be no // text node to hydrate — we must therefore create one @@ -183,7 +181,7 @@ export function sibling(node, is_text = false) { if (next_sibling) { const index = hydrate_nodes.indexOf(/** @type {Text | Comment | Element} */ (next_sibling)); hydrate_nodes.splice(index, 0, text); - /** @type {DocumentFragment} */ (next_sibling.parentNode).insertBefore(text, next_sibling); + next_sibling.before(text); } else { hydrate_nodes.push(text); } @@ -195,6 +193,7 @@ export function sibling(node, is_text = false) { return capture_fragment_from_node(next_sibling); } } + return next_sibling; }