21f0e3a55f6592a1607878ecf78de444c17dfe9f
[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                         cache[ id ] = {};
67                 }
68
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" ) {
72                         if ( pvt ) {
73                                 cache[ id ][ internalKey ] = jQuery.extend(cache[ id ][ internalKey ], name);
74                         } else {
75                                 cache[ id ] = jQuery.extend(cache[ id ], name);
76                         }
77                 }
78
79                 thisCache = cache[ id ];
80
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
83                 // data
84                 if ( pvt ) {
85                         if ( !thisCache[ internalKey ] ) {
86                                 thisCache[ internalKey ] = {};
87                         }
88
89                         thisCache = thisCache[ internalKey ];
90                 }
91
92                 if ( data !== undefined ) {
93                         thisCache[ name ] = data;
94                 }
95
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;
101                 }
102
103                 return getByName ? thisCache[ name ] : thisCache;
104         },
105
106         removeData: function( elem, name, pvt /* Internal Use Only */ ) {
107                 if ( !jQuery.acceptData( elem ) ) {
108                         return;
109                 }
110
111                 var internalKey = jQuery.expando, isNode = elem.nodeType,
112
113                         // See jQuery.data for more information
114                         cache = isNode ? jQuery.cache : elem,
115
116                         // See jQuery.data for more information
117                         id = isNode ? elem[ jQuery.expando ] : jQuery.expando;
118
119                 // If there is already no cache entry for this object, there is no
120                 // purpose in continuing
121                 if ( !cache[ id ] ) {
122                         return;
123                 }
124
125                 if ( name ) {
126                         var thisCache = pvt ? cache[ id ][ internalKey ] : cache[ id ];
127
128                         if ( thisCache ) {
129                                 delete thisCache[ name ];
130
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) ) {
134                                         return;
135                                 }
136                         }
137                 }
138
139                 // See jQuery.data for more information
140                 if ( pvt ) {
141                         delete cache[ id ][ internalKey ];
142
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 ]) ) {
146                                 return;
147                         }
148                 }
149
150                 var internalCache = cache[ id ][ internalKey ];
151
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
154                 // don't care
155                 if ( jQuery.support.deleteExpando || cache != window ) {
156                         delete cache[ id ];
157                 } else {
158                         cache[ id ] = null;
159                 }
160
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 ) {
165                         cache[ id ] = {};
166                         cache[ id ][ internalKey ] = internalCache;
167
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 );
178                         } else {
179                                 elem[ jQuery.expando ] = null;
180                         }
181                 }
182         },
183
184         // For internal use only.
185         _data: function( elem, name, data ) {
186                 return jQuery.data( elem, name, data, true );
187         },
188
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() ];
193
194                         if ( match ) {
195                                 return !(match === true || elem.getAttribute("classid") !== match);
196                         }
197                 }
198
199                 return true;
200         }
201 });
202
203 jQuery.fn.extend({
204         data: function( key, value ) {
205                 var data = null;
206
207                 if ( typeof key === "undefined" ) {
208                         if ( this.length ) {
209                                 data = jQuery.data( this[0] );
210
211                                 if ( this[0].nodeType === 1 ) {
212                                         var attr = this[0].attributes, name;
213                                         for ( var i = 0, l = attr.length; i < l; i++ ) {
214                                                 name = attr[i].name;
215
216                                                 if ( name.indexOf( "data-" ) === 0 ) {
217                                                         name = name.substr( 5 );
218                                                         dataAttr( this[0], name, data[ name ] );
219                                                 }
220                                         }
221                                 }
222                         }
223
224                         return data;
225
226                 } else if ( typeof key === "object" ) {
227                         return this.each(function() {
228                                 jQuery.data( this, key );
229                         });
230                 }
231
232                 var parts = key.split(".");
233                 parts[1] = parts[1] ? "." + parts[1] : "";
234
235                 if ( value === undefined ) {
236                         data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
237
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 );
242                         }
243
244                         return data === undefined && parts[1] ?
245                                 this.data( parts[0] ) :
246                                 data;
247
248                 } else {
249                         return this.each(function() {
250                                 var $this = jQuery( this ),
251                                         args = [ parts[0], value ];
252
253                                 $this.triggerHandler( "setData" + parts[1] + "!", args );
254                                 jQuery.data( this, key, value );
255                                 $this.triggerHandler( "changeData" + parts[1] + "!", args );
256                         });
257                 }
258         },
259
260         removeData: function( key ) {
261                 return this.each(function() {
262                         jQuery.removeData( this, key );
263                 });
264         }
265 });
266
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 );
272
273                 if ( typeof data === "string" ) {
274                         try {
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 ) :
280                                         data;
281                         } catch( e ) {}
282
283                         // Make sure we set the data so it isn't changed later
284                         jQuery.data( elem, key, data );
285
286                 } else {
287                         data = undefined;
288                 }
289         }
290
291         return data;
292 }
293
294 })( jQuery );