diff --git a/src/item/Item.js b/src/item/Item.js index d3a2b99d..51c8688b 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1143,8 +1143,7 @@ function(name) { * @param {item[]} items The items to be added as children */ addChildren: function(items) { - for (var i = 0, l = items && items.length; i < l; i++) - this.insertChild(undefined, items[i]); + this.insertChildren(this._children.length, items); }, /** @@ -1156,6 +1155,10 @@ function(name) { * @param {Item[]} items The items to be appended as children */ insertChildren: function(index, items) { + // We need to clone items because it might be + // an Item#children array. Use Array.prototype.slice because + // in certain cases items is an arguments object + items = items && Array.prototype.slice.apply(items); for (var i = 0, l = items && items.length; i < l; i++) { if (this.insertChild(index, items[i])) index++; diff --git a/test/tests/Group.js b/test/tests/Group.js index 413d134a..5dc66c0f 100644 --- a/test/tests/Group.js +++ b/test/tests/Group.js @@ -70,4 +70,22 @@ test('group.addChildren(otherGroup.children)', function() { equals(function() { return group.children.length; }, 0); +}); + +test('group.insertChildren(0, otherGroup.children)', function() { + var group = new Group(); + group.addChild(new Path()); + group.addChild(new Path()); + equals(function() { + return group.children.length; + }, 2); + + var secondGroup = new Group(); + secondGroup.insertChildren(0, group.children); + equals(function() { + return secondGroup.children.length; + }, 2); + equals(function() { + return group.children.length; + }, 0); }); \ No newline at end of file