X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fdata.js;h=6efe7b01d45680550f40be7da259496159530f9f;hb=2cc03a86fc046e81013d32a662f4043f1c10b436;hp=a1abc9ed6a05cd597da730711c37493045e1d86e;hpb=8e59a99e0ade75dec434f246f52e8b3f7393f359;p=jquery.git diff --git a/src/data.js b/src/data.js index a1abc9e..6efe7b0 100644 --- a/src/data.js +++ b/src/data.js @@ -24,7 +24,7 @@ jQuery.extend({ hasData: function( elem ) { elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ]; - return !!elem && !jQuery.isEmptyObject(elem); + return !!elem && !isEmptyDataObject( elem ); }, data: function( elem, name, data, pvt /* Internal Use Only */ ) { @@ -64,11 +64,16 @@ jQuery.extend({ if ( !cache[ id ] ) { cache[ id ] = {}; + + // TODO: This is a hack for 1.5 ONLY. Avoids exposing jQuery + // metadata on plain JS objects when the object is serialized using + // JSON.stringify + cache[ id ].toJSON = jQuery.noop; } // An object can be passed to jQuery.data instead of a key/value pair; this gets // shallow copied over onto the existing cache - if ( typeof name === "object" ) { + if ( typeof name === "object" || typeof name === "function" ) { if ( pvt ) { cache[ id ][ internalKey ] = jQuery.extend(cache[ id ][ internalKey ], name); } else { @@ -93,6 +98,13 @@ jQuery.extend({ thisCache[ name ] = data; } + // TODO: This is a hack for 1.5 ONLY. It will be removed in 1.6. Users should + // not attempt to inspect the internal events object using jQuery.data, as this + // internal data object is undocumented and subject to change. + if ( name === "events" && !thisCache[name] ) { + return thisCache[ internalKey ] && thisCache[ internalKey ].events; + } + return getByName ? thisCache[ name ] : thisCache; }, @@ -123,7 +135,7 @@ jQuery.extend({ // If there is no data left in the cache, we want to continue // and let the cache object itself get destroyed - if ( !jQuery.isEmptyObject(thisCache) ) { + if ( !isEmptyDataObject(thisCache) ) { return; } } @@ -135,7 +147,7 @@ jQuery.extend({ // Don't destroy the parent cache unless the internal data object // had been the only thing left in it - if ( !jQuery.isEmptyObject(cache[ id ]) ) { + if ( !isEmptyDataObject(cache[ id ]) ) { return; } } @@ -284,4 +296,17 @@ function dataAttr( elem, key, data ) { return data; } +// TODO: This is a hack for 1.5 ONLY to allow objects with a single toJSON +// property to be considered empty objects; this property always exists in +// order to make sure JSON.stringify does not expose internal metadata +function isEmptyDataObject( obj ) { + for ( var name in obj ) { + if ( name !== "toJSON" ) { + return false; + } + } + + return true; +} + })( jQuery );