Skip to content

Commit 201a71d

Browse files
authored
fix insert function (#6445)
1 parent b662c7f commit 201a71d

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

src/runtime/internal/dom.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,54 +34,54 @@ function upper_bound(low: number, high: number, key: (index: number) => number,
3434
function init_hydrate(target: NodeEx) {
3535
if (target.hydrate_init) return;
3636
target.hydrate_init = true;
37-
37+
3838
type NodeEx2 = NodeEx & {claim_order: number};
39-
39+
4040
// We know that all children have claim_order values since the unclaimed have been detached
4141
const children = target.childNodes as NodeListOf<NodeEx2>;
42-
43-
/*
42+
43+
/*
4444
* Reorder claimed children optimally.
4545
* We can reorder claimed children optimally by finding the longest subsequence of
4646
* nodes that are already claimed in order and only moving the rest. The longest
4747
* subsequence subsequence of nodes that are claimed in order can be found by
4848
* computing the longest increasing subsequence of .claim_order values.
49-
*
49+
*
5050
* This algorithm is optimal in generating the least amount of reorder operations
5151
* possible.
52-
*
52+
*
5353
* Proof:
5454
* We know that, given a set of reordering operations, the nodes that do not move
5555
* always form an increasing subsequence, since they do not move among each other
5656
* meaning that they must be already ordered among each other. Thus, the maximal
5757
* set of nodes that do not move form a longest increasing subsequence.
5858
*/
59-
59+
6060
// Compute longest increasing subsequence
6161
// m: subsequence length j => index k of smallest value that ends an increasing subsequence of length j
6262
const m = new Int32Array(children.length + 1);
6363
// Predecessor indices + 1
6464
const p = new Int32Array(children.length);
65-
65+
6666
m[0] = -1;
6767
let longest = 0;
6868
for (let i = 0; i < children.length; i++) {
6969
const current = children[i].claim_order;
7070
// Find the largest subsequence length such that it ends in a value less than our current value
71-
71+
7272
// upper_bound returns first greater value, so we subtract one
7373
const seqLen = upper_bound(1, longest + 1, idx => children[m[idx]].claim_order, current) - 1;
74-
74+
7575
p[i] = m[seqLen] + 1;
76-
76+
7777
const newLen = seqLen + 1;
78-
78+
7979
// We can guarantee that current is the smallest value. Otherwise, we would have generated a longer sequence.
8080
m[newLen] = i;
81-
81+
8282
longest = Math.max(newLen, longest);
8383
}
84-
84+
8585
// The longest increasing subsequence of nodes (initially reversed)
8686
const lis: NodeEx2[] = [];
8787
// The rest of the nodes, nodes that will be moved
@@ -98,10 +98,10 @@ function init_hydrate(target: NodeEx) {
9898
toMove.push(children[last]);
9999
}
100100
lis.reverse();
101-
101+
102102
// We sort the nodes being moved to guarantee that their insertion order matches the claim order
103103
toMove.sort((a, b) => a.claim_order - b.claim_order);
104-
104+
105105
// Finally, we move the nodes
106106
for (let i = 0, j = 0; i < toMove.length; i++) {
107107
while (j < lis.length && toMove[i].claim_order >= lis[j].claim_order) {
@@ -115,7 +115,7 @@ function init_hydrate(target: NodeEx) {
115115
export function append(target: NodeEx, node: NodeEx) {
116116
if (is_hydrating) {
117117
init_hydrate(target);
118-
118+
119119
if ((target.actual_end_child === undefined) || ((target.actual_end_child !== null) && (target.actual_end_child.parentElement !== target))) {
120120
target.actual_end_child = target.firstChild;
121121
}
@@ -132,7 +132,7 @@ export function append(target: NodeEx, node: NodeEx) {
132132
export function insert(target: NodeEx, node: NodeEx, anchor?: NodeEx) {
133133
if (is_hydrating && !anchor) {
134134
append(target, node);
135-
} else if (node.parentNode !== target || (anchor && node.nextSibling !== anchor)) {
135+
} else if (node.parentNode !== target || node.nextSibling != anchor) {
136136
target.insertBefore(node, anchor || null);
137137
}
138138
}
@@ -309,12 +309,12 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
309309
if (nodes.claim_info === undefined) {
310310
nodes.claim_info = {last_index: 0, total_claimed: 0};
311311
}
312-
312+
313313
const resultNode = (() => {
314314
// We first try to find an element after the previous one
315315
for (let i = nodes.claim_info.last_index; i < nodes.length; i++) {
316316
const node = nodes[i];
317-
317+
318318
if (predicate(node)) {
319319
processNode(node);
320320

@@ -325,13 +325,13 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
325325
return node;
326326
}
327327
}
328-
329-
328+
329+
330330
// Otherwise, we try to find one before
331331
// We iterate in reverse so that we don't go too far back
332332
for (let i = nodes.claim_info.last_index - 1; i >= 0; i--) {
333333
const node = nodes[i];
334-
334+
335335
if (predicate(node)) {
336336
processNode(node);
337337

@@ -345,11 +345,11 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
345345
return node;
346346
}
347347
}
348-
348+
349349
// If we can't find any matching node, we create a new one
350350
return createNode();
351351
})();
352-
352+
353353
resultNode.claim_order = nodes.claim_info.total_claimed;
354354
nodes.claim_info.total_claimed += 1;
355355
return resultNode;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default {
2+
props: {
3+
titles: [{ name: 'a' }, { name: 'b' }, { name: 'c' }]
4+
},
5+
6+
html: '<div class="container"><p>a</p><p>b</p><p>c</p></div>',
7+
8+
test({ assert, component, target }) {
9+
component.titles = [{ name: 'b' }, { name: 'c' }, { name: 'a' }];
10+
11+
assert.htmlEqual(target.innerHTML, '<div class="container"><p>b</p><p>c</p><p>a</p></div>');
12+
}
13+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
export let titles;
3+
</script>
4+
<div class="container">
5+
{#each titles as title (title.name)}
6+
<p>{title.name}</p>
7+
{/each}
8+
</div>

0 commit comments

Comments
 (0)