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;
66 // Use a Function as the cache object instead of an Object on JS objects
67 // as a hack to prevent JSON.stringify from serializing it (#8108)
68 cache[ id ] = isNode ? {} : function () {};
71 // An object can be passed to jQuery.data instead of a key/value pair; this gets
72 // shallow copied over onto the existing cache
73 if ( typeof name === "object" || typeof name === "function" ) {
75 cache[ id ][ internalKey ] = jQuery.extend(cache[ id ][ internalKey ], name);
77 cache[ id ] = jQuery.extend(cache[ id ], name);
81 thisCache = cache[ id ];
83 // Internal jQuery data is stored in a separate object inside the object's data
84 // cache in order to avoid key collisions between internal data and user-defined
87 if ( !thisCache[ internalKey ] ) {
88 thisCache[ internalKey ] = {};
91 thisCache = thisCache[ internalKey ];
94 if ( data !== undefined ) {
95 thisCache[ name ] = data;
98 // TODO: This is a hack for 1.5 ONLY. It will be removed in 1.6. Users should
99 // not attempt to inspect the internal events object using jQuery.data, as this
100 // internal data object is undocumented and subject to change.
101 if ( name === "events" && !thisCache[name] ) {
102 return thisCache[ internalKey ] && thisCache[ internalKey ].events;
105 return getByName ? thisCache[ name ] : thisCache;
108 removeData: function( elem, name, pvt /* Internal Use Only */ ) {
109 if ( !jQuery.acceptData( elem ) ) {
113 var internalKey = jQuery.expando, isNode = elem.nodeType,
115 // See jQuery.data for more information
116 cache = isNode ? jQuery.cache : elem,
118 // See jQuery.data for more information
119 id = isNode ? elem[ jQuery.expando ] : jQuery.expando;
121 // If there is already no cache entry for this object, there is no
122 // purpose in continuing
123 if ( !cache[ id ] ) {
128 var thisCache = pvt ? cache[ id ][ internalKey ] : cache[ id ];
131 delete thisCache[ name ];
133 // If there is no data left in the cache, we want to continue
134 // and let the cache object itself get destroyed
135 if ( !jQuery.isEmptyObject(thisCache) ) {
141 // See jQuery.data for more information
143 delete cache[ id ][ internalKey ];
145 // Don't destroy the parent cache unless the internal data object
146 // had been the only thing left in it
147 if ( !jQuery.isEmptyObject(cache[ id ]) ) {
152 var internalCache = cache[ id ][ internalKey ];
154 // Browsers that fail expando deletion also refuse to delete expandos on
155 // the window, but it will allow it on all other JS objects; other browsers
157 if ( jQuery.support.deleteExpando || cache != window ) {
163 // We destroyed the entire user cache at once because it's faster than
164 // iterating through each key, but we need to continue to persist internal
165 // data if it existed
166 if ( internalCache ) {
168 cache[ id ][ internalKey ] = internalCache;
170 // Otherwise, we need to eliminate the expando on the node to avoid
171 // false lookups in the cache for entries that no longer exist
172 } else if ( isNode ) {
173 // IE does not allow us to delete expando properties from nodes,
174 // nor does it have a removeAttribute function on Document nodes;
175 // we must handle all of these cases
176 if ( jQuery.support.deleteExpando ) {
177 delete elem[ jQuery.expando ];
178 } else if ( elem.removeAttribute ) {
179 elem.removeAttribute( jQuery.expando );
181 elem[ jQuery.expando ] = null;
186 // For internal use only.
187 _data: function( elem, name, data ) {
188 return jQuery.data( elem, name, data, true );
191 // A method for determining if a DOM node can handle the data expando
192 acceptData: function( elem ) {
193 if ( elem.nodeName ) {
194 var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
197 return !(match === true || elem.getAttribute("classid") !== match);
206 data: function( key, value ) {
209 if ( typeof key === "undefined" ) {
211 data = jQuery.data( this[0] );
213 if ( this[0].nodeType === 1 ) {
214 var attr = this[0].attributes, name;
215 for ( var i = 0, l = attr.length; i < l; i++ ) {
218 if ( name.indexOf( "data-" ) === 0 ) {
219 name = name.substr( 5 );
220 dataAttr( this[0], name, data[ name ] );
228 } else if ( typeof key === "object" ) {
229 return this.each(function() {
230 jQuery.data( this, key );
234 var parts = key.split(".");
235 parts[1] = parts[1] ? "." + parts[1] : "";
237 if ( value === undefined ) {
238 data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
240 // Try to fetch any internally stored data first
241 if ( data === undefined && this.length ) {
242 data = jQuery.data( this[0], key );
243 data = dataAttr( this[0], key, data );
246 return data === undefined && parts[1] ?
247 this.data( parts[0] ) :
251 return this.each(function() {
252 var $this = jQuery( this ),
253 args = [ parts[0], value ];
255 $this.triggerHandler( "setData" + parts[1] + "!", args );
256 jQuery.data( this, key, value );
257 $this.triggerHandler( "changeData" + parts[1] + "!", args );
262 removeData: function( key ) {
263 return this.each(function() {
264 jQuery.removeData( this, key );
269 function dataAttr( elem, key, data ) {
270 // If nothing was found internally, try to fetch any
271 // data from the HTML5 data-* attribute
272 if ( data === undefined && elem.nodeType === 1 ) {
273 data = elem.getAttribute( "data-" + key );
275 if ( typeof data === "string" ) {
277 data = data === "true" ? true :
278 data === "false" ? false :
279 data === "null" ? null :
280 !jQuery.isNaN( data ) ? parseFloat( data ) :
281 rbrace.test( data ) ? jQuery.parseJSON( data ) :
285 // Make sure we set the data so it isn't changed later
286 jQuery.data( elem, key, data );