Skip to content

Commit

Permalink
Merge pull request #31 from Swatinem/insert-before-anchor
Browse files Browse the repository at this point in the history
correctly insert non-element children before an anchor
  • Loading branch information
Rich-Harris authored Nov 29, 2016
2 parents ad4726c + b63a54b commit 36a4bef
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
7 changes: 7 additions & 0 deletions compiler/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export default function generate ( parsed, source, options ) {
const renderers = [];

const generator = {
appendToTarget ( name ) {
if ( generator.current.useAnchor && generator.current.target === 'target' ) {
return `anchor.parentNode.insertBefore( ${name}, anchor )`;
}
return `${generator.current.target}.appendChild( ${name} )`;
},

addRenderer ( fragment ) {
if ( fragment.autofocus ) {
fragment.initStatements.push( `${fragment.autofocus}.focus();` );
Expand Down
2 changes: 1 addition & 1 deletion compiler/generate/visitors/EachBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {

generator.current.initStatements.push( deindent`
var ${name}_anchor = document.createComment( ${JSON.stringify( `#each ${generator.source.slice( node.expression.start, node.expression.end )}` )} );
${generator.current.target}.appendChild( ${name}_anchor );
${generator.appendToTarget( `${name}_anchor` )};
var ${name}_value = ${snippet};
var ${name}_fragment = document.createDocumentFragment();
Expand Down
11 changes: 2 additions & 9 deletions compiler/generate/visitors/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,7 @@ export default {

if ( isComponent ) return;

if ( generator.current.useAnchor && generator.current.target === 'target' ) {
generator.current.initStatements.push( deindent`
anchor.parentNode.insertBefore( ${name}, anchor );
` );
} else {
generator.current.initStatements.push( deindent`
${generator.current.target}.appendChild( ${name} );
` );
}
generator.current.initStatements.push(
generator.appendToTarget( name ) );
}
};
2 changes: 1 addition & 1 deletion compiler/generate/visitors/IfBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {

generator.current.initStatements.push( deindent`
var ${name}_anchor = document.createComment( ${JSON.stringify( `#if ${generator.source.slice( node.expression.start, node.expression.end )}` )} );
${generator.current.target}.appendChild( ${name}_anchor );
${generator.appendToTarget( `${name}_anchor` )};
` );

if ( node.else ) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/generate/visitors/MustacheTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {

generator.current.initStatements.push( deindent`
var ${name} = document.createTextNode( ${snippet} );
${generator.current.target}.appendChild( ${name} );
${generator.appendToTarget( name )};
` );

generator.addSourcemapLocations( node.expression );
Expand Down
2 changes: 1 addition & 1 deletion compiler/generate/visitors/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {

generator.current.initStatements.push( deindent`
var ${name} = document.createTextNode( ${JSON.stringify( node.data )} );
${generator.current.target}.appendChild( ${name} );
${generator.appendToTarget( name )};
` );

generator.current.teardownStatements.push( deindent`
Expand Down
23 changes: 23 additions & 0 deletions test/compiler/if-block-elseif-text/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default {
data: {
x: 11
},

html: `
before-if-after
`,

test ( assert, component, target ) {
component.set({ x: 4 });
assert.htmlEqual( target.innerHTML, `
before-elseif-after
` );

component.set({ x: 6 });
assert.htmlEqual( target.innerHTML, `
before-else-after
` );

component.teardown();
}
};
1 change: 1 addition & 0 deletions test/compiler/if-block-elseif-text/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
before-{{#if x > 10}}if{{elseif x < 5}}elseif{{else}}else{{/if}}-after

0 comments on commit 36a4bef

Please sign in to comment.