return;
}
- var thisCache, ret,
+ var ret, thisCache,
internalKey = jQuery.expando,
- getByName = typeof name === "string",
// We have to handle DOM nodes and JS objects differently because IE6-7
// can't GC object references properly across the DOM-JS boundary
// Avoid doing any more work than we need to when trying to get data on an
// object that has no data at all
- if ( (!id || !cache[id] || (!pvt && !cache[id].data)) && getByName && data === undefined ) {
+ if ( (!id || !cache[id] || (!pvt && !cache[id].data)) && data === undefined && typeof name === "string" ) {
return;
}
// Only DOM nodes need a new unique ID for each element since their data
// ends up in the global cache
if ( isNode ) {
- elem[ internalKey ] = id = core_deletedIds.pop() || jQuery.guid++;
+ id = elem[ internalKey ] = core_deletedIds.pop() || jQuery.guid++;
} else {
id = internalKey;
}
}
if ( !cache[ id ] ) {
- cache[ id ] = {};
-
- // Avoids exposing jQuery metadata on plain JS objects when the object
+ // Avoid exposing jQuery metadata on plain JS objects when the object
// is serialized using JSON.stringify
- if ( !isNode ) {
- cache[ id ].toJSON = jQuery.noop;
- }
+ cache[ id ] = isNode ? {} : { toJSON: jQuery.noop };
}
// An object can be passed to jQuery.data instead of a key/value pair; this gets
// Check for both converted-to-camel and non-converted data property names
// If a data property was specified
- if ( getByName ) {
+ if ( typeof name === "string" ) {
// First Try to find as-is property data
ret = thisCache[ name ];
return;
}
- var i, l, thisCache,
+ var thisCache, i,
isNode = elem.nodeType,
// See jQuery.data for more information
name = name.concat( jQuery.map( name, jQuery.camelCase ) );
}
- for ( i = 0, l = name.length; i < l; i++ ) {
+ i = name.length;
+ while ( i-- ) {
delete thisCache[ name[i] ];
}
// If there is no data left in the cache, we want to continue
// and let the cache object itself get destroyed
- if ( !( pvt ? isEmptyDataObject : jQuery.isEmptyObject )( thisCache ) ) {
+ if ( pvt ? !isEmptyDataObject(thisCache) : !jQuery.isEmptyObject(thisCache) ) {
return;
}
}
// The following elements throw uncatchable exceptions if you
// attempt to add expando properties to them.
noData: {
+ "applet": true,
"embed": true,
// Ban all objects except for Flash (which handle expandos)
- "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
- "applet": true
+ "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
},
hasData: function( elem ) {
jQuery.fn.extend({
data: function( key, value ) {
var attrs, name,
- elem = this[0],
+ data = null,
i = 0,
- data = null;
+ elem = this[0];
+
+ // Special expections of .data basically thwart jQuery.access,
+ // so implement the relevant behavior ourselves
// Gets all values
if ( key === undefined ) {
for ( ; i < attrs.length; i++ ) {
name = attrs[i].name;
- if ( !name.indexOf( "data-" ) ) {
+ if ( name.indexOf("data-") === 0 ) {
name = jQuery.camelCase( name.slice(5) );
dataAttr( elem, name, data[ name ] );
});
}
- return jQuery.access( this, function( value ) {
-
- if ( value === undefined ) {
- // Try to fetch any internally stored data first
- return elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : null;
- }
+ return arguments.length > 1 ?
+ // Sets one value
this.each(function() {
jQuery.data( this, key, value );
- });
- }, null, value, arguments.length > 1, null, true );
+ }) :
+
+ // Gets one value
+ // Try to fetch any internally stored data first
+ elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : null;
},
removeData: function( key ) {