Skip to content

Commit

Permalink
Make getObjects return a copy of the object array (#5162)
Browse files Browse the repository at this point in the history
* ok

* ok

* ok
  • Loading branch information
asturur authored Aug 11, 2018
1 parent db292df commit 6994b04
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/mixins/canvas_grouping.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
* @param {Object} target
*/
_createGroup: function(target) {
var objects = this.getObjects(),
var objects = this._objects,
isActiveLower = objects.indexOf(this._activeObject) < objects.indexOf(target),
groupObjects = isActiveLower
? [this._activeObject, target]
Expand Down
17 changes: 9 additions & 8 deletions src/mixins/collection.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fabric.Collection = {
* @chainable
*/
insertAt: function (object, index, nonSplicing) {
var objects = this.getObjects();
var objects = this._objects;
if (nonSplicing) {
objects[index] = object;
}
Expand All @@ -60,7 +60,7 @@ fabric.Collection = {
* @chainable
*/
remove: function() {
var objects = this.getObjects(),
var objects = this._objects,
index, somethingRemoved = false;

for (var i = 0, length = arguments.length; i < length; i++) {
Expand Down Expand Up @@ -101,12 +101,13 @@ fabric.Collection = {
/**
* Returns an array of children objects of this instance
* Type parameter introduced in 1.3.10
* since 2.3.5 this method return always a COPY of the array;
* @param {String} [type] When specified, only objects of this type are returned
* @return {Array}
*/
getObjects: function(type) {
if (typeof type === 'undefined') {
return this._objects;
return this._objects.concat();
}
return this._objects.filter(function(o) {
return o.type === type;
Expand All @@ -119,23 +120,23 @@ fabric.Collection = {
* @return {Self} thisArg
*/
item: function (index) {
return this.getObjects()[index];
return this._objects[index];
},

/**
* Returns true if collection contains no objects
* @return {Boolean} true if collection is empty
*/
isEmpty: function () {
return this.getObjects().length === 0;
return this._objects.length === 0;
},

/**
* Returns a size of a collection (i.e: length of an array containing its objects)
* @return {Number} Collection size
*/
size: function() {
return this.getObjects().length;
return this._objects.length;
},

/**
Expand All @@ -144,15 +145,15 @@ fabric.Collection = {
* @return {Boolean} `true` if collection contains an object
*/
contains: function(object) {
return this.getObjects().indexOf(object) > -1;
return this._objects.indexOf(object) > -1;
},

/**
* Returns number representation of a collection complexity
* @return {Number} complexity
*/
complexity: function () {
return this.getObjects().reduce(function (memo, current) {
return this._objects.reduce(function (memo, current) {
memo += current.complexity ? current.complexity() : 0;
return memo;
}, 0);
Expand Down
43 changes: 4 additions & 39 deletions src/shapes/active_selection.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@
* @return {fabric.Group}
*/
toGroup: function() {
var objects = this._objects;
var objects = this._objects.concat();
this._objects = [];
var options = this.toObject();
var options = fabric.Object.prototype.toObject.call(this);
var newGroup = new fabric.Group([]);
delete options.objects;
delete options.type;
newGroup.set(options);
newGroup.type = 'group';
objects.forEach(function(object) {
object.group = newGroup;
object.canvas.remove(object);
object.group = newGroup;
});
newGroup._objects = objects;
if (!this.canvas) {
Expand Down Expand Up @@ -97,24 +96,6 @@
return '#<fabric.ActiveSelection: (' + this.complexity() + ')>';
},

/**
* @private
*/
_set: function(key, value) {
var i = this._objects.length;
if (key === 'canvas') {
while (i--) {
this._objects[i].set(key, value);
}
}
if (this.useSetOnGroup) {
while (i--) {
this._objects[i].setOnGroup(key, value);
}
}
fabric.Object.prototype._set.call(this, key, value);
},

/**
* Decide if the object should cache or not. Create its own cache level
* objectCaching is a global flag, wins over everything
Expand All @@ -127,22 +108,6 @@
return false;
},

/**
* Check if this object or a child object will cast a shadow
* @return {Boolean}
*/
willDrawShadow: function() {
if (this.shadow) {
return this.callSuper('willDrawShadow');
}
for (var i = 0, len = this._objects.length; i < len; i++) {
if (this._objects[i].willDrawShadow()) {
return true;
}
}
return false;
},

/**
* Check if this group or its parent group are caching, recursively up
* @return {Boolean}
Expand Down
22 changes: 10 additions & 12 deletions src/shapes/group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use strict';

var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
min = fabric.util.array.min,
max = fabric.util.array.max;

Expand Down Expand Up @@ -208,12 +207,11 @@
}
}
if (key === 'canvas') {
i = this._objects.length;
while (i--) {
this._objects[i]._set(key, value);
}
}
this.callSuper('_set', key, value);
fabric.Object.prototype._set.call(this, key, value);
},

/**
Expand All @@ -222,16 +220,16 @@
* @return {Object} object representation of an instance
*/
toObject: function(propertiesToInclude) {
var objsToObject = this.getObjects().map(function(obj) {
var objsToObject = this._objects.map(function(obj) {
var originalDefaults = obj.includeDefaultValues;
obj.includeDefaultValues = obj.group.includeDefaultValues;
var _obj = obj.toObject(propertiesToInclude);
obj.includeDefaultValues = originalDefaults;
return _obj;
});
return extend(this.callSuper('toObject', propertiesToInclude), {
objects: objsToObject
});
var obj = fabric.Object.prototype.toObject.call(this, propertiesToInclude);
obj.objects = objsToObject;
return obj;
},

/**
Expand All @@ -245,17 +243,17 @@
objsToObject = sourcePath;
}
else {
objsToObject = this.getObjects().map(function(obj) {
objsToObject = this._objects.map(function(obj) {
var originalDefaults = obj.includeDefaultValues;
obj.includeDefaultValues = obj.group.includeDefaultValues;
var _obj = obj.toDatalessObject(propertiesToInclude);
obj.includeDefaultValues = originalDefaults;
return _obj;
});
}
return extend(this.callSuper('toDatalessObject', propertiesToInclude), {
objects: objsToObject
});
var obj = fabric.Object.prototype.toDatalessObject.call(this, propertiesToInclude);
obj.objects = objsToObject;
return obj;
},

/**
Expand Down Expand Up @@ -296,7 +294,7 @@
*/
willDrawShadow: function() {
if (this.shadow) {
return this.callSuper('willDrawShadow');
return fabric.Object.prototype.willDrawShadow.call(this);
}
for (var i = 0, len = this._objects.length; i < len; i++) {
if (this._objects[i].willDrawShadow()) {
Expand Down
8 changes: 4 additions & 4 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@
* @private
*/
_toObjects: function(methodName, propertiesToInclude) {
return this.getObjects().filter(function(object) {
return this._objects.filter(function(object) {
return !object.excludeFromExport;
}).map(function(instance) {
return this._toObject(instance, methodName, propertiesToInclude);
Expand Down Expand Up @@ -1328,7 +1328,7 @@
createSVGFontFacesMarkup: function() {
var markup = '', fontList = { }, obj, fontFamily,
style, row, rowIndex, _char, charIndex, i, len,
fontPaths = fabric.fontPaths, objects = this.getObjects();
fontPaths = fabric.fontPaths, objects = this._objects;

for (i = 0, len = objects.length; i < len; i++) {
obj = objects[i];
Expand Down Expand Up @@ -1379,7 +1379,7 @@
* @private
*/
_setSVGObjects: function(markup, reviver) {
var instance, i, len, objects = this.getObjects();
var instance, i, len, objects = this._objects;
for (i = 0, len = objects.length; i < len; i++) {
instance = objects[i];
if (instance.excludeFromExport) {
Expand Down Expand Up @@ -1694,7 +1694,7 @@
*/
toString: function () {
return '#<fabric.Canvas (' + this.complexity() + '): ' +
'{ objects: ' + this.getObjects().length + ' }>';
'{ objects: ' + this._objects.length + ' }>';
}
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
collection.add(obj2, obj);
assert.ok(typeof collection.getObjects === 'function', 'has getObjects method');
var returned = collection.getObjects();
assert.equal(returned, collection._objects, 'return the _objects array directly');
assert.notEqual(returned, collection._objects, 'does not return a reference to _objects');
returned = collection.getObjects('a');
assert.notEqual(returned, collection._objects, 'return a new array');
assert.equal(returned.indexOf(obj2), -1, 'object of type B is not included');
Expand Down

0 comments on commit 6994b04

Please sign in to comment.