Made it so that you no longer need to build jQuery in order to run the test suite...
[jquery.git] / src / data.js
1 var expando = "jQuery" + 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[ jQuery.expando ], cache = jQuery.cache, thisCache,
26                         isNode = elem.nodeType;
27
28                 if ( !id && typeof name === "string" && data === undefined ) {
29                         return;
30                 }
31
32                 // Get the data from the object directly
33                 if ( !isNode ) {
34                         cache = elem;
35                         id = jQuery.expando;
36
37                 // Compute a unique ID for the element
38                 } else if ( !id ) {
39                         elem[ jQuery.expando ] = id = ++uuid;
40                 }
41
42                 // Avoid generating a new cache unless none exists and we
43                 // want to manipulate it.
44                 if ( typeof name === "object" ) {
45                         cache[ id ] = jQuery.extend(true, {}, name);
46
47                 } else if ( !cache[ id ] ) {
48                         cache[ id ] = {};
49                 }
50
51                 thisCache = cache[ id ];
52
53                 // Prevent overriding the named cache with undefined values
54                 if ( data !== undefined ) {
55                         thisCache[ name ] = data;
56                 }
57
58                 return typeof name === "string" ? thisCache[ name ] : thisCache;
59         },
60
61         removeData: function( elem, name ) {
62                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
63                         return;
64                 }
65
66                 elem = elem == window ?
67                         windowData :
68                         elem;
69
70                 var id = elem[ jQuery.expando ], cache = jQuery.cache,
71                         isNode = elem.nodeType, thisCache = isNode ? cache[ id ] : id;
72
73                 // If we want to remove a specific section of the element's data
74                 if ( name ) {
75                         if ( thisCache ) {
76                                 // Remove the section of cache data
77                                 delete thisCache[ name ];
78
79                                 // If we've removed all the data, remove the element's cache
80                                 if ( jQuery.isEmptyObject(thisCache) ) {
81                                         jQuery.removeData( elem );
82                                 }
83                         }
84
85                 // Otherwise, we want to remove all of the element's data
86                 } else {
87                         if ( jQuery.support.deleteExpando || !isNode ) {
88                                 delete elem[ jQuery.expando ];
89
90                         } else if ( elem.removeAttribute ) {
91                                 elem.removeAttribute( jQuery.expando );
92                         }
93
94                         // Completely remove the data cache
95                         if ( isNode ) {
96                                 delete cache[ id ];
97                         }
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 });