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 :
35 // Only convert to a number if it doesn't change the string
36 +data + "" === data ? +data :
37 rbrace.test( data ) ? jQuery.parseJSON( data ) :
41 // Make sure we set the data so it isn't changed later
42 dataUser.set( elem, key, data );
51 hasData: function( elem ) {
52 return dataUser.hasData( elem ) || dataPriv.hasData( elem );
55 data: function( elem, name, data ) {
56 return dataUser.access( elem, name, data );
59 removeData: function( elem, name ) {
60 dataUser.remove( elem, name );
63 // TODO: Now that all calls to _data and _removeData have been replaced
64 // with direct calls to dataPriv methods, these can be deprecated.
65 _data: function( elem, name, data ) {
66 return dataPriv.access( elem, name, data );
69 _removeData: function( elem, name ) {
70 dataPriv.remove( elem, name );
75 data: function( key, value ) {
78 attrs = elem && elem.attributes;
81 if ( key === undefined ) {
83 data = dataUser.get( elem );
85 if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
90 // The attrs elements can be null (#14894)
92 name = attrs[ i ].name;
93 if ( name.indexOf( "data-" ) === 0 ) {
94 name = jQuery.camelCase( name.slice(5) );
95 dataAttr( elem, name, data[ name ] );
99 dataPriv.set( elem, "hasDataAttrs", true );
106 // Sets multiple values
107 if ( typeof key === "object" ) {
108 return this.each(function() {
109 dataUser.set( this, key );
113 return access( this, function( value ) {
116 // The calling jQuery object (element matches) is not empty
117 // (and therefore has an element appears at this[ 0 ]) and the
118 // `value` parameter was not undefined. An empty jQuery object
119 // will result in `undefined` for elem = this[ 0 ] which will
120 // throw an exception if an attempt to read a data cache is made.
121 if ( elem && value === undefined ) {
123 // Attempt to get data from the cache
124 // The key will always be camelCased in Data
125 data = dataUser.get( elem, key );
126 if ( data !== undefined ) {
130 // Attempt to "discover" the data in
131 // HTML5 custom data-* attrs
132 data = dataAttr( elem, key );
133 if ( data !== undefined ) {
137 // We tried really hard, but the data doesn't exist.
142 this.each(function() {
144 // We always store the camelCased key
145 dataUser.set( this, key, value );
147 }, null, value, arguments.length > 1, null, true );
150 removeData: function( key ) {
151 return this.each(function() {
152 dataUser.remove( this, key );