Skip to content

chore: consistent nextSibling usage #11694

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

Merged
merged 2 commits into from
May 20, 2024
Merged
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
8 changes: 4 additions & 4 deletions packages/svelte/src/internal/client/dom/blocks/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
HYDRATION_START
} from '../../../../constants.js';
import { hydrate_anchor, hydrate_nodes, hydrating, set_hydrating } from '../hydration.js';
import { clear_text_content, empty } from '../operations.js';
import { clear_text_content, empty, next_sibling } from '../operations.js';
import { remove } from '../reconciler.js';
import { untrack } from '../../runtime.js';
import {
Expand Down Expand Up @@ -175,15 +175,15 @@ export function each(anchor, flags, get_collection, get_key, render_fn, fallback
var key = get_key(value, i);
item = create_item(child_open, child_anchor, prev, null, value, key, i, render_fn, flags);
state.items.set(key, item);
child_anchor = /** @type {Comment} */ (child_anchor.nextSibling);
child_anchor = /** @type {Comment} */ (next_sibling(child_anchor));

prev = item;
}

// remove excess nodes
if (length > 0) {
while (child_anchor !== anchor) {
var next = /** @type {import('#client').TemplateNode} */ (child_anchor.nextSibling);
var next = /** @type {import('#client').TemplateNode} */ (next_sibling(child_anchor));
/** @type {import('#client').TemplateNode} */ (child_anchor).remove();
child_anchor = next;
}
Expand Down Expand Up @@ -501,7 +501,7 @@ function move(item, next, anchor) {
var node = /** @type {import('#client').TemplateNode} */ (item.o);

while (node !== end) {
var next_node = /** @type {import('#client').TemplateNode} */ (node.nextSibling);
var next_node = /** @type {import('#client').TemplateNode} */ (next_sibling(node));
dest.before(node);
node = next_node;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/svelte/src/internal/client/dom/blocks/svelte-head.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hydrate_anchor, hydrate_nodes, hydrating, set_hydrate_nodes } from '../hydration.js';
import { empty } from '../operations.js';
import { empty, next_sibling } from '../operations.js';
import { block } from '../../reactivity/effects.js';
import { HYDRATION_END, HYDRATION_START } from '../../../../constants.js';
import { HYDRATION_START } from '../../../../constants.js';

/**
* @type {Node | undefined}
Expand Down Expand Up @@ -37,11 +37,11 @@ export function head(render_fn) {
head_anchor.nodeType !== 8 ||
/** @type {Comment} */ (head_anchor).data !== HYDRATION_START
) {
head_anchor = /** @type {import('#client').TemplateNode} */ (head_anchor.nextSibling);
head_anchor = /** @type {import('#client').TemplateNode} */ (next_sibling(head_anchor));
}

head_anchor = /** @type {import('#client').TemplateNode} */ (hydrate_anchor(head_anchor));
head_anchor = /** @type {import('#client').TemplateNode} */ (head_anchor.nextSibling);
head_anchor = /** @type {import('#client').TemplateNode} */ (next_sibling(head_anchor));
} else {
anchor = document.head.appendChild(empty());
}
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/internal/client/dom/hydration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DEV } from 'esm-env';
import { HYDRATION_END, HYDRATION_START, HYDRATION_ERROR } from '../../../constants.js';
import * as w from '../warnings.js';
import { next_sibling } from './operations.js';

/**
* Use this variable to guard everything related to hydration code so it can be treeshaken out
Expand Down Expand Up @@ -49,7 +50,7 @@ export function hydrate_anchor(node) {
var nodes = [];
var depth = 0;

while ((current = /** @type {Node} */ (current).nextSibling) !== null) {
while ((current = next_sibling(/** @type {Node} */ (current))) !== null) {
if (current.nodeType === 8) {
var data = /** @type {Comment} */ (current).data;

Expand Down
10 changes: 10 additions & 0 deletions packages/svelte/src/internal/client/dom/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ export function first_child(fragment, is_text) {
return hydrate_anchor(first_node);
}

/**
* @template {Node} N
* @param {N} node
* @returns {Node | null}
*/
/*#__NO_SIDE_EFFECTS__*/
export function next_sibling(node) {
return next_sibling_get.call(node);
}

/**
* @template {Node} N
* @param {N} node
Expand Down
12 changes: 6 additions & 6 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
clear_text_content,
create_element,
empty,
init_operations
init_operations,
next_sibling
} from './dom/operations.js';
import { HYDRATION_ERROR, HYDRATION_START, PassiveDelegatedEvents } from '../../constants.js';
import { flush_sync, push, pop, current_component_context } from './runtime.js';
Expand Down Expand Up @@ -106,6 +107,7 @@ export function mount(component, options) {
if (DEV) {
validate_component(component);
}
init_operations();

const anchor = options.anchor ?? options.target.appendChild(empty());
// Don't flush previous effects to ensure order of outer effects stays consistent
Expand Down Expand Up @@ -133,6 +135,7 @@ export function hydrate(component, options) {
if (DEV) {
validate_component(component);
}
init_operations();

const target = options.target;
const previous_hydrate_nodes = hydrate_nodes;
Expand All @@ -144,12 +147,13 @@ export function hydrate(component, options) {
return flush_sync(() => {
set_hydrating(true);

/** @type {Node | null} */
var node = target.firstChild;
while (
node &&
(node.nodeType !== 8 || /** @type {Comment} */ (node).data !== HYDRATION_START)
) {
node = node.nextSibling;
node = next_sibling(node);
}

if (!node) {
Expand All @@ -172,8 +176,6 @@ export function hydrate(component, options) {
e.hydration_failed();
}

// If an error occured above, the operations might not yet have been initialised.
init_operations();
clear_text_content(target);

set_hydrating(false);
Expand Down Expand Up @@ -202,8 +204,6 @@ export function hydrate(component, options) {
* @returns {Exports}
*/
function _mount(Component, { target, anchor, props = {}, events, context, intro = false }) {
init_operations();

const registered_events = new Set();

const bound_event_listener = handle_event_propagation.bind(null, target);
Expand Down
Loading