All files / src/internal/client/dev elements.js

92.94% Statements 79/85
94.73% Branches 18/19
100% Functions 4/4
92.77% Lines 77/83

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 842x 2x 2x 2x 2x 2x 2x 66x 66x 66x 66x 66x 96x 96x 66x 66x 66x 2x 2x 2x 2x 2x 2x 2x 2x 73x 177x 177x 177x 177x 177x 111x 177x 177x 177x 177x 177x 73x 73x 2x 2x 2x 2x 2x 2x 166x 166x 166x 166x 166x 166x 166x             166x 2x 2x 2x 2x 2x 2x 177x 177x 177x 177x 177x 328x 328x 328x 46x 46x 46x 46x 328x 328x 166x 166x 328x 177x  
import { HYDRATION_END, HYDRATION_START } from '../../../constants.js';
import { hydrate_end, hydrate_start, hydrating } from '../dom/hydration.js';
 
/**
 * @param {import('#client').TemplateNode} from
 * @param {import('#client').TemplateNode} to
 */
function find_nodes_between(from, to) {
	var node = from;
	var nodes = [node];
 
	while (node !== to) {
		nodes.push((node = /** @type {import('#client').TemplateNode} */ (node.nextSibling)));
	}
 
	return nodes;
}
 
/**
 * @param {any} fn
 * @param {string} filename
 * @param {import('../../../compiler/phases/3-transform/client/types.js').SourceLocation[]} locations
 * @returns {any}
 */
export function add_locations(fn, filename, locations) {
	return (/** @type {any[]} */ ...args) => {
		const dom = fn(...args);
 
		const nodes = hydrating
			? find_nodes_between(hydrate_start, hydrate_end)
			: dom.nodeType === 11
				? Array.from(dom.childNodes)
				: [dom];
 
		assign_locations(nodes, filename, locations);
 
		return dom;
	};
}
 
/**
 * @param {Element} element
 * @param {string} filename
 * @param {import('../../../compiler/phases/3-transform/client/types.js').SourceLocation} location
 */
function assign_location(element, filename, location) {
	// @ts-expect-error
	element.__svelte_meta = {
		loc: { file: filename, line: location[0], column: location[1] }
	};
 
	if (location[2]) {
		assign_locations(
			/** @type {import('#client').TemplateNode[]} */ (Array.from(element.childNodes)),
			filename,
			location[2]
		);
	}
}
 
/**
 * @param {import('#client').TemplateNode[]} nodes
 * @param {string} filename
 * @param {import('../../../compiler/phases/3-transform/client/types.js').SourceLocation[]} locations
 */
function assign_locations(nodes, filename, locations) {
	var j = 0;
	var depth = 0;
 
	for (var i = 0; i < nodes.length; i += 1) {
		var node = nodes[i];
 
		if (hydrating && node.nodeType === 8) {
			var comment = /** @type {Comment} */ (node);
			if (comment.data === HYDRATION_START) depth += 1;
			if (comment.data.startsWith(HYDRATION_END)) depth -= 1;
		}
 
		if (depth === 0 && node.nodeType === 1 && locations[j]) {
			assign_location(/** @type {Element} */ (node), filename, locations[j++]);
		}
	}
}