Ensured that arrays are not considered same as plain object on deep extending. Fixes #5991.
This commit is contained in:
parent
2f603359fe
commit
694b625c92
15
src/core.js
15
src/core.js
|
@ -306,7 +306,7 @@ jQuery.fn.init.prototype = jQuery.fn;
|
||||||
|
|
||||||
jQuery.extend = jQuery.fn.extend = function() {
|
jQuery.extend = jQuery.fn.extend = function() {
|
||||||
// copy reference to target object
|
// copy reference to target object
|
||||||
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
|
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy, copyIsArray;
|
||||||
|
|
||||||
// Handle a deep copy situation
|
// Handle a deep copy situation
|
||||||
if ( typeof target === "boolean" ) {
|
if ( typeof target === "boolean" ) {
|
||||||
|
@ -340,10 +340,15 @@ jQuery.extend = jQuery.fn.extend = function() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recurse if we're merging object literal values or arrays
|
// Recurse if we're merging plain objects or arrays
|
||||||
if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
|
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
|
||||||
var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
|
if ( copyIsArray ) {
|
||||||
: jQuery.isArray(copy) ? [] : {};
|
copyIsArray = false;
|
||||||
|
clone = src && jQuery.isArray(src) ? src : [];
|
||||||
|
|
||||||
|
} else {
|
||||||
|
clone = src && jQuery.isPlainObject(src) ? src : {};
|
||||||
|
}
|
||||||
|
|
||||||
// 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 );
|
||||||
|
|
|
@ -661,7 +661,7 @@ test("jQuery.merge()", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("jQuery.extend(Object, Object)", function() {
|
test("jQuery.extend(Object, Object)", function() {
|
||||||
expect(27);
|
expect(26);
|
||||||
|
|
||||||
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" },
|
||||||
|
@ -688,7 +688,6 @@ 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" );
|
ok( jQuery.extend(true, {}, nestedarray).arr !== arr, "Deep extend of object must clone child array" );
|
||||||
|
|
||||||
var empty = {};
|
var empty = {};
|
||||||
|
|
Loading…
Reference in a new issue