6 ], function( jQuery, access, dataPriv, dataUser ) {
8 // Implementation Summary
10 // 1. Enforce API surface and semantic compatibility with 1.9.x branch
11 // 2. Improve the module's maintainability by reducing the storage
12 // paths to a single mechanism.
13 // 3. Use the same single mechanism to support "private" and "user" data.
14 // 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData)
15 // 5. Avoid exposing implementation details on user objects (eg. expando properties)
16 // 6. Provide a clear path for implementation upgrade to WeakMap in 2014
18 var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
19 rmultiDash = /[A-Z]/g;
21 function dataAttr( elem, key, data ) {
24 // If nothing was found internally, try to fetch any
25 // data from the HTML5 data-* attribute
26 if ( data === undefined && elem.nodeType === 1 ) {
27 name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
28 data = elem.getAttribute( name );
30 if ( typeof data === "string" ) {
32 data = data === "true" ? true :
33 data === "false" ? false :
34 data === "null" ? null :
36 // Only convert to a number if it doesn't change the string
37 +data + "" === data ? +data :
38 rbrace.test( data ) ? jQuery.parseJSON( data ) :
42 // Make sure we set the data so it isn't changed later
43 dataUser.set( elem, key, data );
52 hasData: function( elem ) {
53 return dataUser.hasData( elem ) || dataPriv.hasData( elem );
56 data: function( elem, name, data ) {
57 return dataUser.access( elem, name, data );
60 removeData: function( elem, name ) {
61 dataUser.remove( elem, name );
64 // TODO: Now that all calls to _data and _removeData have been replaced
65 // with direct calls to dataPriv methods, these can be deprecated.
66 _data: function( elem, name, data ) {
67 return dataPriv.access( elem, name, data );
70 _removeData: function( elem, name ) {
71 dataPriv.remove( elem, name );
76 data: function( key, value ) {
79 attrs = elem && elem.attributes;
82 if ( key === undefined ) {
84 data = dataUser.get( elem );
86 if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
91 // The attrs elements can be null (#14894)
93 name = attrs[ i ].name;
94 if ( name.indexOf( "data-" ) === 0 ) {
95 name = jQuery.camelCase( name.slice( 5 ) );
96 dataAttr( elem, name, data[ name ] );
100 dataPriv.set( elem, "hasDataAttrs", true );
107 // Sets multiple values
108 if ( typeof key === "object" ) {
109 return this.each( function() {
110 dataUser.set( this, key );
114 return access( this, function( value ) {
117 // The calling jQuery object (element matches) is not empty
118 // (and therefore has an element appears at this[ 0 ]) and the
119 // `value` parameter was not undefined. An empty jQuery object
120 // will result in `undefined` for elem = this[ 0 ] which will
121 // throw an exception if an attempt to read a data cache is made.
122 if ( elem && value === undefined ) {
124 // Attempt to get data from the cache
125 // with the key as-is
126 data = dataUser.get( elem, key ) ||
128 // Try to find dashed key if it exists (gh-2779)
129 // This is for 2.2.x only
130 dataUser.get( elem, key.replace( rmultiDash, "-$&" ).toLowerCase() );
132 if ( data !== undefined ) {
136 camelKey = jQuery.camelCase( key );
138 // Attempt to get data from the cache
139 // with the key camelized
140 data = dataUser.get( elem, camelKey );
141 if ( data !== undefined ) {
145 // Attempt to "discover" the data in
146 // HTML5 custom data-* attrs
147 data = dataAttr( elem, camelKey, undefined );
148 if ( data !== undefined ) {
152 // We tried really hard, but the data doesn't exist.
157 camelKey = jQuery.camelCase( key );
158 this.each( function() {
160 // First, attempt to store a copy or reference of any
161 // data that might've been store with a camelCased key.
162 var data = dataUser.get( this, camelKey );
164 // For HTML5 data-* attribute interop, we have to
165 // store property names with dashes in a camelCase form.
166 // This might not apply to all properties...*
167 dataUser.set( this, camelKey, value );
169 // *... In the case of properties that might _actually_
170 // have dashes, we need to also store a copy of that
171 // unchanged property.
172 if ( key.indexOf( "-" ) > -1 && data !== undefined ) {
173 dataUser.set( this, key, value );
176 }, null, value, arguments.length > 1, null, true );
179 removeData: function( key ) {
180 return this.each( function() {
181 dataUser.remove( this, key );