Offset: allow offset setter to throw for disconnected elements
[jquery.git] / src / data.js
blob80117d12e10e61c89c8b12d372adeaa7d60baa62
1 define([
2         "./core",
3         "./core/access",
4         "./data/var/dataPriv",
5         "./data/var/dataUser"
6 ], function( jQuery, access, dataPriv, dataUser ) {
8 //      Implementation Summary
9 //
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 ) {
22         var name;
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" ) {
31                         try {
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 ) :
38                                         data;
39                         } catch ( e ) {}
41                         // Make sure we set the data so it isn't changed later
42                         dataUser.set( elem, key, data );
43                 } else {
44                         data = undefined;
45                 }
46         }
47         return data;
50 jQuery.extend({
51         hasData: function( elem ) {
52                 return dataUser.hasData( elem ) || dataPriv.hasData( elem );
53         },
55         data: function( elem, name, data ) {
56                 return dataUser.access( elem, name, data );
57         },
59         removeData: function( elem, name ) {
60                 dataUser.remove( elem, name );
61         },
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 );
67         },
69         _removeData: function( elem, name ) {
70                 dataPriv.remove( elem, name );
71         }
72 });
74 jQuery.fn.extend({
75         data: function( key, value ) {
76                 var i, name, data,
77                         elem = this[ 0 ],
78                         attrs = elem && elem.attributes;
80                 // Gets all values
81                 if ( key === undefined ) {
82                         if ( this.length ) {
83                                 data = dataUser.get( elem );
85                                 if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
86                                         i = attrs.length;
87                                         while ( i-- ) {
89                                                 // Support: IE11+
90                                                 // The attrs elements can be null (#14894)
91                                                 if ( attrs[ i ] ) {
92                                                         name = attrs[ i ].name;
93                                                         if ( name.indexOf( "data-" ) === 0 ) {
94                                                                 name = jQuery.camelCase( name.slice(5) );
95                                                                 dataAttr( elem, name, data[ name ] );
96                                                         }
97                                                 }
98                                         }
99                                         dataPriv.set( elem, "hasDataAttrs", true );
100                                 }
101                         }
103                         return data;
104                 }
106                 // Sets multiple values
107                 if ( typeof key === "object" ) {
108                         return this.each(function() {
109                                 dataUser.set( this, key );
110                         });
111                 }
113                 return access( this, function( value ) {
114                         var data;
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 ) {
127                                         return data;
128                                 }
130                                 // Attempt to "discover" the data in
131                                 // HTML5 custom data-* attrs
132                                 data = dataAttr( elem, key );
133                                 if ( data !== undefined ) {
134                                         return data;
135                                 }
137                                 // We tried really hard, but the data doesn't exist.
138                                 return;
139                         }
141                         // Set the data...
142                         this.each(function() {
144                                 // We always store the camelCased key
145                                 dataUser.set( this, key, value );
146                         });
147                 }, null, value, arguments.length > 1, null, true );
148         },
150         removeData: function( key ) {
151                 return this.each(function() {
152                         dataUser.remove( this, key );
153                 });
154         }
157 return jQuery;