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