4 rbrace = /^(?:\{.*\}|\[.*\])$/;
9 // Please use with caution
12 // Unique for each copy of jQuery on the page
13 expando: "jQuery" + jQuery.now(),
15 // The following elements throw uncatchable exceptions if you
16 // attempt to add expando properties to them.
19 // Ban all objects except for Flash (which handle expandos)
20 "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
24 data: function( elem, name, data ) {
25 if ( !jQuery.acceptData( elem ) ) {
29 elem = elem == window ?
33 var id = elem[ jQuery.expando ], cache = jQuery.cache, thisCache,
34 isNode = elem.nodeType,
37 if ( !id && typeof name === "string" && data === undefined ) {
41 // Get the data from the object directly
46 // Compute a unique ID for the element
48 elem[ jQuery.expando ] = id = ++jQuery.uuid;
51 // Avoid generating a new cache unless none exists and we
52 // want to manipulate it.
53 if ( typeof name === "object" ) {
55 cache[ id ] = jQuery.extend(cache[ id ], name);
58 store = jQuery.extend(cache[ id ], name);
59 cache[ id ] = function() {
64 } else if ( !cache[ id ] ) {
70 cache[ id ] = function() {
77 thisCache = isNode ? cache[ id ] : cache[ id ]();
79 // Prevent overriding the named cache with undefined values
80 if ( data !== undefined ) {
81 thisCache[ name ] = data;
84 return typeof name === "string" ? thisCache[ name ] : thisCache;
87 removeData: function( elem, name ) {
88 if ( !jQuery.acceptData( elem ) ) {
92 elem = elem == window ?
96 var isNode = elem.nodeType,
97 id = elem[ jQuery.expando ], cache = jQuery.cache;
98 if ( id && !isNode ) {
101 var thisCache = cache[ id ];
103 // If we want to remove a specific section of the element's data
106 // Remove the section of cache data
107 delete thisCache[ name ];
109 // If we've removed all the data, remove the element's cache
110 if ( jQuery.isEmptyObject(thisCache) ) {
111 jQuery.removeData( elem );
115 // Otherwise, we want to remove all of the element's data
117 if ( jQuery.support.deleteExpando || !isNode ) {
118 delete elem[ jQuery.expando ];
120 } else if ( elem.removeAttribute ) {
121 elem.removeAttribute( jQuery.expando );
124 // Completely remove the data cache
131 // A method for determining if a DOM node can handle the data expando
132 acceptData: function( elem ) {
133 if ( elem.nodeName ) {
134 match = jQuery.noData[ elem.nodeName.toLowerCase() ];
137 return !(match === true || elem.getAttribute("classid") !== match);
146 data: function( key, value ) {
147 if ( typeof key === "undefined" ) {
148 return this.length ? jQuery.data( this[0] ) : null;
150 } else if ( typeof key === "object" ) {
151 return this.each(function() {
152 jQuery.data( this, key );
156 var parts = key.split(".");
157 parts[1] = parts[1] ? "." + parts[1] : "";
159 if ( value === undefined ) {
160 var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
162 // Try to fetch any internally stored data first
163 if ( data === undefined && this.length ) {
164 data = jQuery.data( this[0], key );
166 // If nothing was found internally, try to fetch any
167 // data from the HTML5 data-* attribute
168 if ( data === undefined && this[0].nodeType === 1 ) {
169 data = this[0].getAttribute( "data-" + key );
171 if ( typeof data === "string" ) {
173 data = data === "true" ? true :
174 data === "false" ? false :
175 data === "null" ? null :
176 !jQuery.isNaN( data ) ? parseFloat( data ) :
177 rbrace.test( data ) ? jQuery.parseJSON( data ) :
187 return data === undefined && parts[1] ?
188 this.data( parts[0] ) :
192 return this.each(function() {
193 var $this = jQuery( this ), args = [ parts[0], value ];
195 $this.triggerHandler( "setData" + parts[1] + "!", args );
196 jQuery.data( this, key, value );
197 $this.triggerHandler( "changeData" + parts[1] + "!", args );
202 removeData: function( key ) {
203 return this.each(function() {
204 jQuery.removeData( this, key );