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..ed084a1a9 100644 --- a/lib/common/utils.ts +++ b/lib/common/utils.ts @@ -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); + } } } } 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(); + }); });