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

Commit

Permalink
fix: inline event handler issue
Browse files Browse the repository at this point in the history
close #525
close #540
  • Loading branch information
JiaLiPassion authored and mhevery committed Dec 18, 2016
1 parent 0fd8c03 commit 20b5a5d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export const isBrowser: boolean =
export function patchProperty(obj, prop) {
const desc = Object.getOwnPropertyDescriptor(obj, prop) || {enumerable: true, configurable: true};

const originalDesc = Object.getOwnPropertyDescriptor(obj, 'original' + prop);
if (!originalDesc && desc.get) {
Object.defineProperty(obj, 'original' + prop, {
enumerable: false,
configurable: true,
get: desc.get
});
}

// A property descriptor cannot have getter/setter and be writable
// deleting the writable and value properties avoids this error:
//
Expand Down Expand Up @@ -89,6 +98,23 @@ export function patchProperty(obj, prop) {
// 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;
// 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
// the onclick will be evaluated when first time event was triggered or
// the property is accessed, https://github.com/angular/zone.js/issues/525
// so we should use original native get to retrive the handler
if (r === null) {
let oriDesc = Object.getOwnPropertyDescriptor(obj, 'original' + prop);
if (oriDesc && oriDesc.get) {
r = oriDesc.get.apply(this, arguments);
if (r) {
desc.set.apply(this, [r]);
this.removeAttribute(prop);
}
}
}
return this[_prop] || null;
};

Expand Down
17 changes: 17 additions & 0 deletions test/browser/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ describe('Zone', function() {
expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).not.toHaveBeenCalled();
});

it('should support inline event handler attributes', function() {
var hookSpy = jasmine.createSpy('hook');
var zone = rootZone.fork({
name: 'spy',
onScheduleTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});

zone.run(function() {
button.setAttribute('onclick', 'return');
expect(button.onclick).not.toBe(null);
});
})
});
});
});

0 comments on commit 20b5a5d

Please sign in to comment.