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 return !elem.nodeType || (!!elem[ jQuery.expando ] && !jQuery.isEmptyObject(jQuery.cache[ elem[jQuery.expando] ]));
28 data: function( elem, name, data ) {
29 if ( !jQuery.acceptData( elem ) ) {
33 elem = elem == window ?
37 var isNode = elem.nodeType,
38 id = isNode ? elem[ jQuery.expando ] : null,
39 cache = jQuery.cache, thisCache;
41 if ( isNode && !id && typeof name === "string" && data === undefined ) {
45 // Get the data from the object directly
49 // Compute a unique ID for the element
51 elem[ jQuery.expando ] = id = ++jQuery.uuid;
54 // Avoid generating a new cache unless none exists and we
55 // want to manipulate it.
56 if ( typeof name === "object" ) {
58 cache[ id ] = jQuery.extend(cache[ id ], name);
61 jQuery.extend( cache, name );
64 } else if ( isNode && !cache[ id ] ) {
68 thisCache = isNode ? cache[ id ] : cache;
70 // Prevent overriding the named cache with undefined values
71 if ( data !== undefined ) {
72 thisCache[ name ] = data;
75 return typeof name === "string" ? thisCache[ name ] : thisCache;
78 removeData: function( elem, name ) {
79 if ( !jQuery.acceptData( elem ) ) {
83 elem = elem == window ?
87 var isNode = elem.nodeType,
88 id = isNode ? elem[ jQuery.expando ] : elem,
90 thisCache = isNode ? cache[ id ] : id;
92 // If we want to remove a specific section of the element's data
95 // Remove the section of cache data
96 delete thisCache[ name ];
98 // If we've removed all the data, remove the element's cache
99 if ( isNode && jQuery.isEmptyObject(thisCache) ) {
100 jQuery.removeData( elem );
104 // Otherwise, we want to remove all of the element's data
106 if ( isNode && jQuery.support.deleteExpando ) {
107 delete elem[ jQuery.expando ];
109 } else if ( elem.removeAttribute ) {
110 elem.removeAttribute( jQuery.expando );
112 // Completely remove the data cache
113 } else if ( isNode ) {
116 // Remove all fields from the object
118 for ( var n in elem ) {
125 // A method for determining if a DOM node can handle the data expando
126 acceptData: function( elem ) {
127 if ( elem.nodeName ) {
128 var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
131 return !(match === true || elem.getAttribute("classid") !== match);
140 data: function( key, value ) {
143 if ( typeof key === "undefined" ) {
145 data = jQuery.data( this[0] );
147 if ( this[0].nodeType === 1 ) {
148 var attr = this[0].attributes, name;
149 for ( var i = 0, l = attr.length; i < l; i++ ) {
152 if ( name.indexOf( "data-" ) === 0 ) {
153 name = name.substr( 5 );
154 dataAttr( this[0], name, data[ name ] );
162 } else if ( typeof key === "object" ) {
163 return this.each(function() {
164 jQuery.data( this, key );
168 var parts = key.split(".");
169 parts[1] = parts[1] ? "." + parts[1] : "";
171 if ( value === undefined ) {
172 data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
174 // Try to fetch any internally stored data first
175 if ( data === undefined && this.length ) {
176 data = jQuery.data( this[0], key );
177 data = dataAttr( this[0], key, data );
180 return data === undefined && parts[1] ?
181 this.data( parts[0] ) :
185 return this.each(function() {
186 var $this = jQuery( this ),
187 args = [ parts[0], value ];
189 $this.triggerHandler( "setData" + parts[1] + "!", args );
190 jQuery.data( this, key, value );
191 $this.triggerHandler( "changeData" + parts[1] + "!", args );
196 removeData: function( key ) {
197 return this.each(function() {
198 jQuery.removeData( this, key );
203 function dataAttr( elem, key, data ) {
204 // If nothing was found internally, try to fetch any
205 // data from the HTML5 data-* attribute
206 if ( data === undefined && elem.nodeType === 1 ) {
207 data = elem.getAttribute( "data-" + key );
209 if ( typeof data === "string" ) {
211 data = data === "true" ? true :
212 data === "false" ? false :
213 data === "null" ? null :
214 !jQuery.isNaN( data ) ? parseFloat( data ) :
215 rbrace.test( data ) ? jQuery.parseJSON( data ) :
219 // Make sure we set the data so it isn't changed later
220 jQuery.data( elem, key, data );