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

do not mutate passed object to fromObject #4699

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@
* @param {Object} object Object to create an instance from
* @param {Function} callback Callback to invoke when an image instance is created
*/
fabric.Image.fromObject = function(object, callback) {
fabric.Image.fromObject = function(_object, callback) {
var object = fabric.util.object.clone(_object);
fabric.util.loadImage(object.src, function(img, error) {
if (error) {
callback && callback(null, error);
Expand Down
33 changes: 33 additions & 0 deletions test/unit/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,39 @@
});
});

QUnit.test('fromObject does not mutate data', function(assert) {
var done = assert.async();
assert.ok(typeof fabric.Image.fromObject === 'function');

var obj = fabric.util.object.extend(fabric.util.object.clone(REFERENCE_IMG_OBJECT), {
src: IMG_SRC
});
var brightness = {
type: 'Brightness',
brightness: 0.1
};
var contrast = {
type: 'Contrast',
contrast: 0.1
};
obj.filters = [brightness];
obj.resizeFilter = contrast;
var copyOfFilters = obj.filters;
var copyOfBrighteness = brightness;
var copyOfContrast = contrast;
var copyOfObject = obj;
fabric.Image.fromObject(obj, function(){
assert.ok(copyOfFilters === obj.filters, 'filters array did not mutate');
assert.ok(copyOfBrighteness === copyOfFilters[0], 'filter is same object');
assert.deepEqual(copyOfBrighteness, obj.filters[0], 'did not mutate filter');
assert.deepEqual(copyOfFilters, obj.filters, 'did not mutate array');
assert.deepEqual(copyOfContrast, obj.resizeFilter, 'did not mutate object');
assert.deepEqual(copyOfObject, obj, 'did not change any value');
assert.ok(copyOfContrast === obj.resizeFilter, 'resizefilter is same object');
done();
});
});

QUnit.test('fromURL', function(assert) {
var done = assert.async();
assert.ok(typeof fabric.Image.fromURL === 'function');
Expand Down