"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}`); } }