Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7421: hash.diff

File hash.diff, 3.2 kB (added by Tobie, 2 years ago)

js & tests

  • src/hash.js

    old new  
    55Object.extend(Hash, { 
    66  toQueryString: function(obj) { 
    77    var parts = []; 
    8      
    9          this.prototype._each.call(obj, function(pair) { 
     8 
     9    this.prototype._each.call(obj, function(pair) { 
    1010      if (!pair.key) return; 
    11        
     11 
    1212      if (pair.value && pair.value.constructor == Array) { 
    1313        var values = pair.value.compact(); 
    1414        if (values.length < 2) pair.value = values.reduce(); 
    1515        else { 
    16                key = encodeURIComponent(pair.key); 
     16          key = encodeURIComponent(pair.key); 
    1717          values.each(function(value) { 
    18             value = value != undefined ? encodeURIComponent(value) : ''; 
     18            value = value != undefined ? value : ''; 
    1919            parts.push(key + '=' + encodeURIComponent(value)); 
    2020          }); 
    2121          return; 
     
    2323      } 
    2424      if (pair.value == undefined) pair[1] = ''; 
    2525      parts.push(pair.map(encodeURIComponent).join('=')); 
    26          }); 
    27      
     26    }); 
     27 
    2828    return parts.join('&'); 
    2929  } 
    3030}); 
     
    3232Object.extend(Hash.prototype, Enumerable); 
    3333Object.extend(Hash.prototype, { 
    3434  _each: function(iterator) { 
     35    var cache = []; 
    3536    for (var key in this) { 
    3637      var value = this[key]; 
    3738      if (value && value == Hash.prototype[key]) continue; 
    38        
     39      if (Prototype.BrowserBugs.ForInLoopEnumeratesShadowedProperties) { 
     40        if(cache.include(key)) continue; 
     41        cache.push(key); 
     42      } 
    3943      var pair = [key, value]; 
    4044      pair.key = key; 
    4145      pair.value = value; 
  • src/prototype.js

    old new  
    99      (document.createElement('div').__proto__ !==  
    1010       document.createElement('form').__proto__) 
    1111  }, 
    12    
     12  BrowserBugs: { 
     13    ForInLoopEnumeratesShadowedProperties :function(){ 
     14      var TestObject = function(value) { 
     15        this.key = value; 
     16      }; 
     17      TestObject.prototype.key = 'foo'; 
     18      var instance = new TestObject('bar'); 
     19      var i = 0; 
     20      for (var property in instance) i++; 
     21      return (i != 1); 
     22    }() 
     23  }, 
    1324  ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', 
    1425  emptyFunction: function() {}, 
    1526  K: function(x) { return x } 
  • test/unit/hash.html

    old new  
    104104      assertEqual("color=",                  $H(Fixtures.multiple_all_nil).toQueryString()) 
    105105      assertEqual("color=",                  $H(Fixtures.multiple_empty).toQueryString()) 
    106106       
    107       assertEqual("_each=E&map=M&keys=K&values=V&collect=C&inject=I", Hash.toQueryString(Fixtures.dangerous)) 
     107      assertEnumEqual($w("_each=E map=M keys=K values=V collect=C inject=I").sort(), 
     108        Hash.toQueryString(Fixtures.dangerous).split('&').sort()); 
     109      assertEnumEqual($w('_each=E map=M keys=K values=V collect=C inject=I').sort(), 
     110        $H(Fixtures.dangerous).toQueryString().split('&').sort()); 
    108111    }}, 
    109112     
    110113    testInspect: function(){ with(this) {