aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFilipe Fortes <filipe@fortes.com>2010-01-05 05:25:14 +0800
committerJohn Resig <jeresig@gmail.com>2010-01-07 00:11:22 +0800
commit0d1a2c1b1145ad17bec061a231b8426b3424d144 (patch)
treea0f0907f06a3d641b381aa5f5e5532c943a9c6ef
parent6861b5d4eb16222ed5ea623af6ce75362b55d1d4 (diff)
downloadjquery-0d1a2c1b1145ad17bec061a231b8426b3424d144.tar.gz
jquery-0d1a2c1b1145ad17bec061a231b8426b3424d144.zip
Make sure to do a deep copy on arrays. #5750
-rw-r--r--src/core.js8
-rw-r--r--test/unit/core.js9
2 files changed, 11 insertions, 6 deletions
diff --git a/src/core.js b/src/core.js
index 975d23c0d..58cbbc7e5 100644
--- a/src/core.js
+++ b/src/core.js
@@ -316,10 +316,10 @@ jQuery.extend = jQuery.fn.extend = function() {
continue;
}
- // Recurse if we're merging object literal values
- if ( deep && copy && jQuery.isPlainObject(copy) ) {
- // Don't extend not object literals
- var clone = src && jQuery.isPlainObject(src) ? src : {};
+ // Recurse if we're merging object literal values or arrays
+ if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
+ var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
+ : jQuery.isArray(copy) ? [] : {};
// Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy );
diff --git a/test/unit/core.js b/test/unit/core.js
index d50997b2c..990926d04 100644
--- a/test/unit/core.js
+++ b/test/unit/core.js
@@ -643,7 +643,7 @@ test("jQuery.merge()", function() {
});
test("jQuery.extend(Object, Object)", function() {
- expect(25);
+ expect(27);
var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
@@ -653,7 +653,9 @@ test("jQuery.extend(Object, Object)", function() {
deep1copy = { foo: { bar: true } },
deep2 = { 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);
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" );
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 optionsWithLength = { foo: { length: -1 } };
jQuery.extend(true, empty, optionsWithLength);