Skip to content

Commit 58dba84

Browse files
authored
Merge pull request #1309 from sveltejs/gh-1306
ensure correct order of DOM insertions with neighbouring keyed each blocks
2 parents b4ade9d + dd24744 commit 58dba84

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/generators/nodes/EachBlock.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export default class EachBlock extends Node {
298298
block.builders.update.addBlock(deindent`
299299
var ${each_block_value} = ${snippet};
300300
301-
${blocks} = @updateKeyedEach(${blocks}, #component, changed, "${this.key}", ${dynamic ? '1' : '0'}, ${each_block_value}, ${lookup}, ${updateMountNode}, ${String(this.block.hasOutroMethod)}, ${create_each_block}, "${mountOrIntro}", function(#i) {
301+
${blocks} = @updateKeyedEach(${blocks}, #component, changed, "${this.key}", ${dynamic ? '1' : '0'}, ${each_block_value}, ${lookup}, ${updateMountNode}, ${String(this.block.hasOutroMethod)}, ${create_each_block}, "${mountOrIntro}", ${anchor}, function(#i) {
302302
return @assign(@assign({}, state), {
303303
${this.contextProps.join(',\n')}
304304
});

src/shared/keyed-each.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function outroAndDestroyBlock(block, lookup) {
1010
});
1111
}
1212

13-
export function updateKeyedEach(old_blocks, component, changed, key_prop, dynamic, list, lookup, node, has_outro, create_each_block, intro_method, get_context) {
13+
export function updateKeyedEach(old_blocks, component, changed, key_prop, dynamic, list, lookup, node, has_outro, create_each_block, intro_method, next, get_context) {
1414
var o = old_blocks.length;
1515
var n = list.length;
1616

@@ -39,16 +39,15 @@ export function updateKeyedEach(old_blocks, component, changed, key_prop, dynami
3939
if (key in old_indexes) deltas[key] = Math.abs(i - old_indexes[key]);
4040
}
4141

42-
var next = null;
43-
4442
var will_move = {};
4543
var did_move = {};
4644

4745
var destroy = has_outro ? outroAndDestroyBlock : destroyBlock;
4846

4947
function insert(block) {
50-
block[intro_method](node, next && next.first);
51-
next = lookup[block.key] = block;
48+
block[intro_method](node, next);
49+
lookup[block.key] = block;
50+
next = block.first;
5251
n--;
5352
}
5453

@@ -60,7 +59,7 @@ export function updateKeyedEach(old_blocks, component, changed, key_prop, dynami
6059

6160
if (new_block === old_block) {
6261
// do nothing
63-
next = new_block;
62+
next = new_block.first;
6463
o--;
6564
n--;
6665
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default {
2+
data: {
3+
ones: [{ text: '1' }],
4+
twos: [{ text: '2' }],
5+
},
6+
7+
html: `
8+
<div>1</div>
9+
<div>2</div>
10+
`,
11+
12+
test(assert, component, target) {
13+
component.set({
14+
ones: [{ text: '11' }]
15+
});
16+
17+
assert.htmlEqual(target.innerHTML, `
18+
<div>11</div>
19+
<div>2</div>
20+
`);
21+
},
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{#each ones as one @text}}
2+
<div>{{one.text}}</div>
3+
{{/each}}
4+
5+
{{#each twos as two @text}}
6+
<div>{{two.text}}</div>
7+
{{/each}}

0 commit comments

Comments
 (0)