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

Commit 20b5a5d

Browse files
JiaLiPassionmhevery
authored andcommitted
fix: inline event handler issue
close #525 close #540
1 parent 0fd8c03 commit 20b5a5d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Diff for: lib/common/utils.ts

+26
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ export const isBrowser: boolean =
5454
export function patchProperty(obj, prop) {
5555
const desc = Object.getOwnPropertyDescriptor(obj, prop) || {enumerable: true, configurable: true};
5656

57+
const originalDesc = Object.getOwnPropertyDescriptor(obj, 'original' + prop);
58+
if (!originalDesc && desc.get) {
59+
Object.defineProperty(obj, 'original' + prop, {
60+
enumerable: false,
61+
configurable: true,
62+
get: desc.get
63+
});
64+
}
65+
5766
// A property descriptor cannot have getter/setter and be writable
5867
// deleting the writable and value properties avoids this error:
5968
//
@@ -89,6 +98,23 @@ export function patchProperty(obj, prop) {
8998
// The getter would return undefined for unassigned properties but the default value of an
9099
// unassigned property is null
91100
desc.get = function() {
101+
let r = this[_prop] || null;
102+
// result will be null when use inline event attribute,
103+
// such as <button onclick="func();">OK</button>
104+
// because the onclick function is internal raw uncompiled handler
105+
// the onclick will be evaluated when first time event was triggered or
106+
// the property is accessed, https://github.com/angular/zone.js/issues/525
107+
// so we should use original native get to retrive the handler
108+
if (r === null) {
109+
let oriDesc = Object.getOwnPropertyDescriptor(obj, 'original' + prop);
110+
if (oriDesc && oriDesc.get) {
111+
r = oriDesc.get.apply(this, arguments);
112+
if (r) {
113+
desc.set.apply(this, [r]);
114+
this.removeAttribute(prop);
115+
}
116+
}
117+
}
92118
return this[_prop] || null;
93119
};
94120

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

+17
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ describe('Zone', function() {
125125
expect(hookSpy).toHaveBeenCalled();
126126
expect(eventListenerSpy).not.toHaveBeenCalled();
127127
});
128+
129+
it('should support inline event handler attributes', function() {
130+
var hookSpy = jasmine.createSpy('hook');
131+
var zone = rootZone.fork({
132+
name: 'spy',
133+
onScheduleTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
134+
task: Task): any => {
135+
hookSpy();
136+
return parentZoneDelegate.scheduleTask(targetZone, task);
137+
}
138+
});
139+
140+
zone.run(function() {
141+
button.setAttribute('onclick', 'return');
142+
expect(button.onclick).not.toBe(null);
143+
});
144+
})
128145
});
129146
});
130147
});

0 commit comments

Comments
 (0)