Skip to content

Commit 0ef97b5

Browse files
gibson042timmywil
authored andcommitted
Core: Restore 1.x isPlainObject constructor checks
- Guard isPlainObject against inherited scalar constructors Fixes jquerygh-2982 Close jquerygh-2985
1 parent b38bee5 commit 0ef97b5

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/core.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,10 @@ jQuery.extend( {
233233
return false;
234234
}
235235

236+
// Not own constructor property must be Object
236237
if ( obj.constructor &&
237-
!hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
238+
!hasOwn.call( obj, "constructor" ) &&
239+
!hasOwn.call( obj.constructor.prototype || {}, "isPrototypeOf" ) ) {
238240
return false;
239241
}
240242

test/unit/core.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,19 @@ QUnit.test( "type for `Symbol`", function( assert ) {
268268
} );
269269

270270
QUnit.asyncTest( "isPlainObject", function( assert ) {
271-
assert.expect( 19 );
271+
272+
assert.expect( 22 );
272273

273274
var pass, iframe, doc, parentObj, childObj, deep,
274275
fn = function() {};
275276

276277
// The use case that we want to match
277278
assert.ok( jQuery.isPlainObject( {} ), "{}" );
278279
assert.ok( jQuery.isPlainObject( new window.Object() ), "new Object" );
280+
assert.ok( jQuery.isPlainObject( { constructor: fn } ),
281+
"plain object with constructor property" );
282+
assert.ok( jQuery.isPlainObject( { constructor: "foo" } ),
283+
"plain object with primitive constructor property" );
279284

280285
parentObj = { foo: "bar" };
281286
childObj = Object.create( parentObj );
@@ -310,6 +315,10 @@ QUnit.asyncTest( "isPlainObject", function( assert ) {
310315
// Again, instantiated objects shouldn't be matched
311316
assert.ok( !jQuery.isPlainObject( new fn() ), "new fn" );
312317

318+
// Instantiated objects with primitive constructors shouldn't be matched
319+
fn.prototype.constructor = "foo";
320+
assert.ok( !jQuery.isPlainObject( new fn() ), "new fn with primitive constructor" );
321+
313322
// Deep object
314323
deep = { "foo": { "baz": true }, "foo2": document };
315324
assert.ok( jQuery.isPlainObject( deep ), "Object with objects is still plain" );

0 commit comments

Comments
 (0)