From 2ee54e9ad274b1b06bb9b2ece390d6817b3d748a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82=C4=99biowski-Owczarek?= Date: Mon, 13 Apr 2020 09:01:45 +0200 Subject: [PATCH] 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 --- src/data/Data.js | 2 +- test/unit/data.js | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/data/Data.js b/src/data/Data.js index ce6d8fa9ba..31ff4318c1 100644 --- a/src/data/Data.js +++ b/src/data/Data.js @@ -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. diff --git a/test/unit/data.js b/test/unit/data.js index 55879415fa..3a2047cff5 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -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( "
" ); + 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( "
" ); - // 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() ); } ); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy