fast-xml-parser 5.3.7 → 5.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,8 +10,8 @@ const METADATA_SYMBOL = XmlNode.getMetaDataSymbol();
10
10
  * @param {any} options
11
11
  * @returns
12
12
  */
13
- export default function prettify(node, options){
14
- return compress( node, options);
13
+ export default function prettify(node, options) {
14
+ return compress(node, options);
15
15
  }
16
16
 
17
17
  /**
@@ -21,78 +21,82 @@ export default function prettify(node, options){
21
21
  * @param {string} jPath
22
22
  * @returns object
23
23
  */
24
- function compress(arr, options, jPath){
24
+ function compress(arr, options, jPath) {
25
25
  let text;
26
- const compressedObj = {};
26
+ const compressedObj = {}; //This is intended to be a plain object
27
27
  for (let i = 0; i < arr.length; i++) {
28
28
  const tagObj = arr[i];
29
29
  const property = propName(tagObj);
30
30
  let newJpath = "";
31
- if(jPath === undefined) newJpath = property;
31
+ if (jPath === undefined) newJpath = property;
32
32
  else newJpath = jPath + "." + property;
33
33
 
34
- if(property === options.textNodeName){
35
- if(text === undefined) text = tagObj[property];
34
+ if (property === options.textNodeName) {
35
+ if (text === undefined) text = tagObj[property];
36
36
  else text += "" + tagObj[property];
37
- }else if(property === undefined){
37
+ } else if (property === undefined) {
38
38
  continue;
39
- }else if(tagObj[property]){
40
-
39
+ } else if (tagObj[property]) {
40
+
41
41
  let val = compress(tagObj[property], options, newJpath);
42
42
  const isLeaf = isLeafTag(val, options);
43
- if (tagObj[METADATA_SYMBOL] !== undefined) {
44
- val[METADATA_SYMBOL] = tagObj[METADATA_SYMBOL]; // copy over metadata
45
- }
46
43
 
47
- if(tagObj[":@"]){
48
- assignAttributes( val, tagObj[":@"], newJpath, options);
49
- }else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){
44
+ if (tagObj[":@"]) {
45
+ assignAttributes(val, tagObj[":@"], newJpath, options);
46
+ } else if (Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode) {
50
47
  val = val[options.textNodeName];
51
- }else if(Object.keys(val).length === 0){
52
- if(options.alwaysCreateTextNode) val[options.textNodeName] = "";
48
+ } else if (Object.keys(val).length === 0) {
49
+ if (options.alwaysCreateTextNode) val[options.textNodeName] = "";
53
50
  else val = "";
54
51
  }
55
52
 
56
- if(compressedObj[property] !== undefined && compressedObj.hasOwnProperty(property)) {
57
- if(!Array.isArray(compressedObj[property])) {
58
- compressedObj[property] = [ compressedObj[property] ];
53
+ if (tagObj[METADATA_SYMBOL] !== undefined && typeof val === "object" && val !== null) {
54
+ val[METADATA_SYMBOL] = tagObj[METADATA_SYMBOL]; // copy over metadata
55
+ }
56
+
57
+
58
+ if (compressedObj[property] !== undefined && Object.prototype.hasOwnProperty.call(compressedObj, property)) {
59
+ if (!Array.isArray(compressedObj[property])) {
60
+ compressedObj[property] = [compressedObj[property]];
59
61
  }
60
62
  compressedObj[property].push(val);
61
- }else{
63
+ } else {
62
64
  //TODO: if a node is not an array, then check if it should be an array
63
65
  //also determine if it is a leaf node
64
- if (options.isArray(property, newJpath, isLeaf )) {
66
+ if (options.isArray(property, newJpath, isLeaf)) {
65
67
  compressedObj[property] = [val];
66
- }else{
68
+ } else {
67
69
  compressedObj[property] = val;
68
70
  }
69
71
  }
70
72
  }
71
-
73
+
72
74
  }
73
75
  // if(text && text.length > 0) compressedObj[options.textNodeName] = text;
74
- if(typeof text === "string"){
75
- if(text.length > 0) compressedObj[options.textNodeName] = text;
76
- }else if(text !== undefined) compressedObj[options.textNodeName] = text;
76
+ if (typeof text === "string") {
77
+ if (text.length > 0) compressedObj[options.textNodeName] = text;
78
+ } else if (text !== undefined) compressedObj[options.textNodeName] = text;
79
+
80
+
77
81
  return compressedObj;
78
82
  }
79
83
 
80
- function propName(obj){
84
+ function propName(obj) {
81
85
  const keys = Object.keys(obj);
82
86
  for (let i = 0; i < keys.length; i++) {
83
87
  const key = keys[i];
84
- if(key !== ":@") return key;
88
+ if (key !== ":@") return key;
85
89
  }
86
90
  }
87
91
 
88
- function assignAttributes(obj, attrMap, jpath, options){
92
+ function assignAttributes(obj, attrMap, jpath, options) {
89
93
  if (attrMap) {
90
94
  const keys = Object.keys(attrMap);
91
95
  const len = keys.length; //don't make it inline
92
96
  for (let i = 0; i < len; i++) {
93
97
  const atrrName = keys[i];
94
98
  if (options.isArray(atrrName, jpath + "." + atrrName, true, true)) {
95
- obj[atrrName] = [ attrMap[atrrName] ];
99
+ obj[atrrName] = [attrMap[atrrName]];
96
100
  } else {
97
101
  obj[atrrName] = attrMap[atrrName];
98
102
  }
@@ -100,10 +104,10 @@ function assignAttributes(obj, attrMap, jpath, options){
100
104
  }
101
105
  }
102
106
 
103
- function isLeafTag(obj, options){
107
+ function isLeafTag(obj, options) {
104
108
  const { textNodeName } = options;
105
109
  const propCount = Object.keys(obj).length;
106
-
110
+
107
111
  if (propCount === 0) {
108
112
  return true;
109
113
  }
@@ -8,23 +8,23 @@ if (typeof Symbol !== "function") {
8
8
  METADATA_SYMBOL = Symbol("XML Node Metadata");
9
9
  }
10
10
 
11
- export default class XmlNode{
11
+ export default class XmlNode {
12
12
  constructor(tagname) {
13
13
  this.tagname = tagname;
14
14
  this.child = []; //nested tags, text, cdata, comments in order
15
- this[":@"] = {}; //attributes map
15
+ this[":@"] = Object.create(null); //attributes map
16
16
  }
17
- add(key,val){
17
+ add(key, val) {
18
18
  // this.child.push( {name : key, val: val, isCdata: isCdata });
19
- if(key === "__proto__") key = "#__proto__";
20
- this.child.push( {[key]: val });
19
+ if (key === "__proto__") key = "#__proto__";
20
+ this.child.push({ [key]: val });
21
21
  }
22
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 });
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
28
  }
29
29
  // if requested, add the startIndex
30
30
  if (startIndex !== undefined) {