]> source.dussan.org Git - jquery.git/commitdiff
Avoid side-effects when calling jQuery.hasData
authorRick Waldron <waldron.rick@gmail.com>
Mon, 1 Apr 2013 16:48:30 +0000 (12:48 -0400)
committerRick Waldron <waldron.rick@gmail.com>
Mon, 1 Apr 2013 16:48:30 +0000 (12:48 -0400)
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
src/data.js
test/unit/data.js

index 585d78b20912dde84045ad90434c95fd52bec6bc..189de6259b2d603118f7ab9a79aa15ee4fc5567c 100644 (file)
@@ -21,11 +21,17 @@ function Data() {
 Data.uid = 1;
 
 Data.prototype = {
-       key: function( owner ) {
+       key: function( owner, options ) {
                var descriptor = {},
                        // Check if the owner object already has a cache key
                        unlock = owner[ this.expando ];
 
+               // `readonly` calls from hasData, on owners with no key
+               // should not create new/empty cache records
+               if ( !unlock && (options && options.readonly) ) {
+                       return null;
+               }
+
                // If not, create one
                if ( !unlock ) {
                        unlock = Data.uid++;
@@ -158,7 +164,7 @@ Data.prototype = {
        },
        hasData: function( owner ) {
                return !jQuery.isEmptyObject(
-                       this.cache[ this.key( owner ) ]
+                       this.cache[ this.key( owner, { readonly: true }) ] || {}
                );
        },
        discard: function( owner ) {
index d813ec82dd1f6e6df0d02301127501b7618d74a7..2f9c25ed5f276c4ff6588ac5b47eff54710ba691 100644 (file)
@@ -51,6 +51,17 @@ test( "jQuery._data & _removeData, expected returns", function() {
        );
 });
 
+test( "jQuery.hasData no side effects", function() {
+       expect(1);
+       var obj = {};
+
+       jQuery.hasData( obj );
+
+       equal( Object.getOwnPropertyNames( obj ).length, 0,
+               "No data expandos where added when calling jQuery.hasData(o)"
+       );
+});
+
 function dataTests (elem) {
        var oldCacheLength, dataObj, internalDataObj, expected, actual;