Skip to content

Commit

Permalink
Merge pull request #2407 from asturur/deep-clo
Browse files Browse the repository at this point in the history
Deep clone transformMatrix, strokeDashArray and styles properties
  • Loading branch information
kangax committed Aug 13, 2015
2 parents 1138f67 + 8ff217c commit db34378
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,8 +1121,16 @@
* @return {Object} object representation of an instance
*/
toObject: function(propertiesToInclude) {
var clonedStyles = { }, i, j, row;
for (i in this.styles) {
row = this.styles[i];
clonedStyles[i] = { };
for (j in row) {
clonedStyles[i][j] = clone(row[j]);
}
}
return fabric.util.object.extend(this.callSuper('toObject', propertiesToInclude), {
styles: clone(this.styles)
styles: clonedStyles
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? this.strokeDashArray.concat() : this.strokeDashArray,
strokeLineCap: this.strokeLineCap,
strokeLineJoin: this.strokeLineJoin,
strokeMiterLimit: toFixed(this.strokeMiterLimit, NUM_FRACTION_DIGITS),
Expand All @@ -799,7 +799,7 @@
backgroundColor: this.backgroundColor,
fillRule: this.fillRule,
globalCompositeOperation: this.globalCompositeOperation,
transformMatrix: this.transformMatrix
transformMatrix: this.transformMatrix ? this.transformMatrix.concat() : this.transformMatrix
};

if (!this.includeDefaultValues) {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/itext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 13 additions & 5 deletions test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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() {
Expand Down

0 comments on commit db34378

Please sign in to comment.