Commit ed093d36 authored by Eugene Lazutkin's avatar Eugene Lazutkin

Porting dojo.clone() changes from the trunk. Refs #5284.

git-svn-id: http://svn.dojotoolkit.org/src/branches/1.0/dojo/trunk@11775 560b804f-0ae3-0310-86f3-f6aa0a117693
parent 4dad74b4
......@@ -214,20 +214,24 @@ dojo.clone = function(/*anything*/ o){
r.push(dojo.clone(o[i]));
}
return r; // Array
}else if(dojo.isObject(o)){
if(o.nodeType && o.cloneNode){ // isNode
return o.cloneNode(true); // Node
}else{
var r = new o.constructor(); // specific to dojo.declare()'d classes!
for(var i in o){
if(!(i in r) || r[i] != o[i]){
r[i] = dojo.clone(o[i]);
}
}
return r; // Object
}
if(!dojo.isObject(o)){
return o; /*anything*/
}
if(o.nodeType && o.cloneNode){ // isNode
return o.cloneNode(true); // Node
}
if(o instanceof Date){
return new Date(o.getTime()); // Date
}
// Generic objects
var r = new o.constructor(); // specific to dojo.declare()'d classes!
for(var i in o){
if(!(i in r) || r[i] != o[i]){
r[i] = dojo.clone(o[i]);
}
}
return o; /*anything*/
return r; // Object
}
dojo.trim = function(/*String*/ str){
......
......@@ -151,6 +151,26 @@ tests.register("tests._base.lang",
}
var obj2 = thinger.apply(this, obj1);
t.assertEqual(obj1[0], obj2[0]);
}
},
function clone(t) {
var obj1 = {foo: 'bar', answer: 42, jan102007: new Date(2007, 0, 10),
baz: {
a: null,
b: [
1, "b", 2.3, true, false
//, function(){ return 4; }, /\d+/gm
]
}
};
var obj2 = dojo.clone(obj1);
t.assertEqual(obj1.foo, obj2.foo);
t.assertEqual(obj1.answer, obj2.answer);
t.assertEqual(obj1.jan102007, obj2.jan102007);
t.assertEqual(obj1.baz.a, obj2.baz.a);
for(var i = 0; i < obj1.baz.b.length; ++i){
t.assertEqual(obj1.baz.b[i], obj2.baz.b[i]);
}
}
]
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment