fast-xml-parser 4.0.0 → 4.0.1
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 +4 -0
- package/README.md +3 -4
- package/package.json +1 -1
- package/src/xmlbuilder/json2xml.js +35 -42
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
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.1 / 2022-01-08**
|
|
4
|
+
* fix builder for pi tag
|
|
5
|
+
* fix: support suppressBooleanAttrs by builder
|
|
6
|
+
|
|
3
7
|
**4.0.0 / 2022-01-06**
|
|
4
8
|
* Generating different combined, parser only, builder only, validator only browser bundles
|
|
5
9
|
* Keeping cjs modules as they can be imported in cjs and esm modules both. Otherwise refer `esm` branch.
|
package/README.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# [fast-xml-parser](https://www.npmjs.com/package/fast-xml-parser)
|
|
2
2
|
[](#backers) [](#sponsors) [](https://snyk.io/test/github/naturalintelligence/fast-xml-parser)
|
|
3
3
|
[![NPM quality][quality-image]][quality-url]
|
|
4
|
-
[](https://travis-ci.org/NaturalIntelligence/fast-xml-parser)
|
|
5
4
|
[](https://coveralls.io/github/NaturalIntelligence/fast-xml-parser?branch=master)
|
|
6
5
|
[<img src="https://img.shields.io/badge/Try-me-blue.svg?colorA=FFA500&colorB=0000FF" alt="Try me"/>](https://naturalintelligence.github.io/fast-xml-parser/)
|
|
7
6
|
[](https://npm.im/fast-xml-parser)
|
|
@@ -117,9 +116,9 @@ Check lib folder for different browser bundles
|
|
|
117
116
|
2. [XML Parser](./docs/v4/2.XMLparseOptions.md)
|
|
118
117
|
3. [XML Builder](./docs/v4/3.XMLBuilder.md)
|
|
119
118
|
4. [XML Validator](./docs/v4/4.XMLValidator.md)
|
|
120
|
-
5. [Entites](./docs/5.Entities.md)
|
|
121
|
-
6. [HTML Document Parsing](./docs/6.HTMLParsing.md)
|
|
122
|
-
7. [PI Tag processing](./docs/7.PITags.md)
|
|
119
|
+
5. [Entites](./docs/v4/5.Entities.md)
|
|
120
|
+
6. [HTML Document Parsing](./docs/v4/6.HTMLParsing.md)
|
|
121
|
+
7. [PI Tag processing](./docs/v4/7.PITags.md)
|
|
123
122
|
## Performance
|
|
124
123
|
|
|
125
124
|
### XML Parser
|
package/package.json
CHANGED
|
@@ -68,6 +68,7 @@ function Builder(options) {
|
|
|
68
68
|
this.buildObjectNode = buildObjectNode;
|
|
69
69
|
|
|
70
70
|
this.replaceEntitiesValue = replaceEntitiesValue;
|
|
71
|
+
this.buildAttrPairStr = buildAttrPairStr;
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
Builder.prototype.build = function(jObj) {
|
|
@@ -90,16 +91,16 @@ Builder.prototype.j2x = function(jObj, level) {
|
|
|
90
91
|
if (typeof jObj[key] === 'undefined') {
|
|
91
92
|
// supress undefined node
|
|
92
93
|
} else if (jObj[key] === null) {
|
|
93
|
-
val += this.indentate(level) + '<' + key + '
|
|
94
|
+
if(key[0] === "?") val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
|
|
95
|
+
else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
96
|
+
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
94
97
|
} else if (jObj[key] instanceof Date) {
|
|
95
98
|
val += this.buildTextNode(jObj[key], key, '', level);
|
|
96
99
|
} else if (typeof jObj[key] !== 'object') {
|
|
97
100
|
//premitive type
|
|
98
101
|
const attr = this.isAttribute(key);
|
|
99
102
|
if (attr) {
|
|
100
|
-
|
|
101
|
-
val = this.replaceEntitiesValue(val);
|
|
102
|
-
attrStr += ' ' + attr + '="' + val + '"';
|
|
103
|
+
attrStr += this.buildAttrPairStr(attr, '' + jObj[key]);
|
|
103
104
|
}else {
|
|
104
105
|
//tag value
|
|
105
106
|
if (key === this.options.textNodeName) {
|
|
@@ -117,7 +118,9 @@ Builder.prototype.j2x = function(jObj, level) {
|
|
|
117
118
|
if (typeof item === 'undefined') {
|
|
118
119
|
// supress undefined node
|
|
119
120
|
} else if (item === null) {
|
|
120
|
-
val += this.indentate(level) + '<' + key + '
|
|
121
|
+
if(key[0] === "?") val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
|
|
122
|
+
else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
123
|
+
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
121
124
|
} else if (typeof item === 'object') {
|
|
122
125
|
val += this.processTextOrObjNode(item, key, level)
|
|
123
126
|
} else {
|
|
@@ -130,9 +133,7 @@ Builder.prototype.j2x = function(jObj, level) {
|
|
|
130
133
|
const Ks = Object.keys(jObj[key]);
|
|
131
134
|
const L = Ks.length;
|
|
132
135
|
for (let j = 0; j < L; j++) {
|
|
133
|
-
|
|
134
|
-
val = this.replaceEntitiesValue(val);
|
|
135
|
-
attrStr += ' ' + Ks[j] + '="' + val + '"';
|
|
136
|
+
attrStr += this.buildAttrPairStr(Ks[j], '' + jObj[key][Ks[j]]);
|
|
136
137
|
}
|
|
137
138
|
} else {
|
|
138
139
|
val += this.processTextOrObjNode(jObj[key], key, level)
|
|
@@ -142,6 +143,14 @@ Builder.prototype.j2x = function(jObj, level) {
|
|
|
142
143
|
return {attrStr: attrStr, val: val};
|
|
143
144
|
};
|
|
144
145
|
|
|
146
|
+
function buildAttrPairStr(attrName, val){
|
|
147
|
+
val = this.options.attributeValueProcessor(attrName, '' + val);
|
|
148
|
+
val = this.replaceEntitiesValue(val);
|
|
149
|
+
if (this.options.suppressBooleanAttributes && val === "true") {
|
|
150
|
+
return ' ' + attrName;
|
|
151
|
+
} else return ' ' + attrName + '="' + val + '"';
|
|
152
|
+
}
|
|
153
|
+
|
|
145
154
|
function processTextOrObjNode (object, key, level) {
|
|
146
155
|
const result = this.j2x(object, level + 1);
|
|
147
156
|
if (object[this.options.textNodeName] !== undefined && Object.keys(object).length === 1) {
|
|
@@ -152,34 +161,24 @@ function processTextOrObjNode (object, key, level) {
|
|
|
152
161
|
}
|
|
153
162
|
|
|
154
163
|
function buildObjectNode(val, key, attrStr, level) {
|
|
164
|
+
let tagEndExp = '</' + key + this.tagEndChar;
|
|
165
|
+
let piClosingChar = "";
|
|
166
|
+
|
|
167
|
+
if(key[0] === "?") {
|
|
168
|
+
piClosingChar = "?";
|
|
169
|
+
tagEndExp = "";
|
|
170
|
+
}
|
|
171
|
+
|
|
155
172
|
if (attrStr && val.indexOf('<') === -1) {
|
|
156
173
|
return (
|
|
157
|
-
this.indentate(level) +
|
|
158
|
-
'<' +
|
|
159
|
-
key +
|
|
160
|
-
attrStr +
|
|
161
|
-
'>' +
|
|
174
|
+
this.indentate(level) + '<' + key + attrStr + piClosingChar + '>' +
|
|
162
175
|
val +
|
|
163
|
-
|
|
164
|
-
// + this.indentate(level)
|
|
165
|
-
'</' +
|
|
166
|
-
key +
|
|
167
|
-
this.tagEndChar
|
|
168
|
-
);
|
|
176
|
+
tagEndExp );
|
|
169
177
|
} else {
|
|
170
178
|
return (
|
|
171
|
-
this.indentate(level) +
|
|
172
|
-
'<' +
|
|
173
|
-
key +
|
|
174
|
-
attrStr +
|
|
175
|
-
this.tagEndChar +
|
|
179
|
+
this.indentate(level) + '<' + key + attrStr + piClosingChar + this.tagEndChar +
|
|
176
180
|
val +
|
|
177
|
-
|
|
178
|
-
this.indentate(level) +
|
|
179
|
-
'</' +
|
|
180
|
-
key +
|
|
181
|
-
this.tagEndChar
|
|
182
|
-
);
|
|
181
|
+
this.indentate(level) + tagEndExp );
|
|
183
182
|
}
|
|
184
183
|
}
|
|
185
184
|
|
|
@@ -187,8 +186,8 @@ function buildEmptyObjNode(val, key, attrStr, level) {
|
|
|
187
186
|
if (val !== '') {
|
|
188
187
|
return this.buildObjectNode(val, key, attrStr, level);
|
|
189
188
|
} else {
|
|
190
|
-
return
|
|
191
|
-
|
|
189
|
+
if(key[0] === "?") return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
|
|
190
|
+
else return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
|
|
192
191
|
}
|
|
193
192
|
}
|
|
194
193
|
|
|
@@ -197,16 +196,9 @@ function buildTextValNode(val, key, attrStr, level) {
|
|
|
197
196
|
textValue = this.replaceEntitiesValue(textValue);
|
|
198
197
|
|
|
199
198
|
return (
|
|
200
|
-
this.indentate(level) +
|
|
201
|
-
'<' +
|
|
202
|
-
key +
|
|
203
|
-
attrStr +
|
|
204
|
-
'>' +
|
|
199
|
+
this.indentate(level) + '<' + key + attrStr + '>' +
|
|
205
200
|
textValue +
|
|
206
|
-
'</' +
|
|
207
|
-
key +
|
|
208
|
-
this.tagEndChar
|
|
209
|
-
);
|
|
201
|
+
'</' + key + this.tagEndChar );
|
|
210
202
|
}
|
|
211
203
|
|
|
212
204
|
function replaceEntitiesValue(textValue){
|
|
@@ -225,7 +217,8 @@ function buildEmptyTextNode(val, key, attrStr, level) {
|
|
|
225
217
|
}else if (val !== '') {
|
|
226
218
|
return this.buildTextValNode(val, key, attrStr, level);
|
|
227
219
|
} else {
|
|
228
|
-
return
|
|
220
|
+
if(key[0] === "?") return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
|
|
221
|
+
else return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
|
|
229
222
|
}
|
|
230
223
|
}
|
|
231
224
|
|