Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix insertion order of if blocks and their anchors #571

Merged
merged 2 commits into from
May 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions test/runtime/samples/component-if-placement/Component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#if true}}
<span>Component</span>
{{/if}}
20 changes: 20 additions & 0 deletions test/runtime/samples/component-if-placement/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default {
data: {
flag: true
},

html: `
<span>Before</span>
<span>Component</span>
<span>After</span>
`,

test ( assert, component, target ) {
component.set( { flag: false } );
assert.htmlEqual( target.innerHTML, `
<span>Before</span>
<span>Component</span>
<span>After</span>
`);
}
};
14 changes: 14 additions & 0 deletions test/runtime/samples/component-if-placement/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<span>Before</span>
{{#if flag}}
<Component/>
{{else}}
<Component/>
{{/if}}
<span>After</span>

<script>
import Component from './Component.html';
export default {
components: { Component }
};
</script>