OpenLayers OpenLayers

Ticket #491: 491.2.patch

File 491.2.patch, 2.9 kB (added by crschmidt, 2 years ago)
  • tests/test_Util.html

    old new  
    177177    } 
    178178 
    179179    function test_07_Util_getParameterString(t) { 
    180         t.plan( 2 ); 
     180        t.plan( 4 ); 
    181181 
    182182        var params = { foo: "bar", 
    183183                       chicken: 1.5 
     
    185185 
    186186        t.eq( OpenLayers.Util.getParameterString(params), "foo=bar&chicken=1.5", "getParameterString returns correctly");     
    187187        t.eq( OpenLayers.Util.getParameterString({'a:':'b='}), "a%3A=b%3D", "getParameterString returns correctly with non-ascii keys/values");     
     188         
     189 
     190        // Parameters which are a list should end up being a comma-seperated 
     191        // list of the URL encoded strings 
     192        var params = { foo: ["bar,baz"] }; 
     193        t.eq( OpenLayers.Util.getParameterString(params), "foo=bar%2Cbaz", "getParameterString encodes , correctly in arrays");     
     194         
     195        var params = { foo: ["bar","baz,"] }; 
     196        t.eq( OpenLayers.Util.getParameterString(params), "foo=bar,baz%2C", "getParameterString returns with list of CSVs when given a list. ");     
    188197    } 
    189198 
    190199    function test_08_Util_createAlphaImageDiv(t) { 
  • lib/OpenLayers/Util.js

    old new  
    377387* @returns a concatenation of the properties of an object in  
    378388*    http parameter notation.  
    379389*    (ex. <i>"key1=value1&key2=value2&key3=value3"</i>) 
     390*    If a parameter is actually a list, that parameter will then 
     391*    be set to a comma-seperated list of values (foo,bar) instead 
     392*    of being URL escaped (foo%3Abar).  
    380393* @type String 
    381394*/ 
    382395OpenLayers.Util.getParameterString = function(params) { 
    383396    paramsArray = new Array(); 
    384397     
    385398    for (var key in params) { 
    386         var value = params[key]; 
    387         if ((value != null) && (typeof value != 'function')) { 
    388             paramsArray.push(encodeURIComponent(key) + "=" + 
    389                              encodeURIComponent(value)); 
     399      var value = params[key]; 
     400      if ((value != null) && (typeof value != 'function')) { 
     401        var encodedValue; 
     402        if (typeof value == 'object' && value.constructor == Array) { 
     403          /* value is an array; encode items and separate with "," */ 
     404          var encodedItemArray = new Array(); 
     405          for (var itemIndex=0; itemIndex<value.length; itemIndex++) { 
     406            encodedItemArray.push(encodeURIComponent(value[itemIndex])); 
     407          } 
     408          encodedValue = encodedItemArray.join(","); 
    390409        } 
     410        else { 
     411          /* value is a string; simply encode */ 
     412          encodedValue = encodeURIComponent(value); 
     413        } 
     414        paramsArray.push(encodeURIComponent(key) + "=" + encodedValue); 
     415      } 
    391416    } 
    392417     
    393418    return paramsArray.join("&");