Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});