diff --git a/compiler/generate/visitors/Text.js b/compiler/generate/visitors/Text.js index 074946288b..21ab063b19 100644 --- a/compiler/generate/visitors/Text.js +++ b/compiler/generate/visitors/Text.js @@ -2,8 +2,21 @@ import deindent from '../utils/deindent.js'; export default { enter ( generator, node ) { - generator.current.initStatements.push( deindent` - ${generator.current.target}.appendChild( document.createTextNode( ${JSON.stringify( node.data )} ) ); - ` ); + if ( generator.elementDepth > 1 ) { + generator.current.initStatements.push( deindent` + ${generator.current.target}.appendChild( document.createTextNode( ${JSON.stringify( node.data )} ) ); + ` ); + } else { + const name = generator.current.counter( `text` ); + + generator.current.initStatements.push( deindent` + var ${name} = document.createTextNode( ${JSON.stringify( node.data )} ); + ${generator.current.target}.appendChild( ${name} ); + ` ); + + generator.current.teardownStatements.push( deindent` + ${name}.parentNode.removeChild( ${name} ); + ` ); + } } }; diff --git a/test/compiler/text-node-top-level/_config.js b/test/compiler/text-node-top-level/_config.js new file mode 100644 index 0000000000..0241f32d22 --- /dev/null +++ b/test/compiler/text-node-top-level/_config.js @@ -0,0 +1,3 @@ +export default { + html: 'text' +}; diff --git a/test/compiler/text-node-top-level/main.html b/test/compiler/text-node-top-level/main.html new file mode 100644 index 0000000000..8e27be7d61 --- /dev/null +++ b/test/compiler/text-node-top-level/main.html @@ -0,0 +1 @@ +text