]> source.dussan.org Git - jquery.git/commitdiff
More unit tests and a $.hasData that works for JS objects too.
authorColin Snover <github.com@zetafleet.com>
Wed, 22 Dec 2010 21:03:01 +0000 (15:03 -0600)
committerColin Snover <github.com@zetafleet.com>
Wed, 22 Dec 2010 21:03:01 +0000 (15:03 -0600)
src/data.js
test/unit/data.js

index c3530c835f731a7d34336c53affa4cb39557832d..549e73ed4e074dfea6b0963918901d6bd5421f07 100644 (file)
@@ -22,7 +22,11 @@ jQuery.extend({
        },
 
        hasData: function( elem ) {
-               return !elem.nodeType || (!!elem[ jQuery.expando ] && !jQuery.isEmptyObject(jQuery.cache[ elem[jQuery.expando] ]));
+               if (elem.nodeType) {
+                       elem = jQuery.cache[ elem[jQuery.expando] ];
+               }
+
+               return !!elem && !jQuery.isEmptyObject(elem);
        },
 
        data: function( elem, name, data ) {
index 204d979c345787795ec7135adfcb62716ea414ba..310cd6bc4a8ed6ba7ba5f7096872497ce094ec3c 100644 (file)
@@ -79,12 +79,18 @@ test("jQuery.data", function() {
 });
 
 test("jQuery.hasData", function() {
-       var div = document.createElement( "div" );
-       equals( jQuery.hasData(div), false, "No data exists" );
-       jQuery.data( div, "foo", "bar" );
-       equals( jQuery.hasData(div), true, "Data exists" );
-       jQuery.removeData( div, "foo" );
-       equals( jQuery.hasData(div), false, "Data was removed" );
+       expect(6);
+
+       function testData(obj) {
+               equals( jQuery.hasData(obj), false, "No data exists" );
+               jQuery.data( obj, "foo", "bar" );
+               equals( jQuery.hasData(obj), true, "Data exists" );
+               jQuery.removeData( obj, "foo" );
+               equals( jQuery.hasData(obj), false, "Data was removed" );
+       }
+
+       testData(document.createElement('div'));
+       testData({});
 });
 
 test(".data()", function() {