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 isNode = elem.nodeType,
34 id = isNode ? elem[ jQuery.expando ] : null,
35 cache = jQuery.cache, thisCache;
37 if ( isNode && !id && typeof name === "string" && data === undefined ) {
41 // Get the data from the object directly
45 // Compute a unique ID for the element
47 elem[ jQuery.expando ] = id = ++jQuery.uuid;
50 // Avoid generating a new cache unless none exists and we
51 // want to manipulate it.
52 if ( typeof name === "object" ) {
54 cache[ id ] = jQuery.extend(cache[ id ], name);
57 jQuery.extend( cache, name );
60 } else if ( isNode && !cache[ id ] ) {
64 thisCache = isNode ? cache[ id ] : cache;
66 // Prevent overriding the named cache with undefined values
67 if ( data !== undefined ) {
68 thisCache[ name ] = data;
71 return typeof name === "string" ? thisCache[ name ] : thisCache;
74 removeData: function( elem, name ) {
75 if ( !jQuery.acceptData( elem ) ) {
79 elem = elem == window ?
83 var isNode = elem.nodeType,
84 id = isNode ? elem[ jQuery.expando ] : elem,
86 thisCache = isNode ? cache[ id ] : id;
88 // If we want to remove a specific section of the element's data
91 // Remove the section of cache data
92 delete thisCache[ name ];
94 // If we've removed all the data, remove the element's cache
95 if ( isNode && jQuery.isEmptyObject(thisCache) ) {
96 jQuery.removeData( elem );
100 // Otherwise, we want to remove all of the element's data
102 if ( isNode && jQuery.support.deleteExpando ) {
103 delete elem[ jQuery.expando ];
105 } else if ( elem.removeAttribute ) {
106 elem.removeAttribute( jQuery.expando );
108 // Completely remove the data cache
109 } else if ( isNode ) {
112 // Remove all fields from the object
114 for ( var n in elem ) {
121 // A method for determining if a DOM node can handle the data expando
122 acceptData: function( elem ) {
123 if ( elem.nodeName ) {
124 match = jQuery.noData[ elem.nodeName.toLowerCase() ];
127 return !(match === true || elem.getAttribute("classid") !== match);
136 data: function( key, value ) {
137 if ( typeof key === "undefined" ) {
138 return this.length ? jQuery.data( this[0] ) : null;
140 } else if ( typeof key === "object" ) {
141 return this.each(function() {
142 jQuery.data( this, key );
146 var parts = key.split(".");
147 parts[1] = parts[1] ? "." + parts[1] : "";
149 if ( value === undefined ) {
150 var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
152 // Try to fetch any internally stored data first
153 if ( data === undefined && this.length ) {
154 data = jQuery.data( this[0], key );
156 // If nothing was found internally, try to fetch any
157 // data from the HTML5 data-* attribute
158 if ( data === undefined && this[0].nodeType === 1 ) {
159 data = this[0].getAttribute( "data-" + key );
161 if ( typeof data === "string" ) {
163 data = data === "true" ? true :
164 data === "false" ? false :
165 data === "null" ? null :
166 !jQuery.isNaN( data ) ? parseFloat( data ) :
167 rbrace.test( data ) ? jQuery.parseJSON( data ) :
177 return data === undefined && parts[1] ?
178 this.data( parts[0] ) :
182 return this.each(function() {
183 var $this = jQuery( this ), args = [ parts[0], value ];
185 $this.triggerHandler( "setData" + parts[1] + "!", args );
186 jQuery.data( this, key, value );
187 $this.triggerHandler( "changeData" + parts[1] + "!", args );
192 removeData: function( key ) {
193 return this.each(function() {
194 jQuery.removeData( this, key );