Merge branch 'bug7141' of http://github.com/csnover/jquery into csnover-bug7141
[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                         return this.length ? jQuery.data( this[0] ) : null;
139
140                 } else if ( typeof key === "object" ) {
141                         return this.each(function() {
142                                 jQuery.data( this, key );
143                         });
144                 }
145
146                 var parts = key.split(".");
147                 parts[1] = parts[1] ? "." + parts[1] : "";
148
149                 if ( value === undefined ) {
150                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
151
152                         // Try to fetch any internally stored data first
153                         if ( data === undefined && this.length ) {
154                                 data = jQuery.data( this[0], key );
155
156                                 // If nothing was found internally, try to fetch any
157                                 // data from the HTML5 data-* attribute
158                                 if ( data === undefined && this[0].nodeType === 1 ) {
159                                         data = this[0].getAttribute( "data-" + key );
160
161                                         if ( typeof data === "string" ) {
162                                                 try {
163                                                         data = data === "true" ? true :
164                                                                 data === "false" ? false :
165                                                                 data === "null" ? null :
166                                                                 !jQuery.isNaN( data ) ? parseFloat( data ) :
167                                                                 rbrace.test( data ) ? jQuery.parseJSON( data ) :
168                                                                 data;
169                                                 } catch( e ) {}
170
171                                         } else {
172                                                 data = undefined;
173                                         }
174                                 }
175                         }
176
177                         return data === undefined && parts[1] ?
178                                 this.data( parts[0] ) :
179                                 data;
180
181                 } else {
182                         return this.each(function() {
183                                 var $this = jQuery( this ), 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 })( jQuery );