Edits to README after Karl's review
[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
32                 if ( !id && typeof name === "string" && data === undefined ) {
33                         return;
34                 }
35
36                 // Get the data from the object directly
37                 if ( !isNode ) {
38                         cache = elem;
39                         id = jQuery.expando;
40
41                 // Compute a unique ID for the element
42                 } else if ( !id ) {
43                         elem[ jQuery.expando ] = id = ++jQuery.uuid;
44                 }
45
46                 // Avoid generating a new cache unless none exists and we
47                 // want to manipulate it.
48                 if ( typeof name === "object" ) {
49                         cache[ id ] = jQuery.extend(true, {}, name);
50
51                 } else if ( !cache[ id ] ) {
52                         cache[ id ] = {};
53                 }
54
55                 thisCache = cache[ id ];
56
57                 // Prevent overriding the named cache with undefined values
58                 if ( data !== undefined ) {
59                         thisCache[ name ] = data;
60                 }
61
62                 return typeof name === "string" ? thisCache[ name ] : thisCache;
63         },
64
65         removeData: function( elem, name ) {
66                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
67                         return;
68                 }
69
70                 elem = elem == window ?
71                         windowData :
72                         elem;
73
74                 var id = elem[ jQuery.expando ], cache = jQuery.cache,
75                         isNode = elem.nodeType, thisCache = isNode ? cache[ id ] : id;
76
77                 // If we want to remove a specific section of the element's data
78                 if ( name ) {
79                         if ( thisCache ) {
80                                 // Remove the section of cache data
81                                 delete thisCache[ name ];
82
83                                 // If we've removed all the data, remove the element's cache
84                                 if ( jQuery.isEmptyObject(thisCache) ) {
85                                         jQuery.removeData( elem );
86                                 }
87                         }
88
89                 // Otherwise, we want to remove all of the element's data
90                 } else {
91                         if ( jQuery.support.deleteExpando || !isNode ) {
92                                 delete elem[ jQuery.expando ];
93
94                         } else if ( elem.removeAttribute ) {
95                                 elem.removeAttribute( jQuery.expando );
96                         }
97
98                         // Completely remove the data cache
99                         if ( isNode ) {
100                                 delete cache[ id ];
101                         }
102                 }
103         }
104 });
105
106 jQuery.fn.extend({
107         data: function( key, value ) {
108                 if ( typeof key === "undefined" && this.length ) {
109                         return jQuery.data( this[0] );
110
111                 } else if ( typeof key === "object" ) {
112                         return this.each(function() {
113                                 jQuery.data( this, key );
114                         });
115                 }
116
117                 var parts = key.split(".");
118                 parts[1] = parts[1] ? "." + parts[1] : "";
119
120                 if ( value === undefined ) {
121                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
122
123                         if ( data === undefined && this.length ) {
124                                 data = jQuery.data( this[0], key );
125                         }
126                         return data === undefined && parts[1] ?
127                                 this.data( parts[0] ) :
128                                 data;
129                 } else {
130                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function() {
131                                 jQuery.data( this, key, value );
132                         });
133                 }
134         },
135
136         removeData: function( key ) {
137                 return this.each(function() {
138                         jQuery.removeData( this, key );
139                 });
140         }
141 });