MCPcopy Create free account
hub / github.com/melonjs/melonJS / xmlToObject

Function xmlToObject

packages/melonjs/src/utils/xml.ts:29–70  ·  view source on GitHub ↗
(
	element: Element,
	normalizer?: XMLNormalizer,
)

Source from the content-addressed store, hash-verified

27 * @returns plain object representation of the XML element
28 */
29export function xmlToObject(
30 element: Element,
31 normalizer?: XMLNormalizer,
32): Record<string, unknown> {
33 const obj: Record<string, unknown> = {};
34
35 // only Element nodes (nodeType 1) have attributes; Document nodes (nodeType 9) do not
36 if (element.nodeType === ELEMENT_NODE) {
37 const attributes = element.attributes;
38 for (let i = 0, len = attributes.length; i < len; i++) {
39 const attr = attributes[i];
40 obj[attr.name] = attr.value;
41 }
42 }
43
44 // only allocate the closure when a normalizer needs it
45 const parse = normalizer
46 ? (node: Element) => xmlToObject(node, normalizer)
47 : undefined;
48
49 let text = "";
50
51 const children = element.childNodes;
52 for (let i = 0, len = children.length; i < len; i++) {
53 const node = children[i];
54 if (node.nodeType === ELEMENT_NODE) {
55 if (parse) {
56 normalizer!(obj, node as Element, parse);
57 } else {
58 obj[(node as Element).nodeName] = xmlToObject(node as Element);
59 }
60 } else if (node.nodeType === TEXT_NODE) {
61 text += node.nodeValue!.trim();
62 }
63 }
64
65 if (text) {
66 obj.text = text;
67 }
68
69 return obj;
70}

Callers 3

normalizeTMXFunction · 0.90
parseFunction · 0.90
xml.spec.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected