Simpler workaround for local d3.xhr.

If the error flag is set, the response attribute [1] should be the empty
string or null.  So even if a request fails and the response has been
partially loaded, this logic should still work.

This check will fail for local requests where the response entity body
is the empty string, but I don't think it's possible to detect this
situation in current browser implementations. jQuery has the same issue,
for example.

[1]: http://www.w3.org/TR/XMLHttpRequest/#the-response-attribute
This commit is contained in:
Jason Davies 2012-05-04 07:41:25 +01:00
Родитель 95b1eaaf68
Коммит 53d117e6d3
3 изменённых файлов: 6 добавлений и 40 удалений

19
d3.v2.js поставляемый
Просмотреть файл

@ -494,28 +494,11 @@ d3.xhr = function(url, mime, callback) {
req.onreadystatechange = function() { req.onreadystatechange = function() {
if (req.readyState === 4) { if (req.readyState === 4) {
var s = req.status; var s = req.status;
if (!s && d3_xhrLocal && !d3_xhrCrossDomain(url)) { callback(!s && req.response || s >= 200 && s < 300 || s === 304 ? req : null);
s = req.responseText ? 200 : 404;
}
callback(s >= 200 && s < 300 || s === 304 ? req : null);
} }
}; };
req.send(null); req.send(null);
}; };
var d3_xhrUrl = /^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+))?)?/,
d3_xhrLocation,
d3_xhrLocal = false;
try {
d3_xhrLocation = d3_xhrUrl.exec(document.location.href);
d3_xhrLocal = /^(?:about|app|app\-storage|.+\-extension|file|widget):$/i.test(d3_xhrLocation[1]);
} catch(e) {}
function d3_xhrCrossDomain(url) {
var parts = d3_xhrUrl.exec(url);
return !!(parts && (parts[1] != d3_xhrLocation[1] || parts[2] != d3_xhrLocation[2] ||
(parts[3] || (parts[1] === "http:" ? 80 : 443)) != (d3_xhrLocation[3] || (ajaxLocParts[1] === "http:" ? 80 : 443))));
}
d3.text = function(url, mime, callback) { d3.text = function(url, mime, callback) {
function ready(req) { function ready(req) {
callback(req && req.responseText); callback(req && req.responseText);

8
d3.v2.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -7,25 +7,8 @@ d3.xhr = function(url, mime, callback) {
req.onreadystatechange = function() { req.onreadystatechange = function() {
if (req.readyState === 4) { if (req.readyState === 4) {
var s = req.status; var s = req.status;
if (!s && d3_xhrLocal && !d3_xhrCrossDomain(url)) { callback(!s && req.response || s >= 200 && s < 300 || s === 304 ? req : null);
s = req.responseText ? 200 : 404;
}
callback(s >= 200 && s < 300 || s === 304 ? req : null);
} }
}; };
req.send(null); req.send(null);
}; };
var d3_xhrUrl = /^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+))?)?/,
d3_xhrLocation,
d3_xhrLocal = false;
try {
d3_xhrLocation = d3_xhrUrl.exec(document.location.href);
d3_xhrLocal = /^(?:about|app|app\-storage|.+\-extension|file|widget):$/i.test(d3_xhrLocation[1]);
} catch(e) {}
function d3_xhrCrossDomain(url) {
var parts = d3_xhrUrl.exec(url);
return !!(parts && (parts[1] != d3_xhrLocation[1] || parts[2] != d3_xhrLocation[2] ||
(parts[3] || (parts[1] === "http:" ? 80 : 443)) != (d3_xhrLocation[3] || (ajaxLocParts[1] === "http:" ? 80 : 443))));
}