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 var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
127 return !(match === true || elem.getAttribute("classid") !== match);
136 data: function( key, value ) {
139 if ( typeof key === "undefined" ) {
141 var attr = this[0].attributes, name;
142 data = jQuery.data( this[0] );
144 for ( var i = 0, l = attr.length; i < l; i++ ) {
147 if ( name.indexOf( "data-" ) === 0 ) {
148 name = name.substr( 5 );
149 dataAttr( this[0], name, data[ name ] );
156 } else if ( typeof key === "object" ) {
157 return this.each(function() {
158 jQuery.data( this, key );
162 var parts = key.split(".");
163 parts[1] = parts[1] ? "." + parts[1] : "";
165 if ( value === undefined ) {
166 data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
168 // Try to fetch any internally stored data first
169 if ( data === undefined && this.length ) {
170 data = jQuery.data( this[0], key );
171 data = dataAttr( this[0], key, data );
174 return data === undefined && parts[1] ?
175 this.data( parts[0] ) :
179 return this.each(function() {
180 var $this = jQuery( this ),
181 args = [ parts[0], value ];
183 $this.triggerHandler( "setData" + parts[1] + "!", args );
184 jQuery.data( this, key, value );
185 $this.triggerHandler( "changeData" + parts[1] + "!", args );
190 removeData: function( key ) {
191 return this.each(function() {
192 jQuery.removeData( this, key );
197 function dataAttr( elem, key, data ) {
198 // If nothing was found internally, try to fetch any
199 // data from the HTML5 data-* attribute
200 if ( data === undefined && elem.nodeType === 1 ) {
201 data = elem.getAttribute( "data-" + key );
203 if ( typeof data === "string" ) {
205 data = data === "true" ? true :
206 data === "false" ? false :
207 data === "null" ? null :
208 !jQuery.isNaN( data ) ? parseFloat( data ) :
209 rbrace.test( data ) ? jQuery.parseJSON( data ) :
213 // Make sure we set the data so it isn't changed later
214 jQuery.data( elem, key, data );