1 import { jQuery } from "./core.js";
2 import { access } from "./core/access.js";
3 import { camelCase } from "./core/camelCase.js";
4 import { dataPriv } from "./data/var/dataPriv.js";
5 import { dataUser } from "./data/var/dataUser.js";
7 // Implementation Summary
9 // 1. Enforce API surface and semantic compatibility with 1.9.x branch
10 // 2. Improve the module's maintainability by reducing the storage
11 // paths to a single mechanism.
12 // 3. Use the same single mechanism to support "private" and "user" data.
13 // 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData)
14 // 5. Avoid exposing implementation details on user objects (eg. expando properties)
15 // 6. Provide a clear path for implementation upgrade to WeakMap in 2014
17 var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
18 rmultiDash = /[A-Z]/g;
20 function getData( data ) {
21 if ( data === "true" ) {
25 if ( data === "false" ) {
29 if ( data === "null" ) {
33 // Only convert to a number if it doesn't change the string
34 if ( data === +data + "" ) {
38 if ( rbrace.test( data ) ) {
39 return JSON.parse( data );
45 function dataAttr( elem, key, data ) {
48 // If nothing was found internally, try to fetch any
49 // data from the HTML5 data-* attribute
50 if ( data === undefined && elem.nodeType === 1 ) {
51 name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
52 data = elem.getAttribute( name );
54 if ( typeof data === "string" ) {
56 data = getData( data );
59 // Make sure we set the data so it isn't changed later
60 dataUser.set( elem, key, data );
69 hasData: function( elem ) {
70 return dataUser.hasData( elem ) || dataPriv.hasData( elem );
73 data: function( elem, name, data ) {
74 return dataUser.access( elem, name, data );
77 removeData: function( elem, name ) {
78 dataUser.remove( elem, name );
81 // TODO: Now that all calls to _data and _removeData have been replaced
82 // with direct calls to dataPriv methods, these can be deprecated.
83 _data: function( elem, name, data ) {
84 return dataPriv.access( elem, name, data );
87 _removeData: function( elem, name ) {
88 dataPriv.remove( elem, name );
93 data: function( key, value ) {
96 attrs = elem && elem.attributes;
99 if ( key === undefined ) {
101 data = dataUser.get( elem );
103 if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
108 // The attrs elements can be null (trac-14894)
110 name = attrs[ i ].name;
111 if ( name.indexOf( "data-" ) === 0 ) {
112 name = camelCase( name.slice( 5 ) );
113 dataAttr( elem, name, data[ name ] );
117 dataPriv.set( elem, "hasDataAttrs", true );
124 // Sets multiple values
125 if ( typeof key === "object" ) {
126 return this.each( function() {
127 dataUser.set( this, key );
131 return access( this, function( value ) {
134 // The calling jQuery object (element matches) is not empty
135 // (and therefore has an element appears at this[ 0 ]) and the
136 // `value` parameter was not undefined. An empty jQuery object
137 // will result in `undefined` for elem = this[ 0 ] which will
138 // throw an exception if an attempt to read a data cache is made.
139 if ( elem && value === undefined ) {
141 // Attempt to get data from the cache
142 // The key will always be camelCased in Data
143 data = dataUser.get( elem, key );
144 if ( data !== undefined ) {
148 // Attempt to "discover" the data in
149 // HTML5 custom data-* attrs
150 data = dataAttr( elem, key );
151 if ( data !== undefined ) {
155 // We tried really hard, but the data doesn't exist.
160 this.each( function() {
162 // We always store the camelCased key
163 dataUser.set( this, key, value );
165 }, null, value, arguments.length > 1, null, true );
168 removeData: function( key ) {
169 return this.each( function() {
170 dataUser.remove( this, key );
175 export { jQuery, jQuery as $ };