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
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed Jan 12, 2017
1 parent e1d3240 commit 2810188
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 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
4 changes: 3 additions & 1 deletion lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
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 2810188

Please sign in to comment.