mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
Remove stripCommentsFromJson code and test
Since we dont allow comments in the json anymore anyway
This commit is contained in:
parent
d570f910f8
commit
dc089d7db9
@ -19,92 +19,12 @@ var Translator = (function () {
|
|||||||
xhr.open("GET", file, true);
|
xhr.open("GET", file, true);
|
||||||
xhr.onreadystatechange = function () {
|
xhr.onreadystatechange = function () {
|
||||||
if (xhr.readyState === 4 && xhr.status === 200) {
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
||||||
callback(JSON.parse(stripComments(xhr.responseText)));
|
callback(JSON.parse(xhr.responseText));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* loadJSON(str, options)
|
|
||||||
* Remove any commenting from a json file so it can be parsed.
|
|
||||||
*
|
|
||||||
* argument str string - The string that contains json with comments.
|
|
||||||
* argument opts function - Strip options.
|
|
||||||
*
|
|
||||||
* return the stripped string.
|
|
||||||
*/
|
|
||||||
function stripComments(str, opts) {
|
|
||||||
// strip comments copied from: https://github.com/sindresorhus/strip-json-comments
|
|
||||||
|
|
||||||
var singleComment = 1;
|
|
||||||
var multiComment = 2;
|
|
||||||
|
|
||||||
function stripWithoutWhitespace() {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function stripWithWhitespace(str, start, end) {
|
|
||||||
return str.slice(start, end).replace(/\S/g, " ");
|
|
||||||
}
|
|
||||||
|
|
||||||
opts = opts || {};
|
|
||||||
|
|
||||||
var currentChar;
|
|
||||||
var nextChar;
|
|
||||||
var insideString = false;
|
|
||||||
var insideComment = false;
|
|
||||||
var offset = 0;
|
|
||||||
var ret = "";
|
|
||||||
var strip = opts.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace;
|
|
||||||
|
|
||||||
for (var i = 0; i < str.length; i++) {
|
|
||||||
currentChar = str[i];
|
|
||||||
nextChar = str[i + 1];
|
|
||||||
|
|
||||||
if (!insideComment && currentChar === '"') {
|
|
||||||
var escaped = str[i - 1] === "\\" && str[i - 2] !== "\\";
|
|
||||||
if (!escaped) {
|
|
||||||
insideString = !insideString;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (insideString) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!insideComment && currentChar + nextChar === "//") {
|
|
||||||
ret += str.slice(offset, i);
|
|
||||||
offset = i;
|
|
||||||
insideComment = singleComment;
|
|
||||||
i++;
|
|
||||||
} else if (insideComment === singleComment && currentChar + nextChar === "\r\n") {
|
|
||||||
i++;
|
|
||||||
insideComment = false;
|
|
||||||
ret += strip(str, offset, i);
|
|
||||||
offset = i;
|
|
||||||
continue;
|
|
||||||
} else if (insideComment === singleComment && currentChar === "\n") {
|
|
||||||
insideComment = false;
|
|
||||||
ret += strip(str, offset, i);
|
|
||||||
offset = i;
|
|
||||||
} else if (!insideComment && currentChar + nextChar === "/*") {
|
|
||||||
ret += str.slice(offset, i);
|
|
||||||
offset = i;
|
|
||||||
insideComment = multiComment;
|
|
||||||
i++;
|
|
||||||
continue;
|
|
||||||
} else if (insideComment === multiComment && currentChar + nextChar === "*/") {
|
|
||||||
i++;
|
|
||||||
insideComment = false;
|
|
||||||
ret += strip(str, offset, i + 1);
|
|
||||||
offset = i + 1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret + (insideComment ? strip(str.substr(offset)) : str.substr(offset));
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
coreTranslations: {},
|
coreTranslations: {},
|
||||||
coreTranslationsFallback: {},
|
coreTranslationsFallback: {},
|
||||||
|
@ -171,25 +171,6 @@ describe("Translator", function () {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should strip comments", function (done) {
|
|
||||||
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
|
||||||
dom.window.onload = function () {
|
|
||||||
const { Translator } = dom.window;
|
|
||||||
const file = "StripComments.json";
|
|
||||||
|
|
||||||
Translator.load(mmm, file, false, function () {
|
|
||||||
expect(Translator.translations[mmm.name]).to.be.deep.equal({
|
|
||||||
'FOO"BAR': "Today",
|
|
||||||
N: "N",
|
|
||||||
E: "E",
|
|
||||||
S: "S",
|
|
||||||
W: "W"
|
|
||||||
});
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not load translations, if module fallback exists", function (done) {
|
it("should not load translations, if module fallback exists", function (done) {
|
||||||
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||||
dom.window.onload = function () {
|
dom.window.onload = function () {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user