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