0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

correctly insert non-element children before an anchor

This commit is contained in:
Arpad Borsos 2016-11-29 13:06:09 +01:00
parent ad4726ce76
commit b63a54b502
No known key found for this signature in database
GPG Key ID: 908EDF65263368B4
8 changed files with 37 additions and 13 deletions

View File

@ -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();` );

View File

@ -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();

View File

@ -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 ) );
}
};

View File

@ -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 ) {

View File

@ -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 );

View File

@ -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`

View File

@ -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();
}
};

View File

@ -0,0 +1 @@
before-{{#if x > 10}}if{{elseif x < 5}}elseif{{else}}else{{/if}}-after