pull/10891/head
Rich Harris 2 years ago
parent 512ae90f6b
commit 52c24d2803

@ -123,17 +123,14 @@ export function empty() {
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
export function child(node) { export function child(node) {
const child = first_child_get.call(node); const child = first_child_get.call(node);
if (hydrating) { if (!hydrating) return child;
// Child can be null if we have an element with a single child, like `<p>{text}</p>`, where `text` is empty
if (child === null) { // Child can be null if we have an element with a single child, like `<p>{text}</p>`, where `text` is empty
const text = empty(); if (child === null) {
node.appendChild(text); return node.appendChild(empty());
return text;
} else {
return capture_fragment_from_node(child);
}
} }
return child;
return capture_fragment_from_node(child);
} }
/** /**
@ -144,26 +141,26 @@ export function child(node) {
*/ */
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
export function child_frag(node, is_text) { export function child_frag(node, is_text) {
if (hydrating) { if (!hydrating) {
const first_node = /** @type {import('#client').TemplateNode[]} */ (node)[0]; return first_child_get.call(/** @type {Node} */ (node));
}
// if an {expression} is empty during SSR, there might be no const first_node = /** @type {import('#client').TemplateNode[]} */ (node)[0];
// 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;
}
if (first_node !== null) { // if an {expression} is empty during SSR, there might be no
return capture_fragment_from_node(first_node); // 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__*/ /*#__NO_SIDE_EFFECTS__*/
export function sibling(node, is_text = false) { export function sibling(node, is_text = false) {
const next_sibling = next_sibling_get.call(node); const next_sibling = next_sibling_get.call(node);
if (hydrating) { if (hydrating) {
// if a sibling {expression} is empty during SSR, there might be no // if a sibling {expression} is empty during SSR, there might be no
// text node to hydrate — we must therefore create one // text node to hydrate — we must therefore create one
@ -183,7 +181,7 @@ export function sibling(node, is_text = false) {
if (next_sibling) { if (next_sibling) {
const index = hydrate_nodes.indexOf(/** @type {Text | Comment | Element} */ (next_sibling)); const index = hydrate_nodes.indexOf(/** @type {Text | Comment | Element} */ (next_sibling));
hydrate_nodes.splice(index, 0, text); hydrate_nodes.splice(index, 0, text);
/** @type {DocumentFragment} */ (next_sibling.parentNode).insertBefore(text, next_sibling); next_sibling.before(text);
} else { } else {
hydrate_nodes.push(text); hydrate_nodes.push(text);
} }
@ -195,6 +193,7 @@ export function sibling(node, is_text = false) {
return capture_fragment_from_node(next_sibling); return capture_fragment_from_node(next_sibling);
} }
} }
return next_sibling; return next_sibling;
} }

Loading…
Cancel
Save