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

100% Statements 144/144
100% Branches 30/30
100% Functions 6/6
100% Lines 141/141

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 1422x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3173x 3169x 3169x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3173x 2x 2x 2x 4913x 4913x 2x 2x 2x 2x 2x 2x 2x 2x 7477x 4153x 4153x 3324x 3324x 3324x 3324x 7437x 43x 43x 3324x 3324x 3324x 3324x 2x 2x 2x 2x 2x 2x 2x 2x 3317x 1838x 1838x 1838x 1838x 1838x 726x 726x 726x 1479x 1479x 1479x 3317x 2x 2x 2x 2x 2x 2x 1477x 1477x 1477x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7083x 3853x 3853x 3230x 3230x 3230x 3230x 3230x 3230x 3230x 7083x 2x 2x 2x 2x 2x 3228x 3228x 3228x 3228x 2x 2x 2x 2x 2x 2x 2x 13x 13x  
/** @import { TemplateNode } from '#client' */
import { hydrate_node, hydrating, set_hydrate_node } from './hydration.js';
import { DEV } from 'esm-env';
import { init_array_prototype_warnings } from '../dev/equality.js';
 
// export these for reference in the compiled code, making global name deduplication unnecessary
/** @type {Window} */
export var $window;
 
/** @type {Document} */
export var $document;
 
/**
 * Initialize these lazily to avoid issues when using the runtime in a server context
 * where these globals are not available while avoiding a separate server entry point
 */
export function init_operations() {
	if ($window !== undefined) {
		return;
	}
 
	$window = window;
	$document = document;
 
	var element_prototype = Element.prototype;
 
	// the following assignments improve perf of lookups on DOM nodes
	// @ts-expect-error
	element_prototype.__click = undefined;
	// @ts-expect-error
	element_prototype.__className = '';
	// @ts-expect-error
	element_prototype.__attributes = null;
	// @ts-expect-error
	element_prototype.__e = undefined;
 
	// @ts-expect-error
	Text.prototype.__t = undefined;
 
	if (DEV) {
		// @ts-expect-error
		element_prototype.__svelte_meta = null;
 
		init_array_prototype_warnings();
	}
}
 
/** @returns {Text} */
export function empty() {
	return document.createTextNode('');
}
 
/**
 * Don't mark this as side-effect-free, hydration needs to walk all nodes
 * @template {Node} N
 * @param {N} node
 * @returns {Node | null}
 */
export function child(node) {
	if (!hydrating) {
		return node.firstChild;
	}
 
	var child = /** @type {TemplateNode} */ (hydrate_node.firstChild);
 
	// 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 = hydrate_node.appendChild(empty());
	}
 
	set_hydrate_node(child);
	return child;
}
 
/**
 * Don't mark this as side-effect-free, hydration needs to walk all nodes
 * @param {DocumentFragment | TemplateNode[]} fragment
 * @param {boolean} is_text
 * @returns {Node | null}
 */
export function first_child(fragment, is_text) {
	if (!hydrating) {
		// when not hydrating, `fragment` is a `DocumentFragment` (the result of calling `open_frag`)
		var first = /** @type {DocumentFragment} */ (fragment).firstChild;
 
		// TODO prevent user comments with the empty string when preserveComments is true
		if (first instanceof Comment && first.data === '') return first.nextSibling;
 
		return first;
	}
 
	// if an {expression} is empty during SSR, there might be no
	// text node to hydrate — we must therefore create one
	if (is_text && hydrate_node?.nodeType !== 3) {
		var text = empty();
 
		hydrate_node?.before(text);
		set_hydrate_node(text);
		return text;
	}
 
	return hydrate_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}
 */
export function sibling(node, is_text = false) {
	if (!hydrating) {
		return /** @type {TemplateNode} */ (node.nextSibling);
	}
 
	var next_sibling = /** @type {TemplateNode} */ (hydrate_node.nextSibling);
 
	var type = next_sibling.nodeType;
 
	// if a sibling {expression} is empty during SSR, there might be no
	// text node to hydrate — we must therefore create one
	if (is_text && type !== 3) {
		var text = empty();
		next_sibling?.before(text);
		set_hydrate_node(text);
		return text;
	}
 
	set_hydrate_node(next_sibling);
	return /** @type {TemplateNode} */ (next_sibling);
}
 
/**
 * @template {Node} N
 * @param {N} node
 * @returns {void}
 */
export function clear_text_content(node) {
	node.textContent = '';
}