fast-xml-parser 5.5.6 → 5.5.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.
@@ -113,6 +113,10 @@ export default class OrderedObjParser {
113
113
  // Initialize path matcher for path-expression-matcher
114
114
  this.matcher = new Matcher();
115
115
 
116
+ // Live read-only proxy of matcher — PEM creates and caches this internally.
117
+ // All user callbacks receive this instead of the mutable matcher.
118
+ this.readonlyMatcher = this.matcher.readOnly();
119
+
116
120
  // Flag to track if current node is a stop node (optimization)
117
121
  this.isCurrentNodeStopNode = false;
118
122
 
@@ -225,7 +229,7 @@ function buildAttributesMap(attrStr, jPath, tagName) {
225
229
  if (this.options.trimValues) {
226
230
  parsedVal = parsedVal.trim();
227
231
  }
228
- parsedVal = this.replaceEntitiesValue(parsedVal, tagName, jPath);
232
+ parsedVal = this.replaceEntitiesValue(parsedVal, tagName, this.readonlyMatcher);
229
233
  rawAttrsForMatcher[attrName] = parsedVal;
230
234
  }
231
235
  }
@@ -240,7 +244,7 @@ function buildAttributesMap(attrStr, jPath, tagName) {
240
244
  const attrName = this.resolveNameSpace(matches[i][1]);
241
245
 
242
246
  // Convert jPath to string if needed for ignoreAttributesFn
243
- const jPathStr = this.options.jPath ? jPath.toString() : jPath;
247
+ const jPathStr = this.options.jPath ? jPath.toString() : this.readonlyMatcher;
244
248
  if (this.ignoreAttributesFn(attrName, jPathStr)) {
245
249
  continue
246
250
  }
@@ -259,10 +263,10 @@ function buildAttributesMap(attrStr, jPath, tagName) {
259
263
  if (this.options.trimValues) {
260
264
  oldVal = oldVal.trim();
261
265
  }
262
- oldVal = this.replaceEntitiesValue(oldVal, tagName, jPath);
266
+ oldVal = this.replaceEntitiesValue(oldVal, tagName, this.readonlyMatcher);
263
267
 
264
- // Pass jPath string or matcher based on options.jPath setting
265
- const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
268
+ // Pass jPath string or readonlyMatcher based on options.jPath setting
269
+ const jPathOrMatcher = this.options.jPath ? jPath.toString() : this.readonlyMatcher;
266
270
  const newVal = this.options.attributeValueProcessor(attrName, oldVal, jPathOrMatcher);
267
271
  if (newVal === null || newVal === undefined) {
268
272
  //don't parse
@@ -329,7 +333,7 @@ const parseXml = function (xmlData) {
329
333
  tagName = transformTagName(this.options.transformTagName, tagName, "", this.options).tagName;
330
334
 
331
335
  if (currentNode) {
332
- textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
336
+ textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
333
337
  }
334
338
 
335
339
  //check if last tag of nested tag was unpaired tag
@@ -354,7 +358,7 @@ const parseXml = function (xmlData) {
354
358
  let tagData = readTagExp(xmlData, i, false, "?>");
355
359
  if (!tagData) throw new Error("Pi Tag is not closed.");
356
360
 
357
- textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
361
+ textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
358
362
  if ((this.options.ignoreDeclaration && tagData.tagName === "?xml") || this.options.ignorePiTags) {
359
363
  //do nothing
360
364
  } else {
@@ -365,7 +369,7 @@ const parseXml = function (xmlData) {
365
369
  if (tagData.tagName !== tagData.tagExp && tagData.attrExpPresent) {
366
370
  childNode[":@"] = this.buildAttributesMap(tagData.tagExp, this.matcher, tagData.tagName);
367
371
  }
368
- this.addChild(currentNode, childNode, this.matcher, i);
372
+ this.addChild(currentNode, childNode, this.readonlyMatcher, i);
369
373
  }
370
374
 
371
375
 
@@ -375,7 +379,7 @@ const parseXml = function (xmlData) {
375
379
  if (this.options.commentPropName) {
376
380
  const comment = xmlData.substring(i + 4, endIndex - 2);
377
381
 
378
- textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
382
+ textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
379
383
 
380
384
  currentNode.add(this.options.commentPropName, [{ [this.options.textNodeName]: comment }]);
381
385
  }
@@ -388,9 +392,9 @@ const parseXml = function (xmlData) {
388
392
  const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
389
393
  const tagExp = xmlData.substring(i + 9, closeIndex);
390
394
 
391
- textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
395
+ textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
392
396
 
393
- let val = this.parseTextData(tagExp, currentNode.tagname, this.matcher, true, false, true, true);
397
+ let val = this.parseTextData(tagExp, currentNode.tagname, this.readonlyMatcher, true, false, true, true);
394
398
  if (val == undefined) val = "";
395
399
 
396
400
  //cdata should be set even if it is 0 length string
@@ -422,6 +426,8 @@ const parseXml = function (xmlData) {
422
426
  if (this.options.strictReservedNames &&
423
427
  (tagName === this.options.commentPropName
424
428
  || tagName === this.options.cdataPropName
429
+ || tagName === this.options.textNodeName
430
+ || tagName === this.options.attributesGroupName
425
431
  )) {
426
432
  throw new Error(`Invalid tag name: ${tagName}`);
427
433
  }
@@ -430,7 +436,7 @@ const parseXml = function (xmlData) {
430
436
  if (currentNode && textData) {
431
437
  if (currentNode.tagname !== '!xml') {
432
438
  //when nested tag is found
433
- textData = this.saveTextToParentTag(textData, currentNode, this.matcher, false);
439
+ textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher, false);
434
440
  }
435
441
  }
436
442
 
@@ -520,7 +526,7 @@ const parseXml = function (xmlData) {
520
526
  this.matcher.pop(); // Pop the stop node tag
521
527
  this.isCurrentNodeStopNode = false; // Reset flag
522
528
 
523
- this.addChild(currentNode, childNode, this.matcher, startIndex);
529
+ this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
524
530
  } else {
525
531
  //selfClosing tag
526
532
  if (isSelfClosing) {
@@ -530,7 +536,7 @@ const parseXml = function (xmlData) {
530
536
  if (prefixedAttrs) {
531
537
  childNode[":@"] = prefixedAttrs;
532
538
  }
533
- this.addChild(currentNode, childNode, this.matcher, startIndex);
539
+ this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
534
540
  this.matcher.pop(); // Pop self-closing tag
535
541
  this.isCurrentNodeStopNode = false; // Reset flag
536
542
  }
@@ -539,7 +545,7 @@ const parseXml = function (xmlData) {
539
545
  if (prefixedAttrs) {
540
546
  childNode[":@"] = prefixedAttrs;
541
547
  }
542
- this.addChild(currentNode, childNode, this.matcher, startIndex);
548
+ this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
543
549
  this.matcher.pop(); // Pop unpaired tag
544
550
  this.isCurrentNodeStopNode = false; // Reset flag
545
551
  i = result.closeIndex;
@@ -557,7 +563,7 @@ const parseXml = function (xmlData) {
557
563
  if (prefixedAttrs) {
558
564
  childNode[":@"] = prefixedAttrs;
559
565
  }
560
- this.addChild(currentNode, childNode, this.matcher, startIndex);
566
+ this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
561
567
  currentNode = childNode;
562
568
  }
563
569
  textData = "";
@@ -35,7 +35,7 @@ export default class XMLParser {
35
35
  orderedObjParser.addExternalEntities(this.externalEntities);
36
36
  const orderedResult = orderedObjParser.parseXml(xmlData);
37
37
  if (this.options.preserveOrder || orderedResult === undefined) return orderedResult;
38
- else return prettify(orderedResult, this.options, orderedObjParser.matcher);
38
+ else return prettify(orderedResult, this.options, orderedObjParser.matcher, orderedObjParser.readonlyMatcher);
39
39
  }
40
40
 
41
41
  /**
@@ -68,4 +68,4 @@ export default class XMLParser {
68
68
  static getMetaDataSymbol() {
69
69
  return XmlNode.getMetaDataSymbol();
70
70
  }
71
- }
71
+ }
@@ -35,18 +35,17 @@ function stripAttributePrefix(attrs, prefix) {
35
35
  * @param {Matcher} matcher - Path matcher instance
36
36
  * @returns
37
37
  */
38
- export default function prettify(node, options, matcher) {
39
- return compress(node, options, matcher);
38
+ export default function prettify(node, options, matcher, readonlyMatcher) {
39
+ return compress(node, options, matcher, readonlyMatcher);
40
40
  }
41
41
 
42
42
  /**
43
- *
44
43
  * @param {array} arr
45
44
  * @param {object} options
46
45
  * @param {Matcher} matcher - Path matcher instance
47
46
  * @returns object
48
47
  */
49
- function compress(arr, options, matcher) {
48
+ function compress(arr, options, matcher, readonlyMatcher) {
50
49
  let text;
51
50
  const compressedObj = {}; //This is intended to be a plain object
52
51
  for (let i = 0; i < arr.length; i++) {
@@ -69,11 +68,11 @@ function compress(arr, options, matcher) {
69
68
  continue;
70
69
  } else if (tagObj[property]) {
71
70
 
72
- let val = compress(tagObj[property], options, matcher);
71
+ let val = compress(tagObj[property], options, matcher, readonlyMatcher);
73
72
  const isLeaf = isLeafTag(val, options);
74
73
 
75
74
  if (tagObj[":@"]) {
76
- assignAttributes(val, tagObj[":@"], matcher, options);
75
+ assignAttributes(val, tagObj[":@"], readonlyMatcher, options);
77
76
  } else if (Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode) {
78
77
  val = val[options.textNodeName];
79
78
  } else if (Object.keys(val).length === 0) {
@@ -95,8 +94,8 @@ function compress(arr, options, matcher) {
95
94
  //TODO: if a node is not an array, then check if it should be an array
96
95
  //also determine if it is a leaf node
97
96
 
98
- // Pass jPath string or matcher based on options.jPath setting
99
- const jPathOrMatcher = options.jPath ? matcher.toString() : matcher;
97
+ // Pass jPath string or readonlyMatcher based on options.jPath setting
98
+ const jPathOrMatcher = options.jPath ? readonlyMatcher.toString() : readonlyMatcher;
100
99
  if (options.isArray(property, jPathOrMatcher, isLeaf)) {
101
100
  compressedObj[property] = [val];
102
101
  } else {
@@ -128,7 +127,7 @@ function propName(obj) {
128
127
  }
129
128
  }
130
129
 
131
- function assignAttributes(obj, attrMap, matcher, options) {
130
+ function assignAttributes(obj, attrMap, readonlyMatcher, options) {
132
131
  if (attrMap) {
133
132
  const keys = Object.keys(attrMap);
134
133
  const len = keys.length; //don't make it inline
@@ -143,8 +142,8 @@ function assignAttributes(obj, attrMap, matcher, options) {
143
142
  // For attributes, we need to create a temporary path
144
143
  // Pass jPath string or matcher based on options.jPath setting
145
144
  const jPathOrMatcher = options.jPath
146
- ? matcher.toString() + "." + rawAttrName
147
- : matcher;
145
+ ? readonlyMatcher.toString() + "." + rawAttrName
146
+ : readonlyMatcher;
148
147
 
149
148
  if (options.isArray(atrrName, jPathOrMatcher, true, true)) {
150
149
  obj[atrrName] = [attrMap[atrrName]];