Change the way jQuery.data works so that there is no longer a chance of collision...
[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                 return getByName ? thisCache[ name ] : thisCache;
97         },
98
99         removeData: function( elem, name, pvt /* Internal Use Only */ ) {
100                 if ( !jQuery.acceptData( elem ) ) {
101                         return;
102                 }
103
104                 var internalKey = jQuery.expando, isNode = elem.nodeType,
105
106                         // See jQuery.data for more information
107                         cache = isNode ? jQuery.cache : elem,
108
109                         // See jQuery.data for more information
110                         id = isNode ? elem[ jQuery.expando ] : jQuery.expando;
111
112                 // If there is already no cache entry for this object, there is no
113                 // purpose in continuing
114                 if ( !cache[ id ] ) {
115                         return;
116                 }
117
118                 if ( name ) {
119                         var thisCache = pvt ? cache[ id ][ internalKey ] : cache[ id ];
120
121                         if ( thisCache ) {
122                                 delete thisCache[ name ];
123
124                                 // If there is no data left in the cache, we want to continue
125                                 // and let the cache object itself get destroyed
126                                 if ( !jQuery.isEmptyObject(thisCache) ) {
127                                         return;
128                                 }
129                         }
130                 }
131
132                 // See jQuery.data for more information
133                 if ( pvt ) {
134                         delete cache[ id ][ internalKey ];
135
136                         // Don't destroy the parent cache unless the internal data object
137                         // had been the only thing left in it
138                         if ( !jQuery.isEmptyObject(cache[ id ]) ) {
139                                 return;
140                         }
141                 }
142
143                 var internalCache = cache[ id ][ internalKey ];
144
145                 // Browsers that fail expando deletion also refuse to delete expandos on
146                 // the window, but it will allow it on all other JS objects; other browsers
147                 // don't care
148                 if ( jQuery.support.deleteExpando || cache != window ) {
149                         delete cache[ id ];
150                 } else {
151                         cache[ id ] = null;
152                 }
153
154                 // We destroyed the entire user cache at once because it's faster than
155                 // iterating through each key, but we need to continue to persist internal
156                 // data if it existed
157                 if ( internalCache ) {
158                         cache[ id ] = {};
159                         cache[ id ][ internalKey ] = internalCache;
160
161                 // Otherwise, we need to eliminate the expando on the node to avoid
162                 // false lookups in the cache for entries that no longer exist
163                 } else if ( isNode ) {
164                         // IE does not allow us to delete expando properties from nodes,
165                         // nor does it have a removeAttribute function on Document nodes;
166                         // we must handle all of these cases
167                         if ( jQuery.support.deleteExpando ) {
168                                 delete elem[ jQuery.expando ];
169                         } else if ( elem.removeAttribute ) {
170                                 elem.removeAttribute( jQuery.expando );
171                         } else {
172                                 elem[ jQuery.expando ] = null;
173                         }
174                 }
175         },
176
177         // For internal use only.
178         _data: function( elem, name, data ) {
179                 return jQuery.data( elem, name, data, true );
180         },
181
182         // A method for determining if a DOM node can handle the data expando
183         acceptData: function( elem ) {
184                 if ( elem.nodeName ) {
185                         var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
186
187                         if ( match ) {
188                                 return !(match === true || elem.getAttribute("classid") !== match);
189                         }
190                 }
191
192                 return true;
193         }
194 });
195
196 jQuery.fn.extend({
197         data: function( key, value ) {
198                 var data = null;
199
200                 if ( typeof key === "undefined" ) {
201                         if ( this.length ) {
202                                 data = jQuery.data( this[0] );
203
204                                 if ( this[0].nodeType === 1 ) {
205                                         var attr = this[0].attributes, name;
206                                         for ( var i = 0, l = attr.length; i < l; i++ ) {
207                                                 name = attr[i].name;
208
209                                                 if ( name.indexOf( "data-" ) === 0 ) {
210                                                         name = name.substr( 5 );
211                                                         dataAttr( this[0], name, data[ name ] );
212                                                 }
213                                         }
214                                 }
215                         }
216
217                         return data;
218
219                 } else if ( typeof key === "object" ) {
220                         return this.each(function() {
221                                 jQuery.data( this, key );
222                         });
223                 }
224
225                 var parts = key.split(".");
226                 parts[1] = parts[1] ? "." + parts[1] : "";
227
228                 if ( value === undefined ) {
229                         data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
230
231                         // Try to fetch any internally stored data first
232                         if ( data === undefined && this.length ) {
233                                 data = jQuery.data( this[0], key );
234                                 data = dataAttr( this[0], key, data );
235                         }
236
237                         return data === undefined && parts[1] ?
238                                 this.data( parts[0] ) :
239                                 data;
240
241                 } else {
242                         return this.each(function() {
243                                 var $this = jQuery( this ),
244                                         args = [ parts[0], value ];
245
246                                 $this.triggerHandler( "setData" + parts[1] + "!", args );
247                                 jQuery.data( this, key, value );
248                                 $this.triggerHandler( "changeData" + parts[1] + "!", args );
249                         });
250                 }
251         },
252
253         removeData: function( key ) {
254                 return this.each(function() {
255                         jQuery.removeData( this, key );
256                 });
257         }
258 });
259
260 function dataAttr( elem, key, data ) {
261         // If nothing was found internally, try to fetch any
262         // data from the HTML5 data-* attribute
263         if ( data === undefined && elem.nodeType === 1 ) {
264                 data = elem.getAttribute( "data-" + key );
265
266                 if ( typeof data === "string" ) {
267                         try {
268                                 data = data === "true" ? true :
269                                 data === "false" ? false :
270                                 data === "null" ? null :
271                                 !jQuery.isNaN( data ) ? parseFloat( data ) :
272                                         rbrace.test( data ) ? jQuery.parseJSON( data ) :
273                                         data;
274                         } catch( e ) {}
275
276                         // Make sure we set the data so it isn't changed later
277                         jQuery.data( elem, key, data );
278
279                 } else {
280                         data = undefined;
281                 }
282         }
283
284         return data;
285 }
286
287 })( jQuery );