fast-xml-parser 4.3.5 → 4.4.0

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,5 +1,13 @@
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.4.0 / 2024-05-18**
4
+ * fix #654: parse attribute list correctly for self closing stop node.
5
+ * fix: validator bug when closing tag is not opened. (#647) (By [Ryosuke Fukatani](https://github.com/RyosukeFukatani))
6
+ * fix #581: typings; return type of `tagValueProcessor` & `attributeValueProcessor` (#582) (By [monholm]())
7
+
8
+ **4.3.6 / 2024-03-16**
9
+ * Add support for parsing HTML numeric entities (#645) (By [Jonas Schade ](https://github.com/DerZade))
10
+
3
11
  **4.3.5 / 2024-02-24**
4
12
  * code for v5 is added for experimental use
5
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-xml-parser",
3
- "version": "4.3.5",
3
+ "version": "4.4.0",
4
4
  "description": "Validate XML, Parse XML, Build XML without C/C++ based libraries",
5
5
  "main": "./src/fxp.js",
6
6
  "scripts": {
package/src/fxp.d.ts CHANGED
@@ -345,7 +345,7 @@ type XmlBuilderOptions = {
345
345
  *
346
346
  * Defaults to `(tagName, val, jPath, hasAttributes, isLeafNode) => val`
347
347
  */
348
- tagValueProcessor?: (name: string, value: unknown) => string;
348
+ tagValueProcessor?: (name: string, value: unknown) => unknown;
349
349
 
350
350
  /**
351
351
  * Control how attribute value should be parsed
@@ -358,7 +358,7 @@ type XmlBuilderOptions = {
358
358
  *
359
359
  * Defaults to `(attrName, val, jPath) => val`
360
360
  */
361
- attributeValueProcessor?: (name: string, value: unknown) => string;
361
+ attributeValueProcessor?: (name: string, value: unknown) => unknown;
362
362
 
363
363
  /**
364
364
  * Whether to process default and DOCTYPE entities
@@ -13,6 +13,8 @@ const htmlEntities = {
13
13
  "copyright" : { regex: /&(copy|#169);/g, val: "©" },
14
14
  "reg" : { regex: /&(reg|#174);/g, val: "®" },
15
15
  "inr" : { regex: /&(inr|#8377);/g, val: "₹" },
16
+ "num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 10)) },
17
+ "num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 16)) },
16
18
  };
17
19
 
18
20
  class EntitiesParser{
@@ -1,5 +1,5 @@
1
1
 
2
- const JsArrBuilder = require("./OutputBuilders/JsArrBuilder");
2
+ const JsObjOutputBuilder = require("./OutputBuilders/JsObjBuilder");
3
3
 
4
4
  const defaultOptions = {
5
5
  preserveOrder: false,
@@ -17,12 +17,11 @@ const defaultOptions = {
17
17
  text: '#text'
18
18
  },
19
19
  separateTextProperty: false,
20
- valueParsers: []
21
20
  },
22
21
  attributes:{
23
22
  ignore: false,
24
23
  booleanType: true,
25
- entities: true
24
+ entities: true,
26
25
  },
27
26
 
28
27
  // select: ["img[src]"],
@@ -33,21 +32,13 @@ const defaultOptions = {
33
32
 
34
33
  select: [], // on('select', tag => tag ) will be called if match
35
34
  stop: [], //given tagPath will not be parsed. innerXML will be set as string value
36
- OutputBuilder: new JsArrBuilder(),
35
+ OutputBuilder: new JsObjOutputBuilder(),
37
36
  };
38
37
 
39
38
  const buildOptions = function(options) {
40
39
  const finalOptions = { ... defaultOptions};
41
- finalOptions.tags.valueParsers.push("trim");
42
- finalOptions.tags.valueParsers.push("entities");
43
- if(!this.preserveOrder)
44
- finalOptions.tags.valueParsers.push("join");
45
- finalOptions.tags.valueParsers.push("boolean");
46
- finalOptions.tags.valueParsers.push("number");
47
- finalOptions.tags.valueParsers.push("currency");
48
- finalOptions.tags.valueParsers.push("date");
49
40
  copyProperties(finalOptions,options)
50
- return finalOptions;
41
+ return finalOptions;
51
42
  };
52
43
 
53
44
  function copyProperties(target, source) {
@@ -7,7 +7,7 @@ class BaseOutputBuilder{
7
7
  if(this.options.onAttribute){
8
8
  //TODO: better to pass tag path
9
9
  const v = this.options.onAttribute(name, value, this.tagName);
10
- if(!v) this.attributes[v.name] = v.value;
10
+ if(v) this.attributes[v.name] = v.value;
11
11
  }else{
12
12
  name = this.options.attributes.prefix + name + this.options.attributes.suffix;
13
13
  this.attributes[name] = this.parseValue(value, this.options.attributes.valueParsers);
@@ -21,10 +21,12 @@ class BaseOutputBuilder{
21
21
  */
22
22
  parseValue = function(val, valParsers){
23
23
  for (let i = 0; i < valParsers.length; i++) {
24
- let valParser = this.registeredParsers[valParsers[i]];
24
+ let valParser = valParsers[i];
25
+ if(typeof valParser === "string"){
26
+ valParser = this.registeredParsers[valParser];
27
+ }
25
28
  if(valParser){
26
29
  val = valParser.parse(val);
27
- // if(!valParser.chainable) break;
28
30
  }
29
31
  }
30
32
  return val;
@@ -10,8 +10,8 @@ class OutputBuilder{
10
10
  this.registeredParsers[name] = parserInstance;
11
11
  }
12
12
 
13
- getInstance(){
14
- return new JsArrBuilder(this.options, this.registeredParsers);
13
+ getInstance(parserOptions){
14
+ return new JsArrBuilder(parserOptions, this.options, this.registeredParsers);
15
15
  }
16
16
  }
17
17
 
@@ -20,9 +20,10 @@ const BaseOutputBuilder = require("./BaseOutputBuilder");
20
20
 
21
21
  class JsArrBuilder extends BaseOutputBuilder{
22
22
 
23
- constructor(options,registeredParsers) {
23
+ constructor(parserOptions, options,registeredParsers) {
24
24
  super();
25
25
  this.tagsStack = [];
26
+ this.parserOptions = parserOptions;
26
27
  this.options = options;
27
28
  this.registeredParsers = registeredParsers;
28
29
 
@@ -10,8 +10,8 @@ class OutputBuilder{
10
10
  this.registeredParsers[name] = parserInstance;
11
11
  }
12
12
 
13
- getInstance(){
14
- return new JsMinArrBuilder(this.options, this.registeredParsers);
13
+ getInstance(parserOptions){
14
+ return new JsMinArrBuilder(parserOptions, this.options, this.registeredParsers);
15
15
  }
16
16
  }
17
17
 
@@ -20,9 +20,10 @@ const rootName = '^';
20
20
 
21
21
  class JsMinArrBuilder extends BaseOutputBuilder{
22
22
 
23
- constructor(options,registeredParsers) {
23
+ constructor(parserOptions, options,registeredParsers) {
24
24
  super();
25
25
  this.tagsStack = [];
26
+ this.parserOptions = parserOptions;
26
27
  this.options = options;
27
28
  this.registeredParsers = registeredParsers;
28
29
 
@@ -3,8 +3,8 @@
3
3
  const {buildOptions,registerCommonValueParsers} = require("./ParserOptionsBuilder");
4
4
 
5
5
  class OutputBuilder{
6
- constructor(options){
7
- this.options = buildOptions(options);
6
+ constructor(builderOptions){
7
+ this.options = buildOptions(builderOptions);
8
8
  this.registeredParsers = registerCommonValueParsers();
9
9
  }
10
10
 
@@ -12,8 +12,8 @@ class OutputBuilder{
12
12
  this.registeredParsers[name] = parserInstance;
13
13
  }
14
14
 
15
- getInstance(){
16
- return new JsObjBuilder(this.options, this.registeredParsers);
15
+ getInstance(parserOptions){
16
+ return new JsObjBuilder(parserOptions, this.options, this.registeredParsers);
17
17
  }
18
18
  }
19
19
 
@@ -22,11 +22,12 @@ const rootName = '^';
22
22
 
23
23
  class JsObjBuilder extends BaseOutputBuilder{
24
24
 
25
- constructor(options,registeredParsers) {
25
+ constructor(parserOptions, builderOptions,registeredParsers) {
26
26
  super();
27
27
  //hold the raw detail of a tag and sequence with reference to the output
28
28
  this.tagsStack = [];
29
- this.options = options;
29
+ this.parserOptions = parserOptions;
30
+ this.options = builderOptions;
30
31
  this.registeredParsers = registeredParsers;
31
32
 
32
33
  this.root = {};
@@ -75,13 +76,13 @@ class JsObjBuilder extends BaseOutputBuilder{
75
76
 
76
77
 
77
78
  let resultTag= {
78
- tagName: this.tagName,
79
+ tagName: tagName,
79
80
  value: value
80
81
  };
81
82
 
82
83
  if(this.options.onTagClose !== undefined){
83
84
  //TODO TagPathMatcher
84
- resultTag = this.options.onClose(this.tagName, value, this.textValue, new TagPathMatcher(this.tagsStack,node));
85
+ resultTag = this.options.onClose(tagName, value, this.textValue, new TagPathMatcher(this.tagsStack,node));
85
86
 
86
87
  if(!resultTag) return;
87
88
  }
@@ -36,22 +36,24 @@ const defaultOptions={
36
36
  ]
37
37
  }
38
38
  }
39
+
40
+ //TODO
41
+ const withJoin = ["trim","join", /*"entities",*/"number","boolean","currency"/*, "date"*/]
42
+ const withoutJoin = ["trim", /*"entities",*/"number","boolean","currency"/*, "date"*/]
43
+
39
44
  function buildOptions(options){
40
45
  //clone
41
46
  const finalOptions = { ... defaultOptions};
42
47
 
43
48
  //add config missed in cloning
44
- finalOptions.tags.valueParsers.push("trim")
45
- finalOptions.tags.valueParsers.push("boolean")
46
- finalOptions.tags.valueParsers.push("number")
47
- finalOptions.tags.valueParsers.push("currency")
49
+ finalOptions.tags.valueParsers.push(...withJoin)
50
+ if(!this.preserveOrder)
51
+ finalOptions.tags.valueParsers.push(...withoutJoin);
48
52
 
49
53
  //add config missed in cloning
50
- finalOptions.attributes.valueParsers.push("trim")
51
- finalOptions.attributes.valueParsers.push("boolean")
52
- finalOptions.attributes.valueParsers.push("number")
53
- finalOptions.attributes.valueParsers.push("currency")
54
+ finalOptions.attributes.valueParsers.push(...withJoin)
54
55
 
56
+ //override configuration
55
57
  copyProperties(finalOptions,options);
56
58
  return finalOptions;
57
59
  }
@@ -13,6 +13,8 @@ const htmlEntities = {
13
13
  "copyright" : { regex: /&(copy|#169);/g, val: "©" },
14
14
  "reg" : { regex: /&(reg|#174);/g, val: "®" },
15
15
  "inr" : { regex: /&(inr|#8377);/g, val: "₹" },
16
+ "num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 10)) },
17
+ "num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 16)) },
16
18
  };
17
19
 
18
20
  class EntitiesParser{
package/src/validator.js CHANGED
@@ -103,6 +103,8 @@ exports.validate = function (xmlData, options) {
103
103
  return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' doesn't have proper closing.", getLineNumberForPosition(xmlData, i));
104
104
  } else if (attrStr.trim().length > 0) {
105
105
  return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, tagStartPos));
106
+ } else if (tags.length === 0) {
107
+ return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' has not been opened.", getLineNumberForPosition(xmlData, tagStartPos));
106
108
  } else {
107
109
  const otg = tags.pop();
108
110
  if (tagName !== otg.tagName) {
@@ -40,6 +40,8 @@ class OrderedObjParser{
40
40
  "copyright" : { regex: /&(copy|#169);/g, val: "©" },
41
41
  "reg" : { regex: /&(reg|#174);/g, val: "®" },
42
42
  "inr" : { regex: /&(inr|#8377);/g, val: "₹" },
43
+ "num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 10)) },
44
+ "num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 16)) },
43
45
  };
44
46
  this.addExternalEntities = addExternalEntities;
45
47
  this.parseXml = parseXml;
@@ -309,10 +311,18 @@ const parseXml = function(xmlData) {
309
311
  let tagContent = "";
310
312
  //self-closing tag
311
313
  if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
314
+ if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
315
+ tagName = tagName.substr(0, tagName.length - 1);
316
+ jPath = jPath.substr(0, jPath.length - 1);
317
+ tagExp = tagName;
318
+ }else{
319
+ tagExp = tagExp.substr(0, tagExp.length - 1);
320
+ }
312
321
  i = result.closeIndex;
313
322
  }
314
323
  //unpaired tag
315
324
  else if(this.options.unpairedTags.indexOf(tagName) !== -1){
325
+
316
326
  i = result.closeIndex;
317
327
  }
318
328
  //normal tag