Repository: sveltejs/svelte (JavaScript)
Date: 2017-06-01 02:07:31+00:00
Task type: Bug Fix

Issue:
DOM elements being prepended rather than appended with each block
[Repro](https://svelte.technology/repl?version=1.21.0&gist=a2f4545bbdb3ca5f6b562d27ad278706) — haven't looked any further into the cause yet.


Issue discussion:
I think [this](https://svelte.technology/repl?version=1.21.0&gist=d2a2dd0e01c3d3f62199413bcc6a2b51) example much better. 
[Simpler still](https://svelte.technology/repl?version=1.21.0&gist=a3436f714e161162e9c2a480a286c363) — remove the `<div>` and it goes even more haywire

Solution patch:
diff --git a/src/generators/dom/visitors/EachBlock.ts b/src/generators/dom/visitors/EachBlock.ts
--- a/src/generators/dom/visitors/EachBlock.ts
+++ b/src/generators/dom/visitors/EachBlock.ts
@@ -139,7 +139,7 @@ function keyed ( generator: DomGenerator, block: Block, state: State, node: Node
 		block.builders.mount.addBlock( deindent`
 			var ${iteration} = ${head};
 			while ( ${iteration} ) {
-				${iteration}.${mountOrIntro}( ${block.target}, null );
+				${iteration}.${mountOrIntro}( ${block.target}, anchor );
 				${iteration} = ${iteration}.next;
 			}
 		` );
@@ -284,7 +284,7 @@ function unkeyed ( generator: DomGenerator, block: Block, state: State, node: No
 	if ( !state.parentNode ) {
 		block.builders.mount.addBlock( deindent`
 			for ( var ${i} = 0; ${i} < ${iterations}.length; ${i} += 1 ) {
-				${iterations}[${i}].${mountOrIntro}( ${block.target}, null );
+				${iterations}[${i}].${mountOrIntro}( ${block.target}, anchor );
 			}
 		` );
 	}


Test patch:
diff --git a/test/js/samples/each-block-changed-check/_actual-bundle.js b/test/js/samples/each-block-changed-check/_actual-bundle.js
--- a/test/js/samples/each-block-changed-check/_actual-bundle.js
+++ b/test/js/samples/each-block-changed-check/_actual-bundle.js
@@ -153,7 +153,7 @@ function create_main_fragment ( state, component ) {
 	return {
 		mount: function ( target, anchor ) {
 			for ( var i = 0; i < each_block_iterations.length; i += 1 ) {
-				each_block_iterations[i].mount( target, null );
+				each_block_iterations[i].mount( target, anchor );
 			}
 
 			insertNode( text, target, anchor );
diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js
--- a/test/js/samples/each-block-changed-check/expected-bundle.js
+++ b/test/js/samples/each-block-changed-check/expected-bundle.js
@@ -153,7 +153,7 @@ function create_main_fragment ( state, component ) {
 	return {
 		mount: function ( target, anchor ) {
 			for ( var i = 0; i < each_block_iterations.length; i += 1 ) {
-				each_block_iterations[i].mount( target, null );
+				each_block_iterations[i].mount( target, anchor );
 			}
 
 			insertNode( text, target, anchor );
diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js
--- a/test/js/samples/each-block-changed-check/expected.js
+++ b/test/js/samples/each-block-changed-check/expected.js
@@ -19,7 +19,7 @@ function create_main_fragment ( state, component ) {
 	return {
 		mount: function ( target, anchor ) {
 			for ( var i = 0; i < each_block_iterations.length; i += 1 ) {
-				each_block_iterations[i].mount( target, null );
+				each_block_iterations[i].mount( target, anchor );
 			}
 
 			insertNode( text, target, anchor );
@@ -160,4 +160,4 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio
 	this._torndown = true;
 };
 
-export default SvelteComponent;
\ No newline at end of file
+export default SvelteComponent;