This is why you don't do commits when you're tired. Fixing bugs from 59802928566b6be3...
[jquery.git] / src / data.js
1 var expando = "jQuery" + now(), uuid = 0, windowData = {};
2 var emptyObject = {};
3
4 jQuery.extend({
5         cache: {},
6         
7         expando:expando,
8
9         // The following elements throw uncatchable exceptions if you
10         // attempt to add expando properties to them.
11         noData: {
12                 "embed": true,
13                 "object": true,
14                 "applet": true
15         },
16
17         data: function( elem, name, data ) {
18                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
19                         return;
20                 }
21
22                 elem = elem == window ?
23                         windowData :
24                         elem;
25
26                 var id = elem[ expando ], cache = jQuery.cache, thisCache;
27
28                 // Handle the case where there's no name immediately
29                 if ( !name && !id ) {
30                         return null;
31                 }
32
33                 // Compute a unique ID for the element
34                 if ( !id ) { 
35                         id = ++uuid;
36                 }
37
38                 // Avoid generating a new cache unless none exists and we
39                 // want to manipulate it.
40                 if ( cache[ id ] ) {
41                         thisCache = cache[ id ];
42                 } else if ( typeof data === "undefined" ) {
43                         thisCache = emptyObject;
44                 } else {
45                         thisCache = cache[ id ] = {};
46                 }
47                 
48                 // Prevent overriding the named cache with undefined values
49                 if ( data !== undefined ) {
50                         elem[ expando ] = id;
51                         thisCache[ name ] = data;
52                 }
53                 
54                 return name ? 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
105                 var parts = key.split(".");
106                 parts[1] = parts[1] ? "." + parts[1] : "";
107
108                 if ( value === undefined ) {
109                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
110
111                         if ( data === undefined && this.length ) {
112                                 data = jQuery.data( this[0], key );
113                         }
114                         return data === undefined && parts[1] ?
115                                 this.data( parts[0] ) :
116                                 data;
117                 } else {
118                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function(){
119                                 jQuery.data( this, key, value );
120                         });
121                 }
122         },
123
124         removeData: function( key ){
125                 return this.each(function(){
126                         jQuery.removeData( this, key );
127                 });
128         }
129 });