7 ], function( jQuery, access, camelCase, dataPriv, dataUser ) {
11 // Implementation Summary
13 // 1. Enforce API surface and semantic compatibility with 1.9.x branch
14 // 2. Improve the module's maintainability by reducing the storage
15 // paths to a single mechanism.
16 // 3. Use the same single mechanism to support "private" and "user" data.
17 // 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData)
18 // 5. Avoid exposing implementation details on user objects (eg. expando properties)
19 // 6. Provide a clear path for implementation upgrade to WeakMap in 2014
21 var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
22 rmultiDash = /[A-Z]/g;
24 function getData( data ) {
25 if ( data === "true" ) {
29 if ( data === "false" ) {
33 if ( data === "null" ) {
37 // Only convert to a number if it doesn't change the string
38 if ( data === +data + "" ) {
42 if ( rbrace.test( data ) ) {
43 return JSON.parse( data );
49 function dataAttr( elem, key, data ) {
52 // If nothing was found internally, try to fetch any
53 // data from the HTML5 data-* attribute
54 if ( data === undefined && elem.nodeType === 1 ) {
55 name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
56 data = elem.getAttribute( name );
58 if ( typeof data === "string" ) {
60 data = getData( data );
63 // Make sure we set the data so it isn't changed later
64 dataUser.set( elem, key, data );
73 hasData: function( elem ) {
74 return dataUser.hasData( elem ) || dataPriv.hasData( elem );
77 data: function( elem, name, data ) {
78 return dataUser.access( elem, name, data );
81 removeData: function( elem, name ) {
82 dataUser.remove( elem, name );
85 // TODO: Now that all calls to _data and _removeData have been replaced
86 // with direct calls to dataPriv methods, these can be deprecated.
87 _data: function( elem, name, data ) {
88 return dataPriv.access( elem, name, data );
91 _removeData: function( elem, name ) {
92 dataPriv.remove( elem, name );
97 data: function( key, value ) {
100 attrs = elem && elem.attributes;
103 if ( key === undefined ) {
105 data = dataUser.get( elem );
107 if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
111 // Support: IE 11 only
112 // The attrs elements can be null (#14894)
114 name = attrs[ i ].name;
115 if ( name.indexOf( "data-" ) === 0 ) {
116 name = camelCase( name.slice( 5 ) );
117 dataAttr( elem, name, data[ name ] );
121 dataPriv.set( elem, "hasDataAttrs", true );
128 // Sets multiple values
129 if ( typeof key === "object" ) {
130 return this.each( function() {
131 dataUser.set( this, key );
135 return access( this, function( value ) {
138 // The calling jQuery object (element matches) is not empty
139 // (and therefore has an element appears at this[ 0 ]) and the
140 // `value` parameter was not undefined. An empty jQuery object
141 // will result in `undefined` for elem = this[ 0 ] which will
142 // throw an exception if an attempt to read a data cache is made.
143 if ( elem && value === undefined ) {
145 // Attempt to get data from the cache
146 // The key will always be camelCased in Data
147 data = dataUser.get( elem, key );
148 if ( data !== undefined ) {
152 // Attempt to "discover" the data in
153 // HTML5 custom data-* attrs
154 data = dataAttr( elem, key );
155 if ( data !== undefined ) {
159 // We tried really hard, but the data doesn't exist.
164 this.each( function() {
166 // We always store the camelCased key
167 dataUser.set( this, key, value );
169 }, null, value, arguments.length > 1, null, true );
172 removeData: function( key ) {
173 return this.each( function() {
174 dataUser.remove( this, key );