Move EachBlock and RawMustache detach code into shared

pull/248/head
Arpad Borsos 8 years ago
parent 5d74fb92c3
commit 01cc3564be
No known key found for this signature in database
GPG Key ID: 908EDF65263368B4

@ -132,9 +132,7 @@ export default {
}
}
for ( var ${i} = ${name}_value.length; ${i} < ${iterations}.length; ${i} += 1 ) {
${iterations}[${i}].teardown( true );
}
teardownEach( ${iterations}, true, ${name}_value.length );
${iterations}.length = ${listName}.length;
` );
@ -153,11 +151,9 @@ export default {
` );
}
generator.current.builders.teardown.addBlock( deindent`
for ( var ${i} = 0; ${i} < ${iterations}.length; ${i} += 1 ) {
${iterations}[${i}].teardown( ${isToplevel ? 'detach' : 'false'} );
}
` );
generator.uses.teardownEach = true;
generator.current.builders.teardown.addBlock(
`teardownEach( ${iterations}, ${isToplevel ? 'detach' : 'false'} );` );
if ( node.else ) {
generator.current.builders.teardown.addBlock( deindent`

@ -1,5 +1,3 @@
import deindent from '../../../utils/deindent.js';
export default {
enter ( generator, node ) {
const name = generator.current.getUniqueName( 'raw' );
@ -20,11 +18,8 @@ export default {
const isToplevel = generator.current.localElementDepth === 0;
const mountStatement = `${before}.insertAdjacentHTML( 'afterend', ${snippet} );`;
const detachStatement = deindent`
while ( ${before}.nextSibling && ${before}.nextSibling !== ${after} ) {
${before}.parentNode.removeChild( ${before}.nextSibling );
}
`;
generator.uses.detachBetween = true;
const detachStatement = `detachBetween( ${before}, ${after} );`;
if ( isToplevel ) {
generator.current.builders.mount.addLine( mountStatement );

@ -10,6 +10,18 @@ export function detachNode ( node ) {
node.parentNode.removeChild( node );
}
export function detachBetween ( before, after ) {
while ( before.nextSibling && before.nextSibling !== after ) {
before.parentNode.removeChild( before.nextSibling );
}
}
export function teardownEach ( iterations, detach, start ) {
for ( var i = ( start || 0 ); i < iterations.length; i += 1 ) {
iterations[i].teardown( detach );
}
}
export function createElement ( name ) {
return document.createElement( name );
}

Loading…
Cancel
Save