]> source.dussan.org Git - jquery.git/commitdiff
Fixes #13815: Ensure each element has its own private data object - Tests by @rwldrn
authorCorey Frang <gnarf@gnarf.net>
Wed, 24 Apr 2013 20:07:15 +0000 (16:07 -0400)
committerCorey Frang <gnarf@gnarf.net>
Wed, 24 Apr 2013 21:09:30 +0000 (17:09 -0400)
src/data.js
src/manipulation.js
test/unit/data.js

index 0fd35033acad6c1991f00e7aec453ff3751961a8..344dae25fb5be47bb68d21e1856c066276a622a3 100644 (file)
@@ -90,13 +90,9 @@ Data.prototype = {
 
                // Handle: [ owner, { properties } ] args
                } else {
-                       // Support an expectation from the old data system where plain
-                       // objects used to initialize would be set to the cache by
-                       // reference, instead of having properties and values copied.
-                       // Note, this will kill the connection between
-                       // "this.cache[ unlock ]" and "cache"
+                       // Fresh assignments by object are shallow copied
                        if ( jQuery.isEmptyObject( cache ) ) {
-                               this.cache[ unlock ] = data;
+                               jQuery.extend( this.cache[ unlock ], data );
                        // Otherwise, copy the properties one-by-one to the cache object
                        } else {
                                for ( prop in data ) {
@@ -104,6 +100,7 @@ Data.prototype = {
                                }
                        }
                }
+               return cache;
        },
        get: function( owner, key ) {
                // Either a valid cache is found, or will be created.
index e323a10ec700d4805ab4af0bf622402398ec4247..55d6e851058e424a0aba26c4ba1447f1fdb4f6b0 100644 (file)
@@ -527,11 +527,9 @@ function cloneCopyEvent( src, dest ) {
        // 1. Copy private data: events, handlers, etc.
        if ( data_priv.hasData( src ) ) {
                pdataOld = data_priv.access( src );
-               pdataCur = jQuery.extend( {}, pdataOld );
+               pdataCur = data_priv.set( dest, pdataOld );
                events = pdataOld.events;
 
-               data_priv.set( dest, pdataCur );
-
                if ( events ) {
                        delete pdataCur.handle;
                        pdataCur.events = {};
index a0361504af9ca9b495bf6349cb2a155a1173467c..c2cfe3f3fc8d4649f3e62936fe1f91b1e6c92290 100644 (file)
@@ -244,6 +244,18 @@ test("jQuery(plain Object).data(String, Object).data(String)", function() {
        deepEqual( $obj[0], { exists: true }, "removeData does not clear the object" );
 });
 
+test(".data(object) does not retain references. #13815", function() {
+       expect( 2 );
+
+       var $divs = jQuery("<div></div><div></div>").appendTo("#qunit-fixture");
+
+       $divs.data({ "type": "foo" });
+       $divs.eq( 0 ).data( "type", "bar" );
+
+       equal( $divs.eq( 0 ).data("type"), "bar", "Correct updated value" );
+       equal( $divs.eq( 1 ).data("type"), "foo", "Original value retained" );
+});
+
 test("data-* attributes", function() {
        expect(40);
        var prop, i, l, metadata, elem,