Fixing the problem with data-* based numbers as mention by @jasonwebster in 20673d7.
[jquery.git] / src / data.js
1 (function( jQuery ) {
2
3 var windowData = {},
4         rnum = /^-?[0-9.]+$/;
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 ( data != null ) {
156                                                 data = data === "true" ? true :
157                                                         data === "false" ? false :
158                                                         data === "null" ? null :
159                                                         rnum.test( data ) ? parseFloat( data ) :
160                                                         data;
161                                         }
162                                 }
163                         }
164
165                         return data === undefined && parts[1] ?
166                                 this.data( parts[0] ) :
167                                 data;
168
169                 } else {
170                         return this.each(function() {
171                                 var $this = jQuery( this ), args = [ parts[0], value ];
172
173                                 $this.triggerHandler( "setData" + parts[1] + "!", args );
174                                 jQuery.data( this, key, value );
175                                 $this.triggerHandler( "changeData" + parts[1] + "!", args );
176                         });
177                 }
178         },
179
180         removeData: function( key ) {
181                 return this.each(function() {
182                         jQuery.removeData( this, key );
183                 });
184         }
185 });
186
187 })( jQuery );