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

94.36% Statements 268/284
98.55% Branches 68/69
90% Functions 9/10
94.22% Lines 261/277

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 2782x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 19564x 19564x 19564x 19564x 19564x 11677x 19564x 7887x 574x 574x 7887x 7887x 747x 7887x 7140x 7140x 7887x 19564x 19564x 2x 2x 2x 2x 2x 2x 2x 2x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 8476x 3621x 3621x 4855x 8401x 2189x 2189x 2189x 8476x 8476x 8476x 8476x 8476x 8476x 8476x 8476x 8476x 2440x 2440x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 42x 42x 42x 42x 42x 42x 42x 91x 44x 44x 47x 90x 41x 41x 41x 37x 41x 4x 4x 11x 11x 4x 41x 47x 47x 47x 47x 47x 90x 91x 91x 91x 91x 42x 42x 2x 2x 2x 2x 2x 2x 2x 2x                                 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 12x 6x 6x 6x 12x 5x 5x 5x 4x 5x 1x 1x 9x 9x 1x 5x 6x 6x 6x 6x 6x 12x 12x 12x 12x 12x 5x 5x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 1x 2x 2x 2x 3x 3x 2x 2x 2x 2x 2x 2x 2236x 189x 189x 189x 2236x 1x 1x 1x 1x 189x 189x 189x 2x 2x 4331x 4331x 2028x 2028x 2303x 2303x 2303x 2303x 2303x 2303x 2303x 2x 2x 2x 2x 2x 2x 2x 2x 15070x 9190x 9190x 15070x  
import { hydrate_nodes, hydrating } from './hydration.js';
import { clone_node, empty } from './operations.js';
import { create_fragment_from_html } from './reconciler.js';
import { current_effect } from '../runtime.js';
import { TEMPLATE_FRAGMENT, TEMPLATE_USE_IMPORT_NODE } from '../../../constants.js';
import { effect } from '../reactivity/effects.js';
import { is_array } from '../utils.js';
 
/**
 * @template {import("#client").TemplateNode | import("#client").TemplateNode[]} T
 * @param {T} dom
 * @param {import("#client").Effect} effect
 * @returns {T}
 */
export function push_template_node(
	dom,
	effect = /** @type {import('#client').Effect} */ (current_effect)
) {
	var current_dom = effect.dom;
	if (current_dom === null) {
		effect.dom = dom;
	} else {
		if (!is_array(current_dom)) {
			current_dom = effect.dom = [current_dom];
		}
 
		if (is_array(dom)) {
			current_dom.push(...dom);
		} else {
			current_dom.push(dom);
		}
	}
	return dom;
}
 
/**
 * @param {string} content
 * @param {number} flags
 * @returns {() => Node | Node[]}
 */
/*#__NO_SIDE_EFFECTS__*/
export function template(content, flags) {
	var is_fragment = (flags & TEMPLATE_FRAGMENT) !== 0;
	var use_import_node = (flags & TEMPLATE_USE_IMPORT_NODE) !== 0;
 
	/** @type {Node} */
	var node;
 
	return () => {
		if (hydrating) {
			return push_template_node(is_fragment ? hydrate_nodes : hydrate_nodes[0]);
		}
 
		if (!node) {
			node = create_fragment_from_html(content);
			if (!is_fragment) node = /** @type {Node} */ (node.firstChild);
		}
		var clone = use_import_node ? document.importNode(node, true) : clone_node(node, true);
 
		push_template_node(
			is_fragment
				? /** @type {import('#client').TemplateNode[]} */ ([...clone.childNodes])
				: /** @type {import('#client').TemplateNode} */ (clone)
		);
 
		return clone;
	};
}
 
/**
 * @param {string} content
 * @param {number} flags
 * @returns {() => Node | Node[]}
 */
/*#__NO_SIDE_EFFECTS__*/
export function template_with_script(content, flags) {
	var first = true;
	var fn = template(content, flags);
 
	return () => {
		if (hydrating) return fn();
 
		var node = /** @type {Element | DocumentFragment} */ (fn());
 
		if (first) {
			first = false;
			run_scripts(node);
		}
 
		return node;
	};
}
 
/**
 * @param {string} content
 * @param {number} flags
 * @returns {() => Node | Node[]}
 */
/*#__NO_SIDE_EFFECTS__*/
export function svg_template(content, flags) {
	var is_fragment = (flags & TEMPLATE_FRAGMENT) !== 0;
	var fn = template(`<svg>${content}</svg>`, 0); // we don't need to worry about using importNode for SVGs
 
	/** @type {Element | DocumentFragment} */
	var node;
 
	return () => {
		if (hydrating) {
			return push_template_node(is_fragment ? hydrate_nodes : hydrate_nodes[0]);
		}
 
		if (!node) {
			var svg = /** @type {Element} */ (fn());
 
			if ((flags & TEMPLATE_FRAGMENT) === 0) {
				node = /** @type {Element} */ (svg.firstChild);
			} else {
				node = document.createDocumentFragment();
				while (svg.firstChild) {
					node.appendChild(svg.firstChild);
				}
			}
		}
 
		var clone = clone_node(node, true);
 
		push_template_node(
			is_fragment
				? /** @type {import('#client').TemplateNode[]} */ ([...clone.childNodes])
				: /** @type {import('#client').TemplateNode} */ (clone)
		);
 
		return clone;
	};
}
 
/**
 * @param {string} content
 * @param {number} flags
 * @returns {() => Node | Node[]}
 */
/*#__NO_SIDE_EFFECTS__*/
export function svg_template_with_script(content, flags) {
	var first = true;
	var fn = svg_template(content, flags);

	return () => {
		if (hydrating) return fn();

		var node = /** @type {Element | DocumentFragment} */ (fn());

		if (first) {
			first = false;
			run_scripts(node);
		}

		return node;
	};
}
 
/**
 * @param {string} content
 * @param {number} flags
 * @returns {() => Node | Node[]}
 */
/*#__NO_SIDE_EFFECTS__*/
export function mathml_template(content, flags) {
	var is_fragment = (flags & TEMPLATE_FRAGMENT) !== 0;
	var fn = template(`<math>${content}</math>`, 0); // we don't need to worry about using importNode for MathML
 
	/** @type {Element | DocumentFragment} */
	var node;
 
	return () => {
		if (hydrating) {
			return push_template_node(is_fragment ? hydrate_nodes : hydrate_nodes[0]);
		}
 
		if (!node) {
			var math = /** @type {Element} */ (fn());
 
			if ((flags & TEMPLATE_FRAGMENT) === 0) {
				node = /** @type {Element} */ (math.firstChild);
			} else {
				node = document.createDocumentFragment();
				while (math.firstChild) {
					node.appendChild(math.firstChild);
				}
			}
		}
 
		var clone = clone_node(node, true);
 
		push_template_node(
			is_fragment
				? /** @type {import('#client').TemplateNode[]} */ ([...clone.childNodes])
				: /** @type {import('#client').TemplateNode} */ (clone)
		);
 
		return clone;
	};
}
 
/**
 * Creating a document fragment from HTML that contains script tags will not execute
 * the scripts. We need to replace the script tags with new ones so that they are executed.
 * @param {Element | DocumentFragment} node
 */
function run_scripts(node) {
	// scripts were SSR'd, in which case they will run
	if (hydrating) return;
 
	const scripts =
		/** @type {HTMLElement} */ (node).tagName === 'SCRIPT'
			? [/** @type {HTMLScriptElement} */ (node)]
			: node.querySelectorAll('script');
	for (const script of scripts) {
		var clone = document.createElement('script');
		for (var attribute of script.attributes) {
			clone.setAttribute(attribute.name, attribute.value);
		}
 
		clone.textContent = script.textContent;
		// If node === script tag, replaceWith will do nothing because there's no parent yet,
		// waiting until that's the case using an effect solves this.
		// Don't do it in other circumstances or we could accidentally execute scripts
		// in an adjacent @html tag that was instantiated in the meantime.
		if (script === node) {
			effect(() => script.replaceWith(clone));
		} else {
			script.replaceWith(clone);
		}
	}
}
 
/**
 * @param {Text | Comment | Element} anchor
 */
/*#__NO_SIDE_EFFECTS__*/
export function text(anchor) {
	if (!hydrating) return push_template_node(empty());
 
	var node = hydrate_nodes[0];
 
	if (!node) {
		// if an {expression} is empty during SSR, `hydrate_nodes` will be empty.
		// we need to insert an empty text node
		anchor.before((node = empty()));
	}
 
	return push_template_node(node);
}
 
export function comment() {
	// we're not delegating to `template` here for performance reasons
	if (hydrating) {
		return push_template_node(hydrate_nodes);
	}
	var frag = document.createDocumentFragment();
	var anchor = empty();
	frag.append(anchor);
	push_template_node([anchor]);
 
	return frag;
}
 
/**
 * Assign the created (or in hydration mode, traversed) dom elements to the current block
 * and insert the elements into the dom (in client mode).
 * @param {Text | Comment | Element} anchor
 * @param {import('#client').Dom} dom
 */
export function append(anchor, dom) {
	if (!hydrating) {
		anchor.before(/** @type {Node} */ (dom));
	}
}