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 hasData: function( elem ) {
25 if ( elem.nodeType ) {
26 elem = jQuery.cache[ elem[jQuery.expando] ];
29 return !!elem && !jQuery.isEmptyObject(elem);
32 data: function( elem, name, data ) {
33 if ( !jQuery.acceptData( elem ) ) {
37 elem = elem == window ?
41 var isNode = elem.nodeType,
42 id = isNode ? elem[ jQuery.expando ] : null,
43 cache = jQuery.cache, thisCache;
45 if ( isNode && !id && typeof name === "string" && data === undefined ) {
49 // Get the data from the object directly
53 // Compute a unique ID for the element
55 elem[ jQuery.expando ] = id = ++jQuery.uuid;
58 // Avoid generating a new cache unless none exists and we
59 // want to manipulate it.
60 if ( typeof name === "object" ) {
62 cache[ id ] = jQuery.extend(cache[ id ], name);
65 jQuery.extend( cache, name );
68 } else if ( isNode && !cache[ id ] ) {
72 thisCache = isNode ? cache[ id ] : cache;
74 // Prevent overriding the named cache with undefined values
75 if ( data !== undefined ) {
76 thisCache[ name ] = data;
79 return typeof name === "string" ? thisCache[ name ] : thisCache;
82 removeData: function( elem, name ) {
83 if ( !jQuery.acceptData( elem ) ) {
87 elem = elem == window ?
91 var isNode = elem.nodeType,
92 id = isNode ? elem[ jQuery.expando ] : elem,
94 thisCache = isNode ? cache[ id ] : id;
96 // If we want to remove a specific section of the element's data
99 // Remove the section of cache data
100 delete thisCache[ name ];
102 // If we've removed all the data, remove the element's cache
103 if ( isNode && jQuery.isEmptyObject(thisCache) ) {
104 jQuery.removeData( elem );
108 // Otherwise, we want to remove all of the element's data
110 if ( isNode && jQuery.support.deleteExpando ) {
111 delete elem[ jQuery.expando ];
113 } else if ( elem.removeAttribute ) {
114 elem.removeAttribute( jQuery.expando );
116 // Completely remove the data cache
117 } else if ( isNode ) {
120 // Remove all fields from the object
122 for ( var n in elem ) {
129 // A method for determining if a DOM node can handle the data expando
130 acceptData: function( elem ) {
131 if ( elem.nodeName ) {
132 var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
135 return !(match === true || elem.getAttribute("classid") !== match);
144 data: function( key, value ) {
147 if ( typeof key === "undefined" ) {
149 data = jQuery.data( this[0] );
151 if ( this[0].nodeType === 1 ) {
152 var attr = this[0].attributes, name;
153 for ( var i = 0, l = attr.length; i < l; i++ ) {
156 if ( name.indexOf( "data-" ) === 0 ) {
157 name = name.substr( 5 );
158 dataAttr( this[0], name, data[ name ] );
166 } else if ( typeof key === "object" ) {
167 return this.each(function() {
168 jQuery.data( this, key );
172 var parts = key.split(".");
173 parts[1] = parts[1] ? "." + parts[1] : "";
175 if ( value === undefined ) {
176 data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
178 // Try to fetch any internally stored data first
179 if ( data === undefined && this.length ) {
180 data = jQuery.data( this[0], key );
181 data = dataAttr( this[0], key, data );
184 return data === undefined && parts[1] ?
185 this.data( parts[0] ) :
189 return this.each(function() {
190 var $this = jQuery( this ),
191 args = [ parts[0], value ];
193 $this.triggerHandler( "setData" + parts[1] + "!", args );
194 jQuery.data( this, key, value );
195 $this.triggerHandler( "changeData" + parts[1] + "!", args );
200 removeData: function( key ) {
201 return this.each(function() {
202 jQuery.removeData( this, key );
207 function dataAttr( elem, key, data ) {
208 // If nothing was found internally, try to fetch any
209 // data from the HTML5 data-* attribute
210 if ( data === undefined && elem.nodeType === 1 ) {
211 data = elem.getAttribute( "data-" + key );
213 if ( typeof data === "string" ) {
215 data = data === "true" ? true :
216 data === "false" ? false :
217 data === "null" ? null :
218 !jQuery.isNaN( data ) ? parseFloat( data ) :
219 rbrace.test( data ) ? jQuery.parseJSON( data ) :
223 // Make sure we set the data so it isn't changed later
224 jQuery.data( elem, key, data );