Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict";
// A hack to make a list of strings on type level.
// If you add another type to be parsable i.e. implement a parser for it, also add its name here
const parsable = ["string", "MathMLElement", "HTMLElement", "SVGElement"];
function is_primitive(a) {
return "parseAs" in a
&& parsable.includes(a.parseAs)
&& "content" in a
&& typeof a.content === "string";
}
function backendObject_fromJSON(json_object) {
const parser = new DOMParser;
// just a shorthand we will need several times
const parse = function (s) {
const element = parser.parseFromString(s, "text/xml").childNodes[0];
return element;
};
// the Backend_Object to be
const bo = {};
for (let i in json_object) {
const member = json_object[i];
if (is_primitive(member)) {
switch (member.parseAs) {
case "string":
bo[i] = member.content;
break;
case "MathMLElement":
bo[i] = parse(member.content);
break;
case "HTMLElement":
bo[i] = parse(member.content);
break;
case "SVGElement":
bo[i] = parse(member.content);
break;
}
}
else {
bo[i] = backendObject_fromJSON(member);
}
}
if (bo.label instanceof MathMLElement
&& typeof bo.uri === "string"
&& typeof bo.type === "string") {
return bo;
}
else {
console.error(`This is not a Backend_Object:`, json_object);
throw new TypeError(`This is not a Backend_Object: \n ${json_object}`);
}
}