Skip to content

Commit

Permalink
fix insertion order of if blocks and their anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 6, 2017
1 parent c0b7156 commit 6dfb568
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
37 changes: 19 additions & 18 deletions src/generators/dom/visitors/IfBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@ export default function visitIfBlock ( generator, block, state, node ) {
const anchor = node.needsAnchor ? block.getUniqueName( `${name}_anchor` ) : ( node.next && node.next._state.name ) || 'null';
const params = block.params.join( ', ' );

if ( node.needsAnchor ) {
block.addElement( anchor, `${generator.helper( 'createComment' )}()`, state.parentNode, true );
} else if ( node.next ) {
node.next.usedAsAnchor = true;
}

const branches = getBranches( generator, block, state, node, generator.getUniqueName( `create_if_block` ) );
const branches = getBranches( generator, block, state, node );

const hasElse = isElseBranch( branches[ branches.length - 1 ] );
const if_name = hasElse ? '' : `if ( ${name} ) `;
Expand All @@ -78,6 +72,12 @@ export default function visitIfBlock ( generator, block, state, node ) {
simple( generator, block, state, node, branches[0], dynamic, vars );
}

if ( node.needsAnchor ) {
block.addElement( anchor, `${generator.helper( 'createComment' )}()`, state.parentNode, true );
} else if ( node.next ) {
node.next.usedAsAnchor = true;
}

block.builders.destroy.addLine(
`${if_name}${name}.destroy( ${state.parentNode ? 'false' : 'detach'} );`
);
Expand All @@ -88,11 +88,11 @@ function simple ( generator, block, state, node, branch, dynamic, { name, anchor
var ${name} = (${branch.condition}) && ${branch.block}( ${params}, ${block.component} );
` );

const isToplevel = !state.parentNode;
const isTopLevel = !state.parentNode;
const mountOrIntro = branch.hasIntroMethod ? 'intro' : 'mount';

if ( isToplevel ) {
block.builders.mount.addLine( `if ( ${name} ) ${name}.${mountOrIntro}( ${block.target}, null );` );
if ( isTopLevel ) {
block.builders.mount.addLine( `if ( ${name} ) ${name}.${mountOrIntro}( ${block.target}, anchor );` );
} else {
block.builders.create.addLine( `if ( ${name} ) ${name}.${mountOrIntro}( ${state.parentNode}, null );` );
}
Expand Down Expand Up @@ -169,11 +169,11 @@ function compound ( generator, block, state, node, branches, dynamic, { name, an
var ${name} = ${current_block_and}${current_block}( ${params}, ${block.component} );
` );

const isToplevel = !state.parentNode;
const isTopLevel = !state.parentNode;
const mountOrIntro = branches[0].hasIntroMethod ? 'intro' : 'mount';

if ( isToplevel ) {
block.builders.mount.addLine( `${if_name}${name}.${mountOrIntro}( ${block.target}, null );` );
if ( isTopLevel ) {
block.builders.mount.addLine( `${if_name}${name}.${mountOrIntro}( ${block.target}, anchor );` );
} else {
block.builders.create.addLine( `${if_name}${name}.${mountOrIntro}( ${state.parentNode}, null );` );
}
Expand Down Expand Up @@ -244,13 +244,14 @@ function compoundWithOutros ( generator, block, state, node, branches, dynamic,
` );
}

const isToplevel = !state.parentNode;
const isTopLevel = !state.parentNode;
const mountOrIntro = branches[0].hasIntroMethod ? 'intro' : 'mount';
const initialTarget = isToplevel ? block.target : state.parentNode;

( isToplevel ? block.builders.mount : block.builders.create ).addBlock(
`${if_current_block_index}${if_blocks}[ ${current_block_index} ].${mountOrIntro}( ${initialTarget}, null );`
);
if ( isTopLevel ) {
block.builders.mount.addLine( `${if_current_block_index}${if_blocks}[ ${current_block_index} ].${mountOrIntro}( ${block.target}, anchor );` );
} else {
block.builders.create.addLine( `${if_current_block_index}${if_blocks}[ ${current_block_index} ].${mountOrIntro}( ${state.parentNode}, null );` );
}

const parentNode = state.parentNode || `${anchor}.parentNode`;

Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/if-block-no-update/expected.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { appendNode, assign, createComment, createElement, createText, detachNode, dispatchObservers, insertNode, proto } from "svelte/shared.js";

function create_main_fragment ( state, component ) {
var if_block_anchor = createComment();

function get_block ( state ) {
if ( state.foo ) return create_if_block;
return create_if_block_1;
Expand All @@ -11,10 +9,12 @@ function create_main_fragment ( state, component ) {
var current_block = get_block( state );
var if_block = current_block( state, component );

var if_block_anchor = createComment();

return {
mount: function ( target, anchor ) {
if_block.mount( target, anchor );
insertNode( if_block_anchor, target, anchor );
if_block.mount( target, null );
},

update: function ( changed, state ) {
Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/if-block-simple/expected.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { appendNode, assign, createComment, createElement, createText, detachNode, dispatchObservers, insertNode, proto } from "svelte/shared.js";

function create_main_fragment ( state, component ) {
var if_block_anchor = createComment();

var if_block = (state.foo) && create_if_block( state, component );

var if_block_anchor = createComment();

return {
mount: function ( target, anchor ) {
if ( if_block ) if_block.mount( target, anchor );
insertNode( if_block_anchor, target, anchor );
if ( if_block ) if_block.mount( target, null );
},

update: function ( changed, state ) {
Expand Down
5 changes: 3 additions & 2 deletions test/js/samples/use-elements-as-anchors/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ function create_main_fragment ( state, component ) {
var text_7 = createText( "\n\n\t" );
appendNode( text_7, div );
var text_8 = createText( "\n\n" );
var if_block_4_anchor = createComment();

var if_block_4 = (state.e) && create_if_block_4( state, component );

var if_block_4_anchor = createComment();

return {
mount: function ( target, anchor ) {
insertNode( div, target, anchor );
insertNode( text_8, target, anchor );
if ( if_block_4 ) if_block_4.mount( target, anchor );
insertNode( if_block_4_anchor, target, anchor );
if ( if_block_4 ) if_block_4.mount( target, null );
},

update: function ( changed, state ) {
Expand Down

0 comments on commit 6dfb568

Please sign in to comment.