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

Commit fbe7b13

Browse files
committed
fix: on<property> handling broken in v0.8.7
1 parent 0e2ead2 commit fbe7b13

File tree

2 files changed

+22
-38
lines changed

2 files changed

+22
-38
lines changed

Diff for: lib/common/utils.ts

+21-37
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
declare const WorkerGlobalScope: any;
1616

1717
export const zoneSymbol: (name: string) => string = (n) => `__zone_symbol__${n}`;
18-
const VALUE = zoneSymbol('value');
1918
const _global: any =
2019
typeof window === 'object' && window || typeof self === 'object' && self || global;
2120

@@ -68,25 +67,20 @@ export function patchProperty(obj: any, prop: string) {
6867
return;
6968
}
7069

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-
7770
// A property descriptor cannot have getter/setter and be writable
7871
// deleting the writable and value properties avoids this error:
7972
//
8073
// TypeError: property descriptors must not specify a value or be writable when a
8174
// getter or setter has been specified
8275
delete desc.writable;
8376
delete desc.value;
77+
const originalDescGet = desc.get;
8478

8579
// substr(2) cuz 'onclick' -> 'click', etc
8680
const eventName = prop.substr(2);
8781
const _prop = zoneSymbol('_' + prop);
8882

89-
desc.set = function(fn) {
83+
desc.set = function(newValue) {
9084
// in some of windows's onproperty callback, this is undefined
9185
// so we need to check it
9286
let target = this;
@@ -96,20 +90,14 @@ export function patchProperty(obj: any, prop: string) {
9690
if (!target) {
9791
return;
9892
}
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);
10796
}
10897

109-
if (typeof fn === 'function') {
98+
if (typeof newValue === 'function') {
11099
const wrapFn = function(event: Event) {
111-
let result;
112-
result = fn.apply(this, arguments);
100+
let result = newValue.apply(this, arguments);
113101

114102
if (result != undefined && !result) {
115103
event.preventDefault();
@@ -136,26 +124,22 @@ export function patchProperty(obj: any, prop: string) {
136124
if (!target) {
137125
return null;
138126
}
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);
155140
}
141+
return value;
156142
}
157-
const value = target[_prop] || null;
158-
return value && value.hasOwnProperty(VALUE) ? value[value] : value;
159143
};
160144

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

Diff for: test/browser/browser.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ describe('Zone', function() {
530530

531531
zone.run(function() {
532532
button.setAttribute('onclick', 'return');
533-
expect(button.onclick).not.toBe('return');
533+
expect(button.onclick).not.toBe(null);
534534
});
535535
});
536536

0 commit comments

Comments
 (0)