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

98.64% Statements 146/148
100% Branches 27/27
85.71% Functions 6/7
98.61% Lines 142/144

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 142 143 144 1452x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3024x 3020x 3020x 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 3024x 2x 2x 2x 10725x 10725x 2x 2x 2x 2x 2x 2x 2x 2x 7393x 7393x 3278x 3278x 7353x 41x 41x 3237x 3237x 3237x 2x 2x 2x 2x 2x 2x 2x 2x 5976x 3219x 3219x 3219x 2757x 2757x 2757x 5976x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2755x 2755x 2755x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6764x 6764x 6764x 3700x 3700x 3064x 3064x 3064x 6764x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3062x 3062x 3062x 2x 2x 2x 2x 2x 2x 2x 11x 11x 2x 2x 2x 2x      
import { hydrate_anchor, hydrate_start, hydrating } from './hydration.js';
import { DEV } from 'esm-env';
import { init_array_prototype_warnings } from '../dev/equality.js';
import { current_effect } from '../runtime.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.__nodeValue = ' ';
 
	if (DEV) {
		// @ts-expect-error
		element_prototype.__svelte_meta = null;
 
		init_array_prototype_warnings();
	}
}
 
/** @returns {Text} */
export function empty() {
	return document.createTextNode('');
}
 
/**
 * @template {Node} N
 * @param {N} node
 * @returns {Node | null}
 */
/*#__NO_SIDE_EFFECTS__*/
export function child(node) {
	const child = node.firstChild;
	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) {
		return node.appendChild(empty());
	}
 
	return hydrate_anchor(child);
}
 
/**
 * @param {DocumentFragment | import('#client').TemplateNode[]} fragment
 * @param {boolean} is_text
 * @returns {Node | null}
 */
/*#__NO_SIDE_EFFECTS__*/
export function first_child(fragment, is_text) {
	if (!hydrating) {
		// when not hydrating, `fragment` is a `DocumentFragment` (the result of calling `open_frag`)
		return /** @type {DocumentFragment} */ (fragment).firstChild;
	}
 
	// if an {expression} is empty during SSR, there might be no
	// text node to hydrate — we must therefore create one
	if (is_text && hydrate_start?.nodeType !== 3) {
		var text = empty();
		var dom = /** @type {import('#client').TemplateNode[]} */ (
			/** @type {import('#client').Effect} */ (current_effect).dom
		);
 
		dom.unshift(text);
		hydrate_start?.before(text);
 
		return text;
	}
 
	return hydrate_anchor(hydrate_start);
}
 
/**
 * @template {Node} N
 * @param {N} node
 * @param {boolean} is_text
 * @returns {Node | null}
 */
/*#__NO_SIDE_EFFECTS__*/
export function sibling(node, is_text = false) {
	const next_sibling = node.nextSibling;
 
	if (!hydrating) {
		return next_sibling;
	}
 
	// if a sibling {expression} is empty during SSR, there might be no
	// text node to hydrate — we must therefore create one
	if (is_text && next_sibling?.nodeType !== 3) {
		var text = empty();
		var dom = /** @type {import('#client').TemplateNode[]} */ (
			/** @type {import('#client').Effect} */ (current_effect).dom
		);
 
		dom.unshift(text);
		next_sibling?.before(text);
 
		return text;
	}
 
	return hydrate_anchor(/** @type {Node} */ (next_sibling));
}
 
/**
 * @template {Node} N
 * @param {N} node
 * @returns {void}
 */
export function clear_text_content(node) {
	node.textContent = '';
}
 
/** @param {string} name */
/*#__NO_SIDE_EFFECTS__*/
export function create_element(name) {
	return document.createElement(name);
}