'use strict'; let METADATA_SYMBOL; if (typeof Symbol !== "function") { METADATA_SYMBOL = "@@xmlMetadata"; } else { METADATA_SYMBOL = Symbol("XML Node Metadata"); } export default class XmlNode { constructor(tagname) { this.tagname = tagname; this.child = []; //nested tags, text, cdata, comments in order this[":@"] = Object.create(null); //attributes map } add(key, val) { // this.child.push( {name : key, val: val, isCdata: isCdata }); if (key === "__proto__") key = "#__proto__"; this.child.push({ [key]: val }); } addChild(node, startIndex) { if (node.tagname === "__proto__") node.tagname = "#__proto__"; if (node[":@"] && Object.keys(node[":@"]).length > 0) { this.child.push({ [node.tagname]: node.child, [":@"]: node[":@"] }); } else { this.child.push({ [node.tagname]: node.child }); } // if requested, add the startIndex this.addStartIndex(startIndex); } addStartIndex(startIndex) { if (startIndex !== undefined) { // Note: for now we just overwrite the metadata. If we had more complex metadata, // we might need to do an object append here: metadata = { ...metadata, startIndex } this.child[this.child.length - 1][METADATA_SYMBOL] = { startIndex }; } } addEndIndex(endIndex) { const lastChild = this.child[this.child.length - 1]; // endIndex is write-once: when updateTag drops a node, the last child is a // previously completed sibling whose endIndex must not be overwritten if (lastChild !== undefined && lastChild[METADATA_SYMBOL] !== undefined && lastChild[METADATA_SYMBOL].endIndex === undefined) { lastChild[METADATA_SYMBOL].endIndex = endIndex; } } /** symbol used for metadata */ static getMetaDataSymbol() { return METADATA_SYMBOL; } }