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

Commit

Permalink
fix: #593, only call removeAttribute when have the method (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Jan 17, 2017
1 parent f7330de commit 1401d60
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/browser/property-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function canPatchViaPropertyDescriptor() {
if (desc && !desc.configurable) return false;
}

const xhrDesc = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'onreadystatechange');

// add enumerable and configurable here because in opera
// by default XMLHttpRequest.prototype.onreadystatechange is undefined
// without adding enumerable and configurable will cause onreadystatechange
Expand All @@ -69,7 +71,8 @@ function canPatchViaPropertyDescriptor() {
});
const req = new XMLHttpRequest();
const result = !!req.onreadystatechange;
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', {});
// restore original desc
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {});
return result;
};

Expand Down
11 changes: 6 additions & 5 deletions lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ export function patchProperty(obj, prop) {
// 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
// so we should use original native get to retrieve the handler
if (r === null) {
let oriDesc = Object.getOwnPropertyDescriptor(obj, 'original' + prop);
if (oriDesc && oriDesc.get) {
r = oriDesc.get.apply(this, arguments);
if (originalDesc && originalDesc.get) {
r = originalDesc.get.apply(this, arguments);
if (r) {
desc.set.apply(this, [r]);
this.removeAttribute(prop);
if (typeof this['removeAttribute'] === 'function') {
this.removeAttribute(prop);
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/browser/XMLHttpRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,15 @@ describe('XMLHttpRequest', function() {
});
});
});

it('should not throw error when get XMLHttpRequest.prototype.onreadystatechange the first time',
function() {
const func = function() {
testZone.run(function() {
const req = new XMLHttpRequest();
req.onreadystatechange;
});
};
expect(func).not.toThrow();
});
});

0 comments on commit 1401d60

Please sign in to comment.