Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deep clone transformMatrix, strokeDashArray and styles properties #2407

Merged
merged 3 commits into from
Aug 13, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 newStyle = { }, i, j, row;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newStyleclonedStyles

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
});
}
});
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 ? extend([ ], this.strokeDashArray) : this.strokeDashArray,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we're cloning array because... something might modify it somewhere else?

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 ? extend([ ], this.transformMatrix) : 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