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