Collections cannot contain Collections.

By having both the constructor and the setComponents checking
a white- and a blacklist for allowed types, we ensure that no
Collections can be added through these methods to the list of
components.
This commit is contained in:
Marc Jansen
2012-06-21 18:18:00 +02:00
parent 8d8819ec3c
commit 340caf2720
2 changed files with 129 additions and 4 deletions

View File

@@ -13,16 +13,65 @@ goog.require('ol.Projection');
* @constructor
*/
ol.geom.Collection = function(components) {
/**
* @private
* @type {Array.<Function>}
*/
this.typeBlacklist_ = [
ol.geom.Collection
];
/**
* @private
* @type {Array.<Function>}
*/
this.typeWhitelist_ = [];
/**
* @private
* @type {Array.<ol.geom.Geometry>}
*/
this.components_ = components;
this.components_ = [];
if (arguments.length === 1 && goog.isDef(components)) {
this.setComponents(components);
}
};
goog.inherits(ol.geom.Collection, ol.geom.Geometry);
/**
* Sets the list of disallowed types for the collection.
* @param {Array.<Function>} typeBlacklist Array of constructors to disallow.
*/
ol.geom.Collection.prototype.setTypeBlacklist = function(typeBlacklist){
this.typeBlacklist_ = typeBlacklist;
};
/**
* Gets the list of disallowed types for the collection.
* @return {Array.<Function>} Array of constructors to disallow.
*/
ol.geom.Collection.prototype.getTypeBlacklist = function(){
return this.typeBlacklist_;
};
/**
* Sets the list of always allowed types for the collection.
* @param {Array.<Function>} typeWhitelist Array of constructors to allow.
*/
ol.geom.Collection.prototype.setTypeWhitelist = function(typeWhitelist){
this.typeWhitelist_ = typeWhitelist;
};
/**
* Gets the list of always allowed types for the collection.
* @return {Array.<Function>} Array of constructors to allow.
*/
ol.geom.Collection.prototype.getTypeWhitelist = function(){
return this.typeWhitelist_;
};
/**
* Sets the Collection's components.
*
@@ -38,7 +87,17 @@ ol.geom.Collection.prototype.getComponents = function() {
* @param {Array.<ol.geom.Geometry>} components An array of components.
*/
ol.geom.Collection.prototype.setComponents = function(components) {
this.components_ = components;
var allValidTypes = goog.array.every(
components,
this.isAllowedComponent,
this
);
if (allValidTypes) {
this.components_ = components;
} else {
throw new Error('ol.geom.Collection: at least one component passed to '
+ 'setComponents is not allowed.');
}
};
/**
@@ -48,7 +107,52 @@ ol.geom.Collection.prototype.setComponents = function(components) {
* @param {number} index The index where to add.
*/
ol.geom.Collection.prototype.addComponent = function(component, index) {
goog.array.insertAt(this.components_,component,index);
if (this.isAllowedComponent(component)) {
goog.array.insertAt(this.components_, component, index);
} else {
throw new Error("ol.geom.Collection: The component is not allowed "
+ "to be added.");
}
};
/**
* Checks whether the passed component is an instance of any of the constructors
* listed in the passed list.
*
* @param {ol.geom.Geometry} component The component to check.
* @param {Array.<Function>} list The List of constructors to check the
* component against.
*
* @return {boolean} Whether the passed component is an instance of any of the
* constructors listed in the passed list.
*
* @private
*/
ol.geom.Collection.prototype.isOnList = function(component, list) {
var isOnList = !goog.array.every(list, function(listedConstr){
if (component instanceof listedConstr) {
return false;
} else {
return true;
}
});
return isOnList;
};
/**
* Checks whether the passed component is allowed according to the black and
* whitelists.
*
* @param {ol.geom.Geometry} component The component to check.
* @return {boolean} Whether the passed component is allowed as part of this
* collection according to black- and whitelist.
*/
ol.geom.Collection.prototype.isAllowedComponent = function(component){
var whitelist = this.getTypeWhitelist(),
blacklist = this.getTypeBlacklist(),
isOnWhitelist = this.isOnList(component, whitelist),
isOnBlacklist = this.isOnList(component, blacklist);
return (isOnWhitelist || !isOnBlacklist);
};
/**