From e3518c29b1a9ea07748a713e12a3dd49e9af491d Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Sat, 6 Oct 2012 13:08:07 +0200 Subject: [PATCH] Fix issue where adding the children of one group to another using Item#insertChildren / Item#addChildren does not work. This fixes #119. To avoid duplication I chose to call Item#insertChildren from Item#addChildren. --- src/item/Item.js | 7 +++++-- test/tests/Group.js | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) 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