MCPcopy Create free account
hub / github.com/NaturalIntelligence/fast-xml-parser / XmlNode

Class XmlNode

src/xmlparser/xmlNode.js:11–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9}
10
11export default class XmlNode {
12 constructor(tagname) {
13 this.tagname = tagname;
14 this.child = []; //nested tags, text, cdata, comments in order
15 this[":@"] = Object.create(null); //attributes map
16 }
17 add(key, val) {
18 // this.child.push( {name : key, val: val, isCdata: isCdata });
19 if (key === "__proto__") key = "#__proto__";
20 this.child.push({ [key]: val });
21 }
22 addChild(node, startIndex) {
23 if (node.tagname === "__proto__") node.tagname = "#__proto__";
24 if (node[":@"] && Object.keys(node[":@"]).length > 0) {
25 this.child.push({ [node.tagname]: node.child, [":@"]: node[":@"] });
26 } else {
27 this.child.push({ [node.tagname]: node.child });
28 }
29 // if requested, add the startIndex
30 this.addStartIndex(startIndex);
31 }
32
33 addStartIndex(startIndex) {
34 if (startIndex !== undefined) {
35 // Note: for now we just overwrite the metadata. If we had more complex metadata,
36 // we might need to do an object append here: metadata = { ...metadata, startIndex }
37 this.child[this.child.length - 1][METADATA_SYMBOL] = { startIndex };
38 }
39 }
40
41 addEndIndex(endIndex) {
42 const lastChild = this.child[this.child.length - 1];
43 // endIndex is write-once: when updateTag drops a node, the last child is a
44 // previously completed sibling whose endIndex must not be overwritten
45 if (lastChild !== undefined && lastChild[METADATA_SYMBOL] !== undefined
46 && lastChild[METADATA_SYMBOL].endIndex === undefined) {
47 lastChild[METADATA_SYMBOL].endIndex = endIndex;
48 }
49 }
50 /** symbol used for metadata */
51 static getMetaDataSymbol() {
52 return METADATA_SYMBOL;
53 }
54}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected