Skip to content

Commit 196d7e4

Browse files
authored
chore: simplify add_locations (#11757)
* chore: simplify add_locations * tiny tweak
1 parent 3dfa343 commit 196d7e4

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

packages/svelte/src/internal/client/dev/elements.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,8 @@ export function add_locations(fn, filename, locations) {
1212
return (/** @type {any[]} */ ...args) => {
1313
const dom = fn(...args);
1414

15-
const nodes = hydrating
16-
? is_array(dom)
17-
? dom
18-
: [dom]
19-
: dom.nodeType === 11
20-
? Array.from(dom.childNodes)
21-
: [dom];
22-
23-
assign_locations(nodes, filename, locations);
15+
var node = hydrating && is_array(dom) ? dom[0] : dom.nodeType === 11 ? dom.firstChild : dom;
16+
assign_locations(node, filename, locations);
2417

2518
return dom;
2619
};
@@ -38,34 +31,30 @@ function assign_location(element, filename, location) {
3831
};
3932

4033
if (location[2]) {
41-
assign_locations(
42-
/** @type {import('#client').TemplateNode[]} */ (Array.from(element.childNodes)),
43-
filename,
44-
location[2]
45-
);
34+
assign_locations(element.firstChild, filename, location[2]);
4635
}
4736
}
4837

4938
/**
50-
* @param {import('#client').TemplateNode[]} nodes
39+
* @param {Node | null} node
5140
* @param {string} filename
5241
* @param {import('../../../compiler/phases/3-transform/client/types.js').SourceLocation[]} locations
5342
*/
54-
function assign_locations(nodes, filename, locations) {
55-
var j = 0;
43+
function assign_locations(node, filename, locations) {
44+
var i = 0;
5645
var depth = 0;
5746

58-
for (var i = 0; i < nodes.length; i += 1) {
59-
var node = nodes[i];
60-
47+
while (node && i < locations.length) {
6148
if (hydrating && node.nodeType === 8) {
6249
var comment = /** @type {Comment} */ (node);
6350
if (comment.data === HYDRATION_START) depth += 1;
64-
if (comment.data.startsWith(HYDRATION_END)) depth -= 1;
51+
else if (comment.data[0] === HYDRATION_END) depth -= 1;
6552
}
6653

6754
if (depth === 0 && node.nodeType === 1) {
68-
assign_location(/** @type {Element} */ (node), filename, locations[j++]);
55+
assign_location(/** @type {Element} */ (node), filename, locations[i++]);
6956
}
57+
58+
node = node.nextSibling;
7059
}
7160
}

0 commit comments

Comments
 (0)