Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #27216 from stasm/1115773-loadJSON-sync
Browse files Browse the repository at this point in the history
Bug 1115773 - Allow L10n's loadJSON to be sync. r=gandalf
  • Loading branch information
stasm committed Jan 11, 2015
2 parents 918cc1f + 5052037 commit 1acb64c
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions shared/js/l10n.js
Expand Up @@ -15,46 +15,33 @@
/* jshint browser:true */

var io = {
load: function load(url, callback, sync) {

_load: function(type, url, callback, sync) {
var xhr = new XMLHttpRequest();
var needParse;

if (xhr.overrideMimeType) {
xhr.overrideMimeType('text/plain');
xhr.overrideMimeType(type);
}

xhr.open('GET', url, !sync);

xhr.addEventListener('load', function io_load(e) {
if (e.target.status === 200 || e.target.status === 0) {
callback(null, e.target.responseText);
if (type === 'application/json') {
// Gecko 11.0+ forbids the use of the responseType attribute when
// performing sync requests (NS_ERROR_DOM_INVALID_ACCESS_ERR).
// We'll need to JSON.parse manually.
if (sync) {
needParse = true;
} else {
callback(new L10nError('Not found: ' + url));
xhr.responseType = 'json';
}
});
xhr.addEventListener('error', callback);
xhr.addEventListener('timeout', callback);

// the app: protocol throws on 404, see https://bugzil.la/827243
try {
xhr.send(null);
} catch (e) {
callback(new L10nError('Not found: ' + url));
}
},

loadJSON: function loadJSON(url, callback) {
var xhr = new XMLHttpRequest();

if (xhr.overrideMimeType) {
xhr.overrideMimeType('application/json');
}

xhr.open('GET', url);

xhr.responseType = 'json';
xhr.addEventListener('load', function io_loadjson(e) {
xhr.addEventListener('load', function io_onload(e) {
if (e.target.status === 200 || e.target.status === 0) {
callback(null, e.target.response);
// Sinon.JS's FakeXHR doesn't have the response property
var res = e.target.response || e.target.responseText;
callback(null, needParse ? JSON.parse(res) : res);
} else {
callback(new L10nError('Not found: ' + url));
}
Expand All @@ -68,7 +55,16 @@
} catch (e) {
callback(new L10nError('Not found: ' + url));
}
},

load: function(url, callback, sync) {
return io._load('text/plain', url, callback, sync);
},

loadJSON: function(url, callback, sync) {
return io._load('application/json', url, callback, sync);
}

};

function EventEmitter() {}
Expand Down Expand Up @@ -1630,7 +1626,7 @@

switch (node.getAttribute('name')) {
case 'availableLanguages':
meta.availableLanguages =
meta.availableLanguages =
splitAvailableLanguagesString(node.getAttribute('content'));
break;
case 'defaultLanguage':
Expand Down

0 comments on commit 1acb64c

Please sign in to comment.