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

NaturalIntelligence / fast-xml-parser / 13817442089

12 Mar 2025 05:13PM UTC coverage: 98.946%. Remained the same
13817442089

Pull #734

github

web-flow
Merge 4a6d4de32 into af4f1d2d4
Pull Request #734: [WIP] ci: build with Node 18.x

1087 of 1120 branches covered (97.05%)

8640 of 8732 relevant lines covered (98.95%)

510597.48 hits per line

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

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

5✔
4
import {getAllMatches, isExist} from '../util.js';
5✔
5
import xmlNode from './xmlNode.js';
5✔
6
import readDocType from './DocTypeReader.js';
5✔
7
import toNumber from "strnum";
5✔
8
import getIgnoreAttributesFn from "../ignoreAttributes.js";
5✔
9

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

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

5✔
17
export default class OrderedObjParser{
5✔
18
  constructor(options){
5✔
19
    this.options = options;
825✔
20
    this.currentNode = null;
825✔
21
    this.tagsNodeStack = [];
825✔
22
    this.docTypeEntities = {};
825✔
23
    this.lastEntities = {
825✔
24
      "apos" : { regex: /&(apos|#39|#x27);/g, val : "'"},
825✔
25
      "gt" : { regex: /&(gt|#62|#x3E);/g, val : ">"},
825✔
26
      "lt" : { regex: /&(lt|#60|#x3C);/g, val : "<"},
825✔
27
      "quot" : { regex: /&(quot|#34|#x22);/g, val : "\""},
825✔
28
    };
825✔
29
    this.ampEntity = { regex: /&(amp|#38|#x26);/g, val : "&"};
825✔
30
    this.htmlEntities = {
825✔
31
      "space": { regex: /&(nbsp|#160);/g, val: " " },
825✔
32
      // "lt" : { regex: /&(lt|#60);/g, val: "<" },
825✔
33
      // "gt" : { regex: /&(gt|#62);/g, val: ">" },
825✔
34
      // "amp" : { regex: /&(amp|#38);/g, val: "&" },
825✔
35
      // "quot" : { regex: /&(quot|#34);/g, val: "\"" },
825✔
36
      // "apos" : { regex: /&(apos|#39);/g, val: "'" },
825✔
37
      "cent" : { regex: /&(cent|#162);/g, val: "¢" },
825✔
38
      "pound" : { regex: /&(pound|#163);/g, val: "£" },
825✔
39
      "yen" : { regex: /&(yen|#165);/g, val: "Â¥" },
825✔
40
      "euro" : { regex: /&(euro|#8364);/g, val: "€" },
825✔
41
      "copyright" : { regex: /&(copy|#169);/g, val: "©" },
825✔
42
      "reg" : { regex: /&(reg|#174);/g, val: "®" },
825✔
43
      "inr" : { regex: /&(inr|#8377);/g, val: "₹" },
825✔
44
      "num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => String.fromCodePoint(Number.parseInt(str, 10)) },
825✔
45
      "num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCodePoint(Number.parseInt(str, 16)) },
825✔
46
    };
825✔
47
    this.addExternalEntities = addExternalEntities;
825✔
48
    this.parseXml = parseXml;
825✔
49
    this.parseTextData = parseTextData;
825✔
50
    this.resolveNameSpace = resolveNameSpace;
825✔
51
    this.buildAttributesMap = buildAttributesMap;
825✔
52
    this.isItStopNode = isItStopNode;
825✔
53
    this.replaceEntitiesValue = replaceEntitiesValue;
825✔
54
    this.readStopNodeData = readStopNodeData;
825✔
55
    this.saveTextToParentTag = saveTextToParentTag;
825✔
56
    this.addChild = addChild;
825✔
57
    this.ignoreAttributesFn = getIgnoreAttributesFn(this.options.ignoreAttributes)
825✔
58
  }
825✔
59

5✔
60
}
5✔
61

5✔
62
function addExternalEntities(externalEntities){
825✔
63
  const entKeys = Object.keys(externalEntities);
825✔
64
  for (let i = 0; i < entKeys.length; i++) {
825✔
65
    const ent = entKeys[i];
15✔
66
    this.lastEntities[ent] = {
15✔
67
       regex: new RegExp("&"+ent+";","g"),
15✔
68
       val : externalEntities[ent]
15✔
69
    }
15✔
70
  }
15✔
71
}
825✔
72

5✔
73
/**
5✔
74
 * @param {string} val
5✔
75
 * @param {string} tagName
5✔
76
 * @param {string} jPath
5✔
77
 * @param {boolean} dontTrim
5✔
78
 * @param {boolean} hasAttributes
5✔
79
 * @param {boolean} isLeafNode
5✔
80
 * @param {boolean} escapeEntities
5✔
81
 */
5✔
82
function parseTextData(val, tagName, jPath, dontTrim, hasAttributes, isLeafNode, escapeEntities) {
5,190✔
83
  if (val !== undefined) {
5,190✔
84
    if (this.options.trimValues && !dontTrim) {
5,190✔
85
      val = val.trim();
4,790✔
86
    }
4,790✔
87
    if(val.length > 0){
5,190✔
88
      if(!escapeEntities) val = this.replaceEntitiesValue(val);
2,040✔
89
      
2,040✔
90
      const newval = this.options.tagValueProcessor(tagName, val, jPath, hasAttributes, isLeafNode);
2,040✔
91
      if(newval === null || newval === undefined){
2,040✔
92
        //don't parse
50✔
93
        return val;
50✔
94
      }else if(typeof newval !== typeof val || newval !== val){
2,040✔
95
        //overwrite
20✔
96
        return newval;
20✔
97
      }else if(this.options.trimValues){
1,990✔
98
        return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
1,910✔
99
      }else{
1,970✔
100
        const trimmedVal = val.trim();
60✔
101
        if(trimmedVal === val){
60✔
102
          return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
15✔
103
        }else{
60✔
104
          return val;
45✔
105
        }
45✔
106
      }
60✔
107
    }
2,040✔
108
  }
5,190✔
109
}
5,190✔
110

5✔
111
function resolveNameSpace(tagname) {
1,350✔
112
  if (this.options.removeNSPrefix) {
1,350✔
113
    const tags = tagname.split(':');
110✔
114
    const prefix = tagname.charAt(0) === '/' ? '/' : '';
110!
115
    if (tags[0] === 'xmlns') {
110✔
116
      return '';
35✔
117
    }
35✔
118
    if (tags.length === 2) {
110✔
119
      tagname = prefix + tags[1];
30✔
120
    }
30✔
121
  }
110✔
122
  return tagname;
1,315✔
123
}
1,350✔
124

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

5✔
129
function buildAttributesMap(attrStr, jPath, tagName) {
1,070✔
130
  if (this.options.ignoreAttributes !== true && typeof attrStr === 'string') {
1,070✔
131
    // attrStr = attrStr.replace(/\r?\n/g, ' ');
900✔
132
    //attrStr = attrStr || attrStr.trim();
900✔
133

900✔
134
    const matches = getAllMatches(attrStr, attrsRegx);
900✔
135
    const len = matches.length; //don't make it inline
900✔
136
    const attrs = {};
900✔
137
    for (let i = 0; i < len; i++) {
900✔
138
      const attrName = this.resolveNameSpace(matches[i][1]);
1,350✔
139
      if (this.ignoreAttributesFn(attrName, jPath)) {
1,350✔
140
        continue
50✔
141
      }
50✔
142
      let oldVal = matches[i][4];
1,300✔
143
      let aName = this.options.attributeNamePrefix + attrName;
1,300✔
144
      if (attrName.length) {
1,350✔
145
        if (this.options.transformAttributeName) {
1,265✔
146
          aName = this.options.transformAttributeName(aName);
20✔
147
        }
20✔
148
        if(aName === "__proto__") aName  = "#__proto__";
1,265!
149
        if (oldVal !== undefined) {
1,265✔
150
          if (this.options.trimValues) {
1,180✔
151
            oldVal = oldVal.trim();
1,155✔
152
          }
1,155✔
153
          oldVal = this.replaceEntitiesValue(oldVal);
1,180✔
154
          const newVal = this.options.attributeValueProcessor(attrName, oldVal, jPath);
1,180✔
155
          if(newVal === null || newVal === undefined){
1,180!
156
            //don't parse
×
157
            attrs[aName] = oldVal;
×
158
          }else if(typeof newVal !== typeof oldVal || newVal !== oldVal){
1,180!
159
            //overwrite
×
160
            attrs[aName] = newVal;
×
161
          }else{
1,180✔
162
            //parse
1,180✔
163
            attrs[aName] = parseValue(
1,180✔
164
              oldVal,
1,180✔
165
              this.options.parseAttributeValue,
1,180✔
166
              this.options.numberParseOptions
1,180✔
167
            );
1,180✔
168
          }
1,180✔
169
        } else if (this.options.allowBooleanAttributes) {
1,265✔
170
          attrs[aName] = true;
75✔
171
        }
75✔
172
      }
1,265✔
173
    }
1,350✔
174
    if (!Object.keys(attrs).length) {
900✔
175
      return;
95✔
176
    }
95✔
177
    if (this.options.attributesGroupName) {
900✔
178
      const attrCollection = {};
60✔
179
      attrCollection[this.options.attributesGroupName] = attrs;
60✔
180
      return attrCollection;
60✔
181
    }
60✔
182
    return attrs
745✔
183
  }
745✔
184
}
1,070✔
185

5✔
186
const parseXml = function(xmlData) {
5✔
187
  xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
825✔
188
  const xmlObj = new xmlNode('!xml');
825✔
189
  let currentNode = xmlObj;
825✔
190
  let textData = "";
825✔
191
  let jPath = "";
825✔
192
  for(let i=0; i< xmlData.length; i++){//for each char in XML data
825✔
193
    const ch = xmlData[i];
60,120✔
194
    if(ch === '<'){
60,120✔
195
      // const nextIndex = i+1;
6,760✔
196
      // const _2ndChar = xmlData[nextIndex];
6,760✔
197
      if( xmlData[i+1] === '/') {//Closing Tag
6,760✔
198
        const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.")
2,790✔
199
        let tagName = xmlData.substring(i+2,closeIndex).trim();
2,790✔
200

2,790✔
201
        if(this.options.removeNSPrefix){
2,790✔
202
          const colonIndex = tagName.indexOf(":");
115✔
203
          if(colonIndex !== -1){
115✔
204
            tagName = tagName.substr(colonIndex+1);
50✔
205
          }
50✔
206
        }
115✔
207

2,785✔
208
        if(this.options.transformTagName) {
2,790✔
209
          tagName = this.options.transformTagName(tagName);
50✔
210
        }
50✔
211

2,785✔
212
        if(currentNode){
2,785✔
213
          textData = this.saveTextToParentTag(textData, currentNode, jPath);
2,785✔
214
        }
2,785✔
215

2,785✔
216
        //check if last tag of nested tag was unpaired tag
2,785✔
217
        const lastTagName = jPath.substring(jPath.lastIndexOf(".")+1);
2,785✔
218
        if(tagName && this.options.unpairedTags.indexOf(tagName) !== -1 ){
2,790✔
219
          throw new Error(`Unpaired tag can not be used as closing tag: </${tagName}>`);
5✔
220
        }
5✔
221
        let propIndex = 0
2,780✔
222
        if(lastTagName && this.options.unpairedTags.indexOf(lastTagName) !== -1 ){
2,790✔
223
          propIndex = jPath.lastIndexOf('.', jPath.lastIndexOf('.')-1)
75✔
224
          this.tagsNodeStack.pop();
75✔
225
        }else{
2,790✔
226
          propIndex = jPath.lastIndexOf(".");
2,705✔
227
        }
2,705✔
228
        jPath = jPath.substring(0, propIndex);
2,780✔
229

2,780✔
230
        currentNode = this.tagsNodeStack.pop();//avoid recursion, set the parent tag scope
2,780✔
231
        textData = "";
2,780✔
232
        i = closeIndex;
2,780✔
233
      } else if( xmlData[i+1] === '?') {
6,760✔
234

235✔
235
        let tagData = readTagExp(xmlData,i, false, "?>");
235✔
236
        if(!tagData) throw new Error("Pi Tag is not closed.");
235✔
237

225✔
238
        textData = this.saveTextToParentTag(textData, currentNode, jPath);
225✔
239
        if( (this.options.ignoreDeclaration && tagData.tagName === "?xml") || this.options.ignorePiTags){
235✔
240

25✔
241
        }else{
235✔
242
  
200✔
243
          const childNode = new xmlNode(tagData.tagName);
200✔
244
          childNode.add(this.options.textNodeName, "");
200✔
245
          
200✔
246
          if(tagData.tagName !== tagData.tagExp && tagData.attrExpPresent){
200✔
247
            childNode[":@"] = this.buildAttributesMap(tagData.tagExp, jPath, tagData.tagName);
190✔
248
          }
190✔
249
          this.addChild(currentNode, childNode, jPath)
200✔
250

200✔
251
        }
200✔
252

225✔
253

225✔
254
        i = tagData.closeIndex + 1;
225✔
255
      } else if(xmlData.substr(i + 1, 3) === '!--') {
3,970✔
256
        const endIndex = findClosingIndex(xmlData, "-->", i+4, "Comment is not closed.")
60✔
257
        if(this.options.commentPropName){
60✔
258
          const comment = xmlData.substring(i + 4, endIndex - 2);
25✔
259

25✔
260
          textData = this.saveTextToParentTag(textData, currentNode, jPath);
25✔
261

25✔
262
          currentNode.add(this.options.commentPropName, [ { [this.options.textNodeName] : comment } ]);
25✔
263
        }
25✔
264
        i = endIndex;
55✔
265
      } else if( xmlData.substr(i + 1, 2) === '!D') {
3,735✔
266
        const result = readDocType(xmlData, i);
65✔
267
        this.docTypeEntities = result.entities;
65✔
268
        i = result.i;
65✔
269
      }else if(xmlData.substr(i + 1, 2) === '![') {
3,675✔
270
        const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
255✔
271
        const tagExp = xmlData.substring(i + 9,closeIndex);
255✔
272

255✔
273
        textData = this.saveTextToParentTag(textData, currentNode, jPath);
255✔
274

255✔
275
        let val = this.parseTextData(tagExp, currentNode.tagname, jPath, true, false, true, true);
255✔
276
        if(val == undefined) val = "";
255✔
277

250✔
278
        //cdata should be set even if it is 0 length string
250✔
279
        if(this.options.cdataPropName){
255✔
280
          currentNode.add(this.options.cdataPropName, [ { [this.options.textNodeName] : tagExp } ]);
65✔
281
        }else{
255✔
282
          currentNode.add(this.options.textNodeName, val);
185✔
283
        }
185✔
284
        
250✔
285
        i = closeIndex + 2;
250✔
286
      }else {//Opening tag
3,610✔
287
        let result = readTagExp(xmlData,i, this.options.removeNSPrefix);
3,355✔
288
        let tagName= result.tagName;
3,355✔
289
        const rawTagName = result.rawTagName;
3,355✔
290
        let tagExp = result.tagExp;
3,355✔
291
        let attrExpPresent = result.attrExpPresent;
3,355✔
292
        let closeIndex = result.closeIndex;
3,355✔
293

3,355✔
294
        if (this.options.transformTagName) {
3,355✔
295
          tagName = this.options.transformTagName(tagName);
50✔
296
        }
50✔
297
        
3,355✔
298
        //save text as child node
3,355✔
299
        if (currentNode && textData) {
3,355✔
300
          if(currentNode.tagname !== '!xml'){
2,425✔
301
            //when nested tag is found
2,200✔
302
            textData = this.saveTextToParentTag(textData, currentNode, jPath, false);
2,200✔
303
          }
2,200✔
304
        }
2,425✔
305

3,355✔
306
        //check if last tag was unpaired tag
3,355✔
307
        const lastTag = currentNode;
3,355✔
308
        if(lastTag && this.options.unpairedTags.indexOf(lastTag.tagname) !== -1 ){
3,355✔
309
          currentNode = this.tagsNodeStack.pop();
120✔
310
          jPath = jPath.substring(0, jPath.lastIndexOf("."));
120✔
311
        }
120✔
312
        if(tagName !== xmlObj.tagname){
3,355✔
313
          jPath += jPath ? "." + tagName : tagName;
3,355✔
314
        }
3,355✔
315
        if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) {
3,355✔
316
          let tagContent = "";
125✔
317
          //self-closing tag
125✔
318
          if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
125✔
319
            if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
10!
320
              tagName = tagName.substr(0, tagName.length - 1);
×
321
              jPath = jPath.substr(0, jPath.length - 1);
×
322
              tagExp = tagName;
×
323
            }else{
10✔
324
              tagExp = tagExp.substr(0, tagExp.length - 1);
10✔
325
            }
10✔
326
            i = result.closeIndex;
10✔
327
          }
10✔
328
          //unpaired tag
115✔
329
          else if(this.options.unpairedTags.indexOf(tagName) !== -1){
115✔
330
            
5✔
331
            i = result.closeIndex;
5✔
332
          }
5✔
333
          //normal tag
110✔
334
          else{
110✔
335
            //read until closing tag is found
110✔
336
            const result = this.readStopNodeData(xmlData, rawTagName, closeIndex + 1);
110✔
337
            if(!result) throw new Error(`Unexpected end of ${rawTagName}`);
110!
338
            i = result.i;
110✔
339
            tagContent = result.tagContent;
110✔
340
          }
110✔
341

125✔
342
          const childNode = new xmlNode(tagName);
125✔
343
          if(tagName !== tagExp && attrExpPresent){
125✔
344
            childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
45✔
345
          }
45✔
346
          if(tagContent) {
125✔
347
            tagContent = this.parseTextData(tagContent, tagName, jPath, true, attrExpPresent, true, true);
90✔
348
          }
90✔
349
          
125✔
350
          jPath = jPath.substr(0, jPath.lastIndexOf("."));
125✔
351
          childNode.add(this.options.textNodeName, tagContent);
125✔
352
          
125✔
353
          this.addChild(currentNode, childNode, jPath)
125✔
354
        }else{
3,355✔
355
  //selfClosing tag
3,230✔
356
          if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
3,230✔
357
            if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
235✔
358
              tagName = tagName.substr(0, tagName.length - 1);
80✔
359
              jPath = jPath.substr(0, jPath.length - 1);
80✔
360
              tagExp = tagName;
80✔
361
            }else{
235✔
362
              tagExp = tagExp.substr(0, tagExp.length - 1);
155✔
363
            }
155✔
364
            
235✔
365
            if(this.options.transformTagName) {
235!
366
              tagName = this.options.transformTagName(tagName);
×
367
            }
×
368

235✔
369
            const childNode = new xmlNode(tagName);
235✔
370
            if(tagName !== tagExp && attrExpPresent){
235✔
371
              childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
155✔
372
            }
155✔
373
            this.addChild(currentNode, childNode, jPath)
235✔
374
            jPath = jPath.substr(0, jPath.lastIndexOf("."));
235✔
375
          }
235✔
376
    //opening tag
2,995✔
377
          else{
2,995✔
378
            const childNode = new xmlNode( tagName);
2,995✔
379
            this.tagsNodeStack.push(currentNode);
2,995✔
380
            
2,995✔
381
            if(tagName !== tagExp && attrExpPresent){
2,995✔
382
              childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
680✔
383
            }
680✔
384
            this.addChild(currentNode, childNode, jPath)
2,995✔
385
            currentNode = childNode;
2,995✔
386
          }
2,995✔
387
          textData = "";
3,230✔
388
          i = closeIndex;
3,230✔
389
        }
3,230✔
390
      }
3,355✔
391
    }else{
60,120✔
392
      textData += xmlData[i];
53,360✔
393
    }
53,360✔
394
  }
60,120✔
395
  return xmlObj.child;
785✔
396
}
825✔
397

5✔
398
function addChild(currentNode, childNode, jPath){
3,555✔
399
  const result = this.options.updateTag(childNode.tagname, jPath, childNode[":@"])
3,555✔
400
  if(result === false){
3,555✔
401
  }else if(typeof result === "string"){
3,555✔
402
    childNode.tagname = result
3,530✔
403
    currentNode.addChild(childNode);
3,530✔
404
  }else{
3,530!
405
    currentNode.addChild(childNode);
×
406
  }
×
407
}
3,555✔
408

5✔
409
const replaceEntitiesValue = function(val){
5✔
410

2,900✔
411
  if(this.options.processEntities){
2,900✔
412
    for(let entityName in this.docTypeEntities){
2,850✔
413
      const entity = this.docTypeEntities[entityName];
2,528✔
414
      val = val.replace( entity.regx, entity.val);
2,528✔
415
    }
2,528✔
416
    for(let entityName in this.lastEntities){
2,850✔
417
      const entity = this.lastEntities[entityName];
13,693✔
418
      val = val.replace( entity.regex, entity.val);
13,693✔
419
    }
13,693✔
420
    if(this.options.htmlEntities){
2,850✔
421
      for(let entityName in this.htmlEntities){
175✔
422
        const entity = this.htmlEntities[entityName];
1,908✔
423
        val = val.replace( entity.regex, entity.val);
1,908✔
424
      }
1,908✔
425
    }
175✔
426
    val = val.replace( this.ampEntity.regex, this.ampEntity.val);
2,850✔
427
  }
2,850✔
428
  return val;
2,900✔
429
}
2,900✔
430
function saveTextToParentTag(textData, currentNode, jPath, isLeafNode) {
5,485✔
431
  if (textData) { //store previously collected data as textNode
5,485✔
432
    if(isLeafNode === undefined) isLeafNode = currentNode.child.length === 0
4,850✔
433
    
4,850✔
434
    textData = this.parseTextData(textData,
4,850✔
435
      currentNode.tagname,
4,850✔
436
      jPath,
4,850✔
437
      false,
4,850✔
438
      currentNode[":@"] ? Object.keys(currentNode[":@"]).length !== 0 : false,
4,850✔
439
      isLeafNode);
4,850✔
440

4,850✔
441
    if (textData !== undefined && textData !== "")
4,850✔
442
      currentNode.add(this.options.textNodeName, textData);
4,850✔
443
    textData = "";
4,850✔
444
  }
4,850✔
445
  return textData;
5,485✔
446
}
5,485✔
447

5✔
448
//TODO: use jPath to simplify the logic
5✔
449
/**
5✔
450
 * 
5✔
451
 * @param {string[]} stopNodes 
5✔
452
 * @param {string} jPath
5✔
453
 * @param {string} currentTagName 
5✔
454
 */
5✔
455
function isItStopNode(stopNodes, jPath, currentTagName){
3,355✔
456
  const allNodesExp = "*." + currentTagName;
3,355✔
457
  for (const stopNodePath in stopNodes) {
3,355✔
458
    const stopNodeExp = stopNodes[stopNodePath];
3,156✔
459
    if( allNodesExp === stopNodeExp || jPath === stopNodeExp  ) return true;
3,156✔
460
  }
3,156✔
461
  return false;
3,230✔
462
}
3,355✔
463

5✔
464
/**
5✔
465
 * Returns the tag Expression and where it is ending handling single-double quotes situation
5✔
466
 * @param {string} xmlData 
5✔
467
 * @param {number} i starting index
5✔
468
 * @returns 
5✔
469
 */
5✔
470
function tagExpWithClosingIndex(xmlData, i, closingChar = ">"){
3,740✔
471
  let attrBoundary;
3,740✔
472
  let tagExp = "";
3,740✔
473
  for (let index = i; index < xmlData.length; index++) {
3,740✔
474
    let ch = xmlData[index];
51,905✔
475
    if (attrBoundary) {
51,905✔
476
        if (ch === attrBoundary) attrBoundary = "";//reset
13,640✔
477
    } else if (ch === '"' || ch === "'") {
51,905✔
478
        attrBoundary = ch;
1,490✔
479
    } else if (ch === closingChar[0]) {
38,265✔
480
      if(closingChar[1]){
3,960✔
481
        if(xmlData[index + 1] === closingChar[1]){
460✔
482
          return {
225✔
483
            data: tagExp,
225✔
484
            index: index
225✔
485
          }
225✔
486
        }
225✔
487
      }else{
3,960✔
488
        return {
3,500✔
489
          data: tagExp,
3,500✔
490
          index: index
3,500✔
491
        }
3,500✔
492
      }
3,500✔
493
    } else if (ch === '\t') {
36,775✔
494
      ch = " "
20✔
495
    }
20✔
496
    tagExp += ch;
48,180✔
497
  }
48,180✔
498
}
3,740✔
499

5✔
500
function findClosingIndex(xmlData, str, i, errMsg){
3,365✔
501
  const closingIndex = xmlData.indexOf(str, i);
3,365✔
502
  if(closingIndex === -1){
3,365✔
503
    throw new Error(errMsg)
15✔
504
  }else{
3,365✔
505
    return closingIndex + str.length - 1;
3,350✔
506
  }
3,350✔
507
}
3,365✔
508

5✔
509
function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){
3,740✔
510
  const result = tagExpWithClosingIndex(xmlData, i+1, closingChar);
3,740✔
511
  if(!result) return;
3,740✔
512
  let tagExp = result.data;
3,725✔
513
  const closeIndex = result.index;
3,725✔
514
  const separatorIndex = tagExp.search(/\s/);
3,725✔
515
  let tagName = tagExp;
3,725✔
516
  let attrExpPresent = true;
3,725✔
517
  if(separatorIndex !== -1){//separate tag name and attributes expression
3,740✔
518
    tagName = tagExp.substring(0, separatorIndex);
1,140✔
519
    tagExp = tagExp.substring(separatorIndex + 1).trimStart();
1,140✔
520
  }
1,140✔
521

3,725✔
522
  const rawTagName = tagName;
3,725✔
523
  if(removeNSPrefix){
3,740✔
524
    const colonIndex = tagName.indexOf(":");
275✔
525
    if(colonIndex !== -1){
275✔
526
      tagName = tagName.substr(colonIndex+1);
65✔
527
      attrExpPresent = tagName !== result.data.substr(colonIndex + 1);
65✔
528
    }
65✔
529
  }
275✔
530

3,725✔
531
  return {
3,725✔
532
    tagName: tagName,
3,725✔
533
    tagExp: tagExp,
3,725✔
534
    closeIndex: closeIndex,
3,725✔
535
    attrExpPresent: attrExpPresent,
3,725✔
536
    rawTagName: rawTagName,
3,725✔
537
  }
3,725✔
538
}
3,740✔
539
/**
5✔
540
 * find paired tag for a stop node
5✔
541
 * @param {string} xmlData 
5✔
542
 * @param {string} tagName 
5✔
543
 * @param {number} i 
5✔
544
 */
5✔
545
function readStopNodeData(xmlData, tagName, i){
110✔
546
  const startIndex = i;
110✔
547
  // Starting at 1 since we already have an open tag
110✔
548
  let openTagCount = 1;
110✔
549

110✔
550
  for (; i < xmlData.length; i++) {
110✔
551
    if( xmlData[i] === "<"){ 
4,610✔
552
      if (xmlData[i+1] === "/") {//close tag
410✔
553
          const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`);
250✔
554
          let closeTagName = xmlData.substring(i+2,closeIndex).trim();
250✔
555
          if(closeTagName === tagName){
250✔
556
            openTagCount--;
115✔
557
            if (openTagCount === 0) {
115✔
558
              return {
110✔
559
                tagContent: xmlData.substring(startIndex, i),
110✔
560
                i : closeIndex
110✔
561
              }
110✔
562
            }
110✔
563
          }
115✔
564
          i=closeIndex;
140✔
565
        } else if(xmlData[i+1] === '?') { 
410!
566
          const closeIndex = findClosingIndex(xmlData, "?>", i+1, "StopNode is not closed.")
×
567
          i=closeIndex;
×
568
        } else if(xmlData.substr(i + 1, 3) === '!--') { 
160✔
569
          const closeIndex = findClosingIndex(xmlData, "-->", i+3, "StopNode is not closed.")
5✔
570
          i=closeIndex;
5✔
571
        } else if(xmlData.substr(i + 1, 2) === '![') { 
160✔
572
          const closeIndex = findClosingIndex(xmlData, "]]>", i, "StopNode is not closed.") - 2;
5✔
573
          i=closeIndex;
5✔
574
        } else {
155✔
575
          const tagData = readTagExp(xmlData, i, '>')
150✔
576

150✔
577
          if (tagData) {
150✔
578
            const openTagName = tagData && tagData.tagName;
145✔
579
            if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length-1] !== "/") {
145✔
580
              openTagCount++;
5✔
581
            }
5✔
582
            i=tagData.closeIndex;
145✔
583
          }
145✔
584
        }
150✔
585
      }
410✔
586
  }//end for loop
110✔
587
}
110✔
588

5✔
589
function parseValue(val, shouldParse, options) {
3,105✔
590
  if (shouldParse && typeof val === 'string') {
3,105✔
591
    //console.log(options)
2,145✔
592
    const newval = val.trim();
2,145✔
593
    if(newval === 'true' ) return true;
2,145✔
594
    else if(newval === 'false' ) return false;
2,110✔
595
    else return toNumber(val, options);
2,095✔
596
  } else {
3,105✔
597
    if (isExist(val)) {
960✔
598
      return val;
960✔
599
    } else {
960!
600
      return '';
×
601
    }
×
602
  }
960✔
603
}
3,105✔
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

© 2025 Coveralls, Inc