Skip to content

Commit

Permalink
do not mutate passed object to fromObject (#4699)
Browse files Browse the repository at this point in the history
* do not mutate passed object to fromObject

* add a test

* this is what you get for editing in github.com
  • Loading branch information
stefanhayden authored and asturur committed Feb 9, 2018
1 parent 58f2488 commit e13bc33
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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

0 comments on commit e13bc33

Please sign in to comment.