OpenLayers OpenLayers

Ticket #1063: defaults.patch

File defaults.patch, 2.6 kB (added by tschaub, 1 year ago)

work with toString and null property in applyDefaults

  • lib/OpenLayers/Util.js

    old new  
    487487 
    488488/**  
    489489 * Function: applyDefaults 
    490  * Takes a hashtable and copies any keys that don't exist from 
    491  *     another hashtable, by analogy with OpenLayers.Util.extend() from 
     490 * Takes an object and copies any properties that don't exist from 
     491 *     another properties, by analogy with OpenLayers.Util.extend() from 
    492492 *     Prototype.js. 
    493493 *  
    494494 * Parameters: 
    495  * to - {Object} 
    496  * from - {Object} 
     495 * to - {Object} The destination object. 
     496 * from - {Object} The source object.  Any properties of this object that 
     497 *     are undefined in the to object will be set on the to object. 
    497498 */ 
    498499OpenLayers.Util.applyDefaults = function (to, from) { 
    499500    for (var key in from) { 
    500         if (to[key] == null) { 
     501        if (to[key] === undefined || 
     502            (from.hasOwnProperty 
     503             && from.hasOwnProperty(key) && !to.hasOwnProperty(key))) { 
    501504            to[key] = from[key]; 
    502505        } 
    503506    } 
     507    /** 
     508     * IE doesn't include the toString property when iterating over an object's 
     509     * properties with the for(property in object) syntax.  Explicitly check if 
     510     * the source has its own toString property. 
     511     */ 
     512    if(from.hasOwnProperty 
     513       && from.hasOwnProperty('toString') && !to.hasOwnProperty('toString')) { 
     514        to.toString = from.toString; 
     515    } 
    504516}; 
    505517 
    506518/** 
  • tests/test_Util.html

    old new  
    143143 
    144144    function test_06_Util_applyDefaults(t) { 
    145145     
    146         t.plan(4); 
     146        t.plan(6); 
    147147         
    148148        var to = {  
    149149            'a': "abra", 
    150             'b': "blorg" 
     150            'b': "blorg", 
     151            'n': null 
    151152        }; 
    152153 
    153154        var from = {  
    154155            'b': "zoink", 
    155             'c': "press" 
     156            'c': "press", 
     157            'toString': function() {return 'works'}, 
     158            'n': "broken" 
    156159        }; 
    157160 
    158161        OpenLayers.Util.applyDefaults(to, from); 
     
    161164        t.eq( to["a"], "abra", "key present in to but not from maintained"); 
    162165        t.eq( to["b"], "blorg", "key present in to and from, maintained in to"); 
    163166        t.eq( to["c"], "press", "key present in from and not to successfully copied to to"); 
     167        t.eq(to.toString(), "works", "correctly applies custom toString"); 
     168        t.eq(to.n, null, "correctly preserves null"); 
    164169    } 
    165170 
    166171    function test_07_Util_getParameterString(t) {