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 && !isEmptyDataObject( 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;
68 // TODO: This is a hack for 1.5 ONLY. Avoids exposing jQuery
69 // metadata on plain JS objects when the object is serialized using
71 cache[ id ].toJSON = jQuery.noop;
74 // An object can be passed to jQuery.data instead of a key/value pair; this gets
75 // shallow copied over onto the existing cache
76 if ( typeof name === "object" || typeof name === "function" ) {
78 cache[ id ][ internalKey ] = jQuery.extend(cache[ id ][ internalKey ], name);
80 cache[ id ] = jQuery.extend(cache[ id ], name);
84 thisCache = cache[ id ];
86 // Internal jQuery data is stored in a separate object inside the object's data
87 // cache in order to avoid key collisions between internal data and user-defined
90 if ( !thisCache[ internalKey ] ) {
91 thisCache[ internalKey ] = {};
94 thisCache = thisCache[ internalKey ];
97 if ( data !== undefined ) {
98 thisCache[ name ] = data;
101 // TODO: This is a hack for 1.5 ONLY. It will be removed in 1.6. Users should
102 // not attempt to inspect the internal events object using jQuery.data, as this
103 // internal data object is undocumented and subject to change.
104 if ( name === "events" && !thisCache[name] ) {
105 return thisCache[ internalKey ] && thisCache[ internalKey ].events;
108 return getByName ? thisCache[ name ] : thisCache;
111 removeData: function( elem, name, pvt /* Internal Use Only */ ) {
112 if ( !jQuery.acceptData( elem ) ) {
116 var internalKey = jQuery.expando, isNode = elem.nodeType,
118 // See jQuery.data for more information
119 cache = isNode ? jQuery.cache : elem,
121 // See jQuery.data for more information
122 id = isNode ? elem[ jQuery.expando ] : jQuery.expando;
124 // If there is already no cache entry for this object, there is no
125 // purpose in continuing
126 if ( !cache[ id ] ) {
131 var thisCache = pvt ? cache[ id ][ internalKey ] : cache[ id ];
134 delete thisCache[ name ];
136 // If there is no data left in the cache, we want to continue
137 // and let the cache object itself get destroyed
138 if ( !isEmptyDataObject(thisCache) ) {
144 // See jQuery.data for more information
146 delete cache[ id ][ internalKey ];
148 // Don't destroy the parent cache unless the internal data object
149 // had been the only thing left in it
150 if ( !isEmptyDataObject(cache[ id ]) ) {
155 var internalCache = cache[ id ][ internalKey ];
157 // Browsers that fail expando deletion also refuse to delete expandos on
158 // the window, but it will allow it on all other JS objects; other browsers
160 if ( jQuery.support.deleteExpando || cache != window ) {
166 // We destroyed the entire user cache at once because it's faster than
167 // iterating through each key, but we need to continue to persist internal
168 // data if it existed
169 if ( internalCache ) {
171 cache[ id ][ internalKey ] = internalCache;
173 // Otherwise, we need to eliminate the expando on the node to avoid
174 // false lookups in the cache for entries that no longer exist
175 } else if ( isNode ) {
176 // IE does not allow us to delete expando properties from nodes,
177 // nor does it have a removeAttribute function on Document nodes;
178 // we must handle all of these cases
179 if ( jQuery.support.deleteExpando ) {
180 delete elem[ jQuery.expando ];
181 } else if ( elem.removeAttribute ) {
182 elem.removeAttribute( jQuery.expando );
184 elem[ jQuery.expando ] = null;
189 // For internal use only.
190 _data: function( elem, name, data ) {
191 return jQuery.data( elem, name, data, true );
194 // A method for determining if a DOM node can handle the data expando
195 acceptData: function( elem ) {
196 if ( elem.nodeName ) {
197 var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
200 return !(match === true || elem.getAttribute("classid") !== match);
209 data: function( key, value ) {
212 if ( typeof key === "undefined" ) {
214 data = jQuery.data( this[0] );
216 if ( this[0].nodeType === 1 ) {
217 var attr = this[0].attributes, name;
218 for ( var i = 0, l = attr.length; i < l; i++ ) {
221 if ( name.indexOf( "data-" ) === 0 ) {
222 name = name.substr( 5 );
223 dataAttr( this[0], name, data[ name ] );
231 } else if ( typeof key === "object" ) {
232 return this.each(function() {
233 jQuery.data( this, key );
237 var parts = key.split(".");
238 parts[1] = parts[1] ? "." + parts[1] : "";
240 if ( value === undefined ) {
241 data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
243 // Try to fetch any internally stored data first
244 if ( data === undefined && this.length ) {
245 data = jQuery.data( this[0], key );
246 data = dataAttr( this[0], key, data );
249 return data === undefined && parts[1] ?
250 this.data( parts[0] ) :
254 return this.each(function() {
255 var $this = jQuery( this ),
256 args = [ parts[0], value ];
258 $this.triggerHandler( "setData" + parts[1] + "!", args );
259 jQuery.data( this, key, value );
260 $this.triggerHandler( "changeData" + parts[1] + "!", args );
265 removeData: function( key ) {
266 return this.each(function() {
267 jQuery.removeData( this, key );
272 function dataAttr( elem, key, data ) {
273 // If nothing was found internally, try to fetch any
274 // data from the HTML5 data-* attribute
275 if ( data === undefined && elem.nodeType === 1 ) {
276 data = elem.getAttribute( "data-" + key );
278 if ( typeof data === "string" ) {
280 data = data === "true" ? true :
281 data === "false" ? false :
282 data === "null" ? null :
283 !jQuery.isNaN( data ) ? parseFloat( data ) :
284 rbrace.test( data ) ? jQuery.parseJSON( data ) :
288 // Make sure we set the data so it isn't changed later
289 jQuery.data( elem, key, data );
299 // TODO: This is a hack for 1.5 ONLY to allow objects with a single toJSON
300 // property to be considered empty objects; this property always exists in
301 // order to make sure JSON.stringify does not expose internal metadata
302 function isEmptyDataObject( obj ) {
303 for ( var name in obj ) {
304 if ( name !== "toJSON" ) {