From be8a5648453f5cecbf986ee4d7c649536aa8b630 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 30 Nov 2016 17:11:15 -0500 Subject: [PATCH] remove text nodes when removing iterations of each block (#62) --- compiler/generate/visitors/MustacheTag.js | 6 ++++++ compiler/generate/visitors/Text.js | 2 +- test/compiler/each-block-text-node/_config.js | 14 ++++++++++++++ test/compiler/each-block-text-node/main.html | 3 +++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/compiler/each-block-text-node/_config.js create mode 100644 test/compiler/each-block-text-node/main.html diff --git a/compiler/generate/visitors/MustacheTag.js b/compiler/generate/visitors/MustacheTag.js index 7dc8244eee..cc0c9dbf77 100644 --- a/compiler/generate/visitors/MustacheTag.js +++ b/compiler/generate/visitors/MustacheTag.js @@ -16,5 +16,11 @@ export default { generator.current.updateStatements.push( deindent` ${name}.data = ${snippet}; ` ); + + if ( generator.current.localElementDepth === 0 ) { + generator.current.teardownStatements.push( deindent` + if ( detach ) ${name}.parentNode.removeChild( ${name} ); + ` ); + } } }; diff --git a/compiler/generate/visitors/Text.js b/compiler/generate/visitors/Text.js index 1cb8fc62f7..efdb69ff1c 100644 --- a/compiler/generate/visitors/Text.js +++ b/compiler/generate/visitors/Text.js @@ -15,7 +15,7 @@ export default { ` ); generator.current.teardownStatements.push( deindent` - ${name}.parentNode.removeChild( ${name} ); + if ( detach ) ${name}.parentNode.removeChild( ${name} ); ` ); } } diff --git a/test/compiler/each-block-text-node/_config.js b/test/compiler/each-block-text-node/_config.js new file mode 100644 index 0000000000..b8c9543dad --- /dev/null +++ b/test/compiler/each-block-text-node/_config.js @@ -0,0 +1,14 @@ +export default { + data: { + animals: [ 'alpaca', 'baboon', 'capybara' ] + }, + + html: '(alpaca)(baboon)(capybara)', + + test ( assert, component, target ) { + component.set({ animals: [ 'caribou', 'dogfish' ] }); + assert.htmlEqual( target.innerHTML, '(caribou)(dogfish)' ); + component.set({ animals: [] }); + assert.htmlEqual( target.innerHTML, '' ); + } +}; diff --git a/test/compiler/each-block-text-node/main.html b/test/compiler/each-block-text-node/main.html new file mode 100644 index 0000000000..c053271fa1 --- /dev/null +++ b/test/compiler/each-block-text-node/main.html @@ -0,0 +1,3 @@ +{{#each animals as animal}} + ({{animal}}) +{{/each}}