data should not add expando unless actually adding data
This commit is contained in:
parent
24ffc395a7
commit
97e134fe80
2 changed files with 45 additions and 20 deletions
36
src/data.js
36
src/data.js
|
@ -13,26 +13,33 @@ jQuery.extend({
|
||||||
|
|
||||||
var id = elem[ expando ], cache = jQuery.cache, thisCache;
|
var id = elem[ expando ], cache = jQuery.cache, thisCache;
|
||||||
|
|
||||||
// Compute a unique ID for the element
|
|
||||||
if(!id) id = elem[ expando ] = ++uuid;
|
|
||||||
|
|
||||||
// Handle the case where there's no name immediately
|
// Handle the case where there's no name immediately
|
||||||
if ( !name ) { return id; }
|
if ( !name ) {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute a unique ID for the element
|
||||||
|
if ( !id ) {
|
||||||
|
id = ++uuid;
|
||||||
|
}
|
||||||
|
|
||||||
// Avoid generating a new cache unless none exists and we
|
// Avoid generating a new cache unless none exists and we
|
||||||
// want to manipulate it.
|
// want to manipulate it.
|
||||||
if( cache[ id ] )
|
if ( cache[ id ] ) {
|
||||||
thisCache = cache[ id ];
|
thisCache = cache[ id ];
|
||||||
else if( typeof data === "undefined" )
|
} else if ( typeof data === "undefined" ) {
|
||||||
thisCache = emptyObject;
|
thisCache = emptyObject;
|
||||||
else
|
} else {
|
||||||
thisCache = cache[ id ] = {};
|
thisCache = cache[ id ] = {};
|
||||||
|
}
|
||||||
|
|
||||||
// Prevent overriding the named cache with undefined values
|
// Prevent overriding the named cache with undefined values
|
||||||
if ( data !== undefined ) thisCache[ name ] = data;
|
if ( data !== undefined ) {
|
||||||
|
elem[ expando ] = id;
|
||||||
|
thisCache[ name ] = data;
|
||||||
|
}
|
||||||
|
|
||||||
if(name === true) return thisCache;
|
return name === true ? thisCache : thisCache[ name ];
|
||||||
else return thisCache[name];
|
|
||||||
},
|
},
|
||||||
|
|
||||||
removeData: function( elem, name ) {
|
removeData: function( elem, name ) {
|
||||||
|
@ -49,8 +56,9 @@ jQuery.extend({
|
||||||
delete thisCache[ name ];
|
delete thisCache[ name ];
|
||||||
|
|
||||||
// If we've removed all the data, remove the element's cache
|
// If we've removed all the data, remove the element's cache
|
||||||
if( jQuery.isEmptyObject(thisCache) )
|
if ( jQuery.isEmptyObject(thisCache) ) {
|
||||||
jQuery.removeData( elem );
|
jQuery.removeData( elem );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, we want to remove all of the element's data
|
// Otherwise, we want to remove all of the element's data
|
||||||
|
@ -58,17 +66,19 @@ jQuery.extend({
|
||||||
// Clean up the element expando
|
// Clean up the element expando
|
||||||
try {
|
try {
|
||||||
delete elem[ expando ];
|
delete elem[ expando ];
|
||||||
} catch(e){
|
} catch( e ) {
|
||||||
// IE has trouble directly removing the expando
|
// IE has trouble directly removing the expando
|
||||||
// but it's ok with using removeAttribute
|
// but it's ok with using removeAttribute
|
||||||
if ( elem.removeAttribute )
|
if ( elem.removeAttribute ) {
|
||||||
elem.removeAttribute( expando );
|
elem.removeAttribute( expando );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Completely remove the data cache
|
// Completely remove the data cache
|
||||||
delete cache[ id ];
|
delete cache[ id ];
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
queue: function( elem, type, data ) {
|
queue: function( elem, type, data ) {
|
||||||
if( !elem ) return;
|
if( !elem ) return;
|
||||||
|
|
||||||
|
|
|
@ -1,31 +1,46 @@
|
||||||
module("data");
|
module("data");
|
||||||
|
|
||||||
test("expando", function(){
|
test("expando", function(){
|
||||||
expect(4);
|
expect(7);
|
||||||
|
|
||||||
equals("expando" in jQuery, true, "jQuery is exposing the expando");
|
equals("expando" in jQuery, true, "jQuery is exposing the expando");
|
||||||
|
|
||||||
var obj = {};
|
var obj = {};
|
||||||
jQuery.data(obj, "foo", "bar");
|
jQuery.data(obj);
|
||||||
|
equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
|
||||||
|
|
||||||
equals(jQuery.expando in obj, true, "jQuery.data added an expando to the object");
|
jQuery.data(obj, true);
|
||||||
|
equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
|
||||||
|
|
||||||
|
jQuery.data(obj, 'test');
|
||||||
|
equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
|
||||||
|
|
||||||
|
jQuery.data(obj, "foo", "bar");
|
||||||
|
equals( jQuery.expando in obj, true, "jQuery.data added an expando to the object" );
|
||||||
|
|
||||||
var id = obj[jQuery.expando];
|
var id = obj[jQuery.expando];
|
||||||
equals( id in jQuery.cache, true, "jQuery.data added an entry to jQuery.cache");
|
equals( id in jQuery.cache, true, "jQuery.data added an entry to jQuery.cache" );
|
||||||
|
|
||||||
equals( jQuery.cache[id].foo, "bar", "jQuery.data worked correctly");
|
equals( jQuery.cache[id].foo, "bar", "jQuery.data worked correctly" );
|
||||||
});
|
});
|
||||||
|
|
||||||
test("jQuery.data", function() {
|
test("jQuery.data", function() {
|
||||||
expect(5);
|
expect(6);
|
||||||
var div = jQuery("#foo")[0];
|
var div = jQuery("#foo")[0];
|
||||||
equals( jQuery.data(div, "test"), undefined, "Check for no data exists" );
|
equals( jQuery.data(div, "test"), undefined, "Check for no data exists" );
|
||||||
|
|
||||||
jQuery.data(div, "test", "success");
|
jQuery.data(div, "test", "success");
|
||||||
equals( jQuery.data(div, "test"), "success", "Check for added data" );
|
equals( jQuery.data(div, "test"), "success", "Check for added data" );
|
||||||
|
|
||||||
|
var data = jQuery.data(div, true);
|
||||||
|
same( data, { "test": "success" }, "Return complete data set" );
|
||||||
|
|
||||||
jQuery.data(div, "test", "overwritten");
|
jQuery.data(div, "test", "overwritten");
|
||||||
equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
|
equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
|
||||||
|
|
||||||
jQuery.data(div, "test", undefined);
|
jQuery.data(div, "test", undefined);
|
||||||
equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
|
equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
|
||||||
|
|
||||||
jQuery.data(div, "test", null);
|
jQuery.data(div, "test", null);
|
||||||
ok( jQuery.data(div, "test") === null, "Check for null data");
|
ok( jQuery.data(div, "test") === null, "Check for null data");
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue