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

Commit 1401d60

Browse files
JiaLiPassionmhevery
authored andcommitted
fix: #593, only call removeAttribute when have the method (#594)
1 parent f7330de commit 1401d60

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

Diff for: lib/browser/property-descriptor.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ function canPatchViaPropertyDescriptor() {
5656
if (desc && !desc.configurable) return false;
5757
}
5858

59+
const xhrDesc = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'onreadystatechange');
60+
5961
// add enumerable and configurable here because in opera
6062
// by default XMLHttpRequest.prototype.onreadystatechange is undefined
6163
// without adding enumerable and configurable will cause onreadystatechange
@@ -69,7 +71,8 @@ function canPatchViaPropertyDescriptor() {
6971
});
7072
const req = new XMLHttpRequest();
7173
const result = !!req.onreadystatechange;
72-
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', {});
74+
// restore original desc
75+
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {});
7376
return result;
7477
};
7578

Diff for: lib/common/utils.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,15 @@ export function patchProperty(obj, prop) {
101101
// because the onclick function is internal raw uncompiled handler
102102
// the onclick will be evaluated when first time event was triggered or
103103
// the property is accessed, https://github.com/angular/zone.js/issues/525
104-
// so we should use original native get to retrive the handler
104+
// so we should use original native get to retrieve the handler
105105
if (r === null) {
106-
let oriDesc = Object.getOwnPropertyDescriptor(obj, 'original' + prop);
107-
if (oriDesc && oriDesc.get) {
108-
r = oriDesc.get.apply(this, arguments);
106+
if (originalDesc && originalDesc.get) {
107+
r = originalDesc.get.apply(this, arguments);
109108
if (r) {
110109
desc.set.apply(this, [r]);
111-
this.removeAttribute(prop);
110+
if (typeof this['removeAttribute'] === 'function') {
111+
this.removeAttribute(prop);
112+
}
112113
}
113114
}
114115
}

Diff for: test/browser/XMLHttpRequest.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,15 @@ describe('XMLHttpRequest', function() {
209209
});
210210
});
211211
});
212+
213+
it('should not throw error when get XMLHttpRequest.prototype.onreadystatechange the first time',
214+
function() {
215+
const func = function() {
216+
testZone.run(function() {
217+
const req = new XMLHttpRequest();
218+
req.onreadystatechange;
219+
});
220+
};
221+
expect(func).not.toThrow();
222+
});
212223
});

0 commit comments

Comments
 (0)