4 rbrace = /^(?:\{.*\}|\[.*\])$/;
9 // Please use with caution
12 // Unique for each copy of jQuery on the page
13 // Non-digits removed to match rinlinejQuery
14 expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ),
16 // The following elements throw uncatchable exceptions if you
17 // attempt to add expando properties to them.
20 // Ban all objects except for Flash (which handle expandos)
21 "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
25 hasData: function( elem ) {
26 if ( elem.nodeType ) {
27 elem = jQuery.cache[ elem[jQuery.expando] ];
30 return !!elem && !jQuery.isEmptyObject(elem);
33 data: function( elem, name, data ) {
34 if ( !jQuery.acceptData( elem ) ) {
38 elem = elem == window ?
42 var isNode = elem.nodeType,
43 id = isNode ? elem[ jQuery.expando ] : null,
44 cache = jQuery.cache, thisCache;
46 if ( isNode && !id && typeof name === "string" && data === undefined ) {
50 // Get the data from the object directly
54 // Compute a unique ID for the element
56 elem[ jQuery.expando ] = id = ++jQuery.uuid;
59 // Avoid generating a new cache unless none exists and we
60 // want to manipulate it.
61 if ( typeof name === "object" ) {
63 cache[ id ] = jQuery.extend(cache[ id ], name);
66 jQuery.extend( cache, name );
69 } else if ( isNode && !cache[ id ] ) {
73 thisCache = isNode ? cache[ id ] : cache;
75 // Prevent overriding the named cache with undefined values
76 if ( data !== undefined ) {
77 thisCache[ name ] = data;
80 return typeof name === "string" ? thisCache[ name ] : thisCache;
83 removeData: function( elem, name ) {
84 if ( !jQuery.acceptData( elem ) ) {
88 elem = elem == window ?
92 var isNode = elem.nodeType,
93 id = isNode ? elem[ jQuery.expando ] : elem,
95 thisCache = isNode ? cache[ id ] : id;
97 // If we want to remove a specific section of the element's data
100 // Remove the section of cache data
101 delete thisCache[ name ];
103 // If we've removed all the data, remove the element's cache
104 if ( isNode && jQuery.isEmptyObject(thisCache) ) {
105 jQuery.removeData( elem );
109 // Otherwise, we want to remove all of the element's data
111 if ( isNode && jQuery.support.deleteExpando ) {
112 delete elem[ jQuery.expando ];
114 } else if ( elem.removeAttribute ) {
115 elem.removeAttribute( jQuery.expando );
117 // Completely remove the data cache
118 } else if ( isNode ) {
121 // Remove all fields from the object
123 for ( var n in elem ) {
130 // A method for determining if a DOM node can handle the data expando
131 acceptData: function( elem ) {
132 if ( elem.nodeName ) {
133 var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
136 return !(match === true || elem.getAttribute("classid") !== match);
145 data: function( key, value ) {
148 if ( typeof key === "undefined" ) {
150 data = jQuery.data( this[0] );
152 if ( this[0].nodeType === 1 ) {
153 var attr = this[0].attributes, name;
154 for ( var i = 0, l = attr.length; i < l; i++ ) {
157 if ( name.indexOf( "data-" ) === 0 ) {
158 name = name.substr( 5 );
159 dataAttr( this[0], name, data[ name ] );
167 } else if ( typeof key === "object" ) {
168 return this.each(function() {
169 jQuery.data( this, key );
173 var parts = key.split(".");
174 parts[1] = parts[1] ? "." + parts[1] : "";
176 if ( value === undefined ) {
177 data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
179 // Try to fetch any internally stored data first
180 if ( data === undefined && this.length ) {
181 data = jQuery.data( this[0], key );
182 data = dataAttr( this[0], key, data );
185 return data === undefined && parts[1] ?
186 this.data( parts[0] ) :
190 return this.each(function() {
191 var $this = jQuery( this ),
192 args = [ parts[0], value ];
194 $this.triggerHandler( "setData" + parts[1] + "!", args );
195 jQuery.data( this, key, value );
196 $this.triggerHandler( "changeData" + parts[1] + "!", args );
201 removeData: function( key ) {
202 return this.each(function() {
203 jQuery.removeData( this, key );
208 function dataAttr( elem, key, data ) {
209 // If nothing was found internally, try to fetch any
210 // data from the HTML5 data-* attribute
211 if ( data === undefined && elem.nodeType === 1 ) {
212 data = elem.getAttribute( "data-" + key );
214 if ( typeof data === "string" ) {
216 data = data === "true" ? true :
217 data === "false" ? false :
218 data === "null" ? null :
219 !jQuery.isNaN( data ) ? parseFloat( data ) :
220 rbrace.test( data ) ? jQuery.parseJSON( data ) :
224 // Make sure we set the data so it isn't changed later
225 jQuery.data( elem, key, data );