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

NaturalIntelligence / fast-xml-parser / 25647313727

11 May 2026 02:34AM UTC coverage: 97.61% (-0.06%) from 97.671%
25647313727

push

github

amitguptagwl
release info

1146 of 1193 branches covered (96.06%)

9517 of 9750 relevant lines covered (97.61%)

524886.82 hits per line

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

96.78
/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
import { ExpressionSet } from 'path-expression-matcher';
5✔
11
import { EntityDecoder, XML, CURRENCY, COMMON_HTML } from '@nodable/entities';
5✔
12

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

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

5✔
20
// Helper functions for attribute and namespace handling
5✔
21

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

775✔
31
  // Handle attributesGroupName option
775✔
32
  const attrs = options.attributesGroupName
775✔
33
    ? prefixedAttrs[options.attributesGroupName]
775✔
34
    : prefixedAttrs;
775✔
35

775✔
36
  if (!attrs) return {};
775✔
37

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

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

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

5✔
71
export default class OrderedObjParser {
5✔
72
  constructor(options, externalEntities) {
5✔
73
    this.options = options;
1,730✔
74
    this.currentNode = null;
1,730✔
75
    this.tagsNodeStack = [];
1,730✔
76
    this.parseXml = parseXml;
1,730✔
77
    this.parseTextData = parseTextData;
1,730✔
78
    this.resolveNameSpace = resolveNameSpace;
1,730✔
79
    this.buildAttributesMap = buildAttributesMap;
1,730✔
80
    this.isItStopNode = isItStopNode;
1,730✔
81
    this.replaceEntitiesValue = replaceEntitiesValue;
1,730✔
82
    this.readStopNodeData = readStopNodeData;
1,730✔
83
    this.saveTextToParentTag = saveTextToParentTag;
1,730✔
84
    this.addChild = addChild;
1,730✔
85
    this.ignoreAttributesFn = getIgnoreAttributesFn(this.options.ignoreAttributes)
1,730✔
86
    this.entityExpansionCount = 0;
1,730✔
87
    this.currentExpandedLength = 0;
1,730✔
88
    let namedEntities = { ...XML };
1,730✔
89
    if (this.options.entityDecoder) {
1,730✔
90
      this.entityDecoder = this.options.entityDecoder
15✔
91
    } else {
1,730✔
92
      if (typeof this.options.htmlEntities === "object") namedEntities = this.options.htmlEntities;
1,715!
93
      else if (this.options.htmlEntities === true) namedEntities = { ...COMMON_HTML, ...CURRENCY };
1,715✔
94
      this.entityDecoder = new EntityDecoder({
1,715✔
95
        namedEntities: { ...namedEntities, ...externalEntities },
1,715✔
96
        numericAllowed: this.options.htmlEntities,
1,715✔
97
        limit: {
1,715✔
98
          maxTotalExpansions: this.options.processEntities.maxTotalExpansions,
1,715✔
99
          maxExpandedLength: this.options.processEntities.maxExpandedLength,
1,715✔
100
          applyLimitsTo: this.options.processEntities.appliesTo,
1,715✔
101
        }
1,715✔
102
        //postCheck: resolved => resolved
1,715✔
103
      });
1,715✔
104
    }
1,715✔
105

1,730✔
106
    // Initialize path matcher for path-expression-matcher
1,730✔
107
    this.matcher = new Matcher();
1,730✔
108
    this.readonlyMatcher = this.matcher.readOnly();
1,730✔
109

1,730✔
110
    // Flag to track if current node is a stop node (optimization)
1,730✔
111
    this.isCurrentNodeStopNode = false;
1,730✔
112

1,730✔
113
    // Pre-compile stopNodes expressions
1,730✔
114
    this.stopNodeExpressionsSet = new ExpressionSet();
1,730✔
115
    const stopNodesOpts = this.options.stopNodes;
1,730✔
116
    if (stopNodesOpts && stopNodesOpts.length > 0) {
1,730✔
117
      for (let i = 0; i < stopNodesOpts.length; i++) {
675✔
118
        const stopNodeExp = stopNodesOpts[i];
1,260✔
119
        if (typeof stopNodeExp === 'string') {
1,260✔
120
          // Convert string to Expression object
205✔
121
          this.stopNodeExpressionsSet.add(new Expression(stopNodeExp));
205✔
122
        } else if (stopNodeExp instanceof Expression) {
1,260✔
123
          // Already an Expression object
1,055✔
124
          this.stopNodeExpressionsSet.add(stopNodeExp);
1,055✔
125
        }
1,055✔
126
      }
1,260✔
127
      this.stopNodeExpressionsSet.seal();
675✔
128
    }
675✔
129
  }
1,730✔
130

5✔
131
}
5✔
132

5✔
133

5✔
134
/**
5✔
135
 * @param {string} val
5✔
136
 * @param {string} tagName
5✔
137
 * @param {string|Matcher} jPath - jPath string or Matcher instance based on options.jPath
5✔
138
 * @param {boolean} dontTrim
5✔
139
 * @param {boolean} hasAttributes
5✔
140
 * @param {boolean} isLeafNode
5✔
141
 * @param {boolean} escapeEntities
5✔
142
 */
5✔
143
function parseTextData(val, tagName, jPath, dontTrim, hasAttributes, isLeafNode, escapeEntities) {
6,715✔
144
  const options = this.options;
6,715✔
145
  if (val !== undefined) {
6,715✔
146
    if (options.trimValues && !dontTrim) {
6,715✔
147
      val = val.trim();
6,470✔
148
    }
6,470✔
149
    if (val.length > 0) {
6,715✔
150
      if (!escapeEntities) val = this.replaceEntitiesValue(val, tagName, jPath);
2,030✔
151

1,995✔
152
      // Pass jPath string or matcher based on options.jPath setting
1,995✔
153
      const jPathOrMatcher = options.jPath ? jPath.toString() : jPath;
2,030✔
154
      const newval = options.tagValueProcessor(tagName, val, jPathOrMatcher, hasAttributes, isLeafNode);
2,030✔
155
      if (newval === null || newval === undefined) {
2,030✔
156
        //don't parse
50✔
157
        return val;
50✔
158
      } else if (typeof newval !== typeof val || newval !== val) {
2,030✔
159
        //overwrite
15✔
160
        return newval;
15✔
161
      } else if (options.trimValues) {
1,945✔
162
        return parseValue(val, options.parseTagValue, options.numberParseOptions);
1,870✔
163
      } else {
1,930✔
164
        const trimmedVal = val.trim();
60✔
165
        if (trimmedVal === val) {
60✔
166
          return parseValue(val, options.parseTagValue, options.numberParseOptions);
15✔
167
        } else {
60✔
168
          return val;
45✔
169
        }
45✔
170
      }
60✔
171
    }
2,030✔
172
  }
6,715✔
173
}
6,715✔
174

5✔
175
function resolveNameSpace(tagname) {
10,003,270✔
176
  if (this.options.removeNSPrefix) {
10,003,270✔
177
    const tags = tagname.split(':');
220✔
178
    const prefix = tagname.charAt(0) === '/' ? '/' : '';
220!
179
    if (tags[0] === 'xmlns') {
220✔
180
      return '';
70✔
181
    }
70✔
182
    if (tags.length === 2) {
220✔
183
      tagname = prefix + tags[1];
60✔
184
    }
60✔
185
  }
220✔
186
  return tagname;
10,003,200✔
187
}
10,003,270✔
188

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

5✔
193
function buildAttributesMap(attrStr, jPath, tagName, force = false) {
1,255✔
194
  const options = this.options;
1,255✔
195
  if (force === true || (options.ignoreAttributes !== true && typeof attrStr === 'string')) {
1,255✔
196
    // attrStr = attrStr.replace(/\r?\n/g, ' ');
1,145✔
197
    //attrStr = attrStr || attrStr.trim();
1,145✔
198

1,145✔
199
    const matches = getAllMatches(attrStr, attrsRegx);
1,145✔
200
    const len = matches.length; //don't make it inline
1,145✔
201
    const attrs = {};
1,145✔
202

1,145✔
203
    // Pre-process values once: trim + entity replacement
1,145✔
204
    // Reused in both matcher update and second pass
1,145✔
205
    const processedVals = new Array(len);
1,145✔
206
    let hasRawAttrs = false;
1,145✔
207
    const rawAttrsForMatcher = {};
1,145✔
208

1,145✔
209
    for (let i = 0; i < len; i++) {
1,145✔
210
      const attrName = this.resolveNameSpace(matches[i][1]);
5,001,635✔
211
      const oldVal = matches[i][4];
5,001,635✔
212

5,001,635✔
213
      if (attrName.length && oldVal !== undefined) {
5,001,635✔
214
        let val = oldVal;
5,001,470✔
215
        if (options.trimValues) val = val.trim();
5,001,470✔
216
        val = this.replaceEntitiesValue(val, tagName, this.readonlyMatcher);
5,001,470✔
217
        processedVals[i] = val;
5,001,470✔
218

5,001,470✔
219
        rawAttrsForMatcher[attrName] = val;
5,001,470✔
220
        hasRawAttrs = true;
5,001,470✔
221
      }
5,001,470✔
222
    }
5,001,635✔
223

1,145✔
224
    // Update matcher ONCE before second pass, if applicable
1,145✔
225
    if (hasRawAttrs && typeof jPath === 'object' && jPath.updateCurrent) {
1,145✔
226
      jPath.updateCurrent(rawAttrsForMatcher);
1,005✔
227
    }
1,005✔
228

1,145✔
229
    // Hoist toString() once — path doesn't change during attribute processing
1,145✔
230
    const jPathStr = options.jPath ? jPath.toString() : this.readonlyMatcher;
1,145✔
231

1,145✔
232
    // Second pass: apply processors, build final attrs
1,145✔
233
    let hasAttrs = false;
1,145✔
234
    for (let i = 0; i < len; i++) {
1,145✔
235
      const attrName = this.resolveNameSpace(matches[i][1]);
5,001,635✔
236

5,001,635✔
237
      if (this.ignoreAttributesFn(attrName, jPathStr)) continue;
5,001,635✔
238

5,001,580✔
239
      let aName = options.attributeNamePrefix + attrName;
5,001,580✔
240

5,001,580✔
241
      if (attrName.length) {
5,001,635✔
242
        if (options.transformAttributeName) {
5,001,545✔
243
          aName = options.transformAttributeName(aName);
25✔
244
        }
25✔
245
        aName = sanitizeName(aName, options);
5,001,545✔
246

5,001,545✔
247
        if (matches[i][4] !== undefined) {
5,001,545✔
248
          // Reuse already-processed value — no double entity replacement
5,001,400✔
249
          const oldVal = processedVals[i];
5,001,400✔
250

5,001,400✔
251
          const newVal = options.attributeValueProcessor(attrName, oldVal, jPathStr);
5,001,400✔
252
          if (newVal === null || newVal === undefined) {
5,001,400!
253
            attrs[aName] = oldVal;
×
254
          } else if (typeof newVal !== typeof oldVal || newVal !== oldVal) {
5,001,400✔
255
            attrs[aName] = newVal;
5✔
256
          } else {
5,001,400✔
257
            attrs[aName] = parseValue(oldVal, options.parseAttributeValue, options.numberParseOptions);
5,001,395✔
258
          }
5,001,395✔
259
          hasAttrs = true;
5,001,400✔
260
        } else if (options.allowBooleanAttributes) {
5,001,545✔
261
          attrs[aName] = true;
110✔
262
          hasAttrs = true;
110✔
263
        }
110✔
264
      }
5,001,545✔
265
    }
5,001,635✔
266

1,130✔
267
    if (!hasAttrs) return;
1,145✔
268

1,025✔
269
    if (options.attributesGroupName && !options.preserveOrder) {
1,145✔
270
      const attrCollection = {};
65✔
271
      attrCollection[options.attributesGroupName] = attrs;
65✔
272
      return attrCollection;
65✔
273
    }
65✔
274
    return attrs;
960✔
275
  }
960✔
276
}
1,255✔
277
const parseXml = function (xmlData) {
5✔
278
  xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
1,730✔
279
  const xmlObj = new xmlNode('!xml');
1,730✔
280
  let currentNode = xmlObj;
1,730✔
281
  let textData = "";
1,730✔
282

1,730✔
283
  // Reset matcher for new document
1,730✔
284
  this.matcher.reset();
1,730✔
285
  this.entityDecoder.reset();
1,730✔
286

1,730✔
287
  // Reset entity expansion counters for this document
1,730✔
288
  this.entityExpansionCount = 0;
1,730✔
289
  this.currentExpandedLength = 0;
1,730✔
290
  const options = this.options;
1,730✔
291
  const docTypeReader = new DocTypeReader(options.processEntities);
1,730✔
292
  const xmlLen = xmlData.length;
1,730✔
293
  for (let i = 0; i < xmlLen; i++) {//for each char in XML data
1,730✔
294
    const ch = xmlData[i];
326,245✔
295
    if (ch === '<') {
326,245✔
296
      // const nextIndex = i+1;
11,325✔
297
      // const _2ndChar = xmlData[nextIndex];
11,325✔
298
      const c1 = xmlData.charCodeAt(i + 1);
11,325✔
299
      if (c1 === 47) {//Closing Tag '/'
11,325✔
300
        const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.")
4,105✔
301
        let tagName = xmlData.substring(i + 2, closeIndex).trim();
4,105✔
302

4,105✔
303
        if (options.removeNSPrefix) {
4,105✔
304
          const colonIndex = tagName.indexOf(":");
120✔
305
          if (colonIndex !== -1) {
120✔
306
            tagName = tagName.substr(colonIndex + 1);
50✔
307
          }
50✔
308
        }
120✔
309

4,100✔
310
        tagName = transformTagName(options.transformTagName, tagName, "", options).tagName;
4,100✔
311

4,100✔
312
        if (currentNode) {
4,100✔
313
          textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
4,100✔
314
        }
4,100✔
315

4,065✔
316
        //check if last tag of nested tag was unpaired tag
4,065✔
317
        const lastTagName = this.matcher.getCurrentTag();
4,065✔
318
        if (tagName && options.unpairedTagsSet.has(tagName)) {
4,105✔
319
          throw new Error(`Unpaired tag can not be used as closing tag: </${tagName}>`);
5✔
320
        }
5✔
321
        if (lastTagName && options.unpairedTagsSet.has(lastTagName)) {
4,105!
322
          // Pop the unpaired tag
×
323
          this.matcher.pop();
×
324
          this.tagsNodeStack.pop();
×
325
        }
×
326
        // Pop the closing tag
4,060✔
327
        this.matcher.pop();
4,060✔
328
        this.isCurrentNodeStopNode = false; // Reset flag when closing tag
4,060✔
329

4,060✔
330
        currentNode = this.tagsNodeStack.pop();//avoid recursion, set the parent tag scope
4,060✔
331
        textData = "";
4,060✔
332
        i = closeIndex;
4,060✔
333
      } else if (c1 === 63) { //'?'
11,325✔
334

270✔
335
        let tagData = readTagExp(xmlData, i, false, "?>");
270✔
336
        if (!tagData) throw new Error("Pi Tag is not closed.");
270✔
337

260✔
338
        textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
260✔
339
        const attsMap = this.buildAttributesMap(tagData.tagExp, this.matcher, tagData.tagName, true);
260✔
340
        if (attsMap) {
270✔
341
          const ver = attsMap[this.options.attributeNamePrefix + "version"];
250✔
342
          this.entityDecoder.setXmlVersion(Number(ver) || 1.0);
250✔
343
          docTypeReader.setXmlVersion(Number(ver) || 1.0);
250✔
344
        }
250✔
345
        if ((options.ignoreDeclaration && tagData.tagName === "?xml") || options.ignorePiTags) {
270✔
346
          //do nothing
25✔
347
        } else {
270✔
348

235✔
349
          const childNode = new xmlNode(tagData.tagName);
235✔
350
          childNode.add(options.textNodeName, "");
235✔
351

235✔
352
          if (tagData.tagName !== tagData.tagExp && tagData.attrExpPresent && options.ignoreAttributes !== true) {
235✔
353
            childNode[":@"] = attsMap
135✔
354
          }
135✔
355
          this.addChild(currentNode, childNode, this.readonlyMatcher, i);
235✔
356
        }
235✔
357

260✔
358

260✔
359
        i = tagData.closeIndex + 1;
260✔
360
      } else if (c1 === 33
7,220✔
361
        && xmlData.charCodeAt(i + 2) === 45
6,950✔
362
        && xmlData.charCodeAt(i + 3) === 45) { //'!--'
6,950✔
363
        const endIndex = findClosingIndex(xmlData, "-->", i + 4, "Comment is not closed.")
60✔
364
        if (options.commentPropName) {
60✔
365
          const comment = xmlData.substring(i + 4, endIndex - 2);
25✔
366

25✔
367
          textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
25✔
368

25✔
369
          currentNode.add(options.commentPropName, [{ [options.textNodeName]: comment }]);
25✔
370
        }
25✔
371
        i = endIndex;
55✔
372
      } else if (c1 === 33
6,950✔
373
        && xmlData.charCodeAt(i + 2) === 68) { //'!D'
6,890✔
374
        const result = docTypeReader.readDocType(xmlData, i);
220✔
375
        this.entityDecoder.addInputEntities(result.entities);
220✔
376
        i = result.i;
220✔
377
      } else if (c1 === 33
6,890✔
378
        && xmlData.charCodeAt(i + 2) === 91) { // '!['
6,670✔
379
        const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
190✔
380
        const tagExp = xmlData.substring(i + 9, closeIndex);
190✔
381

190✔
382
        textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
190✔
383

190✔
384
        let val = this.parseTextData(tagExp, currentNode.tagname, this.readonlyMatcher, true, false, true, true);
190✔
385
        if (val == undefined) val = "";
190✔
386

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

185✔
394
        i = closeIndex + 2;
185✔
395
      } else {//Opening tag
6,670✔
396
        let result = readTagExp(xmlData, i, options.removeNSPrefix);
6,480✔
397

6,480✔
398
        // Safety check: readTagExp can return undefined
6,480✔
399
        if (!result) {
6,480!
400
          // Log context for debugging
×
401
          const context = xmlData.substring(Math.max(0, i - 50), Math.min(xmlLen, i + 50));
×
402
          throw new Error(`readTagExp returned undefined at position ${i}. Context: "${context}"`);
×
403
        }
×
404

6,480✔
405
        let tagName = result.tagName;
6,480✔
406
        const rawTagName = result.rawTagName;
6,480✔
407
        let tagExp = result.tagExp;
6,480✔
408
        let attrExpPresent = result.attrExpPresent;
6,480✔
409
        let closeIndex = result.closeIndex;
6,480✔
410

6,480✔
411
        ({ tagName, tagExp } = transformTagName(options.transformTagName, tagName, tagExp, options));
6,480✔
412

6,480✔
413
        if (options.strictReservedNames &&
6,480✔
414
          (tagName === options.commentPropName
6,420✔
415
            || tagName === options.cdataPropName
6,420✔
416
            || tagName === options.textNodeName
6,420✔
417
            || tagName === options.attributesGroupName
6,420✔
418
          )) {
6,480✔
419
          throw new Error(`Invalid tag name: ${tagName}`);
5✔
420
        }
5✔
421

6,425✔
422
        //save text as child node
6,425✔
423
        if (currentNode && textData) {
6,480✔
424
          if (currentNode.tagname !== '!xml') {
4,120✔
425
            //when nested tag is found
3,225✔
426
            textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher, false);
3,225✔
427
          }
3,225✔
428
        }
4,120✔
429

6,425✔
430
        //check if last tag was unpaired tag
6,425✔
431
        const lastTag = currentNode;
6,425✔
432
        if (lastTag && options.unpairedTagsSet.has(lastTag.tagname)) {
6,480!
433
          currentNode = this.tagsNodeStack.pop();
×
434
          this.matcher.pop();
×
435
        }
×
436

6,425✔
437
        // Clean up self-closing syntax BEFORE processing attributes
6,425✔
438
        // This is where tagExp gets the trailing / removed
6,425✔
439
        let isSelfClosing = false;
6,425✔
440
        if (tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1) {
6,480✔
441
          isSelfClosing = true;
295✔
442
          if (tagName[tagName.length - 1] === "/") {
295✔
443
            tagName = tagName.substr(0, tagName.length - 1);
100✔
444
            tagExp = tagName;
100✔
445
          } else {
295✔
446
            tagExp = tagExp.substr(0, tagExp.length - 1);
195✔
447
          }
195✔
448

295✔
449
          // Re-check attrExpPresent after cleaning
295✔
450
          attrExpPresent = (tagName !== tagExp);
295✔
451
        }
295✔
452

6,425✔
453
        // Now process attributes with CLEAN tagExp (no trailing /)
6,425✔
454
        let prefixedAttrs = null;
6,425✔
455
        let rawAttrs = {};
6,425✔
456
        let namespace = undefined;
6,425✔
457

6,425✔
458
        // Extract namespace from rawTagName
6,425✔
459
        namespace = extractNamespace(rawTagName);
6,425✔
460

6,425✔
461
        // Push tag to matcher FIRST (with empty attrs for now) so callbacks see correct path
6,425✔
462
        if (tagName !== xmlObj.tagname) {
6,425✔
463
          this.matcher.push(tagName, {}, namespace);
6,425✔
464
        }
6,425✔
465

6,425✔
466
        // Now build attributes - callbacks will see correct matcher state
6,425✔
467
        if (tagName !== tagExp && attrExpPresent) {
6,480✔
468
          // Build attributes (returns prefixed attributes for the tree)
995✔
469
          // Note: buildAttributesMap now internally updates the matcher with raw attributes
995✔
470
          prefixedAttrs = this.buildAttributesMap(tagExp, this.matcher, tagName);
995✔
471

995✔
472
          if (prefixedAttrs) {
995✔
473
            // Extract raw attributes (without prefix) for our use
775✔
474
            //TODO: seems a performance overhead
775✔
475
            rawAttrs = extractRawAttributes(prefixedAttrs, options);
775✔
476
          }
775✔
477
        }
995✔
478

6,410✔
479
        // Now check if this is a stop node (after attributes are set)
6,410✔
480
        if (tagName !== xmlObj.tagname) {
6,410✔
481
          this.isCurrentNodeStopNode = this.isItStopNode();
6,410✔
482
        }
6,410✔
483

6,410✔
484
        const startIndex = i;
6,410✔
485
        if (this.isCurrentNodeStopNode) {
6,480✔
486
          let tagContent = "";
1,230✔
487

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

1,230✔
505
          const childNode = new xmlNode(tagName);
1,230✔
506

1,230✔
507
          if (prefixedAttrs) {
1,230✔
508
            childNode[":@"] = prefixedAttrs;
20✔
509
          }
20✔
510

1,230✔
511
          // For stop nodes, store raw content as-is without any processing
1,230✔
512
          childNode.add(options.textNodeName, tagContent);
1,230✔
513

1,230✔
514
          this.matcher.pop(); // Pop the stop node tag
1,230✔
515
          this.isCurrentNodeStopNode = false; // Reset flag
1,230✔
516

1,230✔
517
          this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
1,230✔
518
        } else {
6,480✔
519
          //selfClosing tag
5,180✔
520
          if (isSelfClosing) {
5,180✔
521
            ({ tagName, tagExp } = transformTagName(options.transformTagName, tagName, tagExp, options));
285✔
522

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

4,680✔
551
            if (prefixedAttrs) {
4,685✔
552
              childNode[":@"] = prefixedAttrs;
560✔
553
            }
560✔
554
            this.addChild(currentNode, childNode, this.readonlyMatcher, startIndex);
4,680✔
555
            currentNode = childNode;
4,680✔
556
          }
4,680✔
557
          textData = "";
4,965✔
558
          i = closeIndex;
4,965✔
559
        }
4,965✔
560
      }
6,480✔
561
    } else {
326,245✔
562
      textData += xmlData[i];
314,920✔
563
    }
314,920✔
564
  }
326,245✔
565
  return xmlObj.child;
1,565✔
566
}
1,730✔
567

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

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

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

5,003,335✔
593
  if (!entityConfig || !entityConfig.enabled) {
5,003,335✔
594
    return val;
60✔
595
  }
60✔
596

5,003,275✔
597
  // Check if tag is allowed to contain entities
5,003,275✔
598
  if (entityConfig.allowedTags) {
5,003,335✔
599
    const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
30!
600
    const allowed = Array.isArray(entityConfig.allowedTags)
30✔
601
      ? entityConfig.allowedTags.includes(tagName)
30✔
602
      : entityConfig.allowedTags(tagName, jPathOrMatcher);
30!
603

30✔
604
    if (!allowed) {
30✔
605
      return val;
10✔
606
    }
10✔
607
  }
30✔
608

5,003,265✔
609
  // Apply custom tag filter if provided
5,003,265✔
610
  if (entityConfig.tagFilter) {
5,003,335✔
611
    const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
25!
612
    if (!entityConfig.tagFilter(tagName, jPathOrMatcher)) {
25✔
613
      return val; // Skip based on custom filter
15✔
614
    }
15✔
615
  }
25✔
616

5,003,250✔
617
  return this.entityDecoder.decode(val);
5,003,250✔
618
}
5,003,335✔
619

5✔
620

5✔
621
function saveTextToParentTag(textData, parentNode, matcher, isLeafNode) {
7,795✔
622
  if (textData) { //store previously collected data as textNode
7,795✔
623
    if (isLeafNode === undefined) isLeafNode = parentNode.child.length === 0
6,530✔
624

6,530✔
625
    textData = this.parseTextData(textData,
6,530✔
626
      parentNode.tagname,
6,530✔
627
      matcher,
6,530✔
628
      false,
6,530✔
629
      parentNode[":@"] ? Object.keys(parentNode[":@"]).length !== 0 : false,
6,530!
630
      isLeafNode);
6,530✔
631

6,530✔
632
    if (textData !== undefined && textData !== "")
6,530✔
633
      parentNode.add(this.options.textNodeName, textData);
6,530✔
634
    textData = "";
6,495✔
635
  }
6,495✔
636
  return textData;
7,760✔
637
}
7,795✔
638

5✔
639
/**
5✔
640
 * @param {Array<Expression>} stopNodeExpressions - Array of compiled Expression objects
5✔
641
 * @param {Matcher} matcher - Current path matcher
5✔
642
 */
5✔
643
function isItStopNode() {
6,410✔
644
  if (this.stopNodeExpressionsSet.size === 0) return false;
6,410✔
645

2,230✔
646
  return this.matcher.matchesAny(this.stopNodeExpressionsSet);
2,230✔
647
}
6,410✔
648

5✔
649
/**
5✔
650
 * Returns the tag Expression and where it is ending handling single-double quotes situation
5✔
651
 * @param {string} xmlData 
5✔
652
 * @param {number} i starting index
5✔
653
 * @returns 
5✔
654
 */
5✔
655
function tagExpWithClosingIndex(xmlData, i, closingChar = ">") {
6,930✔
656
  //TODO: ignore boolean attributes in tag expression
6,930✔
657
  //TODO: if ignore attributes, dont read full attribute expression but the end. But read for xml declaration
6,930✔
658
  let attrBoundary = 0;
6,930✔
659
  const len = xmlData.length;
6,930✔
660
  const closeCode0 = closingChar.charCodeAt(0);
6,930✔
661
  const closeCode1 = closingChar.length > 1 ? closingChar.charCodeAt(1) : -1;
6,930✔
662

6,930✔
663
  let result = '';
6,930✔
664
  let segmentStart = i;
6,930✔
665

6,930✔
666
  for (let index = i; index < len; index++) {
6,930✔
667
    const code = xmlData.charCodeAt(index);
30,068,910✔
668

30,068,910✔
669
    if (attrBoundary) {
30,068,910✔
670
      if (code === attrBoundary) attrBoundary = 0;
10,014,735✔
671
    } else if (code === 34 || code === 39) { // " or '
30,068,910✔
672
      attrBoundary = code;
5,001,630✔
673
    } else if (code === closeCode0) {
20,054,175✔
674
      if (closeCode1 !== -1) {
7,185✔
675
        if (xmlData.charCodeAt(index + 1) === closeCode1) {
530✔
676
          result += xmlData.substring(segmentStart, index);
260✔
677
          return { data: result, index };
260✔
678
        }
260✔
679
      } else {
7,185✔
680
        result += xmlData.substring(segmentStart, index);
6,655✔
681
        return { data: result, index };
6,655✔
682
      }
6,655✔
683
    } else if (code === 9 && !attrBoundary) { // \t - only replace with space outside attribute values
15,052,545✔
684
      // Flush accumulated segment, add space, start new segment
25✔
685
      result += xmlData.substring(segmentStart, index) + ' ';
25✔
686
      segmentStart = index + 1;
25✔
687
    }
25✔
688
  }
30,068,910✔
689
}
6,930✔
690

5✔
691
function findClosingIndex(xmlData, str, i, errMsg) {
4,365✔
692
  const closingIndex = xmlData.indexOf(str, i);
4,365✔
693
  if (closingIndex === -1) {
4,365✔
694
    throw new Error(errMsg)
15✔
695
  } else {
4,365✔
696
    return closingIndex + str.length - 1;
4,350✔
697
  }
4,350✔
698
}
4,365✔
699

5✔
700
function findClosingChar(xmlData, char, i, errMsg) {
1,380✔
701
  const closingIndex = xmlData.indexOf(char, i);
1,380✔
702
  if (closingIndex === -1) throw new Error(errMsg);
1,380!
703
  return closingIndex; // no offset needed
1,380✔
704
}
1,380✔
705

5✔
706
function readTagExp(xmlData, i, removeNSPrefix, closingChar = ">") {
6,930✔
707
  const result = tagExpWithClosingIndex(xmlData, i + 1, closingChar);
6,930✔
708
  if (!result) return;
6,930✔
709
  let tagExp = result.data;
6,915✔
710
  const closeIndex = result.index;
6,915✔
711
  const separatorIndex = tagExp.search(/\s/);
6,915✔
712
  let tagName = tagExp;
6,915✔
713
  let attrExpPresent = true;
6,915✔
714
  if (separatorIndex !== -1) {//separate tag name and attributes expression
6,930✔
715
    tagName = tagExp.substring(0, separatorIndex);
1,270✔
716
    tagExp = tagExp.substring(separatorIndex + 1).trimStart();
1,270✔
717
  }
1,270✔
718

6,915✔
719
  const rawTagName = tagName;
6,915✔
720
  if (removeNSPrefix) {
6,930✔
721
    const colonIndex = tagName.indexOf(":");
140✔
722
    if (colonIndex !== -1) {
140✔
723
      tagName = tagName.substr(colonIndex + 1);
70✔
724
      attrExpPresent = tagName !== result.data.substr(colonIndex + 1);
70✔
725
    }
70✔
726
  }
140✔
727

6,915✔
728
  return {
6,915✔
729
    tagName: tagName,
6,915✔
730
    tagExp: tagExp,
6,915✔
731
    closeIndex: closeIndex,
6,915✔
732
    attrExpPresent: attrExpPresent,
6,915✔
733
    rawTagName: rawTagName,
6,915✔
734
  }
6,915✔
735
}
6,930✔
736
/**
5✔
737
 * find paired tag for a stop node
5✔
738
 * @param {string} xmlData 
5✔
739
 * @param {string} tagName 
5✔
740
 * @param {number} i 
5✔
741
 */
5✔
742
function readStopNodeData(xmlData, tagName, i) {
1,215✔
743
  const startIndex = i;
1,215✔
744
  // Starting at 1 since we already have an open tag
1,215✔
745
  let openTagCount = 1;
1,215✔
746

1,215✔
747
  const xmllen = xmlData.length;
1,215✔
748
  for (; i < xmllen; i++) {
1,215✔
749
    if (xmlData[i] === "<") {
16,105✔
750
      const c1 = xmlData.charCodeAt(i + 1);
1,570✔
751
      if (c1 === 47) {//close tag '/'
1,570✔
752
        const closeIndex = findClosingChar(xmlData, ">", i, `${tagName} is not closed`);
1,380✔
753
        let closeTagName = xmlData.substring(i + 2, closeIndex).trim();
1,380✔
754
        if (closeTagName === tagName) {
1,380✔
755
          openTagCount--;
1,225✔
756
          if (openTagCount === 0) {
1,225✔
757
            return {
1,215✔
758
              tagContent: xmlData.substring(startIndex, i),
1,215✔
759
              i: closeIndex
1,215✔
760
            }
1,215✔
761
          }
1,215✔
762
        }
1,225✔
763
        i = closeIndex;
165✔
764
      } else if (c1 === 63) { //?
1,570!
765
        const closeIndex = findClosingIndex(xmlData, "?>", i + 1, "StopNode is not closed.")
×
766
        i = closeIndex;
×
767
      } else if (c1 === 33
190✔
768
        && xmlData.charCodeAt(i + 2) === 45
190✔
769
        && xmlData.charCodeAt(i + 3) === 45) { // '!--'
190✔
770
        const closeIndex = findClosingIndex(xmlData, "-->", i + 3, "StopNode is not closed.")
5✔
771
        i = closeIndex;
5✔
772
      } else if (c1 === 33
190✔
773
        && xmlData.charCodeAt(i + 2) === 91) { // '!['
185✔
774
        const closeIndex = findClosingIndex(xmlData, "]]>", i, "StopNode is not closed.") - 2;
5✔
775
        i = closeIndex;
5✔
776
      } else {
185✔
777
        const tagData = readTagExp(xmlData, i, false)
180✔
778

180✔
779
        if (tagData) {
180✔
780
          const openTagName = tagData && tagData.tagName;
175✔
781
          if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length - 1] !== "/") {
175✔
782
            openTagCount++;
10✔
783
          }
10✔
784
          i = tagData.closeIndex;
175✔
785
        }
175✔
786
      }
180✔
787
    }
1,570✔
788
  }//end for loop
1,215✔
789
}
1,215✔
790

5✔
791
function parseValue(val, shouldParse, options) {
5,003,280✔
792
  if (shouldParse && typeof val === 'string') {
5,003,280✔
793
    //console.log(options)
2,100✔
794
    const newval = val.trim();
2,100✔
795
    if (newval === 'true') return true;
2,100✔
796
    else if (newval === 'false') return false;
2,065✔
797
    else return toNumber(val, options);
2,050✔
798
  } else {
5,003,280✔
799
    if (isExist(val)) {
5,001,180✔
800
      return val;
5,001,180✔
801
    } else {
5,001,180!
802
      return '';
×
803
    }
×
804
  }
5,001,180✔
805
}
5,003,280✔
806

5✔
807
function fromCodePoint(str, base, prefix) {
×
808
  const codePoint = Number.parseInt(str, base);
×
809

×
810
  if (codePoint >= 0 && codePoint <= 0x10FFFF) {
×
811
    return String.fromCodePoint(codePoint);
×
812
  } else {
×
813
    return prefix + str + ";";
×
814
  }
×
815
}
×
816

5✔
817
function transformTagName(fn, tagName, tagExp, options) {
10,865✔
818
  if (fn) {
10,865✔
819
    const newTagName = fn(tagName);
160✔
820
    if (tagExp === tagName) {
160✔
821
      tagExp = newTagName
55✔
822
    }
55✔
823
    tagName = newTagName;
160✔
824
  }
160✔
825
  tagName = sanitizeName(tagName, options);
10,865✔
826
  return { tagName, tagExp };
10,865✔
827
}
10,865✔
828

5✔
829

5✔
830

5✔
831
function sanitizeName(name, options) {
5,012,410✔
832
  if (criticalProperties.includes(name)) {
5,012,410✔
833
    throw new Error(`[SECURITY] Invalid name: "${name}" is a reserved JavaScript keyword that could cause prototype pollution`);
30✔
834
  } else if (DANGEROUS_PROPERTY_NAMES.includes(name)) {
5,012,410✔
835
    return options.onDangerousProperty(name);
105✔
836
  }
105✔
837
  return name;
5,012,275✔
838
}
5,012,410✔
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