From 5966b5e3198279ff1876cfca3719cfc24fba0481 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Tue, 11 Aug 2015 03:12:38 +0200 Subject: [PATCH 1/3] toObject returns deep cloning if necessary --- src/shapes/itext.class.js | 10 +++++++++- src/shapes/object.class.js | 4 ++-- test/unit/itext.js | 4 ++++ test/unit/object.js | 18 +++++++++++++----- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/shapes/itext.class.js b/src/shapes/itext.class.js index ca98eb0bc7f..f1deb529555 100644 --- a/src/shapes/itext.class.js +++ b/src/shapes/itext.class.js @@ -1121,8 +1121,16 @@ * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { + var newStyle = { }, i, j, row; + for (i in this.styles) { + row = this.styles[i]; + newStyle[i] = { }; + for (j in row) { + newStyle[i][j] = clone(row[j]); + } + } return fabric.util.object.extend(this.callSuper('toObject', propertiesToInclude), { - styles: clone(this.styles) + styles: newStyle }); } }); diff --git a/src/shapes/object.class.js b/src/shapes/object.class.js index 2569274f79a..0f8bef77f24 100644 --- a/src/shapes/object.class.js +++ b/src/shapes/object.class.js @@ -783,7 +783,7 @@ fill: (this.fill && this.fill.toObject) ? this.fill.toObject() : this.fill, stroke: (this.stroke && this.stroke.toObject) ? this.stroke.toObject() : this.stroke, strokeWidth: toFixed(this.strokeWidth, NUM_FRACTION_DIGITS), - strokeDashArray: this.strokeDashArray, + strokeDashArray: this.strokeDashArray ? extend([ ], this.strokeDashArray) : this.strokeDashArray, strokeLineCap: this.strokeLineCap, strokeLineJoin: this.strokeLineJoin, strokeMiterLimit: toFixed(this.strokeMiterLimit, NUM_FRACTION_DIGITS), @@ -799,7 +799,7 @@ backgroundColor: this.backgroundColor, fillRule: this.fillRule, globalCompositeOperation: this.globalCompositeOperation, - transformMatrix: this.transformMatrix + transformMatrix: this.transformMatrix ? extend([ ], this.transformMatrix) : this.transformMatrix }; if (!this.includeDefaultValues) { diff --git a/test/unit/itext.js b/test/unit/itext.js index 9a7faebdbae..237b00749f8 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -110,6 +110,10 @@ var obj = iText.toObject(); deepEqual(obj.styles, styles); + notEqual(obj.styles[0], styles[0]); + notEqual(obj.styles[0][1], styles[0][1]); + deepEqual(obj.styles[0], styles[0]); + deepEqual(obj.styles[0][1], styles[0][1]); }); test('setSelectionStart', function() { diff --git a/test/unit/object.js b/test/unit/object.js index 85aee8b9dad..492008d094c 100644 --- a/test/unit/object.js +++ b/test/unit/object.js @@ -292,10 +292,12 @@ strokeLineJoin: 'bevil', strokeMiterLimit: 5, flipX: true, - opacity: 0.13 + opacity: 0.13, + transformMatrix: [3, 0, 3, 1, 0, 0] }; - var cObj = new fabric.Object(); + var cObj = new fabric.Object(), + toObjectObj; cObj.includeDefaultValues = false; deepEqual(emptyObjectRepr, cObj.toObject()); @@ -308,9 +310,15 @@ .set('strokeDashArray', [5, 2]) .set('strokeLineCap', 'round') .set('strokeLineJoin', 'bevil') - .set('strokeMiterLimit', 5); - - deepEqual(augmentedObjectRepr, cObj.toObject()); + .set('strokeMiterLimit', 5) + .set('transformMatrix', [3, 0, 3, 1, 0, 0]); + toObjectObj = cObj.toObject(); + deepEqual(augmentedObjectRepr, toObjectObj); + notEqual(augmentedObjectRepr.transformMatrix, toObjectObj.transformMatrix); + deepEqual(augmentedObjectRepr.transformMatrix, toObjectObj.transformMatrix); + notEqual(augmentedObjectRepr.strokeDashArray, toObjectObj.strokeDashArray); + deepEqual(augmentedObjectRepr.strokeDashArray, toObjectObj.strokeDashArray); + }); test('toDatalessObject', function() { From dae413f7452fb7dff024be12e0270063f8ab6479 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Wed, 12 Aug 2015 07:09:05 +0200 Subject: [PATCH 2/3] Update itext.class.js --- src/shapes/itext.class.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/shapes/itext.class.js b/src/shapes/itext.class.js index f1deb529555..e2b36478dc2 100644 --- a/src/shapes/itext.class.js +++ b/src/shapes/itext.class.js @@ -1121,16 +1121,16 @@ * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { - var newStyle = { }, i, j, row; + var clonedStyles = { }, i, j, row; for (i in this.styles) { row = this.styles[i]; - newStyle[i] = { }; + clonedStyles[i] = { }; for (j in row) { - newStyle[i][j] = clone(row[j]); + clonedStyles[i][j] = clone(row[j]); } } return fabric.util.object.extend(this.callSuper('toObject', propertiesToInclude), { - styles: newStyle + styles: clonedStyles }); } }); From 8ff217c59a139e85cdc587d16704a985ca3f52b4 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Thu, 13 Aug 2015 22:28:06 +0200 Subject: [PATCH 3/3] Update object.class.js --- src/shapes/object.class.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shapes/object.class.js b/src/shapes/object.class.js index 0f8bef77f24..7d538f2cf41 100644 --- a/src/shapes/object.class.js +++ b/src/shapes/object.class.js @@ -783,7 +783,7 @@ fill: (this.fill && this.fill.toObject) ? this.fill.toObject() : this.fill, stroke: (this.stroke && this.stroke.toObject) ? this.stroke.toObject() : this.stroke, strokeWidth: toFixed(this.strokeWidth, NUM_FRACTION_DIGITS), - strokeDashArray: this.strokeDashArray ? extend([ ], this.strokeDashArray) : this.strokeDashArray, + strokeDashArray: this.strokeDashArray ? this.strokeDashArray.concat() : this.strokeDashArray, strokeLineCap: this.strokeLineCap, strokeLineJoin: this.strokeLineJoin, strokeMiterLimit: toFixed(this.strokeMiterLimit, NUM_FRACTION_DIGITS), @@ -799,7 +799,7 @@ backgroundColor: this.backgroundColor, fillRule: this.fillRule, globalCompositeOperation: this.globalCompositeOperation, - transformMatrix: this.transformMatrix ? extend([ ], this.transformMatrix) : this.transformMatrix + transformMatrix: this.transformMatrix ? this.transformMatrix.concat() : this.transformMatrix }; if (!this.includeDefaultValues) {