fast-xml-parser 5.3.7 → 5.3.9
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 +10 -0
- package/lib/fxbuilder.min.js +1 -1
- package/lib/fxbuilder.min.js.map +1 -1
- package/lib/fxp.cjs +1 -1
- package/lib/fxp.d.cts +14 -0
- package/lib/fxp.min.js +1 -1
- package/lib/fxp.min.js.map +1 -1
- package/lib/fxparser.min.js +1 -1
- package/lib/fxparser.min.js.map +1 -1
- package/lib/fxvalidator.min.js +1 -1
- package/lib/fxvalidator.min.js.map +1 -1
- package/package.json +1 -1
- package/src/fxp.d.ts +16 -0
- package/src/v6/EntitiesParser.js +87 -87
- package/src/v6/OptionsBuilder.js +10 -10
- package/src/v6/OutputBuilders/BaseOutputBuilder.js +23 -23
- package/src/v6/OutputBuilders/JsArrBuilder.js +29 -29
- package/src/v6/OutputBuilders/JsObjBuilder.js +39 -39
- package/src/v6/OutputBuilders/ParserOptionsBuilder.js +17 -17
- package/src/v6/XMLParser.js +22 -22
- package/src/v6/valueParsers/EntitiesParser.js +85 -85
- package/src/validator.js +34 -34
- package/src/xmlbuilder/json2xml.js +49 -49
- package/src/xmlbuilder/orderedJs2Xml.js +14 -3
- package/src/xmlparser/DocTypeReader.js +1 -1
- package/src/xmlparser/OptionsBuilder.js +2 -0
- package/src/xmlparser/OrderedObjParser.js +11 -0
- package/src/xmlparser/XMLParser.js +21 -21
- package/src/xmlparser/node2json.js +38 -34
- package/src/xmlparser/xmlNode.js +10 -10
package/src/validator.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import {getAllMatches, isName} from './util.js';
|
|
3
|
+
import { getAllMatches, isName } from './util.js';
|
|
4
4
|
|
|
5
5
|
const defaultOptions = {
|
|
6
6
|
allowBooleanAttributes: false, //A tag can have attributes without any value
|
|
@@ -24,19 +24,19 @@ export function validate(xmlData, options) {
|
|
|
24
24
|
// check for byte order mark (BOM)
|
|
25
25
|
xmlData = xmlData.substr(1);
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
for (let i = 0; i < xmlData.length; i++) {
|
|
29
29
|
|
|
30
|
-
if (xmlData[i] === '<' && xmlData[i+1] === '?') {
|
|
31
|
-
i+=2;
|
|
32
|
-
i = readPI(xmlData,i);
|
|
30
|
+
if (xmlData[i] === '<' && xmlData[i + 1] === '?') {
|
|
31
|
+
i += 2;
|
|
32
|
+
i = readPI(xmlData, i);
|
|
33
33
|
if (i.err) return i;
|
|
34
|
-
}else if (xmlData[i] === '<') {
|
|
34
|
+
} else if (xmlData[i] === '<') {
|
|
35
35
|
//starting of tag
|
|
36
36
|
//read until you reach to '>' avoiding any '>' in attribute value
|
|
37
37
|
let tagStartPos = i;
|
|
38
38
|
i++;
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
if (xmlData[i] === '!') {
|
|
41
41
|
i = readCommentAndCDATA(xmlData, i);
|
|
42
42
|
continue;
|
|
@@ -72,14 +72,14 @@ export function validate(xmlData, options) {
|
|
|
72
72
|
if (tagName.trim().length === 0) {
|
|
73
73
|
msg = "Invalid space after '<'.";
|
|
74
74
|
} else {
|
|
75
|
-
msg = "Tag '"+tagName+"' is an invalid name.";
|
|
75
|
+
msg = "Tag '" + tagName + "' is an invalid name.";
|
|
76
76
|
}
|
|
77
77
|
return getErrorObject('InvalidTag', msg, getLineNumberForPosition(xmlData, i));
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
const result = readAttributeStr(xmlData, i);
|
|
81
81
|
if (result === false) {
|
|
82
|
-
return getErrorObject('InvalidAttr', "Attributes for '"+tagName+"' have open quote.", getLineNumberForPosition(xmlData, i));
|
|
82
|
+
return getErrorObject('InvalidAttr', "Attributes for '" + tagName + "' have open quote.", getLineNumberForPosition(xmlData, i));
|
|
83
83
|
}
|
|
84
84
|
let attrStr = result.value;
|
|
85
85
|
i = result.index;
|
|
@@ -100,17 +100,17 @@ export function validate(xmlData, options) {
|
|
|
100
100
|
}
|
|
101
101
|
} else if (closingTag) {
|
|
102
102
|
if (!result.tagClosed) {
|
|
103
|
-
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' doesn't have proper closing.", getLineNumberForPosition(xmlData, i));
|
|
103
|
+
return getErrorObject('InvalidTag', "Closing tag '" + tagName + "' doesn't have proper closing.", getLineNumberForPosition(xmlData, i));
|
|
104
104
|
} else if (attrStr.trim().length > 0) {
|
|
105
|
-
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
105
|
+
return getErrorObject('InvalidTag', "Closing tag '" + tagName + "' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
106
106
|
} else if (tags.length === 0) {
|
|
107
|
-
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' has not been opened.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
107
|
+
return getErrorObject('InvalidTag', "Closing tag '" + tagName + "' has not been opened.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
108
108
|
} else {
|
|
109
109
|
const otg = tags.pop();
|
|
110
110
|
if (tagName !== otg.tagName) {
|
|
111
111
|
let openPos = getLineNumberForPosition(xmlData, otg.tagStartPos);
|
|
112
112
|
return getErrorObject('InvalidTag',
|
|
113
|
-
"Expected closing tag '"+otg.tagName+"' (opened in line "+openPos.line+", col "+openPos.col+") instead of closing tag '"+tagName+"'.",
|
|
113
|
+
"Expected closing tag '" + otg.tagName + "' (opened in line " + openPos.line + ", col " + openPos.col + ") instead of closing tag '" + tagName + "'.",
|
|
114
114
|
getLineNumberForPosition(xmlData, tagStartPos));
|
|
115
115
|
}
|
|
116
116
|
|
|
@@ -131,10 +131,10 @@ export function validate(xmlData, options) {
|
|
|
131
131
|
//if the root level has been reached before ...
|
|
132
132
|
if (reachedRoot === true) {
|
|
133
133
|
return getErrorObject('InvalidXml', 'Multiple possible root nodes found.', getLineNumberForPosition(xmlData, i));
|
|
134
|
-
} else if(options.unpairedTags.indexOf(tagName) !== -1){
|
|
134
|
+
} else if (options.unpairedTags.indexOf(tagName) !== -1) {
|
|
135
135
|
//don't push into stack
|
|
136
136
|
} else {
|
|
137
|
-
tags.push({tagName, tagStartPos});
|
|
137
|
+
tags.push({ tagName, tagStartPos });
|
|
138
138
|
}
|
|
139
139
|
tagFound = true;
|
|
140
140
|
}
|
|
@@ -148,10 +148,10 @@ export function validate(xmlData, options) {
|
|
|
148
148
|
i++;
|
|
149
149
|
i = readCommentAndCDATA(xmlData, i);
|
|
150
150
|
continue;
|
|
151
|
-
} else if (xmlData[i+1] === '?') {
|
|
151
|
+
} else if (xmlData[i + 1] === '?') {
|
|
152
152
|
i = readPI(xmlData, ++i);
|
|
153
153
|
if (i.err) return i;
|
|
154
|
-
} else{
|
|
154
|
+
} else {
|
|
155
155
|
break;
|
|
156
156
|
}
|
|
157
157
|
} else if (xmlData[i] === '&') {
|
|
@@ -159,7 +159,7 @@ export function validate(xmlData, options) {
|
|
|
159
159
|
if (afterAmp == -1)
|
|
160
160
|
return getErrorObject('InvalidChar', "char '&' is not expected.", getLineNumberForPosition(xmlData, i));
|
|
161
161
|
i = afterAmp;
|
|
162
|
-
}else{
|
|
162
|
+
} else {
|
|
163
163
|
if (reachedRoot === true && !isWhiteSpace(xmlData[i])) {
|
|
164
164
|
return getErrorObject('InvalidXml', "Extra text at the end", getLineNumberForPosition(xmlData, i));
|
|
165
165
|
}
|
|
@@ -170,28 +170,28 @@ export function validate(xmlData, options) {
|
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
} else {
|
|
173
|
-
if (
|
|
173
|
+
if (isWhiteSpace(xmlData[i])) {
|
|
174
174
|
continue;
|
|
175
175
|
}
|
|
176
|
-
return getErrorObject('InvalidChar', "char '"+xmlData[i]+"' is not expected.", getLineNumberForPosition(xmlData, i));
|
|
176
|
+
return getErrorObject('InvalidChar', "char '" + xmlData[i] + "' is not expected.", getLineNumberForPosition(xmlData, i));
|
|
177
177
|
}
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
if (!tagFound) {
|
|
181
181
|
return getErrorObject('InvalidXml', 'Start tag expected.', 1);
|
|
182
|
-
}else if (tags.length == 1) {
|
|
183
|
-
|
|
184
|
-
}else if (tags.length > 0) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
182
|
+
} else if (tags.length == 1) {
|
|
183
|
+
return getErrorObject('InvalidTag', "Unclosed tag '" + tags[0].tagName + "'.", getLineNumberForPosition(xmlData, tags[0].tagStartPos));
|
|
184
|
+
} else if (tags.length > 0) {
|
|
185
|
+
return getErrorObject('InvalidXml', "Invalid '" +
|
|
186
|
+
JSON.stringify(tags.map(t => t.tagName), null, 4).replace(/\r?\n/g, '') +
|
|
187
|
+
"' found.", { line: 1, col: 1 });
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
return true;
|
|
191
191
|
};
|
|
192
192
|
|
|
193
|
-
function isWhiteSpace(char){
|
|
194
|
-
return char === ' ' || char === '\t' || char === '\n'
|
|
193
|
+
function isWhiteSpace(char) {
|
|
194
|
+
return char === ' ' || char === '\t' || char === '\n' || char === '\r';
|
|
195
195
|
}
|
|
196
196
|
/**
|
|
197
197
|
* Read Processing insstructions and skip
|
|
@@ -327,25 +327,25 @@ function validateAttributeString(attrStr, options) {
|
|
|
327
327
|
for (let i = 0; i < matches.length; i++) {
|
|
328
328
|
if (matches[i][1].length === 0) {
|
|
329
329
|
//nospace before attribute name: a="sd"b="saf"
|
|
330
|
-
return getErrorObject('InvalidAttr', "Attribute '"+matches[i][2]+"' has no space in starting.", getPositionFromMatch(matches[i]))
|
|
330
|
+
return getErrorObject('InvalidAttr', "Attribute '" + matches[i][2] + "' has no space in starting.", getPositionFromMatch(matches[i]))
|
|
331
331
|
} else if (matches[i][3] !== undefined && matches[i][4] === undefined) {
|
|
332
|
-
return getErrorObject('InvalidAttr', "Attribute '"+matches[i][2]+"' is without value.", getPositionFromMatch(matches[i]));
|
|
332
|
+
return getErrorObject('InvalidAttr', "Attribute '" + matches[i][2] + "' is without value.", getPositionFromMatch(matches[i]));
|
|
333
333
|
} else if (matches[i][3] === undefined && !options.allowBooleanAttributes) {
|
|
334
334
|
//independent attribute: ab
|
|
335
|
-
return getErrorObject('InvalidAttr', "boolean attribute '"+matches[i][2]+"' is not allowed.", getPositionFromMatch(matches[i]));
|
|
335
|
+
return getErrorObject('InvalidAttr', "boolean attribute '" + matches[i][2] + "' is not allowed.", getPositionFromMatch(matches[i]));
|
|
336
336
|
}
|
|
337
337
|
/* else if(matches[i][6] === undefined){//attribute without value: ab=
|
|
338
338
|
return { err: { code:"InvalidAttr",msg:"attribute " + matches[i][2] + " has no value assigned."}};
|
|
339
339
|
} */
|
|
340
340
|
const attrName = matches[i][2];
|
|
341
341
|
if (!validateAttrName(attrName)) {
|
|
342
|
-
return getErrorObject('InvalidAttr', "Attribute '"+attrName+"' is an invalid name.", getPositionFromMatch(matches[i]));
|
|
342
|
+
return getErrorObject('InvalidAttr', "Attribute '" + attrName + "' is an invalid name.", getPositionFromMatch(matches[i]));
|
|
343
343
|
}
|
|
344
|
-
if (!
|
|
344
|
+
if (!Object.prototype.hasOwnProperty.call(attrNames, attrName)) {
|
|
345
345
|
//check for duplicate attribute.
|
|
346
346
|
attrNames[attrName] = 1;
|
|
347
347
|
} else {
|
|
348
|
-
return getErrorObject('InvalidAttr', "Attribute '"+attrName+"' is repeated.", getPositionFromMatch(matches[i]));
|
|
348
|
+
return getErrorObject('InvalidAttr', "Attribute '" + attrName + "' is repeated.", getPositionFromMatch(matches[i]));
|
|
349
349
|
}
|
|
350
350
|
}
|
|
351
351
|
|
|
@@ -14,10 +14,10 @@ const defaultOptions = {
|
|
|
14
14
|
suppressEmptyNode: false,
|
|
15
15
|
suppressUnpairedNode: true,
|
|
16
16
|
suppressBooleanAttributes: true,
|
|
17
|
-
tagValueProcessor: function(key, a) {
|
|
17
|
+
tagValueProcessor: function (key, a) {
|
|
18
18
|
return a;
|
|
19
19
|
},
|
|
20
|
-
attributeValueProcessor: function(attrName, a) {
|
|
20
|
+
attributeValueProcessor: function (attrName, a) {
|
|
21
21
|
return a;
|
|
22
22
|
},
|
|
23
23
|
preserveOrder: false,
|
|
@@ -40,7 +40,7 @@ const defaultOptions = {
|
|
|
40
40
|
export default function Builder(options) {
|
|
41
41
|
this.options = Object.assign({}, defaultOptions, options);
|
|
42
42
|
if (this.options.ignoreAttributes === true || this.options.attributesGroupName) {
|
|
43
|
-
this.isAttribute = function(/*a*/) {
|
|
43
|
+
this.isAttribute = function (/*a*/) {
|
|
44
44
|
return false;
|
|
45
45
|
};
|
|
46
46
|
} else {
|
|
@@ -56,7 +56,7 @@ export default function Builder(options) {
|
|
|
56
56
|
this.tagEndChar = '>\n';
|
|
57
57
|
this.newLine = '\n';
|
|
58
58
|
} else {
|
|
59
|
-
this.indentate = function() {
|
|
59
|
+
this.indentate = function () {
|
|
60
60
|
return '';
|
|
61
61
|
};
|
|
62
62
|
this.tagEndChar = '>';
|
|
@@ -64,25 +64,25 @@ export default function Builder(options) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
Builder.prototype.build = function(jObj) {
|
|
68
|
-
if(this.options.preserveOrder){
|
|
67
|
+
Builder.prototype.build = function (jObj) {
|
|
68
|
+
if (this.options.preserveOrder) {
|
|
69
69
|
return buildFromOrderedJs(jObj, this.options);
|
|
70
|
-
}else {
|
|
71
|
-
if(Array.isArray(jObj) && this.options.arrayNodeName && this.options.arrayNodeName.length > 1){
|
|
70
|
+
} else {
|
|
71
|
+
if (Array.isArray(jObj) && this.options.arrayNodeName && this.options.arrayNodeName.length > 1) {
|
|
72
72
|
jObj = {
|
|
73
|
-
[this.options.arrayNodeName]
|
|
73
|
+
[this.options.arrayNodeName]: jObj
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
return this.j2x(jObj, 0, []).val;
|
|
77
77
|
}
|
|
78
78
|
};
|
|
79
79
|
|
|
80
|
-
Builder.prototype.j2x = function(jObj, level, ajPath) {
|
|
80
|
+
Builder.prototype.j2x = function (jObj, level, ajPath) {
|
|
81
81
|
let attrStr = '';
|
|
82
82
|
let val = '';
|
|
83
83
|
const jPath = ajPath.join('.')
|
|
84
84
|
for (let key in jObj) {
|
|
85
|
-
if(!Object.prototype.hasOwnProperty.call(jObj, key)) continue;
|
|
85
|
+
if (!Object.prototype.hasOwnProperty.call(jObj, key)) continue;
|
|
86
86
|
if (typeof jObj[key] === 'undefined') {
|
|
87
87
|
// supress undefined node only if it is not an attribute
|
|
88
88
|
if (this.isAttribute(key)) {
|
|
@@ -126,17 +126,17 @@ Builder.prototype.j2x = function(jObj, level, ajPath) {
|
|
|
126
126
|
if (typeof item === 'undefined') {
|
|
127
127
|
// supress undefined node
|
|
128
128
|
} else if (item === null) {
|
|
129
|
-
if(key[0] === "?") val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
|
|
129
|
+
if (key[0] === "?") val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
|
|
130
130
|
else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
131
131
|
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
132
132
|
} else if (typeof item === 'object') {
|
|
133
|
-
if(this.options.oneListGroup){
|
|
133
|
+
if (this.options.oneListGroup) {
|
|
134
134
|
const result = this.j2x(item, level + 1, ajPath.concat(key));
|
|
135
135
|
listTagVal += result.val;
|
|
136
136
|
if (this.options.attributesGroupName && item.hasOwnProperty(this.options.attributesGroupName)) {
|
|
137
137
|
listTagAttr += result.attrStr
|
|
138
138
|
}
|
|
139
|
-
}else{
|
|
139
|
+
} else {
|
|
140
140
|
listTagVal += this.processTextOrObjNode(item, key, level, ajPath)
|
|
141
141
|
}
|
|
142
142
|
} else {
|
|
@@ -149,7 +149,7 @@ Builder.prototype.j2x = function(jObj, level, ajPath) {
|
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
|
-
if(this.options.oneListGroup){
|
|
152
|
+
if (this.options.oneListGroup) {
|
|
153
153
|
listTagVal = this.buildObjectNode(listTagVal, key, listTagAttr, level);
|
|
154
154
|
}
|
|
155
155
|
val += listTagVal;
|
|
@@ -166,10 +166,10 @@ Builder.prototype.j2x = function(jObj, level, ajPath) {
|
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
|
-
return {attrStr: attrStr, val: val};
|
|
169
|
+
return { attrStr: attrStr, val: val };
|
|
170
170
|
};
|
|
171
171
|
|
|
172
|
-
Builder.prototype.buildAttrPairStr = function(attrName, val){
|
|
172
|
+
Builder.prototype.buildAttrPairStr = function (attrName, val) {
|
|
173
173
|
val = this.options.attributeValueProcessor(attrName, '' + val);
|
|
174
174
|
val = this.replaceEntitiesValue(val);
|
|
175
175
|
if (this.options.suppressBooleanAttributes && val === "true") {
|
|
@@ -177,7 +177,7 @@ Builder.prototype.buildAttrPairStr = function(attrName, val){
|
|
|
177
177
|
} else return ' ' + attrName + '="' + val + '"';
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
function processTextOrObjNode
|
|
180
|
+
function processTextOrObjNode(object, key, level, ajPath) {
|
|
181
181
|
const result = this.j2x(object, level + 1, ajPath.concat(key));
|
|
182
182
|
if (object[this.options.textNodeName] !== undefined && Object.keys(object).length === 1) {
|
|
183
183
|
return this.buildTextValNode(object[this.options.textNodeName], key, result.attrStr, level);
|
|
@@ -186,43 +186,43 @@ function processTextOrObjNode (object, key, level, ajPath) {
|
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
Builder.prototype.buildObjectNode = function(val, key, attrStr, level) {
|
|
190
|
-
if(val === ""){
|
|
191
|
-
if(key[0] === "?") return
|
|
189
|
+
Builder.prototype.buildObjectNode = function (val, key, attrStr, level) {
|
|
190
|
+
if (val === "") {
|
|
191
|
+
if (key[0] === "?") return this.indentate(level) + '<' + key + attrStr + '?' + this.tagEndChar;
|
|
192
192
|
else {
|
|
193
193
|
return this.indentate(level) + '<' + key + attrStr + this.closeTag(key) + this.tagEndChar;
|
|
194
194
|
}
|
|
195
|
-
}else{
|
|
195
|
+
} else {
|
|
196
196
|
|
|
197
197
|
let tagEndExp = '</' + key + this.tagEndChar;
|
|
198
198
|
let piClosingChar = "";
|
|
199
|
-
|
|
200
|
-
if(key[0] === "?") {
|
|
199
|
+
|
|
200
|
+
if (key[0] === "?") {
|
|
201
201
|
piClosingChar = "?";
|
|
202
202
|
tagEndExp = "";
|
|
203
203
|
}
|
|
204
|
-
|
|
204
|
+
|
|
205
205
|
// attrStr is an empty string in case the attribute came as undefined or null
|
|
206
206
|
if ((attrStr || attrStr === '') && val.indexOf('<') === -1) {
|
|
207
|
-
return (
|
|
207
|
+
return (this.indentate(level) + '<' + key + attrStr + piClosingChar + '>' + val + tagEndExp);
|
|
208
208
|
} else if (this.options.commentPropName !== false && key === this.options.commentPropName && piClosingChar.length === 0) {
|
|
209
209
|
return this.indentate(level) + `<!--${val}-->` + this.newLine;
|
|
210
|
-
}else {
|
|
210
|
+
} else {
|
|
211
211
|
return (
|
|
212
212
|
this.indentate(level) + '<' + key + attrStr + piClosingChar + this.tagEndChar +
|
|
213
213
|
val +
|
|
214
|
-
this.indentate(level) + tagEndExp
|
|
214
|
+
this.indentate(level) + tagEndExp);
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
217
|
}
|
|
218
218
|
|
|
219
|
-
Builder.prototype.closeTag = function(key){
|
|
219
|
+
Builder.prototype.closeTag = function (key) {
|
|
220
220
|
let closeTag = "";
|
|
221
|
-
if(this.options.unpairedTags.indexOf(key) !== -1){ //unpaired
|
|
222
|
-
if(!this.options.suppressUnpairedNode) closeTag = "/"
|
|
223
|
-
}else if(this.options.suppressEmptyNode){ //empty
|
|
221
|
+
if (this.options.unpairedTags.indexOf(key) !== -1) { //unpaired
|
|
222
|
+
if (!this.options.suppressUnpairedNode) closeTag = "/"
|
|
223
|
+
} else if (this.options.suppressEmptyNode) { //empty
|
|
224
224
|
closeTag = "/";
|
|
225
|
-
}else{
|
|
225
|
+
} else {
|
|
226
226
|
closeTag = `></${key}`
|
|
227
227
|
}
|
|
228
228
|
return closeTag;
|
|
@@ -232,38 +232,38 @@ function buildEmptyObjNode(val, key, attrStr, level) {
|
|
|
232
232
|
if (val !== '') {
|
|
233
233
|
return this.buildObjectNode(val, key, attrStr, level);
|
|
234
234
|
} else {
|
|
235
|
-
if(key[0] === "?") return
|
|
235
|
+
if (key[0] === "?") return this.indentate(level) + '<' + key + attrStr + '?' + this.tagEndChar;
|
|
236
236
|
else {
|
|
237
|
-
return
|
|
237
|
+
return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
|
|
238
238
|
// return this.buildTagStr(level,key, attrStr);
|
|
239
239
|
}
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
-
Builder.prototype.buildTextValNode = function(val, key, attrStr, level) {
|
|
243
|
+
Builder.prototype.buildTextValNode = function (val, key, attrStr, level) {
|
|
244
244
|
if (this.options.cdataPropName !== false && key === this.options.cdataPropName) {
|
|
245
|
-
return this.indentate(level) + `<![CDATA[${val}]]>` +
|
|
246
|
-
}else if (this.options.commentPropName !== false && key === this.options.commentPropName) {
|
|
247
|
-
return this.indentate(level) + `<!--${val}-->` +
|
|
248
|
-
}else if(key[0] === "?") {//PI tag
|
|
249
|
-
return
|
|
250
|
-
}else{
|
|
245
|
+
return this.indentate(level) + `<![CDATA[${val}]]>` + this.newLine;
|
|
246
|
+
} else if (this.options.commentPropName !== false && key === this.options.commentPropName) {
|
|
247
|
+
return this.indentate(level) + `<!--${val}-->` + this.newLine;
|
|
248
|
+
} else if (key[0] === "?") {//PI tag
|
|
249
|
+
return this.indentate(level) + '<' + key + attrStr + '?' + this.tagEndChar;
|
|
250
|
+
} else {
|
|
251
251
|
let textValue = this.options.tagValueProcessor(key, val);
|
|
252
252
|
textValue = this.replaceEntitiesValue(textValue);
|
|
253
|
-
|
|
254
|
-
if(
|
|
253
|
+
|
|
254
|
+
if (textValue === '') {
|
|
255
255
|
return this.indentate(level) + '<' + key + attrStr + this.closeTag(key) + this.tagEndChar;
|
|
256
|
-
}else{
|
|
256
|
+
} else {
|
|
257
257
|
return this.indentate(level) + '<' + key + attrStr + '>' +
|
|
258
|
-
|
|
258
|
+
textValue +
|
|
259
259
|
'</' + key + this.tagEndChar;
|
|
260
260
|
}
|
|
261
261
|
}
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
-
Builder.prototype.replaceEntitiesValue = function(textValue){
|
|
265
|
-
if(textValue && textValue.length > 0 && this.options.processEntities){
|
|
266
|
-
for (let i=0; i<this.options.entities.length; i++) {
|
|
264
|
+
Builder.prototype.replaceEntitiesValue = function (textValue) {
|
|
265
|
+
if (textValue && textValue.length > 0 && this.options.processEntities) {
|
|
266
|
+
for (let i = 0; i < this.options.entities.length; i++) {
|
|
267
267
|
const entity = this.options.entities[i];
|
|
268
268
|
textValue = textValue.replace(entity.regex, entity.val);
|
|
269
269
|
}
|
|
@@ -18,10 +18,21 @@ function arrToStr(arr, options, jPath, indentation) {
|
|
|
18
18
|
let xmlStr = "";
|
|
19
19
|
let isPreviousElementTag = false;
|
|
20
20
|
|
|
21
|
+
|
|
22
|
+
if (!Array.isArray(arr)) {
|
|
23
|
+
// Non-array values (e.g. string tag values) should be treated as text content
|
|
24
|
+
if (arr !== undefined && arr !== null) {
|
|
25
|
+
let text = arr.toString();
|
|
26
|
+
text = replaceEntitiesValue(text, options);
|
|
27
|
+
return text;
|
|
28
|
+
}
|
|
29
|
+
return "";
|
|
30
|
+
}
|
|
31
|
+
|
|
21
32
|
for (let i = 0; i < arr.length; i++) {
|
|
22
33
|
const tagObj = arr[i];
|
|
23
34
|
const tagName = propName(tagObj);
|
|
24
|
-
if(tagName === undefined) continue;
|
|
35
|
+
if (tagName === undefined) continue;
|
|
25
36
|
|
|
26
37
|
let newJPath = "";
|
|
27
38
|
if (jPath.length === 0) newJPath = tagName
|
|
@@ -92,7 +103,7 @@ function propName(obj) {
|
|
|
92
103
|
const keys = Object.keys(obj);
|
|
93
104
|
for (let i = 0; i < keys.length; i++) {
|
|
94
105
|
const key = keys[i];
|
|
95
|
-
if(!
|
|
106
|
+
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
|
|
96
107
|
if (key !== ":@") return key;
|
|
97
108
|
}
|
|
98
109
|
}
|
|
@@ -101,7 +112,7 @@ function attr_to_str(attrMap, options) {
|
|
|
101
112
|
let attrStr = "";
|
|
102
113
|
if (attrMap && !options.ignoreAttributes) {
|
|
103
114
|
for (let attr in attrMap) {
|
|
104
|
-
if(!
|
|
115
|
+
if (!Object.prototype.hasOwnProperty.call(attrMap, attr)) continue;
|
|
105
116
|
let attrVal = options.attributeValueProcessor(attr, attrMap[attr]);
|
|
106
117
|
attrVal = replaceEntitiesValue(attrVal, options);
|
|
107
118
|
if (attrVal === true && options.suppressBooleanAttributes) {
|
|
@@ -163,6 +163,7 @@ function buildAttributesMap(attrStr, jPath, tagName) {
|
|
|
163
163
|
aName = this.options.transformAttributeName(aName);
|
|
164
164
|
}
|
|
165
165
|
if (aName === "__proto__") aName = "#__proto__";
|
|
166
|
+
|
|
166
167
|
if (oldVal !== undefined) {
|
|
167
168
|
if (this.options.trimValues) {
|
|
168
169
|
oldVal = oldVal.trim();
|
|
@@ -322,6 +323,13 @@ const parseXml = function (xmlData) {
|
|
|
322
323
|
tagName = newTagName;
|
|
323
324
|
}
|
|
324
325
|
|
|
326
|
+
if (this.options.strictReservedNames &&
|
|
327
|
+
(tagName === this.options.commentPropName
|
|
328
|
+
|| tagName === this.options.cdataPropName
|
|
329
|
+
)) {
|
|
330
|
+
throw new Error(`Invalid tag name: ${tagName}`);
|
|
331
|
+
}
|
|
332
|
+
|
|
325
333
|
//save text as child node
|
|
326
334
|
if (currentNode && textData) {
|
|
327
335
|
if (currentNode.tagname !== '!xml') {
|
|
@@ -409,6 +417,9 @@ const parseXml = function (xmlData) {
|
|
|
409
417
|
//opening tag
|
|
410
418
|
else {
|
|
411
419
|
const childNode = new xmlNode(tagName);
|
|
420
|
+
if (this.tagsNodeStack.length > this.options.maxNestedTags) {
|
|
421
|
+
throw new Error("Maximum nested tags exceeded");
|
|
422
|
+
}
|
|
412
423
|
this.tagsNodeStack.push(currentNode);
|
|
413
424
|
|
|
414
425
|
if (tagName !== tagExp && attrExpPresent) {
|
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import { buildOptions} from './OptionsBuilder.js';
|
|
1
|
+
import { buildOptions } from './OptionsBuilder.js';
|
|
2
2
|
import OrderedObjParser from './OrderedObjParser.js';
|
|
3
3
|
import prettify from './node2json.js';
|
|
4
|
-
import {validate} from "../validator.js";
|
|
4
|
+
import { validate } from "../validator.js";
|
|
5
5
|
import XmlNode from './xmlNode.js';
|
|
6
6
|
|
|
7
|
-
export default class XMLParser{
|
|
8
|
-
|
|
9
|
-
constructor(options){
|
|
7
|
+
export default class XMLParser {
|
|
8
|
+
|
|
9
|
+
constructor(options) {
|
|
10
10
|
this.externalEntities = {};
|
|
11
11
|
this.options = buildOptions(options);
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
* Parse XML dats to JS object
|
|
16
16
|
* @param {string|Uint8Array} xmlData
|
|
17
17
|
* @param {boolean|Object} validationOption
|
|
18
18
|
*/
|
|
19
|
-
parse(xmlData,validationOption){
|
|
20
|
-
if(typeof xmlData !== "string" && xmlData.toString){
|
|
19
|
+
parse(xmlData, validationOption) {
|
|
20
|
+
if (typeof xmlData !== "string" && xmlData.toString) {
|
|
21
21
|
xmlData = xmlData.toString();
|
|
22
|
-
}else if(typeof xmlData !== "string"){
|
|
22
|
+
} else if (typeof xmlData !== "string") {
|
|
23
23
|
throw new Error("XML data is accepted in String or Bytes[] form.")
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
if(
|
|
27
|
-
if(validationOption === true) validationOption = {}; //validate with default options
|
|
28
|
-
|
|
25
|
+
|
|
26
|
+
if (validationOption) {
|
|
27
|
+
if (validationOption === true) validationOption = {}; //validate with default options
|
|
28
|
+
|
|
29
29
|
const result = validate(xmlData, validationOption);
|
|
30
30
|
if (result !== true) {
|
|
31
|
-
|
|
31
|
+
throw Error(`${result.err.msg}:${result.err.line}:${result.err.col}`)
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
}
|
|
34
34
|
const orderedObjParser = new OrderedObjParser(this.options);
|
|
35
35
|
orderedObjParser.addExternalEntities(this.externalEntities);
|
|
36
36
|
const orderedResult = orderedObjParser.parseXml(xmlData);
|
|
37
|
-
if(this.options.preserveOrder || orderedResult === undefined) return orderedResult;
|
|
37
|
+
if (this.options.preserveOrder || orderedResult === undefined) return orderedResult;
|
|
38
38
|
else return prettify(orderedResult, this.options);
|
|
39
39
|
}
|
|
40
40
|
|
|
@@ -43,14 +43,14 @@ export default class XMLParser{
|
|
|
43
43
|
* @param {string} key
|
|
44
44
|
* @param {string} value
|
|
45
45
|
*/
|
|
46
|
-
addEntity(key, value){
|
|
47
|
-
if(value.indexOf("&") !== -1){
|
|
46
|
+
addEntity(key, value) {
|
|
47
|
+
if (value.indexOf("&") !== -1) {
|
|
48
48
|
throw new Error("Entity value can't have '&'")
|
|
49
|
-
}else if(key.indexOf("&") !== -1 || key.indexOf(";") !== -1){
|
|
49
|
+
} else if (key.indexOf("&") !== -1 || key.indexOf(";") !== -1) {
|
|
50
50
|
throw new Error("An entity must be set without '&' and ';'. Eg. use '#xD' for '
'")
|
|
51
|
-
}else if(value === "&"){
|
|
51
|
+
} else if (value === "&") {
|
|
52
52
|
throw new Error("An entity with value '&' is not permitted");
|
|
53
|
-
}else{
|
|
53
|
+
} else {
|
|
54
54
|
this.externalEntities[key] = value;
|
|
55
55
|
}
|
|
56
56
|
}
|