faa44f3de1f43aeab2f481afc291555f286750ae
[jquery.git] / src / data.js
1 (function( jQuery ) {
2
3 var rbrace = /^(?:\{.*\}|\[.*\])$/;
4
5 jQuery.extend({
6         cache: {},
7
8         // Please use with caution
9         uuid: 0,
10
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, "" ),
14
15         // The following elements throw uncatchable exceptions if you
16         // attempt to add expando properties to them.
17         noData: {
18                 "embed": true,
19                 // Ban all objects except for Flash (which handle expandos)
20                 "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
21                 "applet": true
22         },
23
24         hasData: function( elem ) {
25                 elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ];
26
27                 return !!elem && !jQuery.isEmptyObject(elem);
28         },
29
30         data: function( elem, name, data, pvt /* Internal Use Only */ ) {
31                 if ( !jQuery.acceptData( elem ) ) {
32                         return;
33                 }
34
35                 var internalKey = jQuery.expando, getByName = typeof name === "string", thisCache,
36
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,
40
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,
44
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;
48
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 ) {
52                         return;
53                 }
54
55                 if ( !id ) {
56                         // Only DOM nodes need a new unique ID for each element since their data
57                         // ends up in the global cache
58                         if ( isNode ) {
59                                 elem[ jQuery.expando ] = id = ++jQuery.uuid;
60                         } else {
61                                 id = jQuery.expando;
62                         }
63                 }
64
65                 if ( !cache[ id ] ) {
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 () {};
69                 }
70
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" ) {
74                         if ( pvt ) {
75                                 cache[ id ][ internalKey ] = jQuery.extend(cache[ id ][ internalKey ], name);
76                         } else {
77                                 cache[ id ] = jQuery.extend(cache[ id ], name);
78                         }
79                 }
80
81                 thisCache = cache[ id ];
82
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
85                 // data
86                 if ( pvt ) {
87                         if ( !thisCache[ internalKey ] ) {
88                                 thisCache[ internalKey ] = {};
89                         }
90
91                         thisCache = thisCache[ internalKey ];
92                 }
93
94                 if ( data !== undefined ) {
95                         thisCache[ name ] = data;
96                 }
97
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;
103                 }
104
105                 return getByName ? thisCache[ name ] : thisCache;
106         },
107
108         removeData: function( elem, name, pvt /* Internal Use Only */ ) {
109                 if ( !jQuery.acceptData( elem ) ) {
110                         return;
111                 }
112
113                 var internalKey = jQuery.expando, isNode = elem.nodeType,
114
115                         // See jQuery.data for more information
116                         cache = isNode ? jQuery.cache : elem,
117
118                         // See jQuery.data for more information
119                         id = isNode ? elem[ jQuery.expando ] : jQuery.expando;
120
121                 // If there is already no cache entry for this object, there is no
122                 // purpose in continuing
123                 if ( !cache[ id ] ) {
124                         return;
125                 }
126
127                 if ( name ) {
128                         var thisCache = pvt ? cache[ id ][ internalKey ] : cache[ id ];
129
130                         if ( thisCache ) {
131                                 delete thisCache[ name ];
132
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) ) {
136                                         return;
137                                 }
138                         }
139                 }
140
141                 // See jQuery.data for more information
142                 if ( pvt ) {
143                         delete cache[ id ][ internalKey ];
144
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 ]) ) {
148                                 return;
149                         }
150                 }
151
152                 var internalCache = cache[ id ][ internalKey ];
153
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
156                 // don't care
157                 if ( jQuery.support.deleteExpando || cache != window ) {
158                         delete cache[ id ];
159                 } else {
160                         cache[ id ] = null;
161                 }
162
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 ) {
167                         cache[ id ] = {};
168                         cache[ id ][ internalKey ] = internalCache;
169
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 );
180                         } else {
181                                 elem[ jQuery.expando ] = null;
182                         }
183                 }
184         },
185
186         // For internal use only.
187         _data: function( elem, name, data ) {
188                 return jQuery.data( elem, name, data, true );
189         },
190
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() ];
195
196                         if ( match ) {
197                                 return !(match === true || elem.getAttribute("classid") !== match);
198                         }
199                 }
200
201                 return true;
202         }
203 });
204
205 jQuery.fn.extend({
206         data: function( key, value ) {
207                 var data = null;
208
209                 if ( typeof key === "undefined" ) {
210                         if ( this.length ) {
211                                 data = jQuery.data( this[0] );
212
213                                 if ( this[0].nodeType === 1 ) {
214                                         var attr = this[0].attributes, name;
215                                         for ( var i = 0, l = attr.length; i < l; i++ ) {
216                                                 name = attr[i].name;
217
218                                                 if ( name.indexOf( "data-" ) === 0 ) {
219                                                         name = name.substr( 5 );
220                                                         dataAttr( this[0], name, data[ name ] );
221                                                 }
222                                         }
223                                 }
224                         }
225
226                         return data;
227
228                 } else if ( typeof key === "object" ) {
229                         return this.each(function() {
230                                 jQuery.data( this, key );
231                         });
232                 }
233
234                 var parts = key.split(".");
235                 parts[1] = parts[1] ? "." + parts[1] : "";
236
237                 if ( value === undefined ) {
238                         data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
239
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 );
244                         }
245
246                         return data === undefined && parts[1] ?
247                                 this.data( parts[0] ) :
248                                 data;
249
250                 } else {
251                         return this.each(function() {
252                                 var $this = jQuery( this ),
253                                         args = [ parts[0], value ];
254
255                                 $this.triggerHandler( "setData" + parts[1] + "!", args );
256                                 jQuery.data( this, key, value );
257                                 $this.triggerHandler( "changeData" + parts[1] + "!", args );
258                         });
259                 }
260         },
261
262         removeData: function( key ) {
263                 return this.each(function() {
264                         jQuery.removeData( this, key );
265                 });
266         }
267 });
268
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 );
274
275                 if ( typeof data === "string" ) {
276                         try {
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 ) :
282                                         data;
283                         } catch( e ) {}
284
285                         // Make sure we set the data so it isn't changed later
286                         jQuery.data( elem, key, data );
287
288                 } else {
289                         data = undefined;
290                 }
291         }
292
293         return data;
294 }
295
296 })( jQuery );