Skip to content

Commit

Permalink
chore: simplify add_locations (#11757)
Browse files Browse the repository at this point in the history
* chore: simplify add_locations

* tiny tweak
  • Loading branch information
Rich-Harris committed May 23, 2024
1 parent 3dfa343 commit 196d7e4
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions packages/svelte/src/internal/client/dev/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ export function add_locations(fn, filename, locations) {
return (/** @type {any[]} */ ...args) => {
const dom = fn(...args);

const nodes = hydrating
? is_array(dom)
? dom
: [dom]
: dom.nodeType === 11
? Array.from(dom.childNodes)
: [dom];

assign_locations(nodes, filename, locations);
var node = hydrating && is_array(dom) ? dom[0] : dom.nodeType === 11 ? dom.firstChild : dom;
assign_locations(node, filename, locations);

return dom;
};
Expand All @@ -38,34 +31,30 @@ function assign_location(element, filename, location) {
};

if (location[2]) {
assign_locations(
/** @type {import('#client').TemplateNode[]} */ (Array.from(element.childNodes)),
filename,
location[2]
);
assign_locations(element.firstChild, filename, location[2]);
}
}

/**
* @param {import('#client').TemplateNode[]} nodes
* @param {Node | null} node
* @param {string} filename
* @param {import('../../../compiler/phases/3-transform/client/types.js').SourceLocation[]} locations
*/
function assign_locations(nodes, filename, locations) {
var j = 0;
function assign_locations(node, filename, locations) {
var i = 0;
var depth = 0;

for (var i = 0; i < nodes.length; i += 1) {
var node = nodes[i];

while (node && i < locations.length) {
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;
else if (comment.data[0] === HYDRATION_END) depth -= 1;
}

if (depth === 0 && node.nodeType === 1) {
assign_location(/** @type {Element} */ (node), filename, locations[j++]);
assign_location(/** @type {Element} */ (node), filename, locations[i++]);
}

node = node.nextSibling;
}
}

0 comments on commit 196d7e4

Please sign in to comment.