3 var rbrace = /^(?:\{.*\}|\[.*\])$/;
8 // Please use with caution
11 // Unique for each copy of jQuery on the page
12 // Non-digits removed to match rinlinejQuery
13 expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ),
15 // The following elements throw uncatchable exceptions if you
16 // attempt to add expando properties to them.
19 // Ban all objects except for Flash (which handle expandos)
20 "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
24 hasData: function( elem ) {
25 elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ];
27 return !!elem && !jQuery.isEmptyObject(elem);
30 data: function( elem, name, data, pvt /* Internal Use Only */ ) {
31 if ( !jQuery.acceptData( elem ) ) {
35 var internalKey = jQuery.expando, getByName = typeof name === "string", thisCache,
37 // We have to handle DOM nodes and JS objects differently because IE6-7
38 // can't GC object references properly across the DOM-JS boundary
39 isNode = elem.nodeType,
41 // Only DOM nodes need the global jQuery cache; JS object data is
42 // attached directly to the object so GC can occur automatically
43 cache = isNode ? jQuery.cache : elem,
45 // Only defining an ID for JS objects if its cache already exists allows
46 // the code to shortcut on the same path as a DOM node with no cache
47 id = isNode ? elem[ jQuery.expando ] : elem[ jQuery.expando ] && jQuery.expando;
49 // Avoid doing any more work than we need to when trying to get data on an
50 // object that has no data at all
51 if ( (!id || (pvt && id && !cache[ id ][ internalKey ])) && getByName && data === undefined ) {
56 // Only DOM nodes need a new unique ID for each element since their data
57 // ends up in the global cache
59 elem[ jQuery.expando ] = id = ++jQuery.uuid;
69 // An object can be passed to jQuery.data instead of a key/value pair; this gets
70 // shallow copied over onto the existing cache
71 if ( typeof name === "object" ) {
73 cache[ id ][ internalKey ] = jQuery.extend(cache[ id ][ internalKey ], name);
75 cache[ id ] = jQuery.extend(cache[ id ], name);
79 thisCache = cache[ id ];
81 // Internal jQuery data is stored in a separate object inside the object's data
82 // cache in order to avoid key collisions between internal data and user-defined
85 if ( !thisCache[ internalKey ] ) {
86 thisCache[ internalKey ] = {};
89 thisCache = thisCache[ internalKey ];
92 if ( data !== undefined ) {
93 thisCache[ name ] = data;
96 // TODO: This is a hack for 1.5 ONLY. It will be removed in 1.6. Users should
97 // not attempt to inspect the internal events object using jQuery.data, as this
98 // internal data object is undocumented and subject to change.
99 if ( name === "events" && !thisCache[name] ) {
100 return thisCache[ internalKey ] && thisCache[ internalKey ].events;
103 return getByName ? thisCache[ name ] : thisCache;
106 removeData: function( elem, name, pvt /* Internal Use Only */ ) {
107 if ( !jQuery.acceptData( elem ) ) {
111 var internalKey = jQuery.expando, isNode = elem.nodeType,
113 // See jQuery.data for more information
114 cache = isNode ? jQuery.cache : elem,
116 // See jQuery.data for more information
117 id = isNode ? elem[ jQuery.expando ] : jQuery.expando;
119 // If there is already no cache entry for this object, there is no
120 // purpose in continuing
121 if ( !cache[ id ] ) {
126 var thisCache = pvt ? cache[ id ][ internalKey ] : cache[ id ];
129 delete thisCache[ name ];
131 // If there is no data left in the cache, we want to continue
132 // and let the cache object itself get destroyed
133 if ( !jQuery.isEmptyObject(thisCache) ) {
139 // See jQuery.data for more information
141 delete cache[ id ][ internalKey ];
143 // Don't destroy the parent cache unless the internal data object
144 // had been the only thing left in it
145 if ( !jQuery.isEmptyObject(cache[ id ]) ) {
150 var internalCache = cache[ id ][ internalKey ];
152 // Browsers that fail expando deletion also refuse to delete expandos on
153 // the window, but it will allow it on all other JS objects; other browsers
155 if ( jQuery.support.deleteExpando || cache != window ) {
161 // We destroyed the entire user cache at once because it's faster than
162 // iterating through each key, but we need to continue to persist internal
163 // data if it existed
164 if ( internalCache ) {
166 cache[ id ][ internalKey ] = internalCache;
168 // Otherwise, we need to eliminate the expando on the node to avoid
169 // false lookups in the cache for entries that no longer exist
170 } else if ( isNode ) {
171 // IE does not allow us to delete expando properties from nodes,
172 // nor does it have a removeAttribute function on Document nodes;
173 // we must handle all of these cases
174 if ( jQuery.support.deleteExpando ) {
175 delete elem[ jQuery.expando ];
176 } else if ( elem.removeAttribute ) {
177 elem.removeAttribute( jQuery.expando );
179 elem[ jQuery.expando ] = null;
184 // For internal use only.
185 _data: function( elem, name, data ) {
186 return jQuery.data( elem, name, data, true );
189 // A method for determining if a DOM node can handle the data expando
190 acceptData: function( elem ) {
191 if ( elem.nodeName ) {
192 var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
195 return !(match === true || elem.getAttribute("classid") !== match);
204 data: function( key, value ) {
207 if ( typeof key === "undefined" ) {
209 data = jQuery.data( this[0] );
211 if ( this[0].nodeType === 1 ) {
212 var attr = this[0].attributes, name;
213 for ( var i = 0, l = attr.length; i < l; i++ ) {
216 if ( name.indexOf( "data-" ) === 0 ) {
217 name = name.substr( 5 );
218 dataAttr( this[0], name, data[ name ] );
226 } else if ( typeof key === "object" ) {
227 return this.each(function() {
228 jQuery.data( this, key );
232 var parts = key.split(".");
233 parts[1] = parts[1] ? "." + parts[1] : "";
235 if ( value === undefined ) {
236 data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
238 // Try to fetch any internally stored data first
239 if ( data === undefined && this.length ) {
240 data = jQuery.data( this[0], key );
241 data = dataAttr( this[0], key, data );
244 return data === undefined && parts[1] ?
245 this.data( parts[0] ) :
249 return this.each(function() {
250 var $this = jQuery( this ),
251 args = [ parts[0], value ];
253 $this.triggerHandler( "setData" + parts[1] + "!", args );
254 jQuery.data( this, key, value );
255 $this.triggerHandler( "changeData" + parts[1] + "!", args );
260 removeData: function( key ) {
261 return this.each(function() {
262 jQuery.removeData( this, key );
267 function dataAttr( elem, key, data ) {
268 // If nothing was found internally, try to fetch any
269 // data from the HTML5 data-* attribute
270 if ( data === undefined && elem.nodeType === 1 ) {
271 data = elem.getAttribute( "data-" + key );
273 if ( typeof data === "string" ) {
275 data = data === "true" ? true :
276 data === "false" ? false :
277 data === "null" ? null :
278 !jQuery.isNaN( data ) ? parseFloat( data ) :
279 rbrace.test( data ) ? jQuery.parseJSON( data ) :
283 // Make sure we set the data so it isn't changed later
284 jQuery.data( elem, key, data );