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

Commit

Permalink
fix(patch): fix #719, window onproperty callback this is undefined (#723
Browse files Browse the repository at this point in the history
)
  • Loading branch information
JiaLiPassion authored and mhevery committed Apr 10, 2017
1 parent 92a39e2 commit 160531b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
36 changes: 27 additions & 9 deletions lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,17 @@ export function patchProperty(obj: any, prop: string) {
const _prop = zoneSymbol('_' + prop);

desc.set = function(fn) {
if (this[_prop]) {
this.removeEventListener(eventName, this[_prop]);
// in some of windows's onproperty callback, this is undefined
// so we need to check it
let target = this;
if (!target && obj === _global) {
target = _global;
}
if (!target) {
return;
}
if (target[_prop]) {
target.removeEventListener(eventName, target[_prop]);
}

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

this[_prop] = wrapFn;
this.addEventListener(eventName, wrapFn, false);
target[_prop] = wrapFn;
target.addEventListener(eventName, wrapFn, false);
} else {
this[_prop] = null;
target[_prop] = null;
}
};

// The getter would return undefined for unassigned properties but the default value of an
// unassigned property is null
desc.get = function() {
let r = this[_prop] || null;
// in some of windows's onproperty callback, this is undefined
// so we need to check it
let target = this;
if (!target && obj === _global) {
target = _global;
}
if (!target) {
return null;
}
let r = target[_prop] || null;
// result will be null when use inline event attribute,
// such as <button onclick="func();">OK</button>
// because the onclick function is internal raw uncompiled handler
Expand All @@ -118,13 +136,13 @@ export function patchProperty(obj: any, prop: string) {
r = originalDesc.get.apply(this, arguments);
if (r) {
desc.set.apply(this, [r]);
if (typeof this['removeAttribute'] === 'function') {
this.removeAttribute(prop);
if (typeof target['removeAttribute'] === 'function') {
target.removeAttribute(prop);
}
}
}
}
return this[_prop] || null;
return target[_prop] || null;
};

Object.defineProperty(obj, prop, desc);
Expand Down
15 changes: 15 additions & 0 deletions test/browser/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ describe('Zone', function() {
svg.removeEventListener('mouse', eventListenerSpy);
document.body.removeChild(svg);
}));

it('get window onerror should not throw error',
ifEnvSupports(
() => {
return canPatchOnProperty(window, 'onerror');
},
function() {
const testFn = function() {
let onerror = window.onerror;
window.onerror = function() {};
onerror = window.onerror;
};
expect(testFn()).not.toThrow();
}));

}));

describe('eventListener hooks', function() {
Expand Down

0 comments on commit 160531b

Please sign in to comment.