Tests, example change, and Util addition, plus usage in Tile/Image to allow
use of relative URLs, urls with port 80 in them, etc. patch originally from tschaub, revised by euzuro, reviewed by me. git-svn-id: http://svn.openlayers.org/trunk/openlayers@2093 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -709,3 +709,112 @@ OpenLayers.Util.pagePosition = function(forElement) {
|
||||
|
||||
return [valueL, valueT];
|
||||
};
|
||||
|
||||
|
||||
/** Test two URLs for equivalence.
|
||||
*
|
||||
* Setting 'ignoreCase' allows for case-independent comparison.
|
||||
*
|
||||
* Comparison is based on:
|
||||
* - Protocol
|
||||
* - Host (evaluated without the port)
|
||||
* - Port (set 'ignorePort80' to ignore "80" values)
|
||||
* - Hash ( set 'ignoreHash' to disable)
|
||||
* - Pathname (for relative <-> absolute comparison)
|
||||
* - Arguments (so they can be out of order)
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param {String} url1
|
||||
* @param {String} url2
|
||||
* @param {Object} options allows for customization of comparison:
|
||||
* 'ignoreCase' - Default is True
|
||||
* 'ignorePort80' - Default is True
|
||||
* 'ignoreHash' - Default is True
|
||||
*
|
||||
* @returns Whether or not the two URLs are equivalent
|
||||
* @type Boolean
|
||||
*/
|
||||
OpenLayers.Util.isEquivalentUrl = function(url1, url2, options) {
|
||||
options = options || new Object();
|
||||
|
||||
OpenLayers.Util.applyDefaults(options, {
|
||||
ignoreCase: true,
|
||||
ignorePort80: true,
|
||||
ignoreHash: true
|
||||
});
|
||||
|
||||
urlObj1 = OpenLayers.Util.createUrlObject(url1, options);
|
||||
urlObj2 = OpenLayers.Util.createUrlObject(url2, options);
|
||||
|
||||
//compare keys (host, port, etc)
|
||||
for(var key in urlObj1) {
|
||||
if ( (key != "args") && (urlObj1[key] != urlObj2[key]) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// compare search args - irrespective of order
|
||||
for(var key in urlObj1.args) {
|
||||
if(urlObj1.args[key] != urlObj2.args[key]) {
|
||||
return false;
|
||||
}
|
||||
delete urlObj2.args[key];
|
||||
}
|
||||
// urlObj2 shouldn't have any args left
|
||||
for(var key in urlObj2.args) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* @param {String} url
|
||||
* @param {Object} options
|
||||
*
|
||||
* @returns An object with separate url, a, port, host, and args parsed out
|
||||
* and ready for comparison
|
||||
* @type Object
|
||||
*/
|
||||
OpenLayers.Util.createUrlObject = function(url, options) {
|
||||
options = options || new Object();
|
||||
|
||||
var urlObject = new Object();
|
||||
|
||||
if (options.ignoreCase) {
|
||||
url = url.toLowerCase();
|
||||
}
|
||||
|
||||
var a = document.createElement('a');
|
||||
|
||||
a.href = url;
|
||||
|
||||
//protocol
|
||||
urlObject.protocol = a.protocol;
|
||||
|
||||
//pathname (this part allows for relative <-> absolute comparison)
|
||||
urlObject.pathname = a.pathname;
|
||||
|
||||
//hash
|
||||
urlObject.hash = (options.ignoreHash) ? "" : a.hash;
|
||||
|
||||
//host (without port)
|
||||
urlObject.host = a.host;
|
||||
var port = a.port;
|
||||
if (port.length <= 0) {
|
||||
var newHostLength = urlObject.host.length - (port.length);
|
||||
urlObject.host = urlObject.host.substring(0, newHostLength);
|
||||
}
|
||||
|
||||
//port
|
||||
urlObject.port = ((port == "80") && (options.ignorePort80)) ? "" : port;
|
||||
|
||||
//args
|
||||
urlObject.args = OpenLayers.Util.getArgs(a.search);
|
||||
|
||||
return urlObject;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user