Only try to get data attributes for a jQuery-wrapped object if it is actually an...
[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                 var data = null;
138
139                 if ( typeof key === "undefined" ) {
140                         if ( this.length ) {
141                                 data = jQuery.data( this[0] );
142
143                                 if ( this[0].nodeType === 1 ) {
144                                         var attr = this[0].attributes, name;
145                                         for ( var i = 0, l = attr.length; i < l; i++ ) {
146                                                 name = attr[i].name;
147         
148                                                 if ( name.indexOf( "data-" ) === 0 ) {
149                                                         name = name.substr( 5 );
150                                                         dataAttr( this[0], name, data[ name ] );
151                                                 }
152                                         }
153                                 }
154                         }
155
156                         return data;
157
158                 } else if ( typeof key === "object" ) {
159                         return this.each(function() {
160                                 jQuery.data( this, key );
161                         });
162                 }
163
164                 var parts = key.split(".");
165                 parts[1] = parts[1] ? "." + parts[1] : "";
166
167                 if ( value === undefined ) {
168                         data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
169
170                         // Try to fetch any internally stored data first
171                         if ( data === undefined && this.length ) {
172                                 data = jQuery.data( this[0], key );
173                                 data = dataAttr( this[0], key, data );
174                         }
175
176                         return data === undefined && parts[1] ?
177                                 this.data( parts[0] ) :
178                                 data;
179
180                 } else {
181                         return this.each(function() {
182                                 var $this = jQuery( this ),
183                                         args = [ parts[0], value ];
184
185                                 $this.triggerHandler( "setData" + parts[1] + "!", args );
186                                 jQuery.data( this, key, value );
187                                 $this.triggerHandler( "changeData" + parts[1] + "!", args );
188                         });
189                 }
190         },
191
192         removeData: function( key ) {
193                 return this.each(function() {
194                         jQuery.removeData( this, key );
195                 });
196         }
197 });
198
199 function dataAttr( elem, key, data ) {
200         // If nothing was found internally, try to fetch any
201         // data from the HTML5 data-* attribute
202         if ( data === undefined && elem.nodeType === 1 ) {
203                 data = elem.getAttribute( "data-" + key );
204
205                 if ( typeof data === "string" ) {
206                         try {
207                                 data = data === "true" ? true :
208                                 data === "false" ? false :
209                                 data === "null" ? null :
210                                 !jQuery.isNaN( data ) ? parseFloat( data ) :
211                                         rbrace.test( data ) ? jQuery.parseJSON( data ) :
212                                         data;
213                         } catch( e ) {}
214
215                         // Make sure we set the data so it isn't changed later
216                         jQuery.data( elem, key, data );
217
218                 } else {
219                         data = undefined;
220                 }
221         }
222
223         return data;
224 }
225
226 })( jQuery );