More unit tests and a $.hasData that works for JS objects too.
This commit is contained in:
parent
f5d4bf8920
commit
e199ead4cb
2 changed files with 17 additions and 7 deletions
|
@ -22,7 +22,11 @@ jQuery.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
hasData: function( elem ) {
|
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 ) {
|
data: function( elem, name, data ) {
|
||||||
|
|
|
@ -79,12 +79,18 @@ test("jQuery.data", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("jQuery.hasData", function() {
|
test("jQuery.hasData", function() {
|
||||||
var div = document.createElement( "div" );
|
expect(6);
|
||||||
equals( jQuery.hasData(div), false, "No data exists" );
|
|
||||||
jQuery.data( div, "foo", "bar" );
|
function testData(obj) {
|
||||||
equals( jQuery.hasData(div), true, "Data exists" );
|
equals( jQuery.hasData(obj), false, "No data exists" );
|
||||||
jQuery.removeData( div, "foo" );
|
jQuery.data( obj, "foo", "bar" );
|
||||||
equals( jQuery.hasData(div), false, "Data was removed" );
|
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() {
|
test(".data()", function() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue