Skip to content
Permalink
Browse files
Data: Make the data object a regular object again
The change in gh-4603 made the object returned by `elem.data()`
a prototype-less object. That's a desired change to support keys
colliding with `Object.prototype` properties but it's also a breaking
change so it has to wait for jQuery 4.0.0.

A 3.x-only test was added to avoid breaking it in the future on this
branch.

Fixes gh-4665
Ref gh-4603
Closes gh-4666
  • Loading branch information
mgol committed Apr 20, 2020
1 parent 763dd3f commit 65e909844c2d064606217b47e92eff12ebdb79de
Showing 2 changed files with 13 additions and 10 deletions.
@@ -22,7 +22,7 @@ Data.prototype = {

// If not, create one
if ( !value ) {
value = Object.create( null );
value = {};

// We can accept data for non-element nodes in modern browsers,
// but we should not, see #8335.
@@ -990,17 +990,20 @@ QUnit.test( ".data(prop) does not create expando", function( assert ) {
}
} );

QUnit.test( "keys matching Object.prototype properties (gh-3256)", function( assert ) {
assert.expect( 2 );
QUnit.test( ".data() returns a regular object (jQuery <4 only, gh-4665)", function( assert ) {
assert.expect( 4 );

var div = jQuery( "<div></div>" );
function verifyRegularObject( assert, object ) {
assert.strictEqual( object.hasOwnProperty, Object.prototype.hasOwnProperty,
"Data object has the hasOwnProperty method" );
assert.strictEqual( object + "", "[object Object]",
"Data object can be stringified" );
}

assert.strictEqual( div.data( "hasOwnProperty" ), undefined,
"hasOwnProperty not matched (before forced data creation)" );
var elem = jQuery( "<div></div>" );

// Force the creation of a data object for this element.
div.data( { foo: "bar" } );
verifyRegularObject( assert, elem.data() );

assert.strictEqual( div.data( "hasOwnProperty" ), undefined,
"hasOwnProperty not matched (after forced data creation)" );
elem.data( "foo", "bar" );
verifyRegularObject( assert, elem.data() );
} );

4 comments on commit 65e9098

@Miro696
Copy link

@Miro696 Miro696 commented on 65e9098 Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.5.0 work correctly with animate on html and body but 3,5.1 throw error like animate( ) is not a function ex $(*html, body).animate( {
scrollTop: 100, }, 500)

@mgol
Copy link
Member Author

@mgol mgol commented on 65e9098 Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Miro696 comments under a commit are not the best place to report bugs, please do so in a regular way. Before you report one, though, make sure you have a reproducible test case.

@Miro696
Copy link

@Miro696 Miro696 commented on 65e9098 Sep 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Miro696
Copy link

@Miro696 Miro696 commented on 65e9098 Sep 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.