OpenLayers OpenLayers

Changeset 2093

Show
Ignore:
Timestamp:
12/22/06 14:03:32 (2 years ago)
Author:
crschmidt
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/examples/fullScreen.html

    r1486 r2093  
    1515 
    1616            var ol_wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",  
    17                 "http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'});  
     17                "http://labs.metacarta.com:80/wms/vmap0?", {layers: 'basic'});  
    1818 
    1919            var jpl_wms = new OpenLayers.Layer.WMS( "NASA Global Mosaic", 
  • trunk/openlayers/lib/OpenLayers/Tile/Image.js

    r2092 r2093  
    148148        if (this.layer) { 
    149149            var loaded = this.layer.alpha ? this.imgDiv.firstChild.src : this.imgDiv.src; 
    150             if (loaded != this.url) { 
     150            if (!OpenLayers.Util.isEquivalentUrl(loaded, this.url)) { 
    151151                this.imgDiv.style.display = "none"; 
    152152            } 
  • trunk/openlayers/lib/OpenLayers/Util.js

    r2048 r2093  
    710710    return [valueL, valueT]; 
    711711}; 
     712 
     713 
     714/** Test two URLs for equivalence.  
     715 *  
     716 * Setting 'ignoreCase' allows for case-independent comparison. 
     717 *  
     718 * Comparison is based on:  
     719 *  - Protocol 
     720 *  - Host (evaluated without the port) 
     721 *  - Port (set 'ignorePort80' to ignore "80" values) 
     722 *  - Hash ( set 'ignoreHash' to disable) 
     723 *  - Pathname (for relative <-> absolute comparison)  
     724 *  - Arguments (so they can be out of order) 
     725 *   
     726 *  
     727 * 
     728 * @param {String} url1 
     729 * @param {String} url2 
     730 * @param {Object} options allows for customization of comparison: 
     731 *                         'ignoreCase' - Default is True 
     732 *                         'ignorePort80' - Default is True 
     733 *                         'ignoreHash' - Default is True 
     734 * 
     735 * @returns Whether or not the two URLs are equivalent 
     736 * @type Boolean 
     737 */ 
     738OpenLayers.Util.isEquivalentUrl = function(url1, url2, options) { 
     739    options = options || new Object(); 
     740 
     741    OpenLayers.Util.applyDefaults(options, { 
     742        ignoreCase: true, 
     743        ignorePort80: true, 
     744        ignoreHash: true 
     745    }); 
     746 
     747    urlObj1 = OpenLayers.Util.createUrlObject(url1, options); 
     748    urlObj2 = OpenLayers.Util.createUrlObject(url2, options); 
     749 
     750    //compare keys (host, port, etc) 
     751    for(var key in urlObj1) { 
     752        if ( (key != "args") && (urlObj1[key] != urlObj2[key]) ) { 
     753            return false; 
     754        } 
     755    } 
     756 
     757    // compare search args - irrespective of order 
     758    for(var key in urlObj1.args) { 
     759        if(urlObj1.args[key] != urlObj2.args[key]) { 
     760            return false; 
     761        } 
     762        delete urlObj2.args[key]; 
     763    } 
     764    // urlObj2 shouldn't have any args left 
     765    for(var key in urlObj2.args) { 
     766        return false; 
     767    } 
     768     
     769    return true; 
     770}; 
     771 
     772/** 
     773 * @private 
     774 * 
     775 * @param {String} url 
     776 * @param {Object} options 
     777 *  
     778 * @returns An object with separate url, a, port, host, and args parsed out  
     779 *            and ready for comparison 
     780 * @type Object 
     781 */ 
     782OpenLayers.Util.createUrlObject = function(url, options) { 
     783    options = options || new Object(); 
     784 
     785    var urlObject = new Object(); 
     786   
     787    if (options.ignoreCase) { 
     788        url = url.toLowerCase();  
     789    } 
     790 
     791    var a = document.createElement('a'); 
     792 
     793    a.href = url; 
     794     
     795  //protocol 
     796    urlObject.protocol = a.protocol;   
     797   
     798  //pathname (this part allows for relative <-> absolute comparison) 
     799    urlObject.pathname = a.pathname;   
     800     
     801  //hash 
     802    urlObject.hash = (options.ignoreHash) ? "" : a.hash;   
     803     
     804  //host (without port) 
     805    urlObject.host = a.host; 
     806    var port = a.port; 
     807    if (port.length <= 0) { 
     808        var newHostLength = urlObject.host.length - (port.length); 
     809        urlObject.host = urlObject.host.substring(0, newHostLength);  
     810    } 
     811     
     812  //port 
     813    urlObject.port = ((port == "80") && (options.ignorePort80)) ? "" : port; 
     814                                                                      
     815  //args 
     816    urlObject.args = OpenLayers.Util.getArgs(a.search); 
     817 
     818    return urlObject;  
     819}; 
     820  
  • trunk/openlayers/tests/test_Util.html

    r1910 r2093  
    491491        t.eq(OpenLayers.Util.getImagesLocation().substr(OpenLayers.Util.getImagesLocation().length-4,4), "img/", "ImgPath works as expected when set to ''.");  
    492492    } 
    493   // --> 
     493 
     494    function test_15_Util_isEquivalentUrl(t) { 
     495        t.plan(8); 
     496         
     497        var url1, url2, options; 
     498 
     499  //CASE 
     500 
     501        url1 = "http://www.openlayers.org"; 
     502        url2 = "HTTP://WWW.OPENLAYERS.ORG"; 
     503 
     504        t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreCase works");  
     505 
     506  //ARGS 
     507 
     508        url1 = "http://www.openlayers.org?foo=5;bar=6"; 
     509        url2 = "http://www.openlayers.org?bar=6;foo=5"; 
     510 
     511        t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "shuffled arguments works");  
     512 
     513  //PORT 
     514 
     515        url1 = "http://www.openlayers.org:80"; 
     516        url2 = "http://www.openlayers.org"; 
     517 
     518        t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignorePort80 works");  
     519        options = { 
     520            'ignorePort80': false 
     521        }         
     522 
     523        url1 = "http://www.openlayers.org:80"; 
     524        url2 = "http://www.openlayers.org:50"; 
     525 
     526        t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2), "port check works");  
     527 
     528 
     529  //HASH 
     530 
     531        url1 = "http://www.openlayers.org#barf"; 
     532        url2 = "http://www.openlayers.org"; 
     533 
     534        t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreHash works");  
     535        options = { 
     536            'ignoreHash': false 
     537        }         
     538        t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2, options), "ignoreHash FALSE works");  
     539 
     540  //PROTOCOL 
     541 
     542        url1 = "http://www.openlayers.org"; 
     543        url2 = "ftp://www.openlayers.org"; 
     544 
     545        t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreHash works");  
     546 
     547 
     548  //PATHNAME 
     549        url1 = document.location.pathName + "/foo.html"; 
     550        url2 = "foo.html"; 
     551 
     552        t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2), "relative vs. absolute paths works");  
     553         
     554  
     555    } 
     556 
     557   // --> 
    494558  </script> 
    495559</head>