15
15
declare const WorkerGlobalScope : any ;
16
16
17
17
export const zoneSymbol : ( name : string ) => string = ( n ) => `__zone_symbol__${ n } ` ;
18
- const VALUE = zoneSymbol ( 'value' ) ;
19
18
const _global : any =
20
19
typeof window === 'object' && window || typeof self === 'object' && self || global ;
21
20
@@ -68,25 +67,20 @@ export function patchProperty(obj: any, prop: string) {
68
67
return ;
69
68
}
70
69
71
- const originalDesc = Object . getOwnPropertyDescriptor ( obj , 'original' + prop ) ;
72
- if ( ! originalDesc && desc . get ) {
73
- Object . defineProperty (
74
- obj , 'original' + prop , { enumerable : false , configurable : true , get : desc . get } ) ;
75
- }
76
-
77
70
// A property descriptor cannot have getter/setter and be writable
78
71
// deleting the writable and value properties avoids this error:
79
72
//
80
73
// TypeError: property descriptors must not specify a value or be writable when a
81
74
// getter or setter has been specified
82
75
delete desc . writable ;
83
76
delete desc . value ;
77
+ const originalDescGet = desc . get ;
84
78
85
79
// substr(2) cuz 'onclick' -> 'click', etc
86
80
const eventName = prop . substr ( 2 ) ;
87
81
const _prop = zoneSymbol ( '_' + prop ) ;
88
82
89
- desc . set = function ( fn ) {
83
+ desc . set = function ( newValue ) {
90
84
// in some of windows's onproperty callback, this is undefined
91
85
// so we need to check it
92
86
let target = this ;
@@ -96,20 +90,14 @@ export function patchProperty(obj: any, prop: string) {
96
90
if ( ! target ) {
97
91
return ;
98
92
}
99
- if ( target [ _prop ] ) {
100
- target . removeEventListener ( eventName , target [ _prop ] ) ;
101
- }
102
-
103
- if ( typeof fn === 'string' ) {
104
- const src : string = fn ;
105
- fn = new Function ( src ) ;
106
- fn [ VALUE ] = src ;
93
+ let previousValue = target [ _prop ] ;
94
+ if ( previousValue ) {
95
+ target . removeEventListener ( eventName , previousValue ) ;
107
96
}
108
97
109
- if ( typeof fn === 'function' ) {
98
+ if ( typeof newValue === 'function' ) {
110
99
const wrapFn = function ( event : Event ) {
111
- let result ;
112
- result = fn . apply ( this , arguments ) ;
100
+ let result = newValue . apply ( this , arguments ) ;
113
101
114
102
if ( result != undefined && ! result ) {
115
103
event . preventDefault ( ) ;
@@ -136,26 +124,22 @@ export function patchProperty(obj: any, prop: string) {
136
124
if ( ! target ) {
137
125
return null ;
138
126
}
139
- let r = target [ _prop ] || null ;
140
- // result will be null when use inline event attribute,
141
- // such as <button onclick="func();">OK</button>
142
- // because the onclick function is internal raw uncompiled handler
143
- // the onclick will be evaluated when first time event was triggered or
144
- // the property is accessed, https://github.com/angular/zone.js/issues/525
145
- // so we should use original native get to retrieve the handler
146
- if ( r === null ) {
147
- if ( originalDesc && originalDesc . get ) {
148
- r = originalDesc . get . apply ( this , arguments ) ;
149
- if ( r ) {
150
- desc . set . apply ( this , [ r ] ) ;
151
- if ( typeof target [ 'removeAttribute' ] === 'function' ) {
152
- target . removeAttribute ( prop ) ;
153
- }
154
- }
127
+ if ( target . hasOwnProperty ( _prop ) ) {
128
+ return target [ _prop ] ;
129
+ } else {
130
+ // result will be null when use inline event attribute,
131
+ // such as <button onclick="func();">OK</button>
132
+ // because the onclick function is internal raw uncompiled handler
133
+ // the onclick will be evaluated when first time event was triggered or
134
+ // the property is accessed, https://github.com/angular/zone.js/issues/525
135
+ // so we should use original native get to retrieve the handler
136
+ let value = originalDescGet . apply ( this ) ;
137
+ value = desc . set . apply ( this , [ value ] ) ;
138
+ if ( typeof target [ 'removeAttribute' ] === 'function' ) {
139
+ target . removeAttribute ( prop ) ;
155
140
}
141
+ return value ;
156
142
}
157
- const value = target [ _prop ] || null ;
158
- return value && value . hasOwnProperty ( VALUE ) ? value [ value ] : value ;
159
143
} ;
160
144
161
145
Object . defineProperty ( obj , prop , desc ) ;
0 commit comments