From 941d2e055bdc6e7694e12381a97c4ddf81c8d9b4 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 12 Feb 2013 00:45:35 -0500 Subject: [PATCH] No ticket: keep jquery.min.js.gz under 32K --- src/data.js | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/data.js b/src/data.js index e471df4ea..5861c5cb4 100644 --- a/src/data.js +++ b/src/data.js @@ -6,9 +6,8 @@ function internalData( elem, name, data, pvt /* Internal Use Only */ ){ 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 @@ -24,7 +23,7 @@ function internalData( elem, name, data, pvt /* Internal Use Only */ ){ // 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; } @@ -32,20 +31,16 @@ function internalData( elem, name, data, pvt /* Internal Use Only */ ){ // 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 @@ -77,7 +72,7 @@ function internalData( elem, name, data, pvt /* Internal Use Only */ ){ // 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 ]; @@ -100,7 +95,7 @@ function internalRemoveData( elem, name, pvt ) { return; } - var i, l, thisCache, + var thisCache, i, isNode = elem.nodeType, // See jQuery.data for more information @@ -145,13 +140,14 @@ function internalRemoveData( elem, name, pvt ) { 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; } } @@ -192,10 +188,10 @@ jQuery.extend({ // 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 ) { @@ -237,9 +233,12 @@ jQuery.extend({ 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 ) { @@ -251,7 +250,7 @@ jQuery.fn.extend({ 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 ] ); @@ -271,17 +270,16 @@ jQuery.fn.extend({ }); } - 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 ) { -- 2.39.5