All files / src/internal/client/dom hydration.js

100% Statements 73/73
100% Branches 16/16
100% Functions 3/3
100% Lines 71/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 722x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4434x 4434x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1481x 1481x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12729x 6091x 6091x 6638x 6638x 6638x 6638x 12729x 5x 5x 6633x 6633x 6633x 6633x 6633x 12729x 39960x 26762x 26762x 26762x 10065x 26762x 16696x 6631x 6631x 6631x 10065x 10065x 10065x 26762x 33329x 33329x 33329x 2x 2x 2x  
import { HYDRATION_END, HYDRATION_START } from '../../../constants.js';
import * as e from '../errors.js';
 
/**
 * Use this variable to guard everything related to hydration code so it can be treeshaken out
 * if the user doesn't use the `hydrate` method and these code paths are therefore not needed.
 */
export let hydrating = false;
 
/** @param {boolean} value */
export function set_hydrating(value) {
	hydrating = value;
}
 
/**
 * Array of nodes to traverse for hydration. This will be null if we're not hydrating, but for
 * the sake of simplicity we're not going to use `null` checks everywhere and instead rely on
 * the `hydrating` flag to tell whether or not we're in hydration mode at which point this is set.
 * @type {import('#client').TemplateNode[]}
 */
export let hydrate_nodes = /** @type {any} */ (null);
 
/** @param {import('#client').TemplateNode[]} nodes */
export function set_hydrate_nodes(nodes) {
	hydrate_nodes = nodes;
}
 
/**
 * This function is only called when `hydrating` is true. If passed a `<!--[-->` opening
 * hydration marker, it finds the corresponding closing marker and sets `hydrate_nodes`
 * to everything between the markers, before returning the closing marker.
 * @param {Node} node
 * @returns {Node}
 */
export function hydrate_anchor(node) {
	if (node.nodeType !== 8) {
		return node;
	}
 
	var current = /** @type {Node | null} */ (node);
 
	// TODO this could have false positives, if a user comment consisted of `[`. need to tighten that up
	if (/** @type {Comment} */ (current)?.data !== HYDRATION_START) {
		return node;
	}
 
	/** @type {Node[]} */
	var nodes = [];
	var depth = 0;
 
	while ((current = /** @type {Node} */ (current).nextSibling) !== null) {
		if (current.nodeType === 8) {
			var data = /** @type {Comment} */ (current).data;
 
			if (data === HYDRATION_START) {
				depth += 1;
			} else if (data[0] === HYDRATION_END) {
				if (depth === 0) {
					hydrate_nodes = /** @type {import('#client').TemplateNode[]} */ (nodes);
					return current;
				}
 
				depth -= 1;
			}
		}
 
		nodes.push(current);
	}
 
	e.hydration_missing_marker_close();
}