Fix tabs vs spaces in initial workaround commit
[jquery.git] / src / data.js
1 (function( jQuery ) {
2
3 var windowData = {},
4         rbrace = /^(?:\{.*\}|\[.*\])$/;
5
6 jQuery.extend({
7         cache: {},
8
9         // Please use with caution
10         uuid: 0,
11
12         // Unique for each copy of jQuery on the page
13         // Non-digits removed to match rinlinejQuery
14         expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ),
15
16         // The following elements throw uncatchable exceptions if you
17         // attempt to add expando properties to them.
18         noData: {
19                 "embed": true,
20                 // Ban all objects except for Flash (which handle expandos)
21                 "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
22                 "applet": true
23         },
24
25         hasData: function( elem ) {
26                 if ( elem.nodeType ) {
27                         elem = jQuery.cache[ elem[jQuery.expando] ];
28                 }
29
30                 return !!elem && !jQuery.isEmptyObject(elem);
31         },
32
33         data: function( elem, name, data ) {
34                 if ( !jQuery.acceptData( elem ) ) {
35                         return;
36                 }
37
38                 elem = elem == window ?
39                         windowData :
40                         elem;
41
42                 var isNode = elem.nodeType,
43                         id = isNode ? elem[ jQuery.expando ] : null,
44                         cache = jQuery.cache, thisCache;
45
46                 if ( isNode && !id && typeof name === "string" && data === undefined ) {
47                         return;
48                 }
49
50                 // Get the data from the object directly
51                 if ( !isNode ) {
52                         cache = elem;
53
54                 // Compute a unique ID for the element
55                 } else if ( !id ) {
56                         elem[ jQuery.expando ] = id = ++jQuery.uuid;
57                 }
58
59                 // Avoid generating a new cache unless none exists and we
60                 // want to manipulate it.
61                 if ( typeof name === "object" ) {
62                         if ( isNode ) {
63                                 cache[ id ] = jQuery.extend(cache[ id ], name);
64
65                         } else {
66                                 jQuery.extend( cache, name );
67                         }
68
69                 } else if ( isNode && !cache[ id ] ) {
70                         cache[ id ] = {};
71                 }
72
73                 thisCache = isNode ? cache[ id ] : cache;
74
75                 // Prevent overriding the named cache with undefined values
76                 if ( data !== undefined ) {
77                         thisCache[ name ] = data;
78                 }
79
80                 return typeof name === "string" ? thisCache[ name ] : thisCache;
81         },
82
83         removeData: function( elem, name ) {
84                 if ( !jQuery.acceptData( elem ) ) {
85                         return;
86                 }
87
88                 elem = elem == window ?
89                         windowData :
90                         elem;
91
92                 var isNode = elem.nodeType,
93                         id = isNode ? elem[ jQuery.expando ] : elem,
94                         cache = jQuery.cache,
95                         thisCache = isNode ? cache[ id ] : id;
96
97                 // If we want to remove a specific section of the element's data
98                 if ( name ) {
99                         if ( thisCache ) {
100                                 // Remove the section of cache data
101                                 delete thisCache[ name ];
102
103                                 // If we've removed all the data, remove the element's cache
104                                 if ( isNode && jQuery.isEmptyObject(thisCache) ) {
105                                         jQuery.removeData( elem );
106                                 }
107                         }
108
109                 // Otherwise, we want to remove all of the element's data
110                 } else {
111                         if ( isNode && jQuery.support.deleteExpando ) {
112                                 delete elem[ jQuery.expando ];
113
114                         } else if ( elem.removeAttribute ) {
115                                 elem.removeAttribute( jQuery.expando );
116
117                         // Completely remove the data cache
118                         } else if ( isNode ) {
119                                 delete cache[ id ];
120
121                         // Remove all fields from the object
122                         } else {
123                                 for ( var n in elem ) {
124                                         delete elem[ n ];
125                                 }
126                         }
127                 }
128         },
129
130         // A method for determining if a DOM node can handle the data expando
131         acceptData: function( elem ) {
132                 if ( elem.nodeName ) {
133                         var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
134
135                         if ( match ) {
136                                 return !(match === true || elem.getAttribute("classid") !== match);
137                         }
138                 }
139
140                 return true;
141         }
142 });
143
144 jQuery.fn.extend({
145         data: function( key, value ) {
146                 var data = null;
147
148                 if ( typeof key === "undefined" ) {
149                         if ( this.length ) {
150                                 data = jQuery.data( this[0] );
151
152                                 if ( this[0].nodeType === 1 ) {
153                                         var attr = this[0].attributes, name;
154                                         for ( var i = 0, l = attr.length; i < l; i++ ) {
155                                                 name = attr[i].name;
156
157                                                 if ( name.indexOf( "data-" ) === 0 ) {
158                                                         name = name.substr( 5 );
159                                                         dataAttr( this[0], name, data[ name ] );
160                                                 }
161                                         }
162                                 }
163                         }
164
165                         return data;
166
167                 } else if ( typeof key === "object" ) {
168                         return this.each(function() {
169                                 jQuery.data( this, key );
170                         });
171                 }
172
173                 var parts = key.split(".");
174                 parts[1] = parts[1] ? "." + parts[1] : "";
175
176                 if ( value === undefined ) {
177                         data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
178
179                         // Try to fetch any internally stored data first
180                         if ( data === undefined && this.length ) {
181                                 data = jQuery.data( this[0], key );
182                                 data = dataAttr( this[0], key, data );
183                         }
184
185                         return data === undefined && parts[1] ?
186                                 this.data( parts[0] ) :
187                                 data;
188
189                 } else {
190                         return this.each(function() {
191                                 var $this = jQuery( this ),
192                                         args = [ parts[0], value ];
193
194                                 $this.triggerHandler( "setData" + parts[1] + "!", args );
195                                 jQuery.data( this, key, value );
196                                 $this.triggerHandler( "changeData" + parts[1] + "!", args );
197                         });
198                 }
199         },
200
201         removeData: function( key ) {
202                 return this.each(function() {
203                         jQuery.removeData( this, key );
204                 });
205         }
206 });
207
208 function dataAttr( elem, key, data ) {
209         // If nothing was found internally, try to fetch any
210         // data from the HTML5 data-* attribute
211         if ( data === undefined && elem.nodeType === 1 ) {
212                 data = elem.getAttribute( "data-" + key );
213
214                 if ( typeof data === "string" ) {
215                         try {
216                                 data = data === "true" ? true :
217                                 data === "false" ? false :
218                                 data === "null" ? null :
219                                 !jQuery.isNaN( data ) ? parseFloat( data ) :
220                                         rbrace.test( data ) ? jQuery.parseJSON( data ) :
221                                         data;
222                         } catch( e ) {}
223
224                         // Make sure we set the data so it isn't changed later
225                         jQuery.data( elem, key, data );
226
227                 } else {
228                         data = undefined;
229                 }
230         }
231
232         return data;
233 }
234
235 })( jQuery );