Core: Restore 1.x isPlainObject constructor checks

- Guard isPlainObject against inherited scalar constructors

Fixes gh-2982
Close gh-2985
This commit is contained in:
Richard Gibson 2016-03-11 10:48:00 -05:00 committed by Timmy Willison
parent b38bee5ba3
commit 0ef97b5939
2 changed files with 13 additions and 2 deletions

View File

@ -233,8 +233,10 @@ jQuery.extend( {
return false; return false;
} }
// Not own constructor property must be Object
if ( obj.constructor && if ( obj.constructor &&
!hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) { !hasOwn.call( obj, "constructor" ) &&
!hasOwn.call( obj.constructor.prototype || {}, "isPrototypeOf" ) ) {
return false; return false;
} }

View File

@ -268,7 +268,8 @@ QUnit.test( "type for `Symbol`", function( assert ) {
} ); } );
QUnit.asyncTest( "isPlainObject", function( assert ) { QUnit.asyncTest( "isPlainObject", function( assert ) {
assert.expect( 19 );
assert.expect( 22 );
var pass, iframe, doc, parentObj, childObj, deep, var pass, iframe, doc, parentObj, childObj, deep,
fn = function() {}; fn = function() {};
@ -276,6 +277,10 @@ QUnit.asyncTest( "isPlainObject", function( assert ) {
// The use case that we want to match // The use case that we want to match
assert.ok( jQuery.isPlainObject( {} ), "{}" ); assert.ok( jQuery.isPlainObject( {} ), "{}" );
assert.ok( jQuery.isPlainObject( new window.Object() ), "new Object" ); assert.ok( jQuery.isPlainObject( new window.Object() ), "new Object" );
assert.ok( jQuery.isPlainObject( { constructor: fn } ),
"plain object with constructor property" );
assert.ok( jQuery.isPlainObject( { constructor: "foo" } ),
"plain object with primitive constructor property" );
parentObj = { foo: "bar" }; parentObj = { foo: "bar" };
childObj = Object.create( parentObj ); childObj = Object.create( parentObj );
@ -310,6 +315,10 @@ QUnit.asyncTest( "isPlainObject", function( assert ) {
// Again, instantiated objects shouldn't be matched // Again, instantiated objects shouldn't be matched
assert.ok( !jQuery.isPlainObject( new fn() ), "new fn" ); assert.ok( !jQuery.isPlainObject( new fn() ), "new fn" );
// Instantiated objects with primitive constructors shouldn't be matched
fn.prototype.constructor = "foo";
assert.ok( !jQuery.isPlainObject( new fn() ), "new fn with primitive constructor" );
// Deep object // Deep object
deep = { "foo": { "baz": true }, "foo2": document }; deep = { "foo": { "baz": true }, "foo2": document };
assert.ok( jQuery.isPlainObject( deep ), "Object with objects is still plain" ); assert.ok( jQuery.isPlainObject( deep ), "Object with objects is still plain" );