1 var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/,
2 rmultiDash = /([A-Z])/g;
4 function internalData( elem, name, data, pvt ) {
5 if ( !jQuery.acceptData( elem ) ) {
10 internalKey = jQuery.expando,
11 getByName = typeof name === "string",
13 // We have to handle DOM nodes and JS objects differently because IE6-7
14 // can't GC object references properly across the DOM-JS boundary
15 isNode = elem.nodeType,
17 // Only DOM nodes need the global jQuery cache; JS object data is
18 // attached directly to the object so GC can occur automatically
19 cache = isNode ? jQuery.cache : elem,
21 // Only defining an ID for JS objects if its cache already exists allows
22 // the code to shortcut on the same path as a DOM node with no cache
23 id = isNode ? elem[ internalKey ] : elem[ internalKey ] && internalKey;
25 // Avoid doing any more work than we need to when trying to get data on an
26 // object that has no data at all
27 if ( (!id || !cache[id] || (!pvt && !cache[id].data)) && getByName && data === undefined ) {
32 // Only DOM nodes need a new unique ID for each element since their data
33 // ends up in the global cache
35 elem[ internalKey ] = id = core_deletedIds.pop() || jQuery.guid++;
44 // Avoids exposing jQuery metadata on plain JS objects when the object
45 // is serialized using JSON.stringify
47 cache[ id ].toJSON = jQuery.noop;
51 // An object can be passed to jQuery.data instead of a key/value pair; this gets
52 // shallow copied over onto the existing cache
53 if ( typeof name === "object" || typeof name === "function" ) {
55 cache[ id ] = jQuery.extend( cache[ id ], name );
57 cache[ id ].data = jQuery.extend( cache[ id ].data, name );
61 thisCache = cache[ id ];
63 // jQuery data() is stored in a separate object inside the object's internal data
64 // cache in order to avoid key collisions between internal data and user-defined
67 if ( !thisCache.data ) {
71 thisCache = thisCache.data;
74 if ( data !== undefined ) {
75 thisCache[ jQuery.camelCase( name ) ] = data;
78 // Check for both converted-to-camel and non-converted data property names
79 // If a data property was specified
82 // First Try to find as-is property data
83 ret = thisCache[ name ];
85 // Test for null|undefined property data
88 // Try to find the camelCased property
89 ret = thisCache[ jQuery.camelCase( name ) ];
98 function internalRemoveData( elem, name, pvt ) {
99 if ( !jQuery.acceptData( elem ) ) {
105 isNode = elem.nodeType,
107 // See jQuery.data for more information
108 cache = isNode ? jQuery.cache : elem,
109 id = isNode ? elem[ jQuery.expando ] : jQuery.expando;
111 // If there is already no cache entry for this object, there is no
112 // purpose in continuing
113 if ( !cache[ id ] ) {
119 thisCache = pvt ? cache[ id ] : cache[ id ].data;
123 // Support array or space separated string names for data keys
124 if ( !jQuery.isArray( name ) ) {
126 // try the string as a key before any manipulation
127 if ( name in thisCache ) {
131 // split the camel cased version by spaces unless a key with the spaces exists
132 name = jQuery.camelCase( name );
133 if ( name in thisCache ) {
136 name = name.split(" ");
140 // If "name" is an array of keys...
141 // When data is initially created, via ("key", "val") signature,
142 // keys will be converted to camelCase.
143 // Since there is no way to tell _how_ a key was added, remove
144 // both plain key and camelCase key. #12786
145 // This will only penalize the array argument path.
146 name = name.concat( jQuery.map( name, jQuery.camelCase ) );
149 for ( i = 0, l = name.length; i < l; i++ ) {
150 delete thisCache[ name[i] ];
153 // If there is no data left in the cache, we want to continue
154 // and let the cache object itself get destroyed
155 if ( !( pvt ? isEmptyDataObject : jQuery.isEmptyObject )( thisCache ) ) {
161 // See jQuery.data for more information
163 delete cache[ id ].data;
165 // Don't destroy the parent cache unless the internal data object
166 // had been the only thing left in it
167 if ( !isEmptyDataObject( cache[ id ] ) ) {
174 jQuery.cleanData( [ elem ], true );
176 // Use delete when supported for expandos or `cache` is not a window per isWindow (#10080)
177 } else if ( jQuery.support.deleteExpando || cache != cache.window ) {
180 // When all else fails, null
189 // Unique for each copy of jQuery on the page
190 // Non-digits removed to match rinlinejQuery
191 expando: "jQuery" + ( core_version + Math.random() ).replace( /\D/g, "" ),
193 // The following elements throw uncatchable exceptions if you
194 // attempt to add expando properties to them.
197 // Ban all objects except for Flash (which handle expandos)
198 "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
202 hasData: function( elem ) {
203 elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ];
204 return !!elem && !isEmptyDataObject( elem );
207 data: function( elem, name, data ) {
208 return internalData( elem, name, data );
211 removeData: function( elem, name ) {
212 return internalRemoveData( elem, name );
215 // For internal use only.
216 _data: function( elem, name, data ) {
217 return internalData( elem, name, data, true );
220 _removeData: function( elem, name ) {
221 return internalRemoveData( elem, name, true );
224 // A method for determining if a DOM node can handle the data expando
225 acceptData: function( elem ) {
226 // Do not set data on non-element because it will not be cleared (#8335).
227 if ( elem.nodeType && elem.nodeType !== 1 && elem.nodeType !== 9 ) {
231 var noData = elem.nodeName && jQuery.noData[ elem.nodeName.toLowerCase() ];
233 // nodes accept data unless otherwise specified; rejection can be conditional
234 return !noData || noData !== true && elem.getAttribute("classid") === noData;
239 data: function( key, value ) {
246 if ( key === undefined ) {
248 data = jQuery.data( elem );
250 if ( elem.nodeType === 1 && !jQuery._data( elem, "parsedAttrs" ) ) {
251 attrs = elem.attributes;
252 for ( ; i < attrs.length; i++ ) {
253 name = attrs[i].name;
255 if ( !name.indexOf( "data-" ) ) {
256 name = jQuery.camelCase( name.slice(5) );
258 dataAttr( elem, name, data[ name ] );
261 jQuery._data( elem, "parsedAttrs", true );
268 // Sets multiple values
269 if ( typeof key === "object" ) {
270 return this.each(function() {
271 jQuery.data( this, key );
275 return jQuery.access( this, function( value ) {
277 if ( value === undefined ) {
278 // Try to fetch any internally stored data first
279 return elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : null;
282 this.each(function() {
283 jQuery.data( this, key, value );
285 }, null, value, arguments.length > 1, null, true );
288 removeData: function( key ) {
289 return this.each(function() {
290 jQuery.removeData( this, key );
295 function dataAttr( elem, key, data ) {
296 // If nothing was found internally, try to fetch any
297 // data from the HTML5 data-* attribute
298 if ( data === undefined && elem.nodeType === 1 ) {
300 var name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase();
302 data = elem.getAttribute( name );
304 if ( typeof data === "string" ) {
306 data = data === "true" ? true :
307 data === "false" ? false :
308 data === "null" ? null :
309 // Only convert to a number if it doesn't change the string
310 +data + "" === data ? +data :
311 rbrace.test( data ) ? jQuery.parseJSON( data ) :
315 // Make sure we set the data so it isn't changed later
316 jQuery.data( elem, key, data );
326 // checks a cache object for emptiness
327 function isEmptyDataObject( obj ) {
329 for ( name in obj ) {
331 // if the public data object is empty, the private is still empty
332 if ( name === "data" && jQuery.isEmptyObject( obj[name] ) ) {
335 if ( name !== "toJSON" ) {