Further optimize the empty/remove/cleanData logic.
[jquery.git] / src / data.js
1 var expando = "jQuery" + now(), uuid = 0, windowData = {};
2
3 jQuery.extend({
4         cache: {},
5         
6         expando:expando,
7
8         // The following elements throw uncatchable exceptions if you
9         // attempt to add expando properties to them.
10         noData: {
11                 "embed": true,
12                 "object": true,
13                 "applet": true
14         },
15
16         data: function( elem, name, data ) {
17                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
18                         return;
19                 }
20
21                 elem = elem == window ?
22                         windowData :
23                         elem;
24
25                 var id = elem[ expando ], cache = jQuery.cache, thisCache;
26
27                 if ( !id && typeof name === "string" && data === undefined ) {
28                         return null;
29                 }
30
31                 // Compute a unique ID for the element
32                 if ( !id ) { 
33                         id = ++uuid;
34                 }
35
36                 // Avoid generating a new cache unless none exists and we
37                 // want to manipulate it.
38                 if ( typeof name === "object" ) {
39                         elem[ expando ] = id;
40                         thisCache = cache[ id ] = jQuery.extend(true, {}, name);
41
42                 } else if ( !cache[ id ] ) {
43                         elem[ expando ] = id;
44                         cache[ id ] = {};
45                 }
46
47                 thisCache = cache[ id ];
48
49                 // Prevent overriding the named cache with undefined values
50                 if ( data !== undefined ) {
51                         thisCache[ name ] = data;
52                 }
53
54                 return typeof name === "string" ? thisCache[ name ] : thisCache;
55         },
56
57         removeData: function( elem, name ) {
58                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
59                         return;
60                 }
61
62                 elem = elem == window ?
63                         windowData :
64                         elem;
65
66                 var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];
67
68                 // If we want to remove a specific section of the element's data
69                 if ( name ) {
70                         if ( thisCache ) {
71                                 // Remove the section of cache data
72                                 delete thisCache[ name ];
73
74                                 // If we've removed all the data, remove the element's cache
75                                 if ( jQuery.isEmptyObject(thisCache) ) {
76                                         jQuery.removeData( elem );
77                                 }
78                         }
79
80                 // Otherwise, we want to remove all of the element's data
81                 } else {
82                         // Clean up the element expando
83                         try {
84                                 delete elem[ expando ];
85                         } catch( e ) {
86                                 // IE has trouble directly removing the expando
87                                 // but it's ok with using removeAttribute
88                                 if ( elem.removeAttribute ) {
89                                         elem.removeAttribute( expando );
90                                 }
91                         }
92
93                         // Completely remove the data cache
94                         delete cache[ id ];
95                 }
96         }
97 });
98
99 jQuery.fn.extend({
100         data: function( key, value ) {
101                 if ( typeof key === "undefined" && this.length ) {
102                         return jQuery.data( this[0] );
103
104                 } else if ( typeof key === "object" ) {
105                         return this.each(function() {
106                                 jQuery.data( this, key );
107                         });
108                 }
109
110                 var parts = key.split(".");
111                 parts[1] = parts[1] ? "." + parts[1] : "";
112
113                 if ( value === undefined ) {
114                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
115
116                         if ( data === undefined && this.length ) {
117                                 data = jQuery.data( this[0], key );
118                         }
119                         return data === undefined && parts[1] ?
120                                 this.data( parts[0] ) :
121                                 data;
122                 } else {
123                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function() {
124                                 jQuery.data( this, key, value );
125                         });
126                 }
127         },
128
129         removeData: function( key ) {
130                 return this.each(function() {
131                         jQuery.removeData( this, key );
132                 });
133         }
134 });
135
136 var removeExpando = function( elem ) {
137         delete elem[ expando ];
138 };
139
140 try {
141         var div = document.createElement("div");
142         div[ expando ] = true;
143         delete div[ expando ];
144         
145 } catch( e ) {
146         // IE has trouble directly removing the expando
147         // but it's ok with using removeAttribute
148         removeExpando = function( elem ) {
149                 if ( elem.removeAttribute ) {
150                         elem.removeAttribute( expando );
151                 }
152         };
153 }