Escape RegExp braces to sooth JSLint's temper.
[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                 "object": true,
20                 "applet": true
21         },
22
23         data: function( elem, name, data ) {
24                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
25                         return;
26                 }
27
28                 elem = elem == window ?
29                         windowData :
30                         elem;
31
32                 var id = elem[ jQuery.expando ], cache = jQuery.cache, thisCache,
33                         isNode = elem.nodeType,
34                         store;
35
36                 if ( !id && typeof name === "string" && data === undefined ) {
37                         return;
38                 }
39
40                 // Get the data from the object directly
41                 if ( !isNode ) {
42                         cache = elem;
43                         id = jQuery.expando;
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(true, {}, name);
55                         } else {
56                                 store = jQuery.extend(true, {}, name);
57                                 cache[ id ] = function() {
58                                         return store;
59                                 };
60                         }
61
62                 } else if ( !cache[ id ] ) {
63                         if ( isNode ) {
64                                 cache[ id ] = {};
65                         } else {
66                                 store = {};
67                                 cache[ id ] = function() {
68                                         return store;
69                                 };
70                         }
71                         
72                 }
73
74                 thisCache = isNode ? cache[ id ] : cache[ id ]();
75
76                 // Prevent overriding the named cache with undefined values
77                 if ( data !== undefined ) {
78                         thisCache[ name ] = data;
79                 }
80
81                 return typeof name === "string" ? thisCache[ name ] : thisCache;
82         },
83
84         removeData: function( elem, name ) {
85                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
86                         return;
87                 }
88
89                 elem = elem == window ?
90                         windowData :
91                         elem;
92
93                 var isNode = elem.nodeType,
94                         id = elem[ jQuery.expando ], cache = jQuery.cache;
95                 if ( id && !isNode ) {
96                         id = id();
97                 }
98                 var thisCache = cache[ id ];
99
100                 // If we want to remove a specific section of the element's data
101                 if ( name ) {
102                         if ( thisCache ) {
103                                 // Remove the section of cache data
104                                 delete thisCache[ name ];
105
106                                 // If we've removed all the data, remove the element's cache
107                                 if ( jQuery.isEmptyObject(thisCache) ) {
108                                         jQuery.removeData( elem );
109                                 }
110                         }
111
112                 // Otherwise, we want to remove all of the element's data
113                 } else {
114                         if ( jQuery.support.deleteExpando || !isNode ) {
115                                 delete elem[ jQuery.expando ];
116
117                         } else if ( elem.removeAttribute ) {
118                                 elem.removeAttribute( jQuery.expando );
119                         }
120
121                         // Completely remove the data cache
122                         if ( isNode ) {
123                                 delete cache[ id ];
124                         }
125                 }
126         }
127 });
128
129 jQuery.fn.extend({
130         data: function( key, value ) {
131                 if ( typeof key === "undefined" && this.length ) {
132                         return jQuery.data( this[0] );
133
134                 } else if ( typeof key === "object" ) {
135                         return this.each(function() {
136                                 jQuery.data( this, key );
137                         });
138                 }
139
140                 var parts = key.split(".");
141                 parts[1] = parts[1] ? "." + parts[1] : "";
142
143                 if ( value === undefined ) {
144                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
145
146                         // Try to fetch any internally stored data first
147                         if ( data === undefined && this.length ) {
148                                 data = jQuery.data( this[0], key );
149
150                                 // If nothing was found internally, try to fetch any
151                                 // data from the HTML5 data-* attribute
152                                 if ( data === undefined && this[0].nodeType === 1 ) {
153                                         data = this[0].getAttribute( "data-" + key );
154
155                                         if ( typeof data === "string" ) {
156                                                 try {
157                                                         data = data === "true" ? true :
158                                                                 data === "false" ? false :
159                                                                 data === "null" ? null :
160                                                                 !isNaN( data ) ? parseFloat( data ) :
161                                                                 rbrace.test( data ) ? jQuery.parseJSON( data ) :
162                                                                 data;
163                                                 } catch( e ) {}
164
165                                         } else {
166                                                 data = undefined;
167                                         }
168                                 }
169                         }
170
171                         return data === undefined && parts[1] ?
172                                 this.data( parts[0] ) :
173                                 data;
174
175                 } else {
176                         return this.each(function() {
177                                 var $this = jQuery( this ), args = [ parts[0], value ];
178
179                                 $this.triggerHandler( "setData" + parts[1] + "!", args );
180                                 jQuery.data( this, key, value );
181                                 $this.triggerHandler( "changeData" + parts[1] + "!", args );
182                         });
183                 }
184         },
185
186         removeData: function( key ) {
187                 return this.each(function() {
188                         jQuery.removeData( this, key );
189                 });
190         }
191 });
192
193 })( jQuery );