|
1 |
| -import { migratePatchFunc, migrateWarn } from "../main.js"; |
| 1 | +import { migratePatchFunc } from "../main.js"; |
| 2 | +import { patchProto } from "../utils.js"; |
2 | 3 |
|
3 | 4 | function patchDataProto( original, options ) {
|
4 |
| - var i, |
| 5 | + var warningId = options.warningId, |
5 | 6 | apiName = options.apiName,
|
6 |
| - isInstanceMethod = options.isInstanceMethod, |
| 7 | + isInstanceMethod = options.isInstanceMethod; |
7 | 8 |
|
8 |
| - // `Object.prototype` keys are not enumerable so list the |
9 |
| - // official ones here. An alternative would be wrapping |
10 |
| - // data objects with a Proxy but that creates additional issues |
11 |
| - // like breaking object identity on subsequent calls. |
12 |
| - objProtoKeys = [ |
13 |
| - "__proto__", |
14 |
| - "__defineGetter__", |
15 |
| - "__defineSetter__", |
16 |
| - "__lookupGetter__", |
17 |
| - "__lookupSetter__", |
18 |
| - "hasOwnProperty", |
19 |
| - "isPrototypeOf", |
20 |
| - "propertyIsEnumerable", |
21 |
| - "toLocaleString", |
22 |
| - "toString", |
23 |
| - "valueOf" |
24 |
| - ], |
25 |
| - |
26 |
| - // Use a null prototype at the beginning so that we can define our |
27 |
| - // `__proto__` getter & setter. We'll reset the prototype afterwards. |
28 |
| - intermediateDataObj = Object.create( null ); |
29 |
| - |
30 |
| - for ( i = 0; i < objProtoKeys.length; i++ ) { |
31 |
| - ( function( key ) { |
32 |
| - Object.defineProperty( intermediateDataObj, key, { |
33 |
| - get: function() { |
34 |
| - migrateWarn( "data-null-proto", |
35 |
| - "Accessing properties from " + apiName + |
36 |
| - " inherited from Object.prototype is removed" ); |
37 |
| - return ( key + "__cache" ) in intermediateDataObj ? |
38 |
| - intermediateDataObj[ key + "__cache" ] : |
39 |
| - Object.prototype[ key ]; |
40 |
| - }, |
41 |
| - set: function( value ) { |
42 |
| - migrateWarn( "data-null-proto", |
43 |
| - "Setting properties from " + apiName + |
44 |
| - " inherited from Object.prototype is removed" ); |
45 |
| - intermediateDataObj[ key + "__cache" ] = value; |
46 |
| - } |
47 |
| - } ); |
48 |
| - } )( objProtoKeys[ i ] ); |
49 |
| - } |
50 |
| - |
51 |
| - Object.setPrototypeOf( intermediateDataObj, Object.prototype ); |
52 |
| - |
53 |
| - return function jQueryDataProtoPatched() { |
| 9 | + return function apiWithProtoPatched() { |
54 | 10 | var result = original.apply( this, arguments );
|
55 | 11 |
|
56 | 12 | if ( arguments.length !== ( isInstanceMethod ? 0 : 1 ) || result === undefined ) {
|
57 | 13 | return result;
|
58 | 14 | }
|
59 | 15 |
|
60 |
| - // Insert an additional object in the prototype chain between `result` |
61 |
| - // and `Object.prototype`; that intermediate object proxies properties |
62 |
| - // to `Object.prototype`, warning about their usage first. |
63 |
| - Object.setPrototypeOf( result, intermediateDataObj ); |
| 16 | + patchProto( result, { |
| 17 | + warningId: warningId, |
| 18 | + apiName: apiName |
| 19 | + } ); |
64 | 20 |
|
65 | 21 | return result;
|
66 | 22 | };
|
67 | 23 | }
|
68 | 24 |
|
69 |
| -// Yes, we are patching jQuery.data twice; here & above. This is necessary |
70 |
| -// so that each of the two patches can be independently disabled. |
71 | 25 | migratePatchFunc( jQuery, "data",
|
72 | 26 | patchDataProto( jQuery.data, {
|
| 27 | + warningId: "data-null-proto", |
73 | 28 | apiName: "jQuery.data()",
|
74 |
| - isPrivateData: false, |
| 29 | + isInstanceMethod: false |
| 30 | + } ), |
| 31 | + "data-null-proto" ); |
| 32 | +migratePatchFunc( jQuery, "_data", |
| 33 | + patchDataProto( jQuery._data, { |
| 34 | + warningId: "data-null-proto", |
| 35 | + apiName: "jQuery._data()", |
75 | 36 | isInstanceMethod: false
|
76 | 37 | } ),
|
77 | 38 | "data-null-proto" );
|
78 | 39 | migratePatchFunc( jQuery.fn, "data",
|
79 | 40 | patchDataProto( jQuery.fn.data, {
|
| 41 | + warningId: "data-null-proto", |
80 | 42 | apiName: "jQuery.fn.data()",
|
81 |
| - isPrivateData: true, |
82 | 43 | isInstanceMethod: true
|
83 | 44 | } ),
|
84 | 45 | "data-null-proto" );
|
85 |
| - |
86 |
| -// TODO entry in warnings.md |
0 commit comments