Skip to content

Commit b47058f

Browse files
committed
repaired implementation for XHR contructor to work independently of a possibly present mocking framework, resolved infinite loop error
1 parent 8e24263 commit b47058f

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

XMLHttpRequest.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,26 @@
1717
*/
1818

1919
(function () {
20+
21+
// Save reference to earlier defined object implementation (if any)
22+
var oXMLHttpRequest = window.XMLHttpRequest;
23+
2024
// Define on browser type
2125
var bGecko = !!window.controllers;
2226
var bIE = !!window.document.namespaces;
2327
var bIE7 = bIE && window.navigator.userAgent.match(/MSIE 7.0/);
2428

2529
// Enables "XMLHttpRequest()" call next to "new XMLHttpRequest()"
2630
function fXMLHttpRequest() {
27-
this._object = window.XMLHttpRequest && !bIE7 ? new window.XMLHttpRequest() : new window.ActiveXObject("Microsoft.XMLHTTP");
31+
if (!window.XMLHttpRequest || bIE7) {
32+
this._object = new window.ActiveXObject("Microsoft.XMLHTTP");
33+
} // only use initial XHR object internally if current reference to XHR is our normalized replacement
34+
else if (window.XMLHttpRequest.isNormalizedObject) {
35+
this._object = new oXMLHttpRequest();
36+
} // otherwise use whatever is currently referenced by XMLHttpRequest
37+
else {
38+
this._object = new window.XMLHttpRequest();
39+
}
2840
this._listeners = [];
2941
}
3042

@@ -36,8 +48,11 @@
3648

3749
// BUGFIX: Firefox with Firebug installed would break pages if not executed
3850
if (bGecko && oXMLHttpRequest.wrapped) {
39-
cXMLHttpRequest.wrapped = window.XMLHttpRequest.wrapped;
51+
cXMLHttpRequest.wrapped = oXMLHttpRequest.wrapped;
4052
}
53+
54+
// Marker to be able to easily identify our object
55+
cXMLHttpRequest.isNormalizedObject = true;
4156

4257
// Constants
4358
cXMLHttpRequest.UNSENT = 0;

0 commit comments

Comments
 (0)