fast-xml-parser 4.0.8 → 4.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
1
  Note: If you find missing information about particular minor version, that version must have been changed without any functional change in this library.
2
2
 
3
- **4.0.8 / 2022-03-18**
3
+ **4.0.10 / 2022-09-14**
4
+ * fix broken links in demo site (By [Yannick Lang](https://github.com/layaxx))
5
+ * fix #491: tagValueProcessor type definition (By [Andrea Francesco Speziale](https://github.com/andreafspeziale))
6
+ * Add jsdocs for tagValueProcessor
7
+
8
+
9
+ **4.0.9 / 2022-07-10**
10
+ * fix #470: stop-tag can have self-closing tag with same name
11
+ * fix #472: stopNode can have any special tag inside
12
+ * Allow !ATTLIST and !NOTATION with DOCTYPE
13
+ * Add transformTagName option to transform tag names when parsing (#469) (By [Erik Rothoff Andersson](https://github.com/erkie))
14
+
15
+ **4.0.8 / 2022-05-28**
4
16
  * Fix CDATA parsing returning empty string when value = 0 (#451) (By [ndelanou](https://github.com/ndelanou))
5
17
  * Fix stopNodes when same tag appears inside node (#456) (By [patrickshipe](https://github.com/patrickshipe))
6
18
  * fix #468: prettify own properties only
package/README.md CHANGED
@@ -34,7 +34,6 @@ Check [ThankYouBackers](https://github.com/NaturalIntelligence/ThankYouBackers)
34
34
  <a href="https://github.com/prettier" title="Prettier" > <img src="https://avatars0.githubusercontent.com/u/25822731" width="60px" ></a>
35
35
  <a href="http://brain.js.org/" title="brain.js" > <img src="https://avatars2.githubusercontent.com/u/23732838" width="60px" ></a>
36
36
  <a href="https://github.com/aws" title="AWS SDK" > <img src="https://avatars.githubusercontent.com/u/2232217" width="60px" ></a>
37
- <a href="#" title="NHS Connect" > <img src="https://avatars3.githubusercontent.com/u/20316669" width="60px" ></a>
38
37
  <a href="http://www.fda.gov/" title="Food and Drug Administration " > <img src="https://avatars2.githubusercontent.com/u/6471964" width="60px" ></a>
39
38
  <a href="http://www.magento.com/" title="Magento" > <img src="https://avatars2.githubusercontent.com/u/168457" width="60px" ></a>
40
39
 
@@ -50,7 +49,7 @@ Check the list of all known users [here](./USERs.md);
50
49
  * Parse XML to JS Object
51
50
  * Build XML from JS Object
52
51
  * Works with node packages, in browser, and in CLI (press try me button above for demo)
53
- * Faster than any pure JS implementation.
52
+ * Faster than any other pure JS implementation.
54
53
  * It can handle big files (tested up to 100mb).
55
54
  * Controlled parsing using various options
56
55
  * XML Entities, HTML entities, and DOCTYPE entites are supported.
@@ -121,7 +120,7 @@ Check lib folder for different browser bundles
121
120
  2. [XML Parser](./docs/v4/2.XMLparseOptions.md)
122
121
  3. [XML Builder](./docs/v4/3.XMLBuilder.md)
123
122
  4. [XML Validator](./docs/v4/4.XMLValidator.md)
124
- 5. [Entites](./docs/v4/5.Entities.md)
123
+ 5. [Entities](./docs/v4/5.Entities.md)
125
124
  6. [HTML Document Parsing](./docs/v4/6.HTMLParsing.md)
126
125
  7. [PI Tag processing](./docs/v4/7.PITags.md)
127
126
  ## Performance
@@ -191,4 +190,4 @@ Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com
191
190
  # License
192
191
  * MIT License
193
192
 
194
- ![Donate $5](static/img/donation_quote.png)
193
+ ![Donate $5](static/img/donation_quote.png)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-xml-parser",
3
- "version": "4.0.8",
3
+ "version": "4.0.10",
4
4
  "description": "Validate XML, Parse XML, Build XML without C/C++ based libraries",
5
5
  "main": "./src/fxp.js",
6
6
  "scripts": {
@@ -53,7 +53,7 @@
53
53
  "jasmine": "^3.6.4",
54
54
  "nyc": "^15.1.0",
55
55
  "prettier": "^1.19.1",
56
- "publish-please": "^5.5.2",
56
+ "publish-please": "^2.4.1",
57
57
  "webpack": "^5.64.4",
58
58
  "webpack-cli": "^4.9.1"
59
59
  },
package/src/fxp.d.ts CHANGED
@@ -11,7 +11,15 @@ type X2jOptions = {
11
11
  trimValues: boolean;
12
12
  cdataPropName: false | string;
13
13
  commentPropName: false | string;
14
- tagValueProcessor: (tagName: string, tagValue: string, jPath: string, hasAttributes: boolean, isLeafNode: boolean) => string;
14
+ /**
15
+ Control how tag value should be parsed. Called only if tag value is not empty
16
+
17
+ @returns {undefined|null} `undefined` or `null` to set original value.
18
+ @returns {unknown}
19
+ 1. Different value or value with different data type to set new value. <br>
20
+ 2. Same value to set parsed value if `parseTagValue: true`.
21
+ */
22
+ tagValueProcessor: (tagName: string, tagValue: string, jPath: string, hasAttributes: boolean, isLeafNode: boolean) => unknown;
15
23
  attributeValueProcessor: (attrName: string, attrValue: string, jPath: string) => string;
16
24
  numberParseOptions: strnumOptions;
17
25
  stopNodes: string[];
@@ -22,6 +30,7 @@ type X2jOptions = {
22
30
  htmlEntities: boolean;
23
31
  ignoreDeclaration: boolean;
24
32
  ignorePiTags: boolean;
33
+ transformTagName: ((tagName: string) => string) | false;
25
34
  };
26
35
  type strnumOptions = {
27
36
  hex: boolean;
@@ -30,7 +30,8 @@ const defaultOptions = {
30
30
  { regex: new RegExp("\"", "g"), val: "&quot;" }
31
31
  ],
32
32
  processEntities: true,
33
- stopNodes: []
33
+ stopNodes: [],
34
+ transformTagName: false,
34
35
  };
35
36
 
36
37
  function Builder(options) {
@@ -38,6 +38,31 @@ function readDocType(xmlData, i){
38
38
  ){
39
39
  //Not supported
40
40
  i += 8;
41
+ }else if( hasBody &&
42
+ xmlData[i+1] === '!' &&
43
+ xmlData[i+2] === 'A' &&
44
+ xmlData[i+3] === 'T' &&
45
+ xmlData[i+4] === 'T' &&
46
+ xmlData[i+5] === 'L' &&
47
+ xmlData[i+6] === 'I' &&
48
+ xmlData[i+7] === 'S' &&
49
+ xmlData[i+8] === 'T'
50
+ ){
51
+ //Not supported
52
+ i += 8;
53
+ }else if( hasBody &&
54
+ xmlData[i+1] === '!' &&
55
+ xmlData[i+2] === 'N' &&
56
+ xmlData[i+3] === 'O' &&
57
+ xmlData[i+4] === 'T' &&
58
+ xmlData[i+5] === 'A' &&
59
+ xmlData[i+6] === 'T' &&
60
+ xmlData[i+7] === 'I' &&
61
+ xmlData[i+8] === 'O' &&
62
+ xmlData[i+9] === 'N'
63
+ ){
64
+ //Not supported
65
+ i += 9;
41
66
  }else if( //comment
42
67
  xmlData[i+1] === '!' &&
43
68
  xmlData[i+2] === '-' &&
@@ -30,7 +30,8 @@ const defaultOptions = {
30
30
  processEntities: true,
31
31
  htmlEntities: false,
32
32
  ignoreDeclaration: false,
33
- ignorePiTags: false
33
+ ignorePiTags: false,
34
+ transformTagName: false,
34
35
  };
35
36
 
36
37
  const buildOptions = function(options) {
@@ -193,6 +193,10 @@ const parseXml = function(xmlData) {
193
193
  }
194
194
  }
195
195
 
196
+ if(this.options.transformTagName) {
197
+ tagName = this.options.transformTagName(tagName);
198
+ }
199
+
196
200
  if(currentNode){
197
201
  textData = this.saveTextToParentTag(textData, currentNode, jPath);
198
202
  }
@@ -257,12 +261,15 @@ const parseXml = function(xmlData) {
257
261
 
258
262
  i = closeIndex + 2;
259
263
  }else {//Opening tag
260
-
261
264
  let result = readTagExp(xmlData,i, this. options.removeNSPrefix);
262
265
  let tagName= result.tagName;
263
266
  let tagExp = result.tagExp;
264
267
  let attrExpPresent = result.attrExpPresent;
265
268
  let closeIndex = result.closeIndex;
269
+
270
+ if (this.options.transformTagName) {
271
+ tagName = this.options.transformTagName(tagName);
272
+ }
266
273
 
267
274
  //save text as child node
268
275
  if (currentNode && textData) {
@@ -322,6 +329,10 @@ const parseXml = function(xmlData) {
322
329
  }else{
323
330
  tagExp = tagExp.substr(0, tagExp.length - 1);
324
331
  }
332
+
333
+ if(this.options.transformTagName) {
334
+ tagName = this.options.transformTagName(tagName);
335
+ }
325
336
 
326
337
  const childNode = new xmlNode(tagName);
327
338
  if(tagName !== tagExp && attrExpPresent){
@@ -491,7 +502,7 @@ function readStopNodeData(xmlData, tagName, i){
491
502
 
492
503
  for (; i < xmlData.length; i++) {
493
504
  if( xmlData[i] === "<"){
494
- if (xmlData[i+1] === "/") {
505
+ if (xmlData[i+1] === "/") {//close tag
495
506
  const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`);
496
507
  let closeTagName = xmlData.substring(i+2,closeIndex).trim();
497
508
  if(closeTagName === tagName){
@@ -504,12 +515,21 @@ function readStopNodeData(xmlData, tagName, i){
504
515
  }
505
516
  }
506
517
  i=closeIndex;
518
+ } else if(xmlData[i+1] === '?') {
519
+ const closeIndex = findClosingIndex(xmlData, "?>", i+1, "StopNode is not closed.")
520
+ i=closeIndex;
521
+ } else if(xmlData.substr(i + 1, 3) === '!--') {
522
+ const closeIndex = findClosingIndex(xmlData, "-->", i+3, "StopNode is not closed.")
523
+ i=closeIndex;
524
+ } else if(xmlData.substr(i + 1, 2) === '![') {
525
+ const closeIndex = findClosingIndex(xmlData, "]]>", i, "StopNode is not closed.") - 2;
526
+ i=closeIndex;
507
527
  } else {
508
528
  const tagData = readTagExp(xmlData, i, '>')
509
529
 
510
530
  if (tagData) {
511
531
  const openTagName = tagData && tagData.tagName;
512
- if (openTagName === tagName) {
532
+ if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length-1] !== "/") {
513
533
  openTagCount++;
514
534
  }
515
535
  i=tagData.closeIndex;