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

96.77% Statements 60/62
93.75% Branches 15/16
100% Functions 3/3
96.66% Lines 58/60

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 612x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 79x 186x 186x 186x 186x 186x 186x 79x 79x 2x 2x 2x 2x 2x 2x 178x 178x 178x 178x 178x 178x 178x     178x 2x 2x 2x 2x 2x 2x 186x 186x 186x 186x 186x 221x 8x 8x 4x 8x 221x 221x 178x 178x 221x 221x 221x 186x  
/** @import { SourceLocation } from '../../../compiler/phases/3-transform/client/types.js' */
import { HYDRATION_END, HYDRATION_START } from '../../../constants.js';
import { hydrating } from '../dom/hydration.js';
 
/**
 * @param {any} fn
 * @param {string} filename
 * @param {SourceLocation[]} locations
 * @returns {any}
 */
export function add_locations(fn, filename, locations) {
	return (/** @type {any[]} */ ...args) => {
		const dom = fn(...args);
 
		var node = hydrating ? dom : dom.nodeType === 11 ? dom.firstChild : dom;
		assign_locations(node, filename, locations);
 
		return dom;
	};
}
 
/**
 * @param {Element} element
 * @param {string} filename
 * @param {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(element.firstChild, filename, location[2]);
	}
}
 
/**
 * @param {Node | null} node
 * @param {string} filename
 * @param {SourceLocation[]} locations
 */
function assign_locations(node, filename, locations) {
	var i = 0;
	var depth = 0;
 
	while (node && i < locations.length) {
		if (hydrating && node.nodeType === 8) {
			var comment = /** @type {Comment} */ (node);
			if (comment.data === HYDRATION_START) depth += 1;
			else if (comment.data[0] === HYDRATION_END) depth -= 1;
		}
 
		if (depth === 0 && node.nodeType === 1) {
			assign_location(/** @type {Element} */ (node), filename, locations[i++]);
		}
 
		node = node.nextSibling;
	}
}