From b08cc68ffe63f74d6f902009ec788198a6734ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Tue, 4 Mar 2014 09:48:41 +0100 Subject: [PATCH] Fix newly introduced issue in CompoundPath#moveTo() / #lineTo() --- src/path/CompoundPath.js | 17 ++++++++++------- src/path/Path.js | 2 +- test/tests/CompoundPath.js | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index ccda4f2c..26580d34 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -277,10 +277,11 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{ * Helper method that returns the current path and checks if a moveTo() * command is required first. */ - function getCurrentPath(that) { - if (!that._children.length) + function getCurrentPath(that, check) { + var children = that._children; + if (check && children.length === 0) throw new Error('Use a moveTo() command first'); - return that._children[that._children.length - 1]; + return children[children.length - 1]; } var fields = { @@ -297,12 +298,14 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{ }, moveBy: function(/* point */) { - this.moveTo(getCurrentPath(this).getLastSegment()._point.add( - Point.read(arguments))); + var current = getCurrentPath(this, true), + last = current && current.getLastSegment(), + point = Point.read(arguments); + this.moveTo(last ? point.add(last._point) : point); }, closePath: function() { - getCurrentPath(this).closePath(); + getCurrentPath(this, true).closePath(); } }; @@ -311,7 +314,7 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{ 'lineBy', 'cubicCurveBy', 'quadraticCurveBy', 'curveBy', 'arcBy'], function(key) { fields[key] = function() { - var path = getCurrentPath(this); + var path = getCurrentPath(this, true); path[key].apply(path, arguments); }; } diff --git a/src/path/Path.js b/src/path/Path.js index 7f8fa294..d2aa0fcf 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -2221,7 +2221,7 @@ var Path = PathItem.extend(/** @lends Path# */{ */ function getCurrentSegment(that) { var segments = that._segments; - if (segments.length == 0) + if (segments.length === 0) throw new Error('Use a moveTo() command first'); return segments[segments.length - 1]; } diff --git a/test/tests/CompoundPath.js b/test/tests/CompoundPath.js index 74c346ba..ae58d95c 100644 --- a/test/tests/CompoundPath.js +++ b/test/tests/CompoundPath.js @@ -23,7 +23,7 @@ test('moveTo / lineTo', function() { for (var i = 0; i < lists.length; i++) { var list = lists[i]; for (var j = 0; j < list.length; j++) { - path[j == 0 ? 'moveTo' : 'lineTo'](list[j]); + path[j === 0 ? 'moveTo' : 'lineTo'](list[j]); } }