Ticket #1063: defaults.patch
| File defaults.patch, 2.6 kB (added by tschaub, 1 year ago) |
|---|
-
lib/OpenLayers/Util.js
old new 487 487 488 488 /** 489 489 * Function: applyDefaults 490 * Takes a hashtable and copies any keys that don't exist from491 * another hashtable, by analogy with OpenLayers.Util.extend() from490 * Takes an object and copies any properties that don't exist from 491 * another properties, by analogy with OpenLayers.Util.extend() from 492 492 * Prototype.js. 493 493 * 494 494 * 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. 497 498 */ 498 499 OpenLayers.Util.applyDefaults = function (to, from) { 499 500 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))) { 501 504 to[key] = from[key]; 502 505 } 503 506 } 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 } 504 516 }; 505 517 506 518 /** -
tests/test_Util.html
old new 143 143 144 144 function test_06_Util_applyDefaults(t) { 145 145 146 t.plan( 4);146 t.plan(6); 147 147 148 148 var to = { 149 149 'a': "abra", 150 'b': "blorg" 150 'b': "blorg", 151 'n': null 151 152 }; 152 153 153 154 var from = { 154 155 'b': "zoink", 155 'c': "press" 156 'c': "press", 157 'toString': function() {return 'works'}, 158 'n': "broken" 156 159 }; 157 160 158 161 OpenLayers.Util.applyDefaults(to, from); … … 161 164 t.eq( to["a"], "abra", "key present in to but not from maintained"); 162 165 t.eq( to["b"], "blorg", "key present in to and from, maintained in to"); 163 166 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"); 164 169 } 165 170 166 171 function test_07_Util_getParameterString(t) {
