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