diff --git a/src/util/misc.js b/src/util/misc.js index 904de507..fc628499 100644 --- a/src/util/misc.js +++ b/src/util/misc.js @@ -286,7 +286,9 @@ function populateWithProperties(source, destination, properties) { if (properties && Object.prototype.toString.call(properties) === '[object Array]') { for (var i = 0, len = properties.length; i < len; i++) { - destination[properties[i]] = source[properties[i]]; + if (properties[i] in source) { + destination[properties[i]] = source[properties[i]]; + } } } } diff --git a/test/unit/canvas_static.js b/test/unit/canvas_static.js index 391f1b4e..ad4e50e1 100644 --- a/test/unit/canvas_static.js +++ b/test/unit/canvas_static.js @@ -352,6 +352,24 @@ }); }); + test('toJSON custom properties non-existence check', function() { + var rect = new fabric.Rect({ width: 10, height: 20 }); + rect.padding = 123; + canvas.add(rect); + rect.foo = 'bar'; + + canvas.bar = 456; + + var data = canvas.toJSON(['padding', 'foo', 'bar', 'baz']); + ok('padding' in data.objects[0]); + ok('foo' in data.objects[0], 'foo shouldn\'t be included if it\'s not in an object'); + ok(!('bar' in data.objects[0]), 'bar shouldn\'t be included if it\'s not in an object'); + ok(!('baz' in data.objects[0]), 'bar shouldn\'t be included if it\'s not in an object'); + ok(!('foo' in data)); + ok(!('baz' in data)); + ok('bar' in data); + }); + test('remove', function() { ok(typeof canvas.remove == 'function'); var rect1 = makeRect(),