Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

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: 2 additions & 3 deletions packages/svelte/src/internal/client/dom/blocks/css-props.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @import { TemplateNode } from '#client' */
import { render_effect, teardown } from '../../reactivity/effects.js';
import { render_effect } from '../../reactivity/effects.js';
import { hydrating, set_hydrate_node } from '../hydration.js';
import { get_first_child } from '../operations.js';

Expand All @@ -10,7 +9,7 @@ import { get_first_child } from '../operations.js';
*/
export function css_props(element, get_styles) {
if (hydrating) {
set_hydrate_node(/** @type {TemplateNode} */ (get_first_child(element)));
set_hydrate_node(get_first_child(element));
}

render_effect(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dom/blocks/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
var parent_node = /** @type {Element} */ (node);

anchor = hydrating
? set_hydrate_node(/** @type {Comment | Text} */ (get_first_child(parent_node)))
? set_hydrate_node(get_first_child(parent_node))
: parent_node.appendChild(create_text());
}

Expand Down
6 changes: 4 additions & 2 deletions packages/svelte/src/internal/client/dom/blocks/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export function html(node, get_value, svg = false, mathml = false, skip_warning
// We're deliberately not trying to repair mismatches between server and client,
// as it's costly and error-prone (and it's an edge case to have a mismatch anyway)
var hash = /** @type {Comment} */ (hydrate_node).data;

/** @type {TemplateNode | null} */
var next = hydrate_next();
var last = next;

Expand All @@ -73,7 +75,7 @@ export function html(node, get_value, svg = false, mathml = false, skip_warning
(next.nodeType !== COMMENT_NODE || /** @type {Comment} */ (next).data !== '')
) {
last = next;
next = /** @type {TemplateNode} */ (get_next_sibling(next));
next = get_next_sibling(next);
}

if (next === null) {
Expand Down Expand Up @@ -110,7 +112,7 @@ export function html(node, get_value, svg = false, mathml = false, skip_warning

if (svg || mathml) {
while (get_first_child(node)) {
anchor.before(/** @type {Node} */ (get_first_child(node)));
anchor.before(/** @type {TemplateNode} */ (get_first_child(node)));
}
} else {
anchor.before(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio

// If hydrating, use the existing ssr comment as the anchor so that the
// inner open and close methods can pick up the existing nodes correctly
var child_anchor = /** @type {TemplateNode} */ (
hydrating ? get_first_child(element) : element.appendChild(create_text())
);
var child_anchor = hydrating
? get_first_child(element)
: element.appendChild(create_text());

if (hydrating) {
if (child_anchor === null) {
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/dom/blocks/svelte-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export function head(hash, render_fn) {
if (hydrating) {
previous_hydrate_node = hydrate_node;

var head_anchor = /** @type {TemplateNode} */ (get_first_child(document.head));
var head_anchor = get_first_child(document.head);

// There might be multiple head blocks in our app, and they could have been
// rendered in an arbitrary order — find one corresponding to this component
while (
head_anchor !== null &&
(head_anchor.nodeType !== COMMENT_NODE || /** @type {Comment} */ (head_anchor).data !== hash)
) {
head_anchor = /** @type {TemplateNode} */ (get_next_sibling(head_anchor));
head_anchor = get_next_sibling(head_anchor);
}

// If we can't find an opening hydration marker, skip hydration (this can happen
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/dom/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function set_hydrating(value) {
*/
export let hydrate_node;

/** @param {TemplateNode} node */
/** @param {TemplateNode | null} node */
export function set_hydrate_node(node) {
if (node === null) {
w.hydration_mismatch();
Expand All @@ -41,7 +41,7 @@ export function set_hydrate_node(node) {
}

export function hydrate_next() {
return set_hydrate_node(/** @type {TemplateNode} */ (get_next_sibling(hydrate_node)));
return set_hydrate_node(get_next_sibling(hydrate_node));
}

/** @param {TemplateNode} node */
Expand Down
23 changes: 10 additions & 13 deletions packages/svelte/src/internal/client/dom/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,36 +83,34 @@ export function create_text(value = '') {
/**
* @template {Node} N
* @param {N} node
* @returns {Node | null}
*/
/*@__NO_SIDE_EFFECTS__*/
export function get_first_child(node) {
return first_child_getter.call(node);
return /** @type {TemplateNode | null} */ (first_child_getter.call(node));
}

/**
* @template {Node} N
* @param {N} node
* @returns {Node | null}
*/
/*@__NO_SIDE_EFFECTS__*/
export function get_next_sibling(node) {
return next_sibling_getter.call(node);
return /** @type {TemplateNode | null} */ (next_sibling_getter.call(node));
}

/**
* Don't mark this as side-effect-free, hydration needs to walk all nodes
* @template {Node} N
* @param {N} node
* @param {boolean} is_text
* @returns {Node | null}
* @returns {TemplateNode | null}
*/
export function child(node, is_text) {
if (!hydrating) {
return get_first_child(node);
}

var child = /** @type {TemplateNode} */ (get_first_child(hydrate_node));
var child = get_first_child(hydrate_node);

// Child can be null if we have an element with a single child, like `<p>{text}</p>`, where `text` is empty
if (child === null) {
Expand All @@ -130,14 +128,13 @@ export function child(node, is_text) {

/**
* Don't mark this as side-effect-free, hydration needs to walk all nodes
* @param {DocumentFragment | TemplateNode | TemplateNode[]} fragment
* @param {TemplateNode} node
* @param {boolean} [is_text]
* @returns {Node | null}
* @returns {TemplateNode | null}
*/
export function first_child(fragment, is_text = false) {
export function first_child(node, is_text = false) {
if (!hydrating) {
// when not hydrating, `fragment` is a `DocumentFragment` (the result of calling `open_frag`)
var first = /** @type {DocumentFragment} */ (get_first_child(/** @type {Node} */ (fragment)));
var first = get_first_child(node);

// TODO prevent user comments with the empty string when preserveComments is true
if (first instanceof Comment && first.data === '') return get_next_sibling(first);
Expand All @@ -163,7 +160,7 @@ export function first_child(fragment, is_text = false) {
* @param {TemplateNode} node
* @param {number} count
* @param {boolean} is_text
* @returns {Node | null}
* @returns {TemplateNode | null}
*/
export function sibling(node, count = 1, is_text = false) {
let next_sibling = hydrating ? hydrate_node : node;
Expand Down Expand Up @@ -195,7 +192,7 @@ export function sibling(node, count = 1, is_text = false) {
}

set_hydrate_node(next_sibling);
return /** @type {TemplateNode} */ (next_sibling);
return next_sibling;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/internal/client/dom/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function from_html(content, flags) {

if (node === undefined) {
node = create_fragment_from_html(has_start ? content : '<!>' + content);
if (!is_fragment) node = /** @type {Node} */ (get_first_child(node));
if (!is_fragment) node = /** @type {TemplateNode} */ (get_first_child(node));
}

var clone = /** @type {TemplateNode} */ (
Expand Down Expand Up @@ -113,7 +113,7 @@ function from_namespace(content, flags, ns = 'svg') {
if (is_fragment) {
node = document.createDocumentFragment();
while (get_first_child(root)) {
node.appendChild(/** @type {Node} */ (get_first_child(root)));
node.appendChild(/** @type {TemplateNode} */ (get_first_child(root)));
}
} else {
node = /** @type {Element} */ (get_first_child(root));
Expand Down Expand Up @@ -227,7 +227,7 @@ export function from_tree(structure, flags) {
: undefined;

node = fragment_from_tree(structure, ns);
if (!is_fragment) node = /** @type {Node} */ (get_first_child(node));
if (!is_fragment) node = /** @type {TemplateNode} */ (get_first_child(node));
}

var clone = /** @type {TemplateNode} */ (
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ export function destroy_effect(effect, remove_dom = true) {
export function remove_effect_dom(node, end) {
while (node !== null) {
/** @type {TemplateNode | null} */
var next = node === end ? null : /** @type {TemplateNode} */ (get_next_sibling(node));
var next = node === end ? null : get_next_sibling(node);

node.remove();
node = next;
Expand Down Expand Up @@ -715,7 +715,7 @@ export function move_effect(effect, fragment) {

while (node !== null) {
/** @type {TemplateNode | null} */
var next = node === end ? null : /** @type {TemplateNode} */ (get_next_sibling(node));
var next = node === end ? null : get_next_sibling(node);

fragment.append(node);
node = next;
Expand Down
5 changes: 3 additions & 2 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ export function hydrate(component, options) {
const previous_hydrate_node = hydrate_node;

try {
var anchor = /** @type {TemplateNode} */ (get_first_child(target));
var anchor = get_first_child(target);

while (
anchor &&
(anchor.nodeType !== COMMENT_NODE || /** @type {Comment} */ (anchor).data !== HYDRATION_START)
) {
anchor = /** @type {TemplateNode} */ (get_next_sibling(anchor));
anchor = get_next_sibling(anchor);
}

if (!anchor) {
Expand Down
Loading