6 ], function( jQuery, access, dataPriv, dataUser ) {
10 // Implementation Summary
12 // 1. Enforce API surface and semantic compatibility with 1.9.x branch
13 // 2. Improve the module's maintainability by reducing the storage
14 // paths to a single mechanism.
15 // 3. Use the same single mechanism to support "private" and "user" data.
16 // 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData)
17 // 5. Avoid exposing implementation details on user objects (eg. expando properties)
18 // 6. Provide a clear path for implementation upgrade to WeakMap in 2014
20 var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
21 rmultiDash = /[A-Z]/g;
23 function getData( data ) {
24 if ( data === "true" ) {
28 if ( data === "false" ) {
32 if ( data === "null" ) {
36 // Only convert to a number if it doesn't change the string
37 if ( data === +data + "" ) {
41 if ( rbrace.test( data ) ) {
42 return JSON.parse( data );
48 function dataAttr( elem, key, data ) {
51 // If nothing was found internally, try to fetch any
52 // data from the HTML5 data-* attribute
53 if ( data === undefined && elem.nodeType === 1 ) {
54 name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
55 data = elem.getAttribute( name );
57 if ( typeof data === "string" ) {
59 data = getData( data );
62 // Make sure we set the data so it isn't changed later
63 dataUser.set( elem, key, data );
72 hasData: function( elem ) {
73 return dataUser.hasData( elem ) || dataPriv.hasData( elem );
76 data: function( elem, name, data ) {
77 return dataUser.access( elem, name, data );
80 removeData: function( elem, name ) {
81 dataUser.remove( elem, name );
84 // TODO: Now that all calls to _data and _removeData have been replaced
85 // with direct calls to dataPriv methods, these can be deprecated.
86 _data: function( elem, name, data ) {
87 return dataPriv.access( elem, name, data );
90 _removeData: function( elem, name ) {
91 dataPriv.remove( elem, name );
96 data: function( key, value ) {
99 attrs = elem && elem.attributes;
102 if ( key === undefined ) {
104 data = dataUser.get( elem );
106 if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
110 // Support: IE 11 only
111 // The attrs elements can be null (#14894)
113 name = attrs[ i ].name;
114 if ( name.indexOf( "data-" ) === 0 ) {
115 name = jQuery.camelCase( name.slice( 5 ) );
116 dataAttr( elem, name, data[ name ] );
120 dataPriv.set( elem, "hasDataAttrs", true );
127 // Sets multiple values
128 if ( typeof key === "object" ) {
129 return this.each( function() {
130 dataUser.set( this, key );
134 return access( this, function( value ) {
137 // The calling jQuery object (element matches) is not empty
138 // (and therefore has an element appears at this[ 0 ]) and the
139 // `value` parameter was not undefined. An empty jQuery object
140 // will result in `undefined` for elem = this[ 0 ] which will
141 // throw an exception if an attempt to read a data cache is made.
142 if ( elem && value === undefined ) {
144 // Attempt to get data from the cache
145 // The key will always be camelCased in Data
146 data = dataUser.get( elem, key );
147 if ( data !== undefined ) {
151 // Attempt to "discover" the data in
152 // HTML5 custom data-* attrs
153 data = dataAttr( elem, key );
154 if ( data !== undefined ) {
158 // We tried really hard, but the data doesn't exist.
163 this.each( function() {
165 // We always store the camelCased key
166 dataUser.set( this, key, value );
168 }, null, value, arguments.length > 1, null, true );
171 removeData: function( key ) {
172 return this.each( function() {
173 dataUser.remove( this, key );