diff --git a/compiler/CodeGenerator.js b/compiler/CodeGenerator.js index 330cbc66c..28ab066b9 100644 --- a/compiler/CodeGenerator.js +++ b/compiler/CodeGenerator.js @@ -162,9 +162,16 @@ class Generator { typeof node === 'boolean') { this.write(node); return; - } else if (isArray(node) || (node instanceof Container)) { + } else if (isArray(node)) { node.forEach(this.generateCode, this); return; + } else if (node instanceof Container) { + node.forEach((child) => { + if (child.container === node) { + this.generateCode(child); + } + }); + return; } let oldCurrentNode = this._currentNode; diff --git a/compiler/ast/ArrayContainer.js b/compiler/ast/ArrayContainer.js index a0664af38..71f38891c 100644 --- a/compiler/ast/ArrayContainer.js +++ b/compiler/ast/ArrayContainer.js @@ -29,7 +29,7 @@ class ArrayContainer extends Container { var curChild = array[i]; if (curChild === oldChild) { array[i] = newChild; - oldChild.container = null; + oldChild.detach(); newChild.container = this; return true; } @@ -42,8 +42,11 @@ class ArrayContainer extends Container { var childIndex = this.array.indexOf(child); if (childIndex !== -1) { this.array.splice(childIndex, 1); + child.detach(); + return true; + } else { + return false; } - child.container = null; } prependChild(newChild) { @@ -54,6 +57,7 @@ class ArrayContainer extends Container { appendChild(newChild) { ok(newChild, 'Invalid child'); + newChild.detach(); this.array.push(newChild); newChild.container = this; } @@ -92,6 +96,20 @@ class ArrayContainer extends Container { throw new Error('Reference node not found'); } + moveChildrenTo(target) { + ok(target.appendChild, 'Node does not support appendChild(node): ' + target); + + var array = this.array; + var len = array.length; + for (var i=0; iFooHello World"); diff --git a/test/fixtures/codegen/autotest/moveChildrenTo/index.js b/test/fixtures/codegen/autotest/moveChildrenTo/index.js new file mode 100644 index 000000000..bab3844ea --- /dev/null +++ b/test/fixtures/codegen/autotest/moveChildrenTo/index.js @@ -0,0 +1,24 @@ +'use strict'; + +module.exports = function(builder) { + var div = builder.htmlElement( + 'div', + [], + [ + builder.text(builder.literal('Hello World')) + ]); + + var span = builder.htmlElement( + 'span', + [], + [ + builder.text(builder.literal('Foo')) + ]); + + div.moveChildrenTo(span); + + return [ + div, + span + ]; +}; \ No newline at end of file