Bug 1395313 - Let JSON Viewer ignore the BOM. r=Honza

This commit is contained in:
Oriol Brufau 2017-09-12 06:15:00 -04:00
parent fcc1afee27
commit bc513ab7cb
2 changed files with 28 additions and 13 deletions

View File

@ -13,25 +13,28 @@ define(function (require, exports, module) {
const json = document.getElementById("json");
let jsonData;
let prettyURL;
try {
jsonData = JSON.parse(json.textContent);
} catch (err) {
jsonData = err;
}
// Application state object.
let input = {
jsonText: json.textContent,
jsonPretty: null,
json: jsonData,
headers: JSONView.headers,
tabActive: 0,
prettified: false
};
// Remove BOM.
if (input.jsonText.startsWith("\ufeff")) {
input.jsonText = input.jsonText.slice(1);
}
try {
input.json = JSON.parse(input.jsonText);
} catch (err) {
input.json = err;
}
json.remove();
/**
@ -59,7 +62,7 @@ define(function (require, exports, module) {
},
onPrettify: function (data) {
if (jsonData instanceof Error) {
if (input.json instanceof Error) {
// Cannot prettify invalid JSON
return;
}
@ -67,7 +70,7 @@ define(function (require, exports, module) {
theApp.setState({jsonText: input.jsonText});
} else {
if (!input.jsonPretty) {
input.jsonPretty = JSON.stringify(jsonData, null, " ");
input.jsonPretty = JSON.stringify(input.json, null, " ");
}
theApp.setState({jsonText: input.jsonPretty});
}

View File

@ -5,12 +5,24 @@
"use strict";
// In UTF-8 this is a heavy black heart.
const encodedChar = "%E2%9D%A4";
add_task(function* () {
info("Test UTF-8 JSON started");
const encodedChar = "%E2%9D%A4"; // In UTF-8 this is a heavy black heart
const TEST_JSON_URL = "data:application/json;charset=ANSI,[\"" + encodedChar + "\"]";
info("Test 1: UTF-8 is used by default");
yield testUrl("data:application/json,[\"" + encodedChar + "\"]");
info("Test 2: The charset parameter is ignored");
yield testUrl("data:application/json;charset=ANSI,[\"" + encodedChar + "\"]");
info("Test 3: The UTF-8 BOM is tolerated.");
const bom = "%EF%BB%BF";
yield testUrl("data:application/json," + bom + "[\"" + encodedChar + "\"]");
});
function* testUrl(TEST_JSON_URL) {
yield addJsonViewTab(TEST_JSON_URL);
let countBefore = yield getElementCount(".jsonPanelBox .treeTable .treeRow");
@ -24,4 +36,4 @@ add_task(function* () {
".jsonPanelBox .treeTable .stringCell");
is(objectCellText, JSON.stringify(decodeURIComponent(encodedChar)),
"The source has been parsed as UTF-8, ignoring the charset parameter.");
});
}