fast-xml-parser 4.1.2 → 4.1.4

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,16 @@
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.1.4 / 2023-04-08**
4
+ * update typings to let user create XMLBuilder instance without options (#556) (By [Patrick](https://github.com/omggga))
5
+ * fix: IsArray option isn't parsing tags with 0 as value correctly #490 (#557) (By [Aleksandr Murashkin](https://github.com/p-kuen))
6
+ * feature: support oneListGroup to group repeated children tags udder single group
7
+
8
+ **4.1.3 / 2023-02-26**
9
+ * fix #546: Support complex entity value
10
+
11
+ **4.1.2 / 2023-02-12**
12
+ * Security Fix
13
+
3
14
  **4.1.1 / 2023-02-03**
4
15
  * Fix #540: ignoreAttributes breaks unpairedTags
5
16
  * Refactor XML builder code
package/README.md CHANGED
@@ -54,6 +54,7 @@ Check [ThankYouBackers](https://github.com/NaturalIntelligence/ThankYouBackers)
54
54
  <a href="http://www.magento.com/" title="Magento" > <img src="https://avatars2.githubusercontent.com/u/168457" width="60px" ></a>
55
55
  <a href="https://github.com/SAP" title="SAP" > <img src="https://user-images.githubusercontent.com/7692328/204835214-d9d25b58-e3df-408d-87a3-c7d36b578ee4.png" width="60px" ></a>
56
56
  <a href="https://github.com/postmanlabs" title="postman" > <img src="https://user-images.githubusercontent.com/7692328/204835529-e9e290ad-696a-49ad-9d34-08e955704715.png" width="60px" ></a>
57
+ <a href="https://github.com/react-native-community" title="React Native Community" > <img src="https://avatars.githubusercontent.com/u/20269980?v=4" width="60px" ></a>
57
58
 
58
59
  Check the list of all known users [here](./USERs.md);
59
60
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-xml-parser",
3
- "version": "4.1.2",
3
+ "version": "4.1.4",
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": "^2.4.1",
56
+ "publish-please": "^5.5.2",
57
57
  "webpack": "^5.64.4",
58
58
  "webpack-cli": "^4.9.1"
59
59
  },
package/src/fxp.d.ts CHANGED
@@ -65,6 +65,7 @@ type XmlBuilderOptions = {
65
65
  tagValueProcessor: (name: string, value: unknown) => string;
66
66
  attributeValueProcessor: (name: string, value: unknown) => string;
67
67
  processEntities: boolean;
68
+ oneListGroup: boolean;
68
69
  };
69
70
  type XmlBuilderOptionsOptional = Partial<XmlBuilderOptions>;
70
71
 
@@ -94,6 +95,6 @@ export class XMLValidator{
94
95
  static validate( xmlData: string, options?: validationOptionsOptional): true | ValidationError;
95
96
  }
96
97
  export class XMLBuilder {
97
- constructor(options: XmlBuilderOptionsOptional);
98
+ constructor(options?: XmlBuilderOptionsOptional);
98
99
  build(jObj: any): any;
99
100
  }
@@ -33,6 +33,7 @@ const defaultOptions = {
33
33
  stopNodes: [],
34
34
  // transformTagName: false,
35
35
  // transformAttributeName: false,
36
+ oneListGroup: false
36
37
  };
37
38
 
38
39
  function Builder(options) {
@@ -103,6 +104,7 @@ Builder.prototype.j2x = function(jObj, level) {
103
104
  } else if (Array.isArray(jObj[key])) {
104
105
  //repeated nodes
105
106
  const arrLen = jObj[key].length;
107
+ let listTagVal = "";
106
108
  for (let j = 0; j < arrLen; j++) {
107
109
  const item = jObj[key][j];
108
110
  if (typeof item === 'undefined') {
@@ -112,11 +114,19 @@ Builder.prototype.j2x = function(jObj, level) {
112
114
  else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
113
115
  // val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
114
116
  } else if (typeof item === 'object') {
115
- val += this.processTextOrObjNode(item, key, level)
117
+ if(this.options.oneListGroup ){
118
+ listTagVal += this.j2x(item, level + 1).val;
119
+ }else{
120
+ listTagVal += this.processTextOrObjNode(item, key, level)
121
+ }
116
122
  } else {
117
- val += this.buildTextValNode(item, key, '', level);
123
+ listTagVal += this.buildTextValNode(item, key, '', level);
118
124
  }
119
125
  }
126
+ if(this.options.oneListGroup){
127
+ listTagVal = this.buildObjectNode(listTagVal, key, '', level);
128
+ }
129
+ val += listTagVal;
120
130
  } else {
121
131
  //nested node
122
132
  if (this.options.attributesGroupName && key === this.options.attributesGroupName) {
@@ -11,80 +11,34 @@ function readDocType(xmlData, i){
11
11
  {
12
12
  i = i+9;
13
13
  let angleBracketsCount = 1;
14
- let hasBody = false, entity = false, comment = false;
14
+ let hasBody = false, comment = false;
15
15
  let exp = "";
16
16
  for(;i<xmlData.length;i++){
17
- if (xmlData[i] === '<' && !comment) {
18
- if( hasBody &&
19
- xmlData[i+1] === '!' &&
20
- xmlData[i+2] === 'E' &&
21
- xmlData[i+3] === 'N' &&
22
- xmlData[i+4] === 'T' &&
23
- xmlData[i+5] === 'I' &&
24
- xmlData[i+6] === 'T' &&
25
- xmlData[i+7] === 'Y'
26
- ){
27
- i += 7;
28
- entity = true;
29
- }else if( hasBody &&
30
- xmlData[i+1] === '!' &&
31
- xmlData[i+2] === 'E' &&
32
- xmlData[i+3] === 'L' &&
33
- xmlData[i+4] === 'E' &&
34
- xmlData[i+5] === 'M' &&
35
- xmlData[i+6] === 'E' &&
36
- xmlData[i+7] === 'N' &&
37
- xmlData[i+8] === 'T'
38
- ){
39
- //Not supported
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;
66
- }else if( //comment
67
- xmlData[i+1] === '!' &&
68
- xmlData[i+2] === '-' &&
69
- xmlData[i+3] === '-'
70
- ){
71
- comment = true;
72
- }else{
73
- throw new Error("Invalid DOCTYPE");
17
+ if (xmlData[i] === '<' && !comment) { //Determine the tag type
18
+ if( hasBody && isEntity(xmlData, i)){
19
+ i += 7;
20
+ [entityName, val,i] = readEntityExp(xmlData,i+1);
21
+ if(val.indexOf("&") === -1) //Parameter entities are not supported
22
+ entities[ entityName ] = {
23
+ regx : RegExp( `&${entityName};`,"g"),
24
+ val: val
25
+ };
74
26
  }
27
+ else if( hasBody && isElement(xmlData, i)) i += 8;//Not supported
28
+ else if( hasBody && isAttlist(xmlData, i)) i += 8;//Not supported
29
+ else if( hasBody && isNotation(xmlData, i)) i += 9;//Not supported
30
+ else if( isComment) comment = true;
31
+ else throw new Error("Invalid DOCTYPE");
32
+
75
33
  angleBracketsCount++;
76
34
  exp = "";
77
- } else if (xmlData[i] === '>') {
35
+ } else if (xmlData[i] === '>') { //Read tag content
78
36
  if(comment){
79
37
  if( xmlData[i - 1] === "-" && xmlData[i - 2] === "-"){
80
38
  comment = false;
81
39
  angleBracketsCount--;
82
40
  }
83
41
  }else{
84
- if(entity) {
85
- parseEntityExp(exp, entities);
86
- entity = false;
87
- }
88
42
  angleBracketsCount--;
89
43
  }
90
44
  if (angleBracketsCount === 0) {
@@ -105,14 +59,85 @@ function readDocType(xmlData, i){
105
59
  return {entities, i};
106
60
  }
107
61
 
108
- const entityRegex = RegExp("^\\s([a-zA-z0-0]+)[ \t](['\"])([^&]+)\\2");
109
- function parseEntityExp(exp, entities){
110
- const match = entityRegex.exec(exp);
111
- if(match){
112
- entities[ match[1] ] = {
113
- regx : RegExp( `&${match[1]};`,"g"),
114
- val: match[3]
115
- };
62
+ function readEntityExp(xmlData,i){
63
+ //External entities are not supported
64
+ // <!ENTITY ext SYSTEM "http://normal-website.com" >
65
+
66
+ //Parameter entities are not supported
67
+ // <!ENTITY entityname "&anotherElement;">
68
+
69
+ //Internal entities are supported
70
+ // <!ENTITY entityname "replacement text">
71
+
72
+ //read EntityName
73
+ let entityName = "";
74
+ for (; i < xmlData.length && (xmlData[i] !== "'" && xmlData[i] !== '"' ); i++) {
75
+ // if(xmlData[i] === " ") continue;
76
+ // else
77
+ entityName += xmlData[i];
78
+ }
79
+ entityName = entityName.trim();
80
+ if(entityName.indexOf(" ") !== -1) throw new Error("External entites are not supported");
81
+
82
+ //read Entity Value
83
+ const startChar = xmlData[i++];
84
+ let val = ""
85
+ for (; i < xmlData.length && xmlData[i] !== startChar ; i++) {
86
+ val += xmlData[i];
116
87
  }
88
+ return [entityName, val, i];
117
89
  }
90
+
91
+ function isComment(xmlData, i){
92
+ if(xmlData[i+1] === '!' &&
93
+ xmlData[i+2] === '-' &&
94
+ xmlData[i+3] === '-') return true
95
+ return false
96
+ }
97
+ function isEntity(xmlData, i){
98
+ if(xmlData[i+1] === '!' &&
99
+ xmlData[i+2] === 'E' &&
100
+ xmlData[i+3] === 'N' &&
101
+ xmlData[i+4] === 'T' &&
102
+ xmlData[i+5] === 'I' &&
103
+ xmlData[i+6] === 'T' &&
104
+ xmlData[i+7] === 'Y') return true
105
+ return false
106
+ }
107
+ function isElement(xmlData, i){
108
+ if(xmlData[i+1] === '!' &&
109
+ xmlData[i+2] === 'E' &&
110
+ xmlData[i+3] === 'L' &&
111
+ xmlData[i+4] === 'E' &&
112
+ xmlData[i+5] === 'M' &&
113
+ xmlData[i+6] === 'E' &&
114
+ xmlData[i+7] === 'N' &&
115
+ xmlData[i+8] === 'T') return true
116
+ return false
117
+ }
118
+
119
+ function isAttlist(xmlData, i){
120
+ if(xmlData[i+1] === '!' &&
121
+ xmlData[i+2] === 'A' &&
122
+ xmlData[i+3] === 'T' &&
123
+ xmlData[i+4] === 'T' &&
124
+ xmlData[i+5] === 'L' &&
125
+ xmlData[i+6] === 'I' &&
126
+ xmlData[i+7] === 'S' &&
127
+ xmlData[i+8] === 'T') return true
128
+ return false
129
+ }
130
+ function isNotation(xmlData, i){
131
+ if(xmlData[i+1] === '!' &&
132
+ xmlData[i+2] === 'N' &&
133
+ xmlData[i+3] === 'O' &&
134
+ xmlData[i+4] === 'T' &&
135
+ xmlData[i+5] === 'A' &&
136
+ xmlData[i+6] === 'T' &&
137
+ xmlData[i+7] === 'I' &&
138
+ xmlData[i+8] === 'O' &&
139
+ xmlData[i+9] === 'N') return true
140
+ return false
141
+ }
142
+
118
143
  module.exports = readDocType;
@@ -94,8 +94,20 @@ function assignAttributes(obj, attrMap, jpath, options){
94
94
  }
95
95
 
96
96
  function isLeafTag(obj, options){
97
+ const { textNodeName } = options;
97
98
  const propCount = Object.keys(obj).length;
98
- if( propCount === 0 || (propCount === 1 && obj[options.textNodeName]) ) return true;
99
+
100
+ if (propCount === 0) {
101
+ return true;
102
+ }
103
+
104
+ if (
105
+ propCount === 1 &&
106
+ (obj[textNodeName] || typeof obj[textNodeName] === "boolean" || obj[textNodeName] === 0)
107
+ ) {
108
+ return true;
109
+ }
110
+
99
111
  return false;
100
112
  }
101
113
  exports.prettify = prettify;