• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NaturalIntelligence / fast-xml-parser / 23932811504

03 Apr 2026 03:45AM UTC coverage: 97.689% (+0.008%) from 97.681%
23932811504

push

github

amitguptagwl
update path-expression-matcher for performance

1184 of 1233 branches covered (96.03%)

36 of 36 new or added lines in 1 file covered. (100.0%)

20 existing lines in 1 file now uncovered.

9385 of 9607 relevant lines covered (97.69%)

464507.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/src/xmlparser/OrderedObjParser.js
1
'use strict';
5✔
2
///@ts-check
5✔
3

5✔
4
import { getAllMatches, isExist, DANGEROUS_PROPERTY_NAMES, criticalProperties } from '../util.js';
5✔
5
import xmlNode from './xmlNode.js';
5✔
6
import DocTypeReader from './DocTypeReader.js';
5✔
7
import toNumber from "strnum";
5✔
8
import getIgnoreAttributesFn from "../ignoreAttributes.js";
5✔
9
import { Expression, Matcher } from 'path-expression-matcher';
5✔
10

5✔
11
// const regx =
5✔
12
//   '<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
5✔
13
//   .replace(/NAME/g, util.nameRegexp);
5✔
14

5✔
15
//const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
5✔
16
//const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
5✔
17

5✔
18
// Helper functions for attribute and namespace handling
5✔
19

5✔
20
/**
5✔
21
 * Extract raw attributes (without prefix) from prefixed attribute map
5✔
22
 * @param {object} prefixedAttrs - Attributes with prefix from buildAttributesMap
5✔
23
 * @param {object} options - Parser options containing attributeNamePrefix
5✔
24
 * @returns {object} Raw attributes for matcher
5✔
25
 */
5✔
26
function extractRawAttributes(prefixedAttrs, options) {
760✔
27
  if (!prefixedAttrs) return {};
760!
28

760✔
29
  // Handle attributesGroupName option
760✔
30
  const attrs = options.attributesGroupName
760✔
31
    ? prefixedAttrs[options.attributesGroupName]
760✔
32
    : prefixedAttrs;
760✔
33

760✔
34
  if (!attrs) return {};
760!
35

760✔
36
  const rawAttrs = {};
760✔
37
  for (const key in attrs) {
760✔
38
    // Remove the attribute prefix to get raw name
1,390✔
39
    if (key.startsWith(options.attributeNamePrefix)) {
1,390✔
40
      const rawName = key.substring(options.attributeNamePrefix.length);
1,194✔
41
      rawAttrs[rawName] = attrs[key];
1,194✔
42
    } else {
1,390✔
43
      // Attribute without prefix (shouldn't normally happen, but be safe)
196✔
44
      rawAttrs[key] = attrs[key];
196✔
45
    }
196✔
46
  }
1,390✔
47
  return rawAttrs;
760✔
48
}
760✔
49

5✔
50
/**
5✔
51
 * Extract namespace from raw tag name
5✔
52
 * @param {string} rawTagName - Tag name possibly with namespace (e.g., "soap:Envelope")
5✔
53
 * @returns {string|undefined} Namespace or undefined
5✔
54
 */
5✔
55
function extractNamespace(rawTagName) {
6,380✔
56
  if (!rawTagName || typeof rawTagName !== 'string') return undefined;
6,380!
57

6,380✔
58
  const colonIndex = rawTagName.indexOf(':');
6,380✔
59
  if (colonIndex !== -1 && colonIndex > 0) {
6,380✔
60
    const ns = rawTagName.substring(0, colonIndex);
150✔
61
    // Don't treat xmlns as a namespace
150✔
62
    if (ns !== 'xmlns') {
150✔
63
      return ns;
150✔
64
    }
150✔
65
  }
150✔
66
  return undefined;
6,230✔
67
}
6,380✔
68

5✔
69
export default class OrderedObjParser {
5✔
70
  constructor(options) {
5✔
71
    this.options = options;
1,690✔
72
    this.currentNode = null;
1,690✔
73
    this.tagsNodeStack = [];
1,690✔
74
    this.docTypeEntities = {};
1,690✔
75
    this.lastEntities = {
1,690✔
76
      "apos": { regex: /&(apos|#39|#x27);/g, val: "'" },
1,690✔
77
      "gt": { regex: /&(gt|#62|#x3E);/g, val: ">" },
1,690✔
78
      "lt": { regex: /&(lt|#60|#x3C);/g, val: "<" },
1,690✔
79
      "quot": { regex: /&(quot|#34|#x22);/g, val: "\"" },
1,690✔
80
    };
1,690✔
81
    this.ampEntity = { regex: /&(amp|#38|#x26);/g, val: "&" };
1,690✔
82
    this.htmlEntities = {
1,690✔
83
      "space": { regex: /&(nbsp|#160);/g, val: " " },
1,690✔
84
      // "lt" : { regex: /&(lt|#60);/g, val: "<" },
1,690✔
85
      // "gt" : { regex: /&(gt|#62);/g, val: ">" },
1,690✔
86
      // "amp" : { regex: /&(amp|#38);/g, val: "&" },
1,690✔
87
      // "quot" : { regex: /&(quot|#34);/g, val: "\"" },
1,690✔
88
      // "apos" : { regex: /&(apos|#39);/g, val: "'" },
1,690✔
89
      "cent": { regex: /&(cent|#162);/g, val: "¢" },
1,690✔
90
      "pound": { regex: /&(pound|#163);/g, val: "£" },
1,690✔
91
      "yen": { regex: /&(yen|#165);/g, val: "Â¥" },
1,690✔
92
      "euro": { regex: /&(euro|#8364);/g, val: "€" },
1,690✔
93
      "copyright": { regex: /&(copy|#169);/g, val: "©" },
1,690✔
94
      "reg": { regex: /&(reg|#174);/g, val: "®" },
1,690✔
95
      "inr": { regex: /&(inr|#8377);/g, val: "₹" },
1,690✔
96
      "num_dec": { regex: /&#([0-9]{1,7});/g, val: (_, str) => fromCodePoint(str, 10, "&#") },
1,690✔
97
      "num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val: (_, str) => fromCodePoint(str, 16, "&#x") },
1,690✔
98
    };
1,690✔
99
    this.addExternalEntities = addExternalEntities;
1,690✔
100
    this.parseXml = parseXml;
1,690✔
101
    this.parseTextData = parseTextData;
1,690✔
102
    this.resolveNameSpace = resolveNameSpace;
1,690✔
103
    this.buildAttributesMap = buildAttributesMap;
1,690✔
104
    this.isItStopNode = isItStopNode;
1,690✔
105
    this.replaceEntitiesValue = replaceEntitiesValue;
1,690✔
106
    this.readStopNodeData = readStopNodeData;
1,690✔
107
    this.saveTextToParentTag = saveTextToParentTag;
1,690✔
108
    this.addChild = addChild;
1,690✔
109
    this.ignoreAttributesFn = getIgnoreAttributesFn(this.options.ignoreAttributes)
1,690✔
110
    this.entityExpansionCount = 0;
1,690✔
111
    this.currentExpandedLength = 0;
1,690✔
112

1,690✔
113
    // Initialize path matcher for path-expression-matcher
1,690✔
114
    this.matcher = new Matcher();
1,690✔
115

1,690✔
116
    // Live read-only proxy of matcher — PEM creates and caches this internally.
1,690✔
117
    // All user callbacks receive this instead of the mutable matcher.
1,690✔
118
    this.readonlyMatcher = this.matcher.readOnly();
1,690✔
119

1,690✔
120
    // Flag to track if current node is a stop node (optimization)
1,690✔
121
    this.isCurrentNodeStopNode = false;
1,690✔
122

1,690✔
123
    // Pre-compile stopNodes expressions
1,690✔
124
    if (this.options.stopNodes && this.options.stopNodes.length > 0) {
1,690✔
125
      this.stopNodeExpressions = [];
670✔
126
      for (let i = 0; i < this.options.stopNodes.length; i++) {
670✔
127
        const stopNodeExp = this.options.stopNodes[i];
1,255✔
128
        if (typeof stopNodeExp === 'string') {
1,255✔
129
          // Convert string to Expression object
200✔
130
          this.stopNodeExpressions.push(new Expression(stopNodeExp));
200✔
131
        } else if (stopNodeExp instanceof Expression) {
1,255✔
132
          // Already an Expression object
1,055✔
133
          this.stopNodeExpressions.push(stopNodeExp);
1,055✔
134
        }
1,055✔
135
      }
1,255✔
136
    }
670✔
137
  }
1,690✔
138

5✔
139
}
5✔
140

5✔
141
function addExternalEntities(externalEntities) {
1,690✔
142
  const entKeys = Object.keys(externalEntities);
1,690✔
143
  for (let i = 0; i < entKeys.length; i++) {
1,690✔
144
    const ent = entKeys[i];
15✔
145
    const escaped = ent.replace(/[.\-+*:]/g, '\\.');
15✔
146
    this.lastEntities[ent] = {
15✔
147
      regex: new RegExp("&" + escaped + ";", "g"),
15✔
148
      val: externalEntities[ent]
15✔
149
    }
15✔
150
  }
15✔
151
}
1,690✔
152

5✔
153
/**
5✔
154
 * @param {string} val
5✔
155
 * @param {string} tagName
5✔
156
 * @param {string|Matcher} jPath - jPath string or Matcher instance based on options.jPath
5✔
157
 * @param {boolean} dontTrim
5✔
158
 * @param {boolean} hasAttributes
5✔
159
 * @param {boolean} isLeafNode
5✔
160
 * @param {boolean} escapeEntities
5✔
161
 */
5✔
162
function parseTextData(val, tagName, jPath, dontTrim, hasAttributes, isLeafNode, escapeEntities) {
6,695✔
163
  if (val !== undefined) {
6,695✔
164
    if (this.options.trimValues && !dontTrim) {
6,695✔
165
      val = val.trim();
6,450✔
166
    }
6,450✔
167
    if (val.length > 0) {
6,695✔
168
      if (!escapeEntities) val = this.replaceEntitiesValue(val, tagName, jPath);
2,010✔
169

1,975✔
170
      // Pass jPath string or matcher based on options.jPath setting
1,975✔
171
      const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
2,010✔
172
      const newval = this.options.tagValueProcessor(tagName, val, jPathOrMatcher, hasAttributes, isLeafNode);
2,010✔
173
      if (newval === null || newval === undefined) {
2,010✔
174
        //don't parse
50✔
175
        return val;
50✔
176
      } else if (typeof newval !== typeof val || newval !== val) {
2,010✔
177
        //overwrite
20✔
178
        return newval;
20✔
179
      } else if (this.options.trimValues) {
1,925✔
180
        return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
1,845✔
181
      } else {
1,905✔
182
        const trimmedVal = val.trim();
60✔
183
        if (trimmedVal === val) {
60✔
184
          return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
15✔
185
        } else {
60✔
186
          return val;
45✔
187
        }
45✔
188
      }
60✔
189
    }
2,010✔
190
  }
6,695✔
191
}
6,695✔
192

5✔
193
function resolveNameSpace(tagname) {
2,880✔
194
  if (this.options.removeNSPrefix) {
2,880✔
195
    const tags = tagname.split(':');
220✔
196
    const prefix = tagname.charAt(0) === '/' ? '/' : '';
220!
197
    if (tags[0] === 'xmlns') {
220✔
198
      return '';
70✔
199
    }
70✔
200
    if (tags.length === 2) {
220✔
201
      tagname = prefix + tags[1];
60✔
202
    }
60✔
203
  }
220✔
204
  return tagname;
2,810✔
205
}
2,880✔
206

5✔
207
//TODO: change regex to capture NS
5✔
208
//const attrsRegx = new RegExp("([\\w\\-\\.\\:]+)\\s*=\\s*(['\"])((.|\n)*?)\\2","gm");
5✔
209
const attrsRegx = new RegExp('([^\\s=]+)\\s*(=\\s*([\'"])([\\s\\S]*?)\\3)?', 'gm');
5✔
210

5✔
211
function buildAttributesMap(attrStr, jPath, tagName) {
1,180✔
212
  if (this.options.ignoreAttributes !== true && typeof attrStr === 'string') {
1,180✔
213
    // attrStr = attrStr.replace(/\r?\n/g, ' ');
990✔
214
    //attrStr = attrStr || attrStr.trim();
990✔
215

990✔
216
    const matches = getAllMatches(attrStr, attrsRegx);
990✔
217
    const len = matches.length; //don't make it inline
990✔
218
    const attrs = {};
990✔
219

990✔
220
    // Pre-process values once: trim + entity replacement
990✔
221
    // Reused in both matcher update and second pass
990✔
222
    const processedVals = new Array(len);
990✔
223
    let hasRawAttrs = false;
990✔
224
    const rawAttrsForMatcher = {};
990✔
225

990✔
226
    for (let i = 0; i < len; i++) {
990✔
227
      const attrName = this.resolveNameSpace(matches[i][1]);
1,440✔
228
      const oldVal = matches[i][4];
1,440✔
229

1,440✔
230
      if (attrName.length && oldVal !== undefined) {
1,440✔
231
        let val = oldVal;
1,315✔
232
        if (this.options.trimValues) val = val.trim();
1,315✔
233
        val = this.replaceEntitiesValue(val, tagName, this.readonlyMatcher);
1,315✔
234
        processedVals[i] = val;
1,315✔
235

1,315✔
236
        rawAttrsForMatcher[attrName] = val;
1,315✔
237
        hasRawAttrs = true;
1,315✔
238
      }
1,315✔
239
    }
1,440✔
240

990✔
241
    // Update matcher ONCE before second pass, if applicable
990✔
242
    if (hasRawAttrs && typeof jPath === 'object' && jPath.updateCurrent) {
990✔
243
      jPath.updateCurrent(rawAttrsForMatcher);
870✔
244
    }
870✔
245

990✔
246
    // Hoist toString() once — path doesn't change during attribute processing
990✔
247
    const jPathStr = this.options.jPath ? jPath.toString() : this.readonlyMatcher;
990✔
248

990✔
249
    // Second pass: apply processors, build final attrs
990✔
250
    let hasAttrs = false;
990✔
251
    for (let i = 0; i < len; i++) {
990✔
252
      const attrName = this.resolveNameSpace(matches[i][1]);
1,440✔
253

1,440✔
254
      if (this.ignoreAttributesFn(attrName, jPathStr)) continue;
1,440✔
255

1,385✔
256
      let aName = this.options.attributeNamePrefix + attrName;
1,385✔
257

1,385✔
258
      if (attrName.length) {
1,440✔
259
        if (this.options.transformAttributeName) {
1,350✔
260
          aName = this.options.transformAttributeName(aName);
20✔
261
        }
20✔
262
        aName = sanitizeName(aName, this.options);
1,350✔
263

1,350✔
264
        if (matches[i][4] !== undefined) {
1,350✔
265
          // Reuse already-processed value — no double entity replacement
1,245✔
266
          const oldVal = processedVals[i];
1,245✔
267

1,245✔
268
          const newVal = this.options.attributeValueProcessor(attrName, oldVal, jPathStr);
1,245✔
269
          if (newVal === null || newVal === undefined) {
1,245!
UNCOV
270
            attrs[aName] = oldVal;
×
271
          } else if (typeof newVal !== typeof oldVal || newVal !== oldVal) {
1,245✔
272
            attrs[aName] = newVal;
5✔
273
          } else {
1,245✔
274
            attrs[aName] = parseValue(oldVal, this.options.parseAttributeValue, this.options.numberParseOptions);
1,240✔
275
          }
1,240✔
276
          hasAttrs = true;
1,245✔
277
        } else if (this.options.allowBooleanAttributes) {
1,350✔
278
          attrs[aName] = true;
80✔
279
          hasAttrs = true;
80✔
280
        }
80✔
281
      }
1,350✔
282
    }
1,440✔
283

975✔
284
    if (!hasAttrs) return;
990✔
285

880✔
286
    if (this.options.attributesGroupName) {
990✔
287
      const attrCollection = {};
60✔
288
      attrCollection[this.options.attributesGroupName] = attrs;
60✔
289
      return attrCollection;
60✔
290
    }
60✔
291
    return attrs;
820✔
292
  }
820✔
293
}
1,180✔
294
const parseXml = function (xmlData) {
5✔
295
  xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
1,690✔
296
  const xmlObj = new xmlNode('!xml');
1,690✔
297
  let currentNode = xmlObj;
1,690✔
298
  let textData = "";
1,690✔
299

1,690✔
300
  // Reset matcher for new document
1,690✔
301
  this.matcher.reset();
1,690✔
302

1,690✔
303
  // Reset entity expansion counters for this document
1,690✔
304
  this.entityExpansionCount = 0;
1,690✔
305
  this.currentExpandedLength = 0;
1,690✔
306

1,690✔
307
  const docTypeReader = new DocTypeReader(this.options.processEntities);
1,690✔
308
  for (let i = 0; i < xmlData.length; i++) {//for each char in XML data
1,690✔
309
    const ch = xmlData[i];
325,615✔
310
    if (ch === '<') {
325,615✔
311
      // const nextIndex = i+1;
11,230✔
312
      // const _2ndChar = xmlData[nextIndex];
11,230✔
313
      if (xmlData[i + 1] === '/') {//Closing Tag
11,230✔
314
        const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.")
4,080✔
315
        let tagName = xmlData.substring(i + 2, closeIndex).trim();
4,080✔
316

4,080✔
317
        if (this.options.removeNSPrefix) {
4,080✔
318
          const colonIndex = tagName.indexOf(":");
115✔
319
          if (colonIndex !== -1) {
115✔
320
            tagName = tagName.substr(colonIndex + 1);
50✔
321
          }
50✔
322
        }
115✔
323

4,075✔
324
        tagName = transformTagName(this.options.transformTagName, tagName, "", this.options).tagName;
4,075✔
325

4,075✔
326
        if (currentNode) {
4,075✔
327
          textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
4,075✔
328
        }
4,075✔
329

4,040✔
330
        //check if last tag of nested tag was unpaired tag
4,040✔
331
        const lastTagName = this.matcher.getCurrentTag();
4,040✔
332
        if (tagName && this.options.unpairedTags.indexOf(tagName) !== -1) {
4,080✔
333
          throw new Error(`Unpaired tag can not be used as closing tag: </${tagName}>`);
5✔
334
        }
5✔
335
        if (lastTagName && this.options.unpairedTags.indexOf(lastTagName) !== -1) {
4,080!
336
          // Pop the unpaired tag
×
UNCOV
337
          this.matcher.pop();
×
UNCOV
338
          this.tagsNodeStack.pop();
×
UNCOV
339
        }
×
340
        // Pop the closing tag
4,035✔
341
        this.matcher.pop();
4,035✔
342
        this.isCurrentNodeStopNode = false; // Reset flag when closing tag
4,035✔
343

4,035✔
344
        currentNode = this.tagsNodeStack.pop();//avoid recursion, set the parent tag scope
4,035✔
345
        textData = "";
4,035✔
346
        i = closeIndex;
4,035✔
347
      } else if (xmlData[i + 1] === '?') {
11,230✔
348

245✔
349
        let tagData = readTagExp(xmlData, i, false, "?>");
245✔
350
        if (!tagData) throw new Error("Pi Tag is not closed.");
245✔
351

235✔
352
        textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
235✔
353
        if ((this.options.ignoreDeclaration && tagData.tagName === "?xml") || this.options.ignorePiTags) {
245✔
354
          //do nothing
25✔
355
        } else {
245✔
356

210✔
357
          const childNode = new xmlNode(tagData.tagName);
210✔
358
          childNode.add(this.options.textNodeName, "");
210✔
359

210✔
360
          if (tagData.tagName !== tagData.tagExp && tagData.attrExpPresent) {
210✔
361
            childNode[":@"] = this.buildAttributesMap(tagData.tagExp, this.matcher, tagData.tagName);
200✔
362
          }
200✔
363
          this.addChild(currentNode, childNode, this.readonlyMatcher, i);
210✔
364
        }
210✔
365

235✔
366

235✔
367
        i = tagData.closeIndex + 1;
235✔
368
      } else if (xmlData.substr(i + 1, 3) === '!--') {
7,150✔
369
        const endIndex = findClosingIndex(xmlData, "-->", i + 4, "Comment is not closed.")
60✔
370
        if (this.options.commentPropName) {
60✔
371
          const comment = xmlData.substring(i + 4, endIndex - 2);
25✔
372

25✔
373
          textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
25✔
374

25✔
375
          currentNode.add(this.options.commentPropName, [{ [this.options.textNodeName]: comment }]);
25✔
376
        }
25✔
377
        i = endIndex;
55✔
378
      } else if (xmlData.substr(i + 1, 2) === '!D') {
6,905✔
379
        const result = docTypeReader.readDocType(xmlData, i);
220✔
380
        this.docTypeEntities = result.entities;
220✔
381
        i = result.i;
220✔
382
      } else if (xmlData.substr(i + 1, 2) === '![') {
6,845✔
383
        const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
190✔
384
        const tagExp = xmlData.substring(i + 9, closeIndex);
190✔
385

190✔
386
        textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
190✔
387

190✔
388
        let val = this.parseTextData(tagExp, currentNode.tagname, this.readonlyMatcher, true, false, true, true);
190✔
389
        if (val == undefined) val = "";
190✔
390

185✔
391
        //cdata should be set even if it is 0 length string
185✔
392
        if (this.options.cdataPropName) {
190✔
393
          currentNode.add(this.options.cdataPropName, [{ [this.options.textNodeName]: tagExp }]);
35✔
394
        } else {
190✔
395
          currentNode.add(this.options.textNodeName, val);
150✔
396
        }
150✔
397

185✔
398
        i = closeIndex + 2;
185✔
399
      } else {//Opening tag
6,625✔
400
        let result = readTagExp(xmlData, i, this.options.removeNSPrefix);
6,435✔
401

6,435✔
402
        // Safety check: readTagExp can return undefined
6,435✔
403
        if (!result) {
6,435!
404
          // Log context for debugging
×
UNCOV
405
          const context = xmlData.substring(Math.max(0, i - 50), Math.min(xmlData.length, i + 50));
×
UNCOV
406
          throw new Error(`readTagExp returned undefined at position ${i}. Context: "${context}"`);
×
UNCOV
407
        }
×
408

6,435✔
409
        let tagName = result.tagName;
6,435✔
410
        const rawTagName = result.rawTagName;
6,435✔
411
        let tagExp = result.tagExp;
6,435✔
412
        let attrExpPresent = result.attrExpPresent;
6,435✔
413
        let closeIndex = result.closeIndex;
6,435✔
414

6,435✔
415
        ({ tagName, tagExp } = transformTagName(this.options.transformTagName, tagName, tagExp, this.options));
6,435✔
416

6,435✔
417
        if (this.options.strictReservedNames &&
6,435✔
418
          (tagName === this.options.commentPropName
6,375✔
419
            || tagName === this.options.cdataPropName
6,375✔
420
            || tagName === this.options.textNodeName
6,375✔
421
            || tagName === this.options.attributesGroupName
6,375✔
422
          )) {
6,435✔
423
          throw new Error(`Invalid tag name: ${tagName}`);
5✔
424
        }
5✔
425

6,380✔
426
        //save text as child node
6,380✔
427
        if (currentNode && textData) {
6,435✔
428
          if (currentNode.tagname !== '!xml') {
4,105✔
429
            //when nested tag is found
3,225✔
430
            textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher, false);
3,225✔
431
          }
3,225✔
432
        }
4,105✔
433

6,380✔
434
        //check if last tag was unpaired tag
6,380✔
435
        const lastTag = currentNode;
6,380✔
436
        if (lastTag && this.options.unpairedTags.indexOf(lastTag.tagname) !== -1) {
6,435!
UNCOV
437
          currentNode = this.tagsNodeStack.pop();
×
UNCOV
438
          this.matcher.pop();
×
UNCOV
439
        }
×
440

6,380✔
441
        // Clean up self-closing syntax BEFORE processing attributes
6,380✔
442
        // This is where tagExp gets the trailing / removed
6,380✔
443
        let isSelfClosing = false;
6,380✔
444
        if (tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1) {
6,435✔
445
          isSelfClosing = true;
280✔
446
          if (tagName[tagName.length - 1] === "/") {
280✔
447
            tagName = tagName.substr(0, tagName.length - 1);
100✔
448
            tagExp = tagName;
100✔
449
          } else {
280✔
450
            tagExp = tagExp.substr(0, tagExp.length - 1);
180✔
451
          }
180✔
452

280✔
453
          // Re-check attrExpPresent after cleaning
280✔
454
          attrExpPresent = (tagName !== tagExp);
280✔
455
        }
280✔
456

6,380✔
457
        // Now process attributes with CLEAN tagExp (no trailing /)
6,380✔
458
        let prefixedAttrs = null;
6,380✔
459
        let rawAttrs = {};
6,380✔
460
        let namespace = undefined;
6,380✔
461

6,380✔
462
        // Extract namespace from rawTagName
6,380✔
463
        namespace = extractNamespace(rawTagName);
6,380✔
464

6,380✔
465
        // Push tag to matcher FIRST (with empty attrs for now) so callbacks see correct path
6,380✔
466
        if (tagName !== xmlObj.tagname) {
6,380✔
467
          this.matcher.push(tagName, {}, namespace);
6,380✔
468
        }
6,380✔
469

6,380✔
470
        // Now build attributes - callbacks will see correct matcher state
6,380✔
471
        if (tagName !== tagExp && attrExpPresent) {
6,435✔
472
          // Build attributes (returns prefixed attributes for the tree)
980✔
473
          // Note: buildAttributesMap now internally updates the matcher with raw attributes
980✔
474
          prefixedAttrs = this.buildAttributesMap(tagExp, this.matcher, tagName);
980✔
475

980✔
476
          if (prefixedAttrs) {
980✔
477
            // Extract raw attributes (without prefix) for our use
760✔
478
            rawAttrs = extractRawAttributes(prefixedAttrs, this.options);
760✔
479
          }
760✔
480
        }
980✔
481

6,365✔
482
        // Now check if this is a stop node (after attributes are set)
6,365✔
483
        if (tagName !== xmlObj.tagname) {
6,365✔
484
          this.isCurrentNodeStopNode = this.isItStopNode(this.stopNodeExpressions, this.matcher);
6,365✔
485
        }
6,365✔
486

6,365✔
487
        const startIndex = i;
6,365✔
488
        if (this.isCurrentNodeStopNode) {
6,435✔
489
          let tagContent = "";
1,225✔
490

1,225✔
491
          // For self-closing tags, content is empty
1,225✔
492
          if (isSelfClosing) {
1,225✔
493
            i = result.closeIndex;
10✔
494
          }
10✔
495
          //unpaired tag
1,215✔
496
          else if (this.options.unpairedTags.indexOf(tagName) !== -1) {
1,215✔
497
            i = result.closeIndex;
5✔
498
          }
5✔
499
          //normal tag
1,210✔
500
          else {
1,210✔
501
            //read until closing tag is found
1,210✔
502
            const result = this.readStopNodeData(xmlData, rawTagName, closeIndex + 1);
1,210✔
503
            if (!result) throw new Error(`Unexpected end of ${rawTagName}`);
1,210!
504
            i = result.i;
1,210✔
505
            tagContent = result.tagContent;
1,210✔
506
          }
1,210✔
507

1,225✔
508
          const childNode = new xmlNode(tagName);
1,225✔
509

1,225✔
510
          if (prefixedAttrs) {
1,225✔
511
            childNode[":@"] = prefixedAttrs;
20✔
512
          }
20✔
513

1,225✔
514
          // For stop nodes, store raw content as-is without any processing
1,225✔
515
          childNode.add(this.options.textNodeName, tagContent);
1,225✔
516

1,225✔
517
          this.matcher.pop(); // Pop the stop node tag
1,225✔
518
          this.isCurrentNodeStopNode = false; // Reset flag
1,225✔
519

1,225✔
520
          this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
1,225✔
521
        } else {
6,435✔
522
          //selfClosing tag
5,140✔
523
          if (isSelfClosing) {
5,140✔
524
            ({ tagName, tagExp } = transformTagName(this.options.transformTagName, tagName, tagExp, this.options));
270✔
525

270✔
526
            const childNode = new xmlNode(tagName);
270✔
527
            if (prefixedAttrs) {
270✔
528
              childNode[":@"] = prefixedAttrs;
90✔
529
            }
90✔
530
            this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
270✔
531
            this.matcher.pop(); // Pop self-closing tag
270✔
532
            this.isCurrentNodeStopNode = false; // Reset flag
270✔
533
          }
270✔
534
          else if (this.options.unpairedTags.indexOf(tagName) !== -1) {//unpaired tag
4,870✔
535
            const childNode = new xmlNode(tagName);
210✔
536
            if (prefixedAttrs) {
210✔
537
              childNode[":@"] = prefixedAttrs;
90✔
538
            }
90✔
539
            this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
210✔
540
            this.matcher.pop(); // Pop unpaired tag
210✔
541
            this.isCurrentNodeStopNode = false; // Reset flag
210✔
542
            i = result.closeIndex;
210✔
543
            // Continue to next iteration without changing currentNode
210✔
544
            continue;
210✔
545
          }
210✔
546
          //opening tag
4,660✔
547
          else {
4,660✔
548
            const childNode = new xmlNode(tagName);
4,660✔
549
            if (this.tagsNodeStack.length > this.options.maxNestedTags) {
4,660✔
550
              throw new Error("Maximum nested tags exceeded");
5✔
551
            }
5✔
552
            this.tagsNodeStack.push(currentNode);
4,655✔
553

4,655✔
554
            if (prefixedAttrs) {
4,660✔
555
              childNode[":@"] = prefixedAttrs;
560✔
556
            }
560✔
557
            this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
4,655✔
558
            currentNode = childNode;
4,655✔
559
          }
4,655✔
560
          textData = "";
4,925✔
561
          i = closeIndex;
4,925✔
562
        }
4,925✔
563
      }
6,435✔
564
    } else {
325,615✔
565
      textData += xmlData[i];
314,385✔
566
    }
314,385✔
567
  }
325,615✔
568
  return xmlObj.child;
1,525✔
569
}
1,690✔
570

5✔
571
function addChild(currentNode, childNode, matcher, startIndex) {
6,570✔
572
  // unset startIndex if not requested
6,570✔
573
  if (!this.options.captureMetaData) startIndex = undefined;
6,570✔
574

6,570✔
575
  // Pass jPath string or matcher based on options.jPath setting
6,570✔
576
  const jPathOrMatcher = this.options.jPath ? matcher.toString() : matcher;
6,570✔
577
  const result = this.options.updateTag(childNode.tagname, jPathOrMatcher, childNode[":@"])
6,570✔
578
  if (result === false) {
6,570✔
579
    //do nothing
25✔
580
  } else if (typeof result === "string") {
6,570✔
581
    childNode.tagname = result
6,545✔
582
    currentNode.addChild(childNode, startIndex);
6,545✔
583
  } else {
6,545!
UNCOV
584
    currentNode.addChild(childNode, startIndex);
×
UNCOV
585
  }
×
586
}
6,570✔
587

5✔
588
/**
5✔
589
 * @param {object} val - Entity object with regex and val properties
5✔
590
 * @param {string} tagName - Tag name
5✔
591
 * @param {string|Matcher} jPath - jPath string or Matcher instance based on options.jPath
5✔
592
 */
5✔
593
function replaceEntitiesValue(val, tagName, jPath) {
3,160✔
594
  const entityConfig = this.options.processEntities;
3,160✔
595

3,160✔
596
  if (!entityConfig || !entityConfig.enabled) {
3,160✔
597
    return val;
60✔
598
  }
60✔
599

3,100✔
600
  // Check if tag is allowed to contain entities
3,100✔
601
  if (entityConfig.allowedTags) {
3,160✔
602
    const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
30!
603
    const allowed = Array.isArray(entityConfig.allowedTags)
30✔
604
      ? entityConfig.allowedTags.includes(tagName)
30✔
605
      : entityConfig.allowedTags(tagName, jPathOrMatcher);
30!
606

30✔
607
    if (!allowed) {
30✔
608
      return val;
10✔
609
    }
10✔
610
  }
30✔
611

3,090✔
612
  // Apply custom tag filter if provided
3,090✔
613
  if (entityConfig.tagFilter) {
3,160✔
614
    const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
25!
615
    if (!entityConfig.tagFilter(tagName, jPathOrMatcher)) {
25✔
616
      return val; // Skip based on custom filter
15✔
617
    }
15✔
618
  }
25✔
619

3,075✔
620
  // Replace DOCTYPE entities
3,075✔
621
  for (const entityName of Object.keys(this.docTypeEntities)) {
3,160✔
622
    const entity = this.docTypeEntities[entityName];
405✔
623
    const matches = val.match(entity.regx);
405✔
624

405✔
625
    if (matches) {
405✔
626
      // Track expansions
215✔
627
      this.entityExpansionCount += matches.length;
215✔
628

215✔
629
      // Check expansion limit
215✔
630
      if (entityConfig.maxTotalExpansions &&
215✔
631
        this.entityExpansionCount > entityConfig.maxTotalExpansions) {
215✔
632
        throw new Error(
15✔
633
          `Entity expansion limit exceeded: ${this.entityExpansionCount} > ${entityConfig.maxTotalExpansions}`
15✔
634
        );
15✔
635
      }
15✔
636

200✔
637
      // Store length before replacement
200✔
638
      const lengthBefore = val.length;
200✔
639
      val = val.replace(entity.regx, entity.val);
200✔
640

200✔
641
      // Check expanded length immediately after replacement
200✔
642
      if (entityConfig.maxExpandedLength) {
200✔
643
        this.currentExpandedLength += (val.length - lengthBefore);
200✔
644

200✔
645
        if (this.currentExpandedLength > entityConfig.maxExpandedLength) {
200✔
646
          throw new Error(
15✔
647
            `Total expanded content size exceeded: ${this.currentExpandedLength} > ${entityConfig.maxExpandedLength}`
15✔
648
          );
15✔
649
        }
15✔
650
      }
200✔
651
    }
215✔
652
  }
405✔
653
  if (val.indexOf('&') === -1) return val;
3,160✔
654
  // Replace standard entities
125✔
655
  for (const entityName of Object.keys(this.lastEntities)) {
3,160✔
656
    const entity = this.lastEntities[entityName];
505✔
657
    const matches = val.match(entity.regex);
505✔
658
    if (matches) {
505✔
659
      this.entityExpansionCount += matches.length;
85✔
660
      if (entityConfig.maxTotalExpansions &&
85✔
661
        this.entityExpansionCount > entityConfig.maxTotalExpansions) {
85!
UNCOV
662
        throw new Error(
×
UNCOV
663
          `Entity expansion limit exceeded: ${this.entityExpansionCount} > ${entityConfig.maxTotalExpansions}`
×
UNCOV
664
        );
×
UNCOV
665
      }
×
666
    }
85✔
667
    val = val.replace(entity.regex, entity.val);
505✔
668
  }
505✔
669
  if (val.indexOf('&') === -1) return val;
3,160✔
670

95✔
671
  // Replace HTML entities if enabled
95✔
672
  if (this.options.htmlEntities) {
3,160✔
673
    for (const entityName of Object.keys(this.htmlEntities)) {
45✔
674
      const entity = this.htmlEntities[entityName];
445✔
675
      const matches = val.match(entity.regex);
445✔
676
      if (matches) {
445✔
677
        //console.log(matches);
50✔
678
        this.entityExpansionCount += matches.length;
50✔
679
        if (entityConfig.maxTotalExpansions &&
50✔
680
          this.entityExpansionCount > entityConfig.maxTotalExpansions) {
50✔
681
          throw new Error(
5✔
682
            `Entity expansion limit exceeded: ${this.entityExpansionCount} > ${entityConfig.maxTotalExpansions}`
5✔
683
          );
5✔
684
        }
5✔
685
      }
50✔
686
      val = val.replace(entity.regex, entity.val);
440✔
687
    }
440✔
688
  }
40✔
689

90✔
690
  // Replace ampersand entity last
90✔
691
  val = val.replace(this.ampEntity.regex, this.ampEntity.val);
90✔
692

90✔
693
  return val;
90✔
694
}
3,160✔
695

5✔
696

5✔
697
function saveTextToParentTag(textData, parentNode, matcher, isLeafNode) {
7,745✔
698
  if (textData) { //store previously collected data as textNode
7,745✔
699
    if (isLeafNode === undefined) isLeafNode = parentNode.child.length === 0
6,510✔
700

6,510✔
701
    textData = this.parseTextData(textData,
6,510✔
702
      parentNode.tagname,
6,510✔
703
      matcher,
6,510✔
704
      false,
6,510✔
705
      parentNode[":@"] ? Object.keys(parentNode[":@"]).length !== 0 : false,
6,510!
706
      isLeafNode);
6,510✔
707

6,510✔
708
    if (textData !== undefined && textData !== "")
6,510✔
709
      parentNode.add(this.options.textNodeName, textData);
6,510✔
710
    textData = "";
6,475✔
711
  }
6,475✔
712
  return textData;
7,710✔
713
}
7,745✔
714

5✔
715
//TODO: use jPath to simplify the logic
5✔
716
/**
5✔
717
 * @param {Array<Expression>} stopNodeExpressions - Array of compiled Expression objects
5✔
718
 * @param {Matcher} matcher - Current path matcher
5✔
719
 */
5✔
720
function isItStopNode(stopNodeExpressions, matcher) {
6,365✔
721
  if (!stopNodeExpressions || stopNodeExpressions.length === 0) return false;
6,365✔
722

2,220✔
723
  for (let i = 0; i < stopNodeExpressions.length; i++) {
6,365✔
724
    if (matcher.matches(stopNodeExpressions[i])) {
3,505✔
725
      return true;
1,225✔
726
    }
1,225✔
727
  }
3,505✔
728
  return false;
995✔
729
}
6,365✔
730

5✔
731
/**
5✔
732
 * Returns the tag Expression and where it is ending handling single-double quotes situation
5✔
733
 * @param {string} xmlData 
5✔
734
 * @param {number} i starting index
5✔
735
 * @returns 
5✔
736
 */
5✔
737
function tagExpWithClosingIndex(xmlData, i, closingChar = ">") {
6,855✔
738
  let attrBoundary;
6,855✔
739
  let tagExp = "";
6,855✔
740
  for (let index = i; index < xmlData.length; index++) {
6,855✔
741
    let ch = xmlData[index];
67,695✔
742
    if (attrBoundary) {
67,695✔
743
      if (ch === attrBoundary) attrBoundary = "";//reset
14,510✔
744
    } else if (ch === '"' || ch === "'") {
67,695✔
745
      attrBoundary = ch;
1,580✔
746
    } else if (ch === closingChar[0]) {
53,185✔
747
      if (closingChar[1]) {
7,085✔
748
        if (xmlData[index + 1] === closingChar[1]) {
480✔
749
          return {
235✔
750
            data: tagExp,
235✔
751
            index: index
235✔
752
          }
235✔
753
        }
235✔
754
      } else {
7,085✔
755
        return {
6,605✔
756
          data: tagExp,
6,605✔
757
          index: index
6,605✔
758
        }
6,605✔
759
      }
6,605✔
760
    } else if (ch === '\t') {
51,605✔
761
      ch = " "
20✔
762
    }
20✔
763
    tagExp += ch;
60,855✔
764
  }
60,855✔
765
}
6,855✔
766

5✔
767
function findClosingIndex(xmlData, str, i, errMsg) {
5,710✔
768
  const closingIndex = xmlData.indexOf(str, i);
5,710✔
769
  if (closingIndex === -1) {
5,710✔
770
    throw new Error(errMsg)
15✔
771
  } else {
5,710✔
772
    return closingIndex + str.length - 1;
5,695✔
773
  }
5,695✔
774
}
5,710✔
775

5✔
776
function readTagExp(xmlData, i, removeNSPrefix, closingChar = ">") {
6,855✔
777
  const result = tagExpWithClosingIndex(xmlData, i + 1, closingChar);
6,855✔
778
  if (!result) return;
6,855✔
779
  let tagExp = result.data;
6,840✔
780
  const closeIndex = result.index;
6,840✔
781
  const separatorIndex = tagExp.search(/\s/);
6,840✔
782
  let tagName = tagExp;
6,840✔
783
  let attrExpPresent = true;
6,840✔
784
  if (separatorIndex !== -1) {//separate tag name and attributes expression
6,855✔
785
    tagName = tagExp.substring(0, separatorIndex);
1,230✔
786
    tagExp = tagExp.substring(separatorIndex + 1).trimStart();
1,230✔
787
  }
1,230✔
788

6,840✔
789
  const rawTagName = tagName;
6,840✔
790
  if (removeNSPrefix) {
6,855✔
791
    const colonIndex = tagName.indexOf(":");
300✔
792
    if (colonIndex !== -1) {
300✔
793
      tagName = tagName.substr(colonIndex + 1);
65✔
794
      attrExpPresent = tagName !== result.data.substr(colonIndex + 1);
65✔
795
    }
65✔
796
  }
300✔
797

6,840✔
798
  return {
6,840✔
799
    tagName: tagName,
6,840✔
800
    tagExp: tagExp,
6,840✔
801
    closeIndex: closeIndex,
6,840✔
802
    attrExpPresent: attrExpPresent,
6,840✔
803
    rawTagName: rawTagName,
6,840✔
804
  }
6,840✔
805
}
6,855✔
806
/**
5✔
807
 * find paired tag for a stop node
5✔
808
 * @param {string} xmlData 
5✔
809
 * @param {string} tagName 
5✔
810
 * @param {number} i 
5✔
811
 */
5✔
812
function readStopNodeData(xmlData, tagName, i) {
1,210✔
813
  const startIndex = i;
1,210✔
814
  // Starting at 1 since we already have an open tag
1,210✔
815
  let openTagCount = 1;
1,210✔
816

1,210✔
817
  for (; i < xmlData.length; i++) {
1,210✔
818
    if (xmlData[i] === "<") {
15,985✔
819
      if (xmlData[i + 1] === "/") {//close tag
1,555✔
820
        const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`);
1,370✔
821
        let closeTagName = xmlData.substring(i + 2, closeIndex).trim();
1,370✔
822
        if (closeTagName === tagName) {
1,370✔
823
          openTagCount--;
1,215✔
824
          if (openTagCount === 0) {
1,215✔
825
            return {
1,210✔
826
              tagContent: xmlData.substring(startIndex, i),
1,210✔
827
              i: closeIndex
1,210✔
828
            }
1,210✔
829
          }
1,210✔
830
        }
1,215✔
831
        i = closeIndex;
160✔
832
      } else if (xmlData[i + 1] === '?') {
1,555!
UNCOV
833
        const closeIndex = findClosingIndex(xmlData, "?>", i + 1, "StopNode is not closed.")
×
UNCOV
834
        i = closeIndex;
×
835
      } else if (xmlData.substr(i + 1, 3) === '!--') {
185✔
836
        const closeIndex = findClosingIndex(xmlData, "-->", i + 3, "StopNode is not closed.")
5✔
837
        i = closeIndex;
5✔
838
      } else if (xmlData.substr(i + 1, 2) === '![') {
185✔
839
        const closeIndex = findClosingIndex(xmlData, "]]>", i, "StopNode is not closed.") - 2;
5✔
840
        i = closeIndex;
5✔
841
      } else {
180✔
842
        const tagData = readTagExp(xmlData, i, '>')
175✔
843

175✔
844
        if (tagData) {
175✔
845
          const openTagName = tagData && tagData.tagName;
170✔
846
          if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length - 1] !== "/") {
170✔
847
            openTagCount++;
5✔
848
          }
5✔
849
          i = tagData.closeIndex;
170✔
850
        }
170✔
851
      }
175✔
852
    }
1,555✔
853
  }//end for loop
1,210✔
854
}
1,210✔
855

5✔
856
function parseValue(val, shouldParse, options) {
3,100✔
857
  if (shouldParse && typeof val === 'string') {
3,100✔
858
    //console.log(options)
2,080✔
859
    const newval = val.trim();
2,080✔
860
    if (newval === 'true') return true;
2,080✔
861
    else if (newval === 'false') return false;
2,045✔
862
    else return toNumber(val, options);
2,030✔
863
  } else {
3,100✔
864
    if (isExist(val)) {
1,020✔
865
      return val;
1,020✔
866
    } else {
1,020!
UNCOV
867
      return '';
×
UNCOV
868
    }
×
869
  }
1,020✔
870
}
3,100✔
871

5✔
872
function fromCodePoint(str, base, prefix) {
50✔
873
  const codePoint = Number.parseInt(str, base);
50✔
874

50✔
875
  if (codePoint >= 0 && codePoint <= 0x10FFFF) {
50✔
876
    return String.fromCodePoint(codePoint);
40✔
877
  } else {
50✔
878
    return prefix + str + ";";
10✔
879
  }
10✔
880
}
50✔
881

5✔
882
function transformTagName(fn, tagName, tagExp, options) {
10,780✔
883
  if (fn) {
10,780✔
884
    const newTagName = fn(tagName);
160✔
885
    if (tagExp === tagName) {
160✔
886
      tagExp = newTagName
55✔
887
    }
55✔
888
    tagName = newTagName;
160✔
889
  }
160✔
890
  tagName = sanitizeName(tagName, options);
10,780✔
891
  return { tagName, tagExp };
10,780✔
892
}
10,780✔
893

5✔
894

5✔
895

5✔
896
function sanitizeName(name, options) {
12,130✔
897
  if (criticalProperties.includes(name)) {
12,130✔
898
    throw new Error(`[SECURITY] Invalid name: "${name}" is a reserved JavaScript keyword that could cause prototype pollution`);
30✔
899
  } else if (DANGEROUS_PROPERTY_NAMES.includes(name)) {
12,130✔
900
    return options.onDangerousProperty(name);
105✔
901
  }
105✔
902
  return name;
11,995✔
903
}
12,130✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc