Skip to content
Permalink
Browse files

Fix #1907 where the never-ending loop prevention used a coersion comp…

…arison which sometimes dropped values incorrectly. Also fixed a bug where on deep copies the target copied over itself (i = 2 addition). Last made code handle the case when a property might have a string in it that should be overwritten by an object.
  • Loading branch information
davids549 committed Nov 17, 2007
1 parent 6853370 commit bf8f3fe0942cb3d2583f0965e44594088a38ccef
Showing with 20 additions and 3 deletions.
  1. +8 −2 src/core.js
  2. +12 −1 test/unit/core.js
@@ -516,8 +516,14 @@ jQuery.extend = jQuery.fn.extend = function() {
if ( target.constructor == Boolean ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}

// Handle case when target is a string or something (possible in deep copy)
if ( typeof target != "object" )
target = {};

// extend jQuery itself if only one argument is passed
if ( length == 1 ) {
target = this;
@@ -530,12 +536,12 @@ jQuery.extend = jQuery.fn.extend = function() {
// Extend the base object
for ( var name in options ) {
// Prevent never-ending loop
if ( target == options[ name ] )
if ( target === options[ name ] )
continue;

// Recurse if we're merging object values
if ( deep && typeof options[ name ] == "object" && target[ name ] && !options[ name ].nodeType )
jQuery.extend( target[ name ], options[ name ] );
target[ name ] = jQuery.extend( target[ name ], options[ name ] );

// Don't bring in undefined values
else if ( options[ name ] != undefined )
@@ -811,7 +811,7 @@ test("is(String)", function() {
});

test("$.extend(Object, Object)", function() {
expect(11);
expect(14);

var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
@@ -836,6 +836,17 @@ test("$.extend(Object, Object)", function() {
isObj( 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" );

var target = {};
var recursive = { foo:target, bar:5 };
jQuery.extend(true, target, recursive);
isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );

var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
ok( ret.foo.length == 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );

var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );

var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
options1 = { xnumber2: 1, xstring2: "x" },

0 comments on commit bf8f3fe

Please sign in to comment.
You can’t perform that action at this time.