diff --git a/src/document/Document.js b/src/document/Document.js index c907773d..0f86d10e 100644 --- a/src/document/Document.js +++ b/src/document/Document.js @@ -108,10 +108,6 @@ var Document = this.Document = Base.extend({ this._currentStyle = PathStyle.create(null, style); }, - getIndex: function() { - return this._index; - }, - activate: function() { if (this._index != null) { this._scope.document = this; @@ -119,7 +115,17 @@ var Document = this.Document = Base.extend({ } return false; }, - + + remove: function() { + var res = Base.splice(this._scope.documents, null, this._index, 1); + this._scope = this._index = null; + return !!res.length; + }, + + getIndex: function() { + return this._index; + }, + getSelectedItems: function() { // TODO: return groups if their children are all selected, // and filter out their children from the list. diff --git a/src/item/Item.js b/src/item/Item.js index e5854f7f..29f68ae6 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -294,11 +294,9 @@ var Item = this.Item = Base.extend({ */ _removeFromParent: function() { if (this._parent) { - var ok = !!Base.splice(this._parent._children, null, - this._index, 1).length; - this._parent = null; - this._index = null; - return ok; + var res = Base.splice(this._parent._children, null, this._index, 1); + this._parent = this._index = null; + return !!res.length; } return false; }, diff --git a/test/lib/helpers.js b/test/lib/helpers.js index 36447c3a..b5a5e85b 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -1,5 +1,4 @@ -// Let's be strict - +// Override equals to convert functions to message and execute them as tests() function equals(actual, expected, message) { if (typeof actual === 'function') { if (!message) { @@ -15,9 +14,18 @@ function equals(actual, expected, message) { } actual = actual(); } + // Let's be strict return strictEqual(actual, expected, message); } +function test(testName, expected) { + return QUnit.test(testName, function() { + var doc = new Document(); + expected(); + doc.remove(); + }); +} + function compareNumbers(number1, number2, message) { if (number1 !== 0) number1 = Math.round(number1 * 100) / 100; diff --git a/test/tests/Color.js b/test/tests/Color.js index 88ece4c8..32da7d1f 100644 --- a/test/tests/Color.js +++ b/test/tests/Color.js @@ -1,7 +1,6 @@ module('Color'); test('Set named color', function() { - var doc = new Document(); var path = new Path(); path.fillColor = 'red'; compareRGBColors(path.fillColor, new RGBColor(1, 0, 0)); @@ -9,7 +8,6 @@ test('Set named color', function() { }); test('Set color to hex', function() { - var doc = new Document(); var path = new Path(); path.fillColor = '#ff0000'; compareRGBColors(path.fillColor, new RGBColor(1, 0, 0)); @@ -22,7 +20,6 @@ test('Set color to hex', function() { }); test('Set color to object', function() { - var doc = new Document(); var path = new Path(); path.fillColor = { red: 1, green: 0, blue: 1}; compareRGBColors(path.fillColor, new RGBColor(1, 0, 1)); @@ -35,7 +32,6 @@ test('Set color to object', function() { }); test('Set color to array', function() { - var doc = new Document(); var path = new Path(); path.fillColor = [1, 0, 0]; compareRGBColors(path.fillColor, new RGBColor(1, 0, 0)); diff --git a/test/tests/Group.js b/test/tests/Group.js index 30713029..72bbe51c 100644 --- a/test/tests/Group.js +++ b/test/tests/Group.js @@ -1,28 +1,25 @@ module('Group'); test('new Group()', function() { - var doc = new Document(); var group = new Group(); equals(function() { - return doc.activeLayer.children[0] == group; + return paper.document.activeLayer.children[0] == group; }, true); }); test('new Group([item])', function() { - var doc = new Document(); var path = new Path(); var group = new Group([path]); equals(function() { - return doc.activeLayer.children.length == 1; - }, true); + return paper.document.activeLayer.children.length; + }, 1); equals(function() { return group.children[0] == path; }, true); }); test('Group bounds', function() { - var doc = new Document(); - doc.currentStyle = { + paper.document.currentStyle = { strokeWidth: 5, strokeColor: 'black' }; diff --git a/test/tests/Item.js b/test/tests/Item.js index 0bf2cf39..a31488cb 100644 --- a/test/tests/Item.js +++ b/test/tests/Item.js @@ -1,7 +1,7 @@ module('Item'); test('copyTo(document)', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); var secondDoc = new Document(); var copy = path.copyTo(secondDoc); @@ -17,7 +17,7 @@ test('copyTo(document)', function() { }); test('copyTo(layer)', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); var layer = new Layer(); @@ -31,19 +31,19 @@ test('copyTo(layer)', function() { }); test('clone()', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); var copy = path.clone(); equals(function() { - return doc.activeLayer.children.length == 2; - }, true); + return doc.activeLayer.children.length; + }, 2); equals(function() { return path != copy; }, true); }); test('appendChild(item)', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); doc.activeLayer.appendChild(path); equals(function() { @@ -52,7 +52,7 @@ test('appendChild(item)', function() { }); test('item.parent / item.isChild / item.isParent', function() { - var doc = new Document(); + var doc = paper.document; var secondDoc = new Document(); var path = new Path(); doc.activeLayer.appendChild(path); @@ -81,7 +81,7 @@ test('item.parent / item.isChild / item.isParent', function() { }); test('item.lastChild / item.firstChild', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); var secondPath = new Path(); equals(function() { @@ -93,7 +93,7 @@ test('item.lastChild / item.firstChild', function() { }); test('appendBottom(item)', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); var secondPath = new Path(); doc.activeLayer.appendBottom(secondPath); @@ -103,7 +103,7 @@ test('appendBottom(item)', function() { }); test('moveAbove(item)', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); var secondPath = new Path(); path.moveAbove(secondPath); @@ -113,7 +113,7 @@ test('moveAbove(item)', function() { }); test('moveBelow(item)', function() { - var doc = new Document(); + var doc = paper.document; var firstPath = new Path(); var secondPath = new Path(); equals(function() { @@ -126,7 +126,7 @@ test('moveBelow(item)', function() { }); test('isDescendant(item) / isAncestor(item)', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); equals(function() { return path.isDescendant(doc.activeLayer); @@ -152,7 +152,7 @@ test('isDescendant(item) / isAncestor(item)', function() { }); test('isGroupedWith', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); var secondPath = new Path(); var group = new Group([path]); @@ -191,7 +191,6 @@ test('isGroupedWith', function() { }); test('getPreviousSibling() / getNextSibling()', function() { - var doc = new Document(); var firstPath = new Path(); var secondPath = new Path(); equals(function() { @@ -206,7 +205,6 @@ test('getPreviousSibling() / getNextSibling()', function() { }); test('hidden', function() { - var doc = new Document(); var firstPath = new Path(); firstPath.visible = false; equals(function() { @@ -215,7 +213,7 @@ test('hidden', function() { }); test('reverseChildren()', function() { - var doc = new Document(); + var doc = paper.document; var path = new Path(); var secondPath = new Path(); var thirdPath = new Path(); @@ -235,6 +233,7 @@ test('reverseChildren()', function() { }); test('Check item#document when moving items across documents', function() { + var doc = paper.document; var doc1 = new Document(); var path = new Path(); var group = new Group(); @@ -256,7 +255,6 @@ test('Check item#document when moving items across documents', function() { }); test('group.selected', function() { - var doc = new Document(); var path = new Path([0, 0]); var path2 = new Path([0, 0]); var group = new Group([path, path2]); diff --git a/test/tests/Layer.js b/test/tests/Layer.js index 7eaf3677..c8cd0b59 100644 --- a/test/tests/Layer.js +++ b/test/tests/Layer.js @@ -1,7 +1,7 @@ module('Layer'); test('previousSibling / nextSibling', function() { - var doc = new Document(); + var doc = paper.document; var firstLayer = doc.activeLayer; var secondLayer = new Layer(); equals(function() { @@ -40,7 +40,7 @@ test('previousSibling / nextSibling', function() { }); test('moveAbove / moveBelow', function() { - var doc = new Document(); + var doc = paper.document; var firstLayer = doc.activeLayer; var secondLayer = new Layer(); secondLayer.moveBelow(firstLayer); diff --git a/test/tests/Path.js b/test/tests/Path.js index 02ef2f83..278489d7 100644 --- a/test/tests/Path.js +++ b/test/tests/Path.js @@ -1,7 +1,6 @@ module('Path'); test('path.join(path)', function() { - var doc = new Document(); var path = new Path(); path.add(0, 0); path.add(10, 0); @@ -13,7 +12,7 @@ test('path.join(path)', function() { path.join(path2); equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } }'); equals(function() { - return doc.activeLayer.children.length; + return paper.document.activeLayer.children.length; }, 1); var path = new Path(); @@ -55,7 +54,6 @@ test('path.join(path)', function() { }); test('path.remove()', function() { - var doc = new Document(); var path = new Path(); path.add(0, 0); path.add(10, 0); @@ -80,20 +78,19 @@ test('path.remove()', function() { path.remove(); equals(function() { - return doc.activeLayer.children.length; + return paper.document.activeLayer.children.length; }, 0); }); test('Is the path deselected after setting a new list of segments?', function() { - var doc = new Document(); var path = new Path([0, 0]); path.selected = true; equals(function() { return path.selected; }, true); equals(function() { - return doc.selectedItems.length; + return paper.document.selectedItems.length; }, 1); path.segments = [[0, 10]]; @@ -101,12 +98,11 @@ test('Is the path deselected after setting a new list of segments?', function() return path.selected; }, false); equals(function() { - return doc.selectedItems.length; + return paper.document.selectedItems.length; }, 0); }); test('Path#reverse', function() { - var doc = new Document(); var path = new Path.Circle([100, 100], 30); path.reverse(); equals(path.segments.toString(), '{ point: { x: 100, y: 130 }, handleIn: { x: -16.56854, y: 0 }, handleOut: { x: 16.56854, y: 0 } },{ point: { x: 130, y: 100 }, handleIn: { x: 0, y: 16.56854 }, handleOut: { x: 0, y: -16.56854 } },{ point: { x: 100, y: 70 }, handleIn: { x: 16.56854, y: 0 }, handleOut: { x: -16.56854, y: 0 } },{ point: { x: 70, y: 100 }, handleIn: { x: 0, y: -16.56854 }, handleOut: { x: 0, y: 16.56854 } }'); diff --git a/test/tests/PathStyle.js b/test/tests/PathStyle.js index 5fbcf4b1..5865d14b 100644 --- a/test/tests/PathStyle.js +++ b/test/tests/PathStyle.js @@ -1,20 +1,18 @@ module('Path Style'); test('currentStyle', function() { - var doc = new Document(); - doc.currentStyle.fillColor = 'black'; + paper.document.currentStyle.fillColor = 'black'; var path = new Path(); compareRGBColors(path.fillColor, 'black', 'path.fillColor'); // When changing the current style of the document, the style of // paths created using document.currentStyle should not change. - doc.currentStyle.fillColor = 'red'; + paper.document.currentStyle.fillColor = 'red'; compareRGBColors(path.fillColor, 'black', 'path.fillColor'); }); test('setting currentStyle to an object', function() { - var doc = new Document(); - doc.currentStyle = { + paper.document.currentStyle = { fillColor: 'red', strokeColor: 'green' }; @@ -24,7 +22,6 @@ test('setting currentStyle to an object', function() { }); test('setting path styles to an object', function() { - var doc = new Document(); var path = new Path(); path.style = { fillColor: 'red', @@ -35,7 +32,6 @@ test('setting path styles to an object', function() { }); test('setting group styles to an object', function() { - var doc = new Document(); var group = new Group(); var path = new Path(); group.appendTop(path); @@ -48,7 +44,6 @@ test('setting group styles to an object', function() { }); test('getting group styles', function() { - var doc = new Document(); var group = new Group(); var path = new Path(); path.fillColor = 'red'; @@ -72,7 +67,6 @@ test('getting group styles', function() { }); test('setting group styles', function() { - var doc = new Document(); var group = new Group(); var path = new Path(); path.fillColor = 'red'; @@ -96,7 +90,6 @@ test('setting group styles', function() { }); test('setting group styles 2', function() { - var doc = new Document(); var group = new Group(); var path = new Path(); path.fillColor = 'red'; diff --git a/test/tests/Path_Bounds.js b/test/tests/Path_Bounds.js index b03cda90..2037d1cb 100644 --- a/test/tests/Path_Bounds.js +++ b/test/tests/Path_Bounds.js @@ -1,7 +1,6 @@ module('Path Bounds'); test('path.bounds', function() { - var doc = new Document(); var path = new Path([ new Segment(new Point(121, 334), new Point(-19, 38), new Point(30.7666015625, -61.53369140625)), new Segment(new Point(248, 320), new Point(-42, -74), new Point(42, 74)), diff --git a/test/tests/Path_Curves.js b/test/tests/Path_Curves.js index be1129ab..92c714fe 100644 --- a/test/tests/Path_Curves.js +++ b/test/tests/Path_Curves.js @@ -1,8 +1,6 @@ module('Path Curves'); test('path.curves Synchronisation', function() { - - var doc = new Document(); var path = new Path(); path.add(new Point(0, 100)); diff --git a/test/tests/Path_Length.js b/test/tests/Path_Length.js index 9627f4c5..c75d5440 100644 --- a/test/tests/Path_Length.js +++ b/test/tests/Path_Length.js @@ -1,7 +1,6 @@ module('Path Length'); test('path.length', function() { - var doc = new Document(); var path = new Path([ new Segment(new Point(121, 334), new Point(-19, 38), new Point(30.7666015625, -61.53369140625)), new Segment(new Point(248, 320), new Point(-42, -74), new Point(42, 74)) @@ -15,7 +14,6 @@ test('path.length', function() { }); test('curve.getParameter with straight curve', function() { - var doc = new Document(); var path = new Path(); path.moveTo(100, 100); path.lineTo(500, 500); diff --git a/test/tests/Path_Shapes.js b/test/tests/Path_Shapes.js index 2b5f0cf6..b0d8f2b4 100644 --- a/test/tests/Path_Shapes.js +++ b/test/tests/Path_Shapes.js @@ -39,7 +39,6 @@ test('new Path.Arc(from, through, to)', function() { }); test('new Path.RegularPolygon(center, numSides, radius)', function() { - var doc = new Document(); var path = new Path.RegularPolygon(new Point(50, 50), 3, 10); var expectedSegments = [{ point: { x: 41.33984, y: 55 } }, { point: { x: 50, y: 40 } }, { point: { x: 58.66016, y: 55 } }]; equals(path.segments.toString(), '{ point: { x: 41.33975, y: 55 } },{ point: { x: 50, y: 40 } },{ point: { x: 58.66025, y: 55 } }'); @@ -50,12 +49,10 @@ test('new Path.RegularPolygon(center, numSides, radius)', function() { }); test('new Path.Star(center, numSides, radius1, radius2)', function() { - var doc = new Document(); var path = new Path.Star(new Point(100, 100), 10, 10, 20); var expectedSegments = [new Segment(new Point(100, 90)), new Segment(new Point(106.18017578125, 80.97900390625)), new Segment(new Point(105.8779296875, 91.90966796875)), new Segment(new Point(116.18017578125, 88.244140625)), new Segment(new Point(109.5107421875, 96.90966796875)), new Segment(new Point(120, 100)), new Segment(new Point(109.5107421875, 103.09033203125)), new Segment(new Point(116.18017578125, 111.755859375)), new Segment(new Point(105.8779296875, 108.09033203125)), new Segment(new Point(106.18017578125, 119.02099609375)), new Segment(new Point(100, 110)), new Segment(new Point(93.81982421875, 119.02099609375)), new Segment(new Point(94.1220703125, 108.09033203125)), new Segment(new Point(83.81982421875, 111.755859375)), new Segment(new Point(90.4892578125, 103.09033203125)), new Segment(new Point(80, 100)), new Segment(new Point(90.4892578125, 96.90966796875)), new Segment(new Point(83.81982421875, 88.244140625)), new Segment(new Point(94.1220703125, 91.90966796875)), new Segment(new Point(93.81982421875, 80.97900390625))]; equals(path.segments.toString(), '{ point: { x: 100, y: 90 } },{ point: { x: 106.18034, y: 80.97887 } },{ point: { x: 105.87785, y: 91.90983 } },{ point: { x: 116.18034, y: 88.24429 } },{ point: { x: 109.51057, y: 96.90983 } },{ point: { x: 120, y: 100 } },{ point: { x: 109.51057, y: 103.09017 } },{ point: { x: 116.18034, y: 111.75571 } },{ point: { x: 105.87785, y: 108.09017 } },{ point: { x: 106.18034, y: 119.02113 } },{ point: { x: 100, y: 110 } },{ point: { x: 93.81966, y: 119.02113 } },{ point: { x: 94.12215, y: 108.09017 } },{ point: { x: 83.81966, y: 111.75571 } },{ point: { x: 90.48943, y: 103.09017 } },{ point: { x: 80, y: 100 } },{ point: { x: 90.48943, y: 96.90983 } },{ point: { x: 83.81966, y: 88.24429 } },{ point: { x: 94.12215, y: 91.90983 } },{ point: { x: 93.81966, y: 80.97887 } }'); - var doc = new Document(); var path = new Path.Star(new Point(100, 100), 5, 20, 10); var expectedSegments = [new Segment(new Point(100, 80)), new Segment(new Point(105.8779296875, 91.90966796875)), new Segment(new Point(119.02099609375, 93.81982421875)), new Segment(new Point(109.5107421875, 103.09033203125)), new Segment(new Point(111.755859375, 116.18017578125)), new Segment(new Point(100, 110)), new Segment(new Point(88.244140625, 116.18017578125)), new Segment(new Point(90.4892578125, 103.09033203125)), new Segment(new Point(80.97900390625, 93.81982421875)), new Segment(new Point(94.1220703125, 91.90966796875))]; equals(path.segments.toString(), '{ point: { x: 100, y: 80 } },{ point: { x: 105.87785, y: 91.90983 } },{ point: { x: 119.02113, y: 93.81966 } },{ point: { x: 109.51057, y: 103.09017 } },{ point: { x: 111.75571, y: 116.18034 } },{ point: { x: 100, y: 110 } },{ point: { x: 88.24429, y: 116.18034 } },{ point: { x: 90.48943, y: 103.09017 } },{ point: { x: 80.97887, y: 93.81966 } },{ point: { x: 94.12215, y: 91.90983 } }'); diff --git a/test/tests/PlacedSymbol.js b/test/tests/PlacedSymbol.js index 99d8592b..8a4c724e 100644 --- a/test/tests/PlacedSymbol.js +++ b/test/tests/PlacedSymbol.js @@ -1,7 +1,6 @@ module('Placed Symbol'); test('placedSymbol bounds', function() { - var doc = new Document(); var path = new Path.Circle([50, 50], 50); path.strokeWidth = 1; path.strokeCap = 'round';