5 "./data/var/data_priv",
7 ], function( jQuery, rnotwhite, access, data_priv, data_user ) {
9 // Implementation Summary
11 // 1. Enforce API surface and semantic compatibility with 1.9.x branch
12 // 2. Improve the module's maintainability by reducing the storage
13 // paths to a single mechanism.
14 // 3. Use the same single mechanism to support "private" and "user" data.
15 // 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData)
16 // 5. Avoid exposing implementation details on user objects (eg. expando properties)
17 // 6. Provide a clear path for implementation upgrade to WeakMap in 2014
19 var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
20 rmultiDash = /([A-Z])/g;
22 function dataAttr( elem, key, data ) {
25 // If nothing was found internally, try to fetch any
26 // data from the HTML5 data-* attribute
27 if ( data === undefined && elem.nodeType === 1 ) {
28 name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase();
29 data = elem.getAttribute( name );
31 if ( typeof data === "string" ) {
33 data = data === "true" ? true :
34 data === "false" ? false :
35 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 data_user.set( elem, key, data );
52 hasData: function( elem ) {
53 return data_user.hasData( elem ) || data_priv.hasData( elem );
56 data: function( elem, name, data ) {
57 return data_user.access( elem, name, data );
60 removeData: function( elem, name ) {
61 data_user.remove( elem, name );
64 // TODO: Now that all calls to _data and _removeData have been replaced
65 // with direct calls to data_priv methods, these can be deprecated.
66 _data: function( elem, name, data ) {
67 return data_priv.access( elem, name, data );
70 _removeData: function( elem, name ) {
71 data_priv.remove( elem, name );
76 data: function( key, value ) {
79 attrs = elem && elem.attributes;
82 if ( key === undefined ) {
84 data = data_user.get( elem );
86 if ( elem.nodeType === 1 && !data_priv.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 data_priv.set( elem, "hasDataAttrs", true );
107 // Sets multiple values
108 if ( typeof key === "object" ) {
109 return this.each(function() {
110 data_user.set( this, key );
114 return access( this, function( value ) {
116 camelKey = jQuery.camelCase( key );
118 // The calling jQuery object (element matches) is not empty
119 // (and therefore has an element appears at this[ 0 ]) and the
120 // `value` parameter was not undefined. An empty jQuery object
121 // will result in `undefined` for elem = this[ 0 ] which will
122 // throw an exception if an attempt to read a data cache is made.
123 if ( elem && value === undefined ) {
124 // Attempt to get data from the cache
125 // with the key as-is
126 data = data_user.get( elem, key );
127 if ( data !== undefined ) {
131 // Attempt to get data from the cache
132 // with the key camelized
133 data = data_user.get( elem, camelKey );
134 if ( data !== undefined ) {
138 // Attempt to "discover" the data in
139 // HTML5 custom data-* attrs
140 data = dataAttr( elem, camelKey, undefined );
141 if ( data !== undefined ) {
145 // We tried really hard, but the data doesn't exist.
150 this.each(function() {
151 // First, attempt to store a copy or reference of any
152 // data that might've been store with a camelCased key.
153 var data = data_user.get( this, camelKey );
155 // For HTML5 data-* attribute interop, we have to
156 // store property names with dashes in a camelCase form.
157 // This might not apply to all properties...*
158 data_user.set( this, camelKey, value );
160 // *... In the case of properties that might _actually_
161 // have dashes, we need to also store a copy of that
162 // unchanged property.
163 if ( key.indexOf("-") !== -1 && data !== undefined ) {
164 data_user.set( this, key, value );
167 }, null, value, arguments.length > 1, null, true );
170 removeData: function( key ) {
171 return this.each(function() {
172 data_user.remove( this, key );