data should not add expando unless actually adding data
authorBrandon Aaron <brandon.aaron@gmail.com>
Tue, 15 Sep 2009 21:14:08 +0000 (21:14 +0000)
committerBrandon Aaron <brandon.aaron@gmail.com>
Tue, 15 Sep 2009 21:14:08 +0000 (21:14 +0000)
src/data.js
test/unit/data.js

index c18e73c..085db0b 100644 (file)
@@ -13,26 +13,33 @@ jQuery.extend({
 \r
                var id = elem[ expando ], cache = jQuery.cache, thisCache;\r
 \r
-               // Compute a unique ID for the element\r
-               if(!id) id = elem[ expando ] = ++uuid;\r
-\r
                // Handle the case where there's no name immediately\r
-               if ( !name ) { return id; }\r
+               if ( !name ) {\r
+                       return id;\r
+               }\r
+\r
+               // Compute a unique ID for the element\r
+               if ( !id ) { \r
+                       id = ++uuid;\r
+               }\r
 \r
                // Avoid generating a new cache unless none exists and we\r
                // want to manipulate it.\r
-               if( cache[ id ] )\r
+               if ( cache[ id ] ) {\r
                        thisCache = cache[ id ];\r
-               else if( typeof data === "undefined" )\r
+               } else if ( typeof data === "undefined" ) {\r
                        thisCache = emptyObject;\r
-               else\r
+               } else {\r
                        thisCache = cache[ id ] = {};\r
+               }\r
                \r
                // Prevent overriding the named cache with undefined values\r
-               if ( data !== undefined ) thisCache[ name ] = data;\r
-\r
-               if(name === true) return thisCache;\r
-               else return thisCache[name];\r
+               if ( data !== undefined ) {\r
+                       elem[ expando ] = id;\r
+                       thisCache[ name ] = data;\r
+               }\r
+               \r
+               return name === true ? thisCache : thisCache[ name ];\r
        },\r
 \r
        removeData: function( elem, name ) {\r
@@ -49,8 +56,9 @@ jQuery.extend({
                                delete thisCache[ name ];\r
 \r
                                // If we've removed all the data, remove the element's cache\r
-                               if( jQuery.isEmptyObject(thisCache) )\r
+                               if ( jQuery.isEmptyObject(thisCache) ) {\r
                                        jQuery.removeData( elem );\r
+                               }\r
                        }\r
 \r
                // Otherwise, we want to remove all of the element's data\r
@@ -58,17 +66,19 @@ jQuery.extend({
                        // Clean up the element expando\r
                        try {\r
                                delete elem[ expando ];\r
-                       } catch(e){\r
+                       } catch( e ) {\r
                                // IE has trouble directly removing the expando\r
                                // but it's ok with using removeAttribute\r
-                               if ( elem.removeAttribute )\r
+                               if ( elem.removeAttribute ) {\r
                                        elem.removeAttribute( expando );\r
+                               }\r
                        }\r
 \r
                        // Completely remove the data cache\r
                        delete cache[ id ];\r
                }\r
        },\r
+       \r
        queue: function( elem, type, data ) {\r
                if( !elem ) return;\r
 \r
index 46e46ed..6a367f7 100644 (file)
@@ -1,31 +1,46 @@
 module("data");\r
 \r
 test("expando", function(){\r
-       expect(4);\r
+       expect(7);\r
        \r
        equals("expando" in jQuery, true, "jQuery is exposing the expando");\r
        \r
        var obj = {};\r
+       jQuery.data(obj);\r
+       equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );\r
+       \r
+       jQuery.data(obj, true);\r
+       equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );\r
+       \r
+       jQuery.data(obj, 'test');\r
+       equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );\r
+       \r
        jQuery.data(obj, "foo", "bar");\r
-\r
-       equals(jQuery.expando in obj, true, "jQuery.data added an expando to the object");      \r
+       equals( jQuery.expando in obj, true, "jQuery.data added an expando to the object" );\r
        \r
        var id = obj[jQuery.expando];\r
-       equals( id in jQuery.cache, true, "jQuery.data added an entry to jQuery.cache");\r
+       equals( id in jQuery.cache, true, "jQuery.data added an entry to jQuery.cache" );\r
        \r
-       equals( jQuery.cache[id].foo, "bar", "jQuery.data worked correctly");\r
+       equals( jQuery.cache[id].foo, "bar", "jQuery.data worked correctly" );\r
 });\r
 \r
 test("jQuery.data", function() {\r
-       expect(5);\r
+       expect(6);\r
        var div = jQuery("#foo")[0];\r
        equals( jQuery.data(div, "test"), undefined, "Check for no data exists" );\r
+       \r
        jQuery.data(div, "test", "success");\r
        equals( jQuery.data(div, "test"), "success", "Check for added data" );\r
+       \r
+       var data = jQuery.data(div, true);\r
+       same( data, { "test": "success" }, "Return complete data set" );\r
+       \r
        jQuery.data(div, "test", "overwritten");\r
        equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );\r
+       \r
        jQuery.data(div, "test", undefined);\r
        equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");\r
+       \r
        jQuery.data(div, "test", null);\r
        ok( jQuery.data(div, "test") === null, "Check for null data");\r
 });\r