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:
crschmidt
2006-12-22 19:03:32 +00:00
parent 88ac5a803c
commit 01cf10f53d
4 changed files with 176 additions and 3 deletions

View File

@@ -14,7 +14,7 @@
var map = new OpenLayers.Map('map');
var ol_wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'});
"http://labs.metacarta.com:80/wms/vmap0?", {layers: 'basic'});
var jpl_wms = new OpenLayers.Layer.WMS( "NASA Global Mosaic",
"http://wms.jpl.nasa.gov/wms.cgi",

View File

@@ -147,7 +147,7 @@ OpenLayers.Tile.Image.prototype =
// from the map, in which case this check is not needed.
if (this.layer) {
var loaded = this.layer.alpha ? this.imgDiv.firstChild.src : this.imgDiv.src;
if (loaded != this.url) {
if (!OpenLayers.Util.isEquivalentUrl(loaded, this.url)) {
this.imgDiv.style.display = "none";
}
}

View File

@@ -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;
};

View File

@@ -490,6 +490,70 @@
OpenLayers.ImgPath = '';
t.eq(OpenLayers.Util.getImagesLocation().substr(OpenLayers.Util.getImagesLocation().length-4,4), "img/", "ImgPath works as expected when set to ''.");
}
function test_15_Util_isEquivalentUrl(t) {
t.plan(8);
var url1, url2, options;
//CASE
url1 = "http://www.openlayers.org";
url2 = "HTTP://WWW.OPENLAYERS.ORG";
t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreCase works");
//ARGS
url1 = "http://www.openlayers.org?foo=5;bar=6";
url2 = "http://www.openlayers.org?bar=6;foo=5";
t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "shuffled arguments works");
//PORT
url1 = "http://www.openlayers.org:80";
url2 = "http://www.openlayers.org";
t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignorePort80 works");
options = {
'ignorePort80': false
}
url1 = "http://www.openlayers.org:80";
url2 = "http://www.openlayers.org:50";
t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2), "port check works");
//HASH
url1 = "http://www.openlayers.org#barf";
url2 = "http://www.openlayers.org";
t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreHash works");
options = {
'ignoreHash': false
}
t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2, options), "ignoreHash FALSE works");
//PROTOCOL
url1 = "http://www.openlayers.org";
url2 = "ftp://www.openlayers.org";
t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreHash works");
//PATHNAME
url1 = document.location.pathName + "/foo.html";
url2 = "foo.html";
t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2), "relative vs. absolute paths works");
}
// -->
</script>
</head>