Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 160531b

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(patch): fix #719, window onproperty callback this is undefined (#723)
1 parent 92a39e2 commit 160531b

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

lib/common/utils.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,17 @@ export function patchProperty(obj: any, prop: string) {
8181
const _prop = zoneSymbol('_' + prop);
8282

8383
desc.set = function(fn) {
84-
if (this[_prop]) {
85-
this.removeEventListener(eventName, this[_prop]);
84+
// in some of windows's onproperty callback, this is undefined
85+
// so we need to check it
86+
let target = this;
87+
if (!target && obj === _global) {
88+
target = _global;
89+
}
90+
if (!target) {
91+
return;
92+
}
93+
if (target[_prop]) {
94+
target.removeEventListener(eventName, target[_prop]);
8695
}
8796

8897
if (typeof fn === 'function') {
@@ -96,17 +105,26 @@ export function patchProperty(obj: any, prop: string) {
96105
return result;
97106
};
98107

99-
this[_prop] = wrapFn;
100-
this.addEventListener(eventName, wrapFn, false);
108+
target[_prop] = wrapFn;
109+
target.addEventListener(eventName, wrapFn, false);
101110
} else {
102-
this[_prop] = null;
111+
target[_prop] = null;
103112
}
104113
};
105114

106115
// The getter would return undefined for unassigned properties but the default value of an
107116
// unassigned property is null
108117
desc.get = function() {
109-
let r = this[_prop] || null;
118+
// in some of windows's onproperty callback, this is undefined
119+
// so we need to check it
120+
let target = this;
121+
if (!target && obj === _global) {
122+
target = _global;
123+
}
124+
if (!target) {
125+
return null;
126+
}
127+
let r = target[_prop] || null;
110128
// result will be null when use inline event attribute,
111129
// such as <button onclick="func();">OK</button>
112130
// because the onclick function is internal raw uncompiled handler
@@ -118,13 +136,13 @@ export function patchProperty(obj: any, prop: string) {
118136
r = originalDesc.get.apply(this, arguments);
119137
if (r) {
120138
desc.set.apply(this, [r]);
121-
if (typeof this['removeAttribute'] === 'function') {
122-
this.removeAttribute(prop);
139+
if (typeof target['removeAttribute'] === 'function') {
140+
target.removeAttribute(prop);
123141
}
124142
}
125143
}
126144
}
127-
return this[_prop] || null;
145+
return target[_prop] || null;
128146
};
129147

130148
Object.defineProperty(obj, prop, desc);

test/browser/browser.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ describe('Zone', function() {
138138
svg.removeEventListener('mouse', eventListenerSpy);
139139
document.body.removeChild(svg);
140140
}));
141+
142+
it('get window onerror should not throw error',
143+
ifEnvSupports(
144+
() => {
145+
return canPatchOnProperty(window, 'onerror');
146+
},
147+
function() {
148+
const testFn = function() {
149+
let onerror = window.onerror;
150+
window.onerror = function() {};
151+
onerror = window.onerror;
152+
};
153+
expect(testFn()).not.toThrow();
154+
}));
155+
141156
}));
142157

143158
describe('eventListener hooks', function() {

0 commit comments

Comments
 (0)