From 15471c76ab482218612651b5ecec89faa1ef9cd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 22 Mar 2017 23:26:26 +0100 Subject: [PATCH] Boolean: Always return CompoundPath items. Relates to #1221 --- src/path/PathItem.Boolean.js | 13 ++++++------- test/helpers.js | 33 +++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/path/PathItem.Boolean.js b/src/path/PathItem.Boolean.js index 8bfa38f0..72ca5c5f 100644 --- a/src/path/PathItem.Boolean.js +++ b/src/path/PathItem.Boolean.js @@ -61,12 +61,11 @@ PathItem.inject(new function() { : res; } - function createResult(ctor, paths, reduce, path1, path2, options) { - var result = new ctor(Item.NO_INSERT); + function createResult(paths, simplify, path1, path2, options) { + var result = new CompoundPath(Item.NO_INSERT); result.addChildren(paths, true); // See if the item can be reduced to just a simple Path. - if (reduce) - result = result.reduce({ simplify: true }); + result = result.reduce({ simplify: simplify }); if (!(options && options.insert == false)) { // Insert the resulting path above whichever of the two paths appear // further up in the stack. @@ -157,7 +156,7 @@ PathItem.inject(new function() { }); } - return createResult(CompoundPath, paths, true, path1, path2, options); + return createResult(paths, true, path1, path2, options); } function splitBoolean(path1, path2, subtract) { @@ -194,7 +193,7 @@ PathItem.inject(new function() { } // At the end, add what's left from our path after all the splitting. addPath(_path1); - return createResult(CompoundPath, paths, true, path1, path2); + return createResult(paths, false, path1, path2); } /* @@ -1114,7 +1113,7 @@ PathItem.inject(new function() { * @return {Group} the resulting group item */ divide: function(path, options) { - return createResult(Group, [ + return createResult([ this.subtract(path, options), this.intersect(path, options) ], true, this, path, options); diff --git a/test/helpers.js b/test/helpers.js index 85c6da48..4192b66e 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -475,17 +475,34 @@ var compareBoolean = function(actual, expected, message, options) { message = getFunctionMessage(actual); actual = actual(); } - var style = { - strokeColor: 'black', - fillColor: expected && (expected.closed - || expected.firstChild && expected.firstChild.closed && 'yellow') - || null - }; - if (actual) + var parent, + index, + style = { + strokeColor: 'black', + fillColor: expected && (expected.closed + || expected.firstChild && expected.firstChild.closed && 'yellow') + || null + }; + if (actual) { + parent = actual.parent; + index = actual.index; + // Remove it from parent already now, in case we're comparing children + // of compound-paths, so we can apply styling to them. + if (parent && parent instanceof CompoundPath) { + actual.remove(); + } else { + parent = null; + } actual.style = style; - if (expected) + } + if (expected) { expected.style = style; + } equals(actual, expected, message, Base.set({ rasterize: true }, options)); + if (parent) { + // Insert it back. + parent.insertChild(index, actual); + } }; var createSVG = function(str, attrs) {