Make sure that .html(Function) gets the correct previous value.
[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 ( typeof name === "object" ) {
41                         elem[ expando ] = id;
42                         thisCache = cache[ id ] = jQuery.extend(true, {}, name);
43                 } else if ( cache[ id ] ) {
44                         thisCache = cache[ id ];
45                 } else if ( typeof data === "undefined" ) {
46                         thisCache = emptyObject;
47                 } else {
48                         thisCache = cache[ id ] = {};
49                 }
50
51                 // Prevent overriding the named cache with undefined values
52                 if ( data !== undefined ) {
53                         elem[ expando ] = id;
54                         thisCache[ name ] = data;
55                 }
56
57                 return typeof name === "string" ? thisCache[ name ] : thisCache;
58         },
59
60         removeData: function( elem, name ) {
61                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
62                         return;
63                 }
64
65                 elem = elem == window ?
66                         windowData :
67                         elem;
68
69                 var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];
70
71                 // If we want to remove a specific section of the element's data
72                 if ( name ) {
73                         if ( thisCache ) {
74                                 // Remove the section of cache data
75                                 delete thisCache[ name ];
76
77                                 // If we've removed all the data, remove the element's cache
78                                 if ( jQuery.isEmptyObject(thisCache) ) {
79                                         jQuery.removeData( elem );
80                                 }
81                         }
82
83                 // Otherwise, we want to remove all of the element's data
84                 } else {
85                         // Clean up the element expando
86                         try {
87                                 delete elem[ expando ];
88                         } catch( e ) {
89                                 // IE has trouble directly removing the expando
90                                 // but it's ok with using removeAttribute
91                                 if ( elem.removeAttribute ) {
92                                         elem.removeAttribute( expando );
93                                 }
94                         }
95
96                         // Completely remove the data cache
97                         delete cache[ id ];
98                 }
99         }
100 });
101
102 jQuery.fn.extend({
103         data: function( key, value ) {
104                 if ( typeof key === "undefined" && this.length ) {
105                         return jQuery.data( this[0] );
106
107                 } else if ( typeof key === "object" ) {
108                         return this.each(function() {
109                                 jQuery.data( this, key );
110                         });
111                 }
112
113                 var parts = key.split(".");
114                 parts[1] = parts[1] ? "." + parts[1] : "";
115
116                 if ( value === undefined ) {
117                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
118
119                         if ( data === undefined && this.length ) {
120                                 data = jQuery.data( this[0], key );
121                         }
122                         return data === undefined && parts[1] ?
123                                 this.data( parts[0] ) :
124                                 data;
125                 } else {
126                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function() {
127                                 jQuery.data( this, key, value );
128                         });
129                 }
130         },
131
132         removeData: function( key ) {
133                 return this.each(function() {
134                         jQuery.removeData( this, key );
135                 });
136         }
137 });