1
1
import { migratePatchFunc , migrateWarn } from "../main.js" ;
2
- import { camelCase } from "../utils.js" ;
2
+ import { camelCase , patchProto } from "../utils.js" ;
3
3
4
4
var rmultiDash = / [ A - Z ] / g,
5
5
rnothtmlwhite = / [ ^ \x20 \t \r \n \f ] + / g,
6
- origJQueryData = jQuery . data ;
6
+ origJQueryData = jQuery . data ,
7
+ origJQueryPrivateData = jQuery . _data ;
7
8
8
9
function unCamelCase ( str ) {
9
10
return str . replace ( rmultiDash , "-$&" ) . toLowerCase ( ) ;
10
11
}
11
12
12
13
function patchDataCamelCase ( origData , options ) {
13
14
var apiName = options . apiName ,
14
- isInstanceMethod = options . isInstanceMethod ;
15
+ isPrivateData = options . isPrivateData ,
16
+ isInstanceMethod = options . isInstanceMethod ,
17
+ origJQueryStaticData = isPrivateData ? origJQueryPrivateData : origJQueryData ;
15
18
16
19
function objectSetter ( elem , obj ) {
17
20
var curData , key ;
@@ -23,7 +26,7 @@ function patchDataCamelCase( origData, options ) {
23
26
24
27
// Don't use the instance method here to avoid `data-*` attributes
25
28
// detection this early.
26
- curData = origJQueryData ( elem ) ;
29
+ curData = origJQueryStaticData ( elem ) ;
27
30
28
31
for ( key in obj ) {
29
32
if ( key !== camelCase ( key ) ) {
@@ -56,7 +59,7 @@ function patchDataCamelCase( origData, options ) {
56
59
57
60
// Don't use the instance method here to avoid `data-*` attributes
58
61
// detection this early.
59
- curData = origJQueryData ( elem ) ;
62
+ curData = origJQueryStaticData ( elem ) ;
60
63
61
64
if ( curData && name in curData ) {
62
65
migrateWarn ( "data-camelCase" ,
@@ -66,7 +69,7 @@ function patchDataCamelCase( origData, options ) {
66
69
curData [ name ] = value ;
67
70
}
68
71
69
- origJQueryData ( elem , name , value ) ;
72
+ origJQueryStaticData ( elem , name , value ) ;
70
73
71
74
// Since the "set" path can have two possible entry points
72
75
// return the expected data based on which path was taken.
@@ -124,7 +127,7 @@ function patchDataCamelCase( origData, options ) {
124
127
125
128
// Don't use the instance method here to avoid `data-*` attributes
126
129
// detection this early.
127
- curData = origJQueryData ( elem ) ;
130
+ curData = origJQueryStaticData ( elem ) ;
128
131
129
132
if ( curData && name in curData ) {
130
133
migrateWarn ( "data-camelCase" ,
@@ -139,11 +142,13 @@ function patchDataCamelCase( origData, options ) {
139
142
}
140
143
141
144
function patchRemoveDataCamelCase ( origRemoveData , options ) {
142
- var isInstanceMethod = options . isInstanceMethod ;
145
+ var isPrivateData = options . isPrivateData ,
146
+ isInstanceMethod = options . isInstanceMethod ,
147
+ origJQueryStaticData = isPrivateData ? origJQueryPrivateData : origJQueryData ;
143
148
144
149
function remove ( elem , keys ) {
145
150
var i , singleKey , unCamelCasedKeys ,
146
- curData = jQuery . data ( elem ) ;
151
+ curData = origJQueryStaticData ( elem ) ;
147
152
148
153
if ( keys === undefined ) {
149
154
origRemoveData ( elem ) ;
@@ -223,104 +228,64 @@ function patchRemoveDataCamelCase( origRemoveData, options ) {
223
228
migratePatchFunc ( jQuery , "data" ,
224
229
patchDataCamelCase ( jQuery . data , {
225
230
apiName : "jQuery.data()" ,
231
+ isPrivateData : false ,
232
+ isInstanceMethod : false
233
+ } ) ,
234
+ "data-camelCase" ) ;
235
+ migratePatchFunc ( jQuery , "_data" ,
236
+ patchDataCamelCase ( jQuery . _data , {
237
+ apiName : "jQuery._data()" ,
238
+ isPrivateData : true ,
226
239
isInstanceMethod : false
227
240
} ) ,
228
241
"data-camelCase" ) ;
229
242
migratePatchFunc ( jQuery . fn , "data" ,
230
243
patchDataCamelCase ( jQuery . fn . data , {
231
244
apiName : "jQuery.fn.data()" ,
245
+ isPrivateData : false ,
232
246
isInstanceMethod : true
233
247
} ) ,
234
248
"data-camelCase" ) ;
235
249
236
250
migratePatchFunc ( jQuery , "removeData" ,
237
251
patchRemoveDataCamelCase ( jQuery . removeData , {
252
+ isPrivateData : false ,
253
+ isInstanceMethod : false
254
+ } ) ,
255
+ "data-camelCase" ) ;
256
+ migratePatchFunc ( jQuery , "_removeData" ,
257
+ patchRemoveDataCamelCase ( jQuery . _removeData , {
258
+ isPrivateData : true ,
238
259
isInstanceMethod : false
239
260
} ) ,
240
261
"data-camelCase" ) ;
241
-
242
262
migratePatchFunc ( jQuery . fn , "removeData" ,
243
263
244
264
// No, it's not a typo - we're intentionally passing
245
265
// the static method here as we need something working on
246
266
// a single element.
247
267
patchRemoveDataCamelCase ( jQuery . removeData , {
268
+ isPrivateData : false ,
248
269
isInstanceMethod : true
249
270
} ) ,
250
271
"data-camelCase" ) ;
251
272
252
-
253
273
function patchDataProto ( original , options ) {
254
-
255
- // Support: IE 9 - 10 only, iOS 7 - 8 only
256
- // Older IE doesn't have a way to change an existing prototype.
257
- // Just return the original method there.
258
- // Older WebKit supports `__proto__` but not `Object.setPrototypeOf`.
259
- // To avoid complicating code, don't patch the API there either.
260
- if ( ! Object . setPrototypeOf ) {
261
- return original ;
262
- }
263
-
264
- var i ,
274
+ var warningId = options . warningId ,
265
275
apiName = options . apiName ,
266
- isInstanceMethod = options . isInstanceMethod ,
267
-
268
- // `Object.prototype` keys are not enumerable so list the
269
- // official ones here. An alternative would be wrapping
270
- // data objects with a Proxy but that creates additional issues
271
- // like breaking object identity on subsequent calls.
272
- objProtoKeys = [
273
- "__proto__" ,
274
- "__defineGetter__" ,
275
- "__defineSetter__" ,
276
- "__lookupGetter__" ,
277
- "__lookupSetter__" ,
278
- "hasOwnProperty" ,
279
- "isPrototypeOf" ,
280
- "propertyIsEnumerable" ,
281
- "toLocaleString" ,
282
- "toString" ,
283
- "valueOf"
284
- ] ,
285
-
286
- // Use a null prototype at the beginning so that we can define our
287
- // `__proto__` getter & setter. We'll reset the prototype afterwards.
288
- intermediateDataObj = Object . create ( null ) ;
289
-
290
- for ( i = 0 ; i < objProtoKeys . length ; i ++ ) {
291
- ( function ( key ) {
292
- Object . defineProperty ( intermediateDataObj , key , {
293
- get : function ( ) {
294
- migrateWarn ( "data-null-proto" ,
295
- "Accessing properties from " + apiName +
296
- " inherited from Object.prototype is deprecated" ) ;
297
- return ( key + "__cache" ) in intermediateDataObj ?
298
- intermediateDataObj [ key + "__cache" ] :
299
- Object . prototype [ key ] ;
300
- } ,
301
- set : function ( value ) {
302
- migrateWarn ( "data-null-proto" ,
303
- "Setting properties from " + apiName +
304
- " inherited from Object.prototype is deprecated" ) ;
305
- intermediateDataObj [ key + "__cache" ] = value ;
306
- }
307
- } ) ;
308
- } ) ( objProtoKeys [ i ] ) ;
309
- }
310
-
311
- Object . setPrototypeOf ( intermediateDataObj , Object . prototype ) ;
276
+ isInstanceMethod = options . isInstanceMethod ;
312
277
313
- return function jQueryDataProtoPatched ( ) {
278
+ return function apiWithProtoPatched ( ) {
314
279
var result = original . apply ( this , arguments ) ;
315
280
316
281
if ( arguments . length !== ( isInstanceMethod ? 0 : 1 ) || result === undefined ) {
317
282
return result ;
318
283
}
319
284
320
- // Insert an additional object in the prototype chain between ` result`
321
- // and `Object.prototype`; that intermediate object proxies properties
322
- // to `Object.prototype`, warning about their usage first.
323
- Object . setPrototypeOf ( result , intermediateDataObj ) ;
285
+ patchProto ( result , {
286
+ warningId : warningId ,
287
+ apiName : apiName
288
+ } ) ;
324
289
325
290
return result ;
326
291
} ;
@@ -330,15 +295,22 @@ function patchDataProto( original, options ) {
330
295
// so that each of the two patches can be independently disabled.
331
296
migratePatchFunc ( jQuery , "data" ,
332
297
patchDataProto ( jQuery . data , {
298
+ warningId : "data-null-proto" ,
333
299
apiName : "jQuery.data()" ,
334
- isPrivateData : false ,
300
+ isInstanceMethod : false
301
+ } ) ,
302
+ "data-null-proto" ) ;
303
+ migratePatchFunc ( jQuery , "_data" ,
304
+ patchDataProto ( jQuery . _data , {
305
+ warningId : "data-null-proto" ,
306
+ apiName : "jQuery._data()" ,
335
307
isInstanceMethod : false
336
308
} ) ,
337
309
"data-null-proto" ) ;
338
310
migratePatchFunc ( jQuery . fn , "data" ,
339
311
patchDataProto ( jQuery . fn . data , {
312
+ warningId : "data-null-proto" ,
340
313
apiName : "jQuery.fn.data()" ,
341
- isPrivateData : true ,
342
314
isInstanceMethod : true
343
315
} ) ,
344
316
"data-null-proto" ) ;
0 commit comments