OpenLayers OpenLayers

Ticket #384: equivalent_url.patch

File equivalent_url.patch, 2.6 kB (added by euzuro, 2 years ago)

Patch version of js file

  • lib/OpenLayers/Util.js

    old new  
    709709 
    710710    return [valueL, valueT]; 
    711711}; 
     712 
     713 
     714/** Test two URLs for equivalence.  Allows for comparison of relative and 
     715 *  absolute URLs.  By default ignores case, port 80, and hash.  Assumes URLs 
     716 *  are properly encoded. 
     717 * 
     718 * @param {String} url1 First URL 
     719 * @param {String} url2 Second URL 
     720 * @param {Object} options Optional object to control type of comparison 
     721 * 
     722 * @returns True or false depending on equivalence of two URLs. 
     723 * @type Boolean 
     724 */ 
     725OpenLayers.Util.isEquivalentUrl = function(url1, url2, options) { 
     726    options = (options == null) ? {} : options; 
     727    OpenLayers.Util.applyDefaults(options, { 
     728        ignoreCase: true, 
     729        ignorePort80: true, 
     730        ignoreHash: true 
     731    }); 
     732    if(options.ignoreCase) { 
     733        url1 = url1.toLowerCase(); 
     734        url2 = url2.toLowerCase(); 
     735    } 
     736    var a1 = document.createElement('a'); 
     737    a1.href = url1; 
     738    var a2 = document.createElement('a'); 
     739    a2.href = url2; 
     740 
     741    // compare protocol 
     742    if(a1.protocol != a2.protocol) { 
     743        return false; 
     744    } 
     745 
     746    // compare host (without port) 
     747    var host1 = a1.host; 
     748    var port1 = a1.port; 
     749    if(port1.length > 0) { 
     750        host1 = host1.substring(0, host1.length - (port1.length + 1)); 
     751    } 
     752    var host2 = a2.host; 
     753    var port2 = a2.port; 
     754    if(port2.length > 0) { 
     755        host2 = host2.substring(0, host2.length - (port2.length + 1)); 
     756    } 
     757    if(host1 != host2) { 
     758        return false; 
     759    } 
     760 
     761    // compare port 
     762    if((port1.length == 0) && options.ignorePort80) { 
     763        port1 = "80" 
     764    } 
     765    if((port2.length == 0) && options.ignorePort80) { 
     766        port2 = "80" 
     767    } 
     768    if(port1 != port2) { 
     769        return false; 
     770    } 
     771     
     772    // compare pathname (this part allows for relative <-> absolute comparison 
     773    if(a1.pathname != a2.pathname) { 
     774        return false; 
     775    } 
     776     
     777    // compare search args - irrespective of order 
     778    var args1 = OpenLayers.Util.getArgs(a1.search); 
     779    var args2 = OpenLayers.Util.getArgs(a2.search); 
     780    for(var key in args1) { 
     781        if(args1[key] != args2[key]) { 
     782            return false; 
     783        } 
     784    } 
     785    for(var key in args2) { 
     786        if(args1[key] != args2[key]) { 
     787            return false; 
     788        } 
     789    } 
     790     
     791    if(!options.ignoreHash) { 
     792        if(a1.hash != a2.hash) { 
     793            return false; 
     794        } 
     795    } 
     796 
     797    return true; 
     798}