Skip to content

Commit b6da2d0

Browse files
committed
MAGETWO-43201: Require JS loading via CDN fails
- Proper support of IE9
1 parent 2b350bb commit b6da2d0

File tree

1 file changed

+50
-35
lines changed

1 file changed

+50
-35
lines changed

lib/web/mage/requirejs/text.js

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,41 @@ define(['module'], function (module) {
3636
return external;
3737
}
3838

39+
/**
40+
* Checks that url match current location
41+
*
42+
* @param {String} url
43+
* @returns {Boolean}
44+
*/
45+
function sameDomain(url) {
46+
var uProtocol, uHostName, uPort,
47+
xdRegExp = /^([\w:]+)?\/\/([^\/\\]+)/i,
48+
location = window.location,
49+
match = xdRegExp.exec(url);
50+
51+
if (!match) {
52+
return true;
53+
}
54+
uProtocol = match[1];
55+
uHostName = match[2];
56+
57+
uHostName = uHostName.split(':');
58+
uPort = uHostName[1] || '';
59+
uHostName = uHostName[0];
60+
61+
return (!uProtocol || uProtocol === location.protocol) &&
62+
(!uHostName || uHostName.toLowerCase() === location.hostname.toLowerCase()) &&
63+
((!uPort && !uHostName) || uPort === location.port);
64+
}
65+
3966
/**
4067
* @returns {XMLHttpRequest|XDomainRequest|null}
4168
*/
42-
function createRequest() {
43-
var xhr = null;
69+
function createRequest(url) {
70+
var xhr = new XMLHttpRequest();
4471

45-
if (typeof XDomainRequest !== 'undefined') {
72+
if (!sameDomain(url) && typeof XDomainRequest !== 'undefined') {
4673
xhr = new XDomainRequest();
47-
} else if (typeof XMLHttpRequest !== 'undefined') {
48-
xhr = new XMLHttpRequest();
4974
}
5075

5176
return xhr;
@@ -60,56 +85,46 @@ define(['module'], function (module) {
6085
* @param {Object} headers
6186
*/
6287
function getContent(url, callback, fail, headers) {
63-
var xhr = createRequest(),
88+
var xhr = createRequest(url),
6489
header,
6590
errorHandler = fail || Function();
6691

67-
xhr.open('GET', url, true);
68-
69-
//Allow plugins direct access to xhr headers
70-
for (header in headers) {
71-
if (headers.hasOwnProperty(header)) {
72-
xhr.setRequestHeader(header.toLowerCase(), headers[header]);
92+
if ('setRequestHeader' in xhr && headers) {
93+
for (header in headers) {
94+
if (headers.hasOwnProperty(header)) {
95+
xhr.setRequestHeader(header.toLowerCase(), headers[header]);
96+
}
7397
}
7498
}
7599

76-
//Allow overrides specified in config
77100
if (defaultConfig.onXhr) {
78101
defaultConfig.onXhr(xhr, url);
79102
}
80103

81104
/**
82105
* onload handler
83106
*/
84-
xhr.onreadystatechange = function () {
85-
var status = xhr.status || 0,
86-
error;
87-
88-
if (xhr.readyState !== 4) {
89-
return;
90-
}
91-
92-
if (status > 399 && status < 600) {
93-
//An http 4xx or 5xx error. Signal an error.
94-
error = new Error(url + ' HTTP status: ' + status);
95-
error.xhr = xhr;
96-
97-
errorHandler(error);
98-
99-
if (defaultConfig.onXhrFailure) {
100-
defaultConfig.onXhrFailure(xhr, url);
101-
}
102-
103-
return;
104-
}
107+
xhr.onload = function () {
105108

106109
callback(xhr.responseText);
107110

108111
if (defaultConfig.onXhrComplete) {
109112
defaultConfig.onXhrComplete(xhr, url);
110113
}
111114
};
112-
xhr.send(null);
115+
/**
116+
* onerror handler
117+
*/
118+
xhr.onerror = function (event) {
119+
errorHandler(event);
120+
121+
if (defaultConfig.onXhrFailure) {
122+
defaultConfig.onXhrFailure(xhr, url, event);
123+
}
124+
};
125+
126+
xhr.open('GET', url);
127+
xhr.send();
113128
}
114129

115130
/**

0 commit comments

Comments
 (0)