Data: Don't expose jQuery.acceptData
[jquery.git] / src / data / Data.js
bloba19476ea6c0a1c24d636c2e1bf962fb03303e126
1 define( [
2         "../core",
3         "../var/rnotwhite",
4         "./var/acceptData"
5 ], function( jQuery, rnotwhite, acceptData ) {
7 function Data() {
8         this.expando = jQuery.expando + Data.uid++;
11 Data.uid = 1;
13 Data.prototype = {
15         register: function( owner ) {
16                 var value = {};
18                 // If it is a node unlikely to be stringify-ed or looped over
19                 // use plain assignment
20                 if ( owner.nodeType ) {
21                         owner[ this.expando ] = value;
23                 // Otherwise secure it in a non-enumerable, non-writable property
24                 // configurability must be true to allow the property to be
25                 // deleted with the delete operator
26                 } else {
27                         Object.defineProperty( owner, this.expando, {
28                                 value: value,
29                                 writable: true,
30                                 configurable: true
31                         } );
32                 }
33                 return owner[ this.expando ];
34         },
35         cache: function( owner ) {
37                 // We can accept data for non-element nodes in modern browsers,
38                 // but we should not, see #8335.
39                 // Always return an empty object.
40                 if ( !acceptData( owner ) ) {
41                         return {};
42                 }
44                 // Check if the owner object already has a cache
45                 var cache = owner[ this.expando ];
47                 // If so, return it
48                 if ( cache ) {
49                         return cache;
50                 }
52                 // If not, register one
53                 return this.register( owner );
54         },
55         set: function( owner, data, value ) {
56                 var prop,
57                         cache = this.cache( owner );
59                 // Handle: [ owner, key, value ] args
60                 // Always use camelCase key (gh-2257)
61                 if ( typeof data === "string" ) {
62                         cache[ jQuery.camelCase( data ) ] = value;
64                 // Handle: [ owner, { properties } ] args
65                 } else {
67                         // Copy the properties one-by-one to the cache object
68                         for ( prop in data ) {
69                                 cache[ jQuery.camelCase( prop ) ] = data[ prop ];
70                         }
71                 }
72                 return cache;
73         },
74         get: function( owner, key ) {
75                 var cache = this.cache( owner );
77                 return key === undefined ?
78                         cache :
80                         // Always use camelCase key (gh-2257)
81                         cache[ jQuery.camelCase( key ) ];
82         },
83         access: function( owner, key, value ) {
85                 // In cases where either:
86                 //
87                 //   1. No key was specified
88                 //   2. A string key was specified, but no value provided
89                 //
90                 // Take the "read" path and allow the get method to determine
91                 // which value to return, respectively either:
92                 //
93                 //   1. The entire cache object
94                 //   2. The data stored at the key
95                 //
96                 if ( key === undefined ||
97                                 ( ( key && typeof key === "string" ) && value === undefined ) ) {
99                         return this.get( owner, key );
100                 }
102                 // When the key is not a string, or both a key and value
103                 // are specified, set or extend (existing objects) with either:
104                 //
105                 //   1. An object of properties
106                 //   2. A key and value
107                 //
108                 this.set( owner, key, value );
110                 // Since the "set" path can have two possible entry points
111                 // return the expected data based on which path was taken[*]
112                 return value !== undefined ? value : key;
113         },
114         remove: function( owner, key ) {
115                 var i,
116                         cache = owner[ this.expando ];
118                 if ( cache === undefined ) {
119                         return;
120                 }
122                 if ( key !== undefined ) {
124                         // Support array or space separated string of keys
125                         if ( jQuery.isArray( key ) ) {
127                                 // If key is an array of keys...
128                                 // We always set camelCase keys, so remove that.
129                                 key = key.map( jQuery.camelCase );
130                         } else {
131                                 key = jQuery.camelCase( key );
133                                 // If a key with the spaces exists, use it.
134                                 // Otherwise, create an array by matching non-whitespace
135                                 key = key in cache ?
136                                         [ key ] :
137                                         ( key.match( rnotwhite ) || [] );
138                         }
140                         i = key.length;
142                         while ( i-- ) {
143                                 delete cache[ key[ i ] ];
144                         }
145                 }
147                 // Remove the expando if there's no more data
148                 if ( key === undefined || jQuery.isEmptyObject( cache ) ) {
149                         delete owner[ this.expando ];
150                 }
151         },
152         hasData: function( owner ) {
153                 var cache = owner[ this.expando ];
154                 return cache !== undefined && !jQuery.isEmptyObject( cache );
155         }
158 return Data;
159 } );