@@ -5,61 +5,67 @@ define([
55] , function ( jQuery , rnotwhite ) {
66
77function Data ( ) {
8- // Support: Android<4,
9- // Old WebKit does not have Object.preventExtensions/freeze method,
10- // return new empty object instead with no [[set]] accessor
11- Object . defineProperty ( this . cache = { } , 0 , {
12- get : function ( ) {
13- return { } ;
14- }
15- } ) ;
16-
178 this . expando = jQuery . expando + Data . uid ++ ;
189}
1910
2011Data . uid = 1 ;
2112Data . accepts = jQuery . acceptData ;
2213
2314Data . prototype = {
24- key : function ( owner ) {
25- // We can accept data for non-element nodes in modern browsers,
26- // but we should not, see #8335.
27- // Always return the key for a frozen object.
28- if ( ! Data . accepts ( owner ) ) {
29- return 0 ;
30- }
31-
32- // Check if the owner object already has a cache key
33- var unlock = owner [ this . expando ] ;
3415
35- // If not, create one
36- if ( ! unlock ) {
37- unlock = Data . uid ++ ;
16+ register : function ( owner , initial ) {
17+ var descriptor = { } ,
18+ value = initial || { } ;
3819
20+ try {
3921 // If it is a node unlikely to be stringify-ed or looped over
4022 // use plain assignment
4123 if ( owner . nodeType ) {
42- owner [ this . expando ] = unlock ;
24+ owner [ this . expando ] = value ;
25+
4326 // Otherwise secure it in a non-enumerable, non-writable property
27+ // configurability must be true to allow the property to be
28+ // deleted with the delete operator
4429 } else {
45- Object . defineProperty ( owner , this . expando , { value : unlock } ) ;
30+ descriptor [ this . expando ] = {
31+ value : value ,
32+ writable : true ,
33+ configurable : true
34+ } ;
35+ Object . defineProperties ( owner , descriptor ) ;
4636 }
37+
38+ // Support: Android < 4
39+ // Fallback to a less secure definition
40+ } catch ( e ) {
41+ descriptor [ this . expando ] = value ;
42+ jQuery . extend ( owner , descriptor ) ;
4743 }
4844
49- // Ensure the cache object
50- if ( ! this . cache [ unlock ] ) {
51- this . cache [ unlock ] = { } ;
45+ return owner [ this . expando ] ;
46+ } ,
47+ cache : function ( owner , initial ) {
48+ // We can accept data for non-element nodes in modern browsers,
49+ // but we should not, see #8335.
50+ // Always return an empty object.
51+ if ( ! Data . accepts ( owner ) ) {
52+ return { } ;
5253 }
5354
54- return unlock ;
55+ // Check if the owner object already has a cache
56+ var cache = owner [ this . expando ] ;
57+
58+ // If so, return it
59+ if ( cache ) {
60+ return cache ;
61+ }
62+
63+ // If not, register one
64+ return this . register ( owner , initial ) ;
5565 } ,
5666 set : function ( owner , data , value ) {
5767 var prop ,
58- // There may be an unlock assigned to this node,
59- // if there is no entry for this "owner", create one inline
60- // and set the unlock as though an owner entry had always existed
61- unlock = this . key ( owner ) ,
62- cache = this . cache [ unlock ] ;
68+ cache = this . cache ( owner ) ;
6369
6470 // Handle: [ owner, key, value ] args
6571 if ( typeof data === "string" ) {
@@ -69,7 +75,8 @@ Data.prototype = {
6975 } else {
7076 // Fresh assignments by object are shallow copied
7177 if ( jQuery . isEmptyObject ( cache ) ) {
72- jQuery . extend ( this . cache [ unlock ] , data ) ;
78+
79+ jQuery . extend ( cache , data ) ;
7380 // Otherwise, copy the properties one-by-one to the cache object
7481 } else {
7582 for ( prop in data ) {
@@ -80,11 +87,7 @@ Data.prototype = {
8087 return cache ;
8188 } ,
8289 get : function ( owner , key ) {
83- // Either a valid cache is found, or will be created.
84- // New caches will be created and the unlock returned,
85- // allowing direct access to the newly created
86- // empty data object. A valid owner object must be provided.
87- var cache = this . cache [ this . key ( owner ) ] ;
90+ var cache = this . cache ( owner ) ;
8891
8992 return key === undefined ?
9093 cache : cache [ key ] ;
@@ -125,11 +128,10 @@ Data.prototype = {
125128 } ,
126129 remove : function ( owner , key ) {
127130 var i , name , camel ,
128- unlock = this . key ( owner ) ,
129- cache = this . cache [ unlock ] ;
131+ cache = this . cache ( owner ) ;
130132
131133 if ( key === undefined ) {
132- this . cache [ unlock ] = { } ;
134+ this . register ( owner ) ;
133135
134136 } else {
135137 // Support array or space separated string of keys
@@ -156,19 +158,19 @@ Data.prototype = {
156158 }
157159
158160 i = name . length ;
161+
159162 while ( i -- ) {
160163 delete cache [ name [ i ] ] ;
161164 }
162165 }
163166 } ,
164167 hasData : function ( owner ) {
165- return ! jQuery . isEmptyObject (
166- this . cache [ owner [ this . expando ] ] || { }
167- ) ;
168+ var cache = owner [ this . expando ] ;
169+ return cache !== undefined && ! jQuery . isEmptyObject ( cache ) ;
168170 } ,
169171 discard : function ( owner ) {
170172 if ( owner [ this . expando ] ) {
171- delete this . cache [ owner [ this . expando ] ] ;
173+ delete owner [ this . expando ] ;
172174 }
173175 }
174176} ;
0 commit comments