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

NaturalIntelligence / fast-xml-parser / 23146869289

16 Mar 2026 01:45PM UTC coverage: 97.709% (-0.1%) from 97.837%
23146869289

push

github

amitguptagwl
refactor: performance improvement

1144 of 1190 branches covered (96.13%)

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

44 existing lines in 4 files now uncovered.

9382 of 9602 relevant lines covered (97.71%)

464759.33 hits per line

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

97.46
/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,368✔
39
    if (key.startsWith(options.attributeNamePrefix)) {
1,368✔
40
      const rawName = key.substring(options.attributeNamePrefix.length);
1,181✔
41
      rawAttrs[rawName] = attrs[key];
1,181✔
42
    } else {
1,368✔
43
      // Attribute without prefix (shouldn't normally happen, but be safe)
187✔
44
      rawAttrs[key] = attrs[key];
187✔
45
    }
187✔
46
  }
1,368✔
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
    // Flag to track if current node is a stop node (optimization)
1,690✔
117
    this.isCurrentNodeStopNode = false;
1,690✔
118

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

5✔
135
}
5✔
136

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

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

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

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

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

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

990✔
212
    const matches = getAllMatches(attrStr, attrsRegx);
990✔
213
    const len = matches.length; //don't make it inline
990✔
214
    const attrs = {};
990✔
215

990✔
216
    // First pass: parse all attributes and update matcher with raw values
990✔
217
    // This ensures the matcher has all attribute values when processors run
990✔
218
    const rawAttrsForMatcher = {};
990✔
219
    for (let i = 0; i < len; i++) {
990✔
220
      const attrName = this.resolveNameSpace(matches[i][1]);
1,440✔
221
      const oldVal = matches[i][4];
1,440✔
222

1,440✔
223
      if (attrName.length && oldVal !== undefined) {
1,440✔
224
        let parsedVal = oldVal;
1,315✔
225
        if (this.options.trimValues) {
1,315✔
226
          parsedVal = parsedVal.trim();
1,290✔
227
        }
1,290✔
228
        parsedVal = this.replaceEntitiesValue(parsedVal, tagName, jPath);
1,315✔
229
        rawAttrsForMatcher[attrName] = parsedVal;
1,315✔
230
      }
1,315✔
231
    }
1,440✔
232

990✔
233
    // Update matcher with raw attribute values BEFORE running processors
990✔
234
    if (Object.keys(rawAttrsForMatcher).length > 0 && typeof jPath === 'object' && jPath.updateCurrent) {
990✔
235
      jPath.updateCurrent(rawAttrsForMatcher);
865✔
236
    }
865✔
237

990✔
238
    // Second pass: now process attributes with matcher having full attribute context
990✔
239
    for (let i = 0; i < len; i++) {
990✔
240
      const attrName = this.resolveNameSpace(matches[i][1]);
1,440✔
241

1,440✔
242
      // Convert jPath to string if needed for ignoreAttributesFn
1,440✔
243
      const jPathStr = this.options.jPath ? jPath.toString() : jPath;
1,440✔
244
      if (this.ignoreAttributesFn(attrName, jPathStr)) {
1,440✔
245
        continue
55✔
246
      }
55✔
247

1,385✔
248
      let oldVal = matches[i][4];
1,385✔
249
      let aName = this.options.attributeNamePrefix + attrName;
1,385✔
250

1,385✔
251
      if (attrName.length) {
1,440✔
252
        if (this.options.transformAttributeName) {
1,350✔
253
          aName = this.options.transformAttributeName(aName);
20✔
254
        }
20✔
255
        //if (aName === "__proto__") aName = "#__proto__";
1,350✔
256
        aName = sanitizeName(aName, this.options);
1,350✔
257

1,350✔
258
        if (oldVal !== undefined) {
1,350✔
259
          if (this.options.trimValues) {
1,245✔
260
            oldVal = oldVal.trim();
1,220✔
261
          }
1,220✔
262
          oldVal = this.replaceEntitiesValue(oldVal, tagName, jPath);
1,245✔
263

1,245✔
264
          // Pass jPath string or matcher based on options.jPath setting
1,245✔
265
          const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
1,245✔
266
          const newVal = this.options.attributeValueProcessor(attrName, oldVal, jPathOrMatcher);
1,245✔
267
          if (newVal === null || newVal === undefined) {
1,245!
268
            //don't parse
×
269
            attrs[aName] = oldVal;
×
270
          } else if (typeof newVal !== typeof oldVal || newVal !== oldVal) {
1,245✔
271
            //overwrite
5✔
272
            attrs[aName] = newVal;
5✔
273
          } else {
1,245✔
274
            //parse
1,240✔
275
            attrs[aName] = parseValue(
1,240✔
276
              oldVal,
1,240✔
277
              this.options.parseAttributeValue,
1,240✔
278
              this.options.numberParseOptions
1,240✔
279
            );
1,240✔
280
          }
1,240✔
281
        } else if (this.options.allowBooleanAttributes) {
1,350✔
282
          attrs[aName] = true;
80✔
283
        }
80✔
284
      }
1,350✔
285
    }
1,440✔
286

975✔
287
    if (!Object.keys(attrs).length) {
990✔
288
      return;
95✔
289
    }
95✔
290
    if (this.options.attributesGroupName) {
990✔
291
      const attrCollection = {};
60✔
292
      attrCollection[this.options.attributesGroupName] = attrs;
60✔
293
      return attrCollection;
60✔
294
    }
60✔
295
    return attrs
820✔
296
  }
820✔
297
}
1,180✔
298

5✔
299
const parseXml = function (xmlData) {
5✔
300
  xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
1,690✔
301
  const xmlObj = new xmlNode('!xml');
1,690✔
302
  let currentNode = xmlObj;
1,690✔
303
  let textData = "";
1,690✔
304

1,690✔
305
  // Reset matcher for new document
1,690✔
306
  this.matcher.reset();
1,690✔
307

1,690✔
308
  // Reset entity expansion counters for this document
1,690✔
309
  this.entityExpansionCount = 0;
1,690✔
310
  this.currentExpandedLength = 0;
1,690✔
311

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

4,080✔
322
        if (this.options.removeNSPrefix) {
4,080✔
323
          const colonIndex = tagName.indexOf(":");
115✔
324
          if (colonIndex !== -1) {
115✔
325
            tagName = tagName.substr(colonIndex + 1);
50✔
326
          }
50✔
327
        }
115✔
328

4,075✔
329
        tagName = transformTagName(this.options.transformTagName, tagName, "", this.options).tagName;
4,075✔
330

4,075✔
331
        if (currentNode) {
4,075✔
332
          textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
4,075✔
333
        }
4,075✔
334

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

4,035✔
349
        currentNode = this.tagsNodeStack.pop();//avoid recursion, set the parent tag scope
4,035✔
350
        textData = "";
4,035✔
351
        i = closeIndex;
4,035✔
352
      } else if (xmlData[i + 1] === '?') {
11,230✔
353

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

235✔
357
        textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
235✔
358
        if ((this.options.ignoreDeclaration && tagData.tagName === "?xml") || this.options.ignorePiTags) {
245✔
359
          //do nothing
25✔
360
        } else {
245✔
361

210✔
362
          const childNode = new xmlNode(tagData.tagName);
210✔
363
          childNode.add(this.options.textNodeName, "");
210✔
364

210✔
365
          if (tagData.tagName !== tagData.tagExp && tagData.attrExpPresent) {
210✔
366
            childNode[":@"] = this.buildAttributesMap(tagData.tagExp, this.matcher, tagData.tagName);
200✔
367
          }
200✔
368
          this.addChild(currentNode, childNode, this.matcher, i);
210✔
369
        }
210✔
370

235✔
371

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

25✔
378
          textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
25✔
379

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

190✔
391
        textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
190✔
392

190✔
393
        let val = this.parseTextData(tagExp, currentNode.tagname, this.matcher, true, false, true, true);
190✔
394
        if (val == undefined) val = "";
190✔
395

185✔
396
        //cdata should be set even if it is 0 length string
185✔
397
        if (this.options.cdataPropName) {
190✔
398
          currentNode.add(this.options.cdataPropName, [{ [this.options.textNodeName]: tagExp }]);
35✔
399
        } else {
190✔
400
          currentNode.add(this.options.textNodeName, val);
150✔
401
        }
150✔
402

185✔
403
        i = closeIndex + 2;
185✔
404
      } else {//Opening tag
6,625✔
405
        let result = readTagExp(xmlData, i, this.options.removeNSPrefix);
6,435✔
406

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

6,435✔
414
        let tagName = result.tagName;
6,435✔
415
        const rawTagName = result.rawTagName;
6,435✔
416
        let tagExp = result.tagExp;
6,435✔
417
        let attrExpPresent = result.attrExpPresent;
6,435✔
418
        let closeIndex = result.closeIndex;
6,435✔
419

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

6,435✔
422
        if (this.options.strictReservedNames &&
6,435✔
423
          (tagName === this.options.commentPropName
6,375✔
424
            || tagName === this.options.cdataPropName
6,375✔
425
          )) {
6,435✔
426
          throw new Error(`Invalid tag name: ${tagName}`);
5✔
427
        }
5✔
428

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

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

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

280✔
456
          // Re-check attrExpPresent after cleaning
280✔
457
          attrExpPresent = (tagName !== tagExp);
280✔
458
        }
280✔
459

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

6,380✔
465
        // Extract namespace from rawTagName
6,380✔
466
        namespace = extractNamespace(rawTagName);
6,380✔
467

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

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

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

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

6,365✔
490
        const startIndex = i;
6,365✔
491
        if (this.isCurrentNodeStopNode) {
6,435✔
492
          let tagContent = "";
1,225✔
493

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

1,225✔
511
          const childNode = new xmlNode(tagName);
1,225✔
512

1,225✔
513
          if (prefixedAttrs) {
1,225✔
514
            childNode[":@"] = prefixedAttrs;
20✔
515
          }
20✔
516

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

1,225✔
520
          this.matcher.pop(); // Pop the stop node tag
1,225✔
521
          this.isCurrentNodeStopNode = false; // Reset flag
1,225✔
522

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

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

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

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

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

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

4,405✔
599
  if (!entityConfig || !entityConfig.enabled) {
4,405✔
600
    return val;
80✔
601
  }
80✔
602

4,325✔
603
  // Check if tag is allowed to contain entities
4,325✔
604
  if (entityConfig.allowedTags) {
4,405✔
605
    const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
30!
606
    const allowed = Array.isArray(entityConfig.allowedTags)
30✔
607
      ? entityConfig.allowedTags.includes(tagName)
30✔
608
      : entityConfig.allowedTags(tagName, jPathOrMatcher);
30!
609

30✔
610
    if (!allowed) {
30✔
611
      return val;
10✔
612
    }
10✔
613
  }
30✔
614

4,315✔
615
  // Apply custom tag filter if provided
4,315✔
616
  if (entityConfig.tagFilter) {
4,405✔
617
    const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
25!
618
    if (!entityConfig.tagFilter(tagName, jPathOrMatcher)) {
25✔
619
      return val; // Skip based on custom filter
15✔
620
    }
15✔
621
  }
25✔
622

4,300✔
623
  // Replace DOCTYPE entities
4,300✔
624
  for (const entityName of Object.keys(this.docTypeEntities)) {
4,405✔
625
    const entity = this.docTypeEntities[entityName];
490✔
626
    const matches = val.match(entity.regx);
490✔
627

490✔
628
    if (matches) {
490✔
629
      // Track expansions
245✔
630
      this.entityExpansionCount += matches.length;
245✔
631

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

230✔
640
      // Store length before replacement
230✔
641
      const lengthBefore = val.length;
230✔
642
      val = val.replace(entity.regx, entity.val);
230✔
643

230✔
644
      // Check expanded length immediately after replacement
230✔
645
      if (entityConfig.maxExpandedLength) {
230✔
646
        this.currentExpandedLength += (val.length - lengthBefore);
230✔
647

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

120✔
673
  // Replace HTML entities if enabled
120✔
674
  if (this.options.htmlEntities) {
4,405✔
675
    for (const entityName of Object.keys(this.htmlEntities)) {
60✔
676
      const entity = this.htmlEntities[entityName];
595✔
677
      const matches = val.match(entity.regex);
595✔
678
      if (matches) {
595✔
679
        //console.log(matches);
70✔
680
        this.entityExpansionCount += matches.length;
70✔
681
        if (entityConfig.maxTotalExpansions &&
70✔
682
          this.entityExpansionCount > entityConfig.maxTotalExpansions) {
70✔
683
          throw new Error(
5✔
684
            `Entity expansion limit exceeded: ${this.entityExpansionCount} > ${entityConfig.maxTotalExpansions}`
5✔
685
          );
5✔
686
        }
5✔
687
      }
70✔
688
      val = val.replace(entity.regex, entity.val);
590✔
689
    }
590✔
690
  }
55✔
691

115✔
692
  // Replace ampersand entity last
115✔
693
  val = val.replace(this.ampEntity.regex, this.ampEntity.val);
115✔
694

115✔
695
  return val;
115✔
696
}
4,405✔
697

5✔
698

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

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

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

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

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

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

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

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

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

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

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

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

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

5✔
874
function fromCodePoint(str, base, prefix) {
90✔
875
  const codePoint = Number.parseInt(str, base);
90✔
876

90✔
877
  if (codePoint >= 0 && codePoint <= 0x10FFFF) {
90✔
878
    return String.fromCodePoint(codePoint);
75✔
879
  } else {
90✔
880
    return prefix + str + ";";
15✔
881
  }
15✔
882
}
90✔
883

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

5✔
896

5✔
897

5✔
898
function sanitizeName(name, options) {
12,130✔
899
  if (criticalProperties.includes(name)) {
12,130✔
900
    throw new Error(`[SECURITY] Invalid name: "${name}" is a reserved JavaScript keyword that could cause prototype pollution`);
30✔
901
  } else if (DANGEROUS_PROPERTY_NAMES.includes(name)) {
12,130✔
902
    return options.onDangerousProperty(name);
105✔
903
  }
105✔
904
  return name;
11,995✔
905
}
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