diff --git a/lib/browser/property-descriptor.ts b/lib/browser/property-descriptor.ts index f6776203f..dfd8ac95c 100644 --- a/lib/browser/property-descriptor.ts +++ b/lib/browser/property-descriptor.ts @@ -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 @@ -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; }; diff --git a/lib/common/utils.ts b/lib/common/utils.ts index e5c681f93..6f348d50a 100644 --- a/lib/common/utils.ts +++ b/lib/common/utils.ts @@ -108,7 +108,9 @@ export function patchProperty(obj, prop) { r = oriDesc.get.apply(this, arguments); if (r) { desc.set.apply(this, [r]); - this.removeAttribute(prop); + if (typeof this['removeAttribute'] === 'function') { + this.removeAttribute(prop); + } } } } diff --git a/test/browser/XMLHttpRequest.spec.ts b/test/browser/XMLHttpRequest.spec.ts index 6a9743b0d..de56a0efe 100644 --- a/test/browser/XMLHttpRequest.spec.ts +++ b/test/browser/XMLHttpRequest.spec.ts @@ -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(); + }); });