OpenLayers OpenLayers

Changeset 2162

Show
Ignore:
Timestamp:
01/19/07 09:21:12 (2 years ago)
Author:
crschmidt
Message:

isEquivilantURL() support for browsers, as best we can figure it. Tested in
IE6, FF, Moz, Opera, IE7, Safari.

This may not work in all situations. It's included in util for utility, but
it is possible it may fail in some cases in some environments. If you find
this to be the case, please open a bug and we will work to resolve it.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Util.js

    r2138 r2162  
    747747    urlObj2 = OpenLayers.Util.createUrlObject(url2, options); 
    748748 
    749     //compare keys (host, port, etc) 
     749    //compare all keys (host, port, etc) 
    750750    for(var key in urlObj1) { 
    751         if ( (key != "args") && (urlObj1[key] != urlObj2[key]) ) { 
    752             return false; 
    753         } 
     751        if (options.test) { 
     752            alert(key + "\n1:" + urlObj1[key] + "\n2:" + urlObj2[key]); 
     753        } 
     754        var val1 = urlObj1[key]; 
     755        var val2 = urlObj2[key]; 
     756         
     757        switch(key) { 
     758            case "args": 
     759                //do nothing, they'll be treated below 
     760                break; 
     761            case "host": 
     762            case "port": 
     763            case "protocol": 
     764                if ((val1 == "") || (val2 == "")) { 
     765                    //these will be blank for relative urls, so no need to  
     766                    // compare them here -- call break.  
     767                    //  
     768                    break; 
     769                }  
     770                // otherwise continue with default compare 
     771                // 
     772            default:  
     773                if ( (key != "args") && (urlObj1[key] != urlObj2[key]) ) { 
     774                    return false; 
     775                } 
     776                break; 
     777        } 
     778         
    754779    } 
    755780 
     
    789814 
    790815    var a = document.createElement('a'); 
    791  
    792816    a.href = url; 
    793      
    794   //protocol 
    795     urlObject.protocol = a.protocol;   
    796    
    797   //pathname (this part allows for relative <-> absolute comparison) 
    798     urlObject.pathname = a.pathname;   
    799      
    800     //Test to see if the pathname includes the arguments (this happens in Opera) 
    801     var qIndex = urlObject.pathname.indexOf("?"); 
    802     if (qIndex != -1) { 
    803         urlObject.pathname = urlObject.pathname.substring(0, qIndex); 
    804     } 
    805  
    806      
    807   //hash 
    808     urlObject.hash = (options.ignoreHash) ? "" : a.hash;   
    809817     
    810818  //host (without port) 
     
    815823        urlObject.host = urlObject.host.substring(0, newHostLength);  
    816824    } 
    817      
     825 
     826  //protocol 
     827    urlObject.protocol = a.protocol;   
     828 
    818829  //port 
    819830    urlObject.port = ((port == "80") && (options.ignorePort80)) ? "" : port; 
    820831                                                                      
     832  //hash 
     833    urlObject.hash = (options.ignoreHash) ? "" : a.hash;   
     834     
    821835  //args 
    822     urlObject.args = OpenLayers.Util.getArgs(a.search); 
     836    var queryString = a.search; 
     837    if (!queryString) { 
     838        var qMark = url.indexOf("?"); 
     839        queryString = (qMark != -1) ? url.substr(qMark) : ""; 
     840    } 
     841    urlObject.args = OpenLayers.Util.getArgs(queryString); 
     842 
     843 
     844  //pathname (this part allows for relative <-> absolute comparison) 
     845    if ( ((urlObject.protocol == "file:") && (url.indexOf("file:") != -1)) ||  
     846         ((urlObject.protocol != "file:") && (urlObject.host != "")) ) { 
     847 
     848        urlObject.pathname = a.pathname;   
     849 
     850        //Test to see if the pathname includes the arguments (Opera) 
     851        var qIndex = urlObject.pathname.indexOf("?"); 
     852        if (qIndex != -1) { 
     853            urlObject.pathname = urlObject.pathname.substring(0, qIndex); 
     854        } 
     855 
     856    } else { 
     857        var relStr = OpenLayers.Util.removeTail(url); 
     858 
     859        var backs = 0; 
     860        do { 
     861            var index = relStr.indexOf("../"); 
     862 
     863            if (index == 0) { 
     864                backs++ 
     865                relStr = relStr.substr(3); 
     866            } else if (index >= 0) { 
     867                var prevChunk = relStr.substr(0,index - 1); 
     868                 
     869                var slash = prevChunk.indexOf("/"); 
     870                prevChunk = (slash != -1) ? prevChunk.substr(0, slash +1) 
     871                                          : ""; 
     872                 
     873                var postChunk = relStr.substr(index + 3);                 
     874                relStr = prevChunk + postChunk; 
     875            } 
     876        } while(index != -1) 
     877 
     878        var windowAnchor = document.createElement("a"); 
     879        var windowUrl = window.location.href; 
     880        if (options.ignoreCase) { 
     881            windowUrl = windowUrl.toLowerCase(); 
     882        } 
     883        windowAnchor.href = windowUrl; 
     884 
     885      //set protocol of window 
     886        urlObject.protocol = windowAnchor.protocol; 
     887 
     888        var splitter = (windowAnchor.pathname.indexOf("/") != -1) ? "/" : "\\"; 
     889        var dirs = windowAnchor.pathname.split(splitter); 
     890        dirs.pop(); //remove filename 
     891        while ((backs > 0) && (dirs.length > 0)) { 
     892            dirs.pop(); 
     893            backs--; 
     894        } 
     895        relStr = dirs.join("/") + "/"+ relStr; 
     896        urlObject.pathname = relStr; 
     897    } 
     898     
     899    if ((urlObject.protocol == "file:") || (urlObject.protocol == "")) { 
     900        urlObject.host = "localhost"; 
     901    } 
    823902 
    824903    return urlObject;  
    825904}; 
    826905  
     906/** 
     907 * @param {String} url 
     908 *  
     909 * @returns The string with all queryString and Hash removed 
     910 * @type String 
     911 */ 
     912OpenLayers.Util.removeTail = function(url) { 
     913    var head = null; 
     914     
     915    var qMark = url.indexOf("?"); 
     916    var hashMark = url.indexOf("#"); 
     917 
     918    if (qMark == -1) { 
     919        head = (hashMark != -1) ? url.substr(0,hashMark) : url; 
     920    } else { 
     921        head = (hashMark != -1) ? url.substr(0,Math.min(qMark, hashMark))  
     922                                  : url.substr(0, qMark); 
     923    } 
     924    return head; 
     925}; 
  • trunk/openlayers/tests/test_Util.html

    r2139 r2162  
    548548 
    549549  //PATHNAME 
    550         url1 = "foo.html"; 
    551         url2 = "../tests/foo.html"; 
     550        url1 = "foo.html?bar=now#go"; 
     551        url2 = "../tests/../tests/foo.html?bar=now#go"; 
    552552 
    553553        t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "relative vs. absolute paths works");