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

Commit

Permalink
fix: on<property> handling broken in v0.8.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Apr 21, 2017
1 parent 0e2ead2 commit fbe7b13
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
58 changes: 21 additions & 37 deletions lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
declare const WorkerGlobalScope: any;

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

Expand Down Expand Up @@ -68,25 +67,20 @@ export function patchProperty(obj: any, prop: string) {
return;
}

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:
//
// TypeError: property descriptors must not specify a value or be writable when a
// getter or setter has been specified
delete desc.writable;
delete desc.value;
const originalDescGet = desc.get;

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

desc.set = function(fn) {
desc.set = function(newValue) {
// in some of windows's onproperty callback, this is undefined
// so we need to check it
let target = this;
Expand All @@ -96,20 +90,14 @@ export function patchProperty(obj: any, prop: string) {
if (!target) {
return;
}
if (target[_prop]) {
target.removeEventListener(eventName, target[_prop]);
}

if (typeof fn === 'string') {
const src: string = fn;
fn = new Function(src);
fn[VALUE] = src;
let previousValue = target[_prop];
if (previousValue) {
target.removeEventListener(eventName, previousValue);
}

if (typeof fn === 'function') {
if (typeof newValue === 'function') {
const wrapFn = function(event: Event) {
let result;
result = fn.apply(this, arguments);
let result = newValue.apply(this, arguments);

if (result != undefined && !result) {
event.preventDefault();
Expand All @@ -136,26 +124,22 @@ export function patchProperty(obj: any, prop: string) {
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
// 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 retrieve the handler
if (r === null) {
if (originalDesc && originalDesc.get) {
r = originalDesc.get.apply(this, arguments);
if (r) {
desc.set.apply(this, [r]);
if (typeof target['removeAttribute'] === 'function') {
target.removeAttribute(prop);
}
}
if (target.hasOwnProperty(_prop)) {
return target[_prop];
} else {
// 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 retrieve the handler
let value = originalDescGet.apply(this);
value = desc.set.apply(this, [value]);
if (typeof target['removeAttribute'] === 'function') {
target.removeAttribute(prop);
}
return value;
}
const value = target[_prop] || null;
return value && value.hasOwnProperty(VALUE) ? value[value] : value;
};

Object.defineProperty(obj, prop, desc);
Expand Down
2 changes: 1 addition & 1 deletion test/browser/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ describe('Zone', function() {

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

Expand Down

0 comments on commit fbe7b13

Please sign in to comment.