Merge branch 'master' of github.com:jquery/jquery

This commit is contained in:
jeresig 2010-01-06 11:32:39 -05:00
commit e424e01c47
2 changed files with 11 additions and 6 deletions

View file

@ -316,10 +316,10 @@ jQuery.extend = jQuery.fn.extend = function() {
continue; continue;
} }
// Recurse if we're merging object literal values // Recurse if we're merging object literal values or arrays
if ( deep && copy && jQuery.isPlainObject(copy) ) { if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
// Don't extend not object literals var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
var clone = src && jQuery.isPlainObject(src) ? src : {}; : jQuery.isArray(copy) ? [] : {};
// Never move original objects, clone them // Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy ); target[ name ] = jQuery.extend( deep, clone, copy );

View file

@ -643,7 +643,7 @@ test("jQuery.merge()", function() {
}); });
test("jQuery.extend(Object, Object)", function() { test("jQuery.extend(Object, Object)", function() {
expect(25); expect(27);
var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
options = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
@ -653,7 +653,9 @@ test("jQuery.extend(Object, Object)", function() {
deep1copy = { foo: { bar: true } }, deep1copy = { foo: { bar: true } },
deep2 = { foo: { baz: true }, foo2: document }, deep2 = { foo: { baz: true }, foo2: document },
deep2copy = { foo: { baz: true }, foo2: document }, deep2copy = { foo: { baz: true }, foo2: document },
deepmerged = { foo: { bar: true, baz: true }, foo2: document }; deepmerged = { foo: { bar: true, baz: true }, foo2: document },
arr = [1, 2, 3],
nestedarray = { arr: arr };
jQuery.extend(settings, options); jQuery.extend(settings, options);
same( settings, merged, "Check if extended: settings must be extended" ); same( settings, merged, "Check if extended: settings must be extended" );
@ -668,6 +670,9 @@ test("jQuery.extend(Object, Object)", function() {
same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" ); same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" ); equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
ok( jQuery.extend(true, [], arr) !== arr, "Deep extend of array must clone array" );
ok( jQuery.extend(true, {}, nestedarray).arr !== arr, "Deep extend of object must clone child array" );
var empty = {}; var empty = {};
var optionsWithLength = { foo: { length: -1 } }; var optionsWithLength = { foo: { length: -1 } };
jQuery.extend(true, empty, optionsWithLength); jQuery.extend(true, empty, optionsWithLength);