fast-xml-parser 4.5.2 → 4.5.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 +17 -0
- package/package.json +10 -9
- package/src/fxp.d.ts +102 -18
- package/src/xmlbuilder/orderedJs2Xml.js +14 -3
- package/src/xmlparser/DocTypeReader.js +377 -131
- package/src/xmlparser/OptionsBuilder.js +85 -41
- package/src/xmlparser/OrderedObjParser.js +313 -191
- package/test_output.txt +21 -0
- package/test_output_2.txt +21 -0
- package/test_output_3.txt +11 -0
- package/test_output_4.txt +26 -0
- package/test_output_5.txt +17 -0
- package/test_output_6.txt +98 -0
- package/test_output_7.txt +48 -0
- package/test_output_8.txt +30 -0
- package/test_output_9.txt +41 -0
|
@@ -1,153 +1,399 @@
|
|
|
1
1
|
const util = require('../util');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
3
|
+
class DocTypeReader {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.suppressValidationErr = !options;
|
|
6
|
+
this.options = options || {};
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
readDocType(xmlData, i) {
|
|
10
|
+
const entities = Object.create(null);
|
|
11
|
+
|
|
12
|
+
if (xmlData[i + 3] === 'O' &&
|
|
13
|
+
xmlData[i + 4] === 'C' &&
|
|
14
|
+
xmlData[i + 5] === 'T' &&
|
|
15
|
+
xmlData[i + 6] === 'Y' &&
|
|
16
|
+
xmlData[i + 7] === 'P' &&
|
|
17
|
+
xmlData[i + 8] === 'E') {
|
|
18
|
+
|
|
19
|
+
i = i + 9;
|
|
20
|
+
let angleBracketsCount = 1;
|
|
21
|
+
let hasBody = false, comment = false;
|
|
22
|
+
let exp = "";
|
|
23
|
+
|
|
24
|
+
for (; i < xmlData.length; i++) {
|
|
25
|
+
if (xmlData[i] === '<' && !comment) { //Determine the tag type
|
|
26
|
+
if (hasBody && hasSeq(xmlData, "!ENTITY", i)) {
|
|
27
|
+
i += 7;
|
|
28
|
+
let entityName, val;
|
|
29
|
+
[entityName, val, i] = this.readEntityExp(xmlData, i + 1, this.suppressValidationErr);
|
|
30
|
+
if (val.indexOf("&") === -1) { //Parameter entities are not supported
|
|
31
|
+
const escaped = entityName.replace(/[.\-+*:]/g, '\\.');
|
|
32
|
+
entities[entityName] = {
|
|
33
|
+
regx: RegExp(`&${escaped};`, "g"),
|
|
34
|
+
val: val
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
} else if (hasBody && hasSeq(xmlData, "!ELEMENT", i)) {
|
|
38
|
+
i += 8; //Not supported
|
|
39
|
+
const { index } = this.readElementExp(xmlData, i + 1);
|
|
40
|
+
i = index;
|
|
41
|
+
} else if (hasBody && hasSeq(xmlData, "!ATTLIST", i)) {
|
|
42
|
+
i += 8; //Not supported
|
|
43
|
+
// const {index} = this.readAttlistExp(xmlData,i+1);
|
|
44
|
+
// i = index;
|
|
45
|
+
} else if (hasBody && hasSeq(xmlData, "!NOTATION", i)) {
|
|
46
|
+
i += 9; //Not supported
|
|
47
|
+
const { index } = this.readNotationExp(xmlData, i + 1, this.suppressValidationErr);
|
|
48
|
+
i = index;
|
|
49
|
+
} else if (hasSeq(xmlData, "!--", i)) {
|
|
50
|
+
comment = true;
|
|
51
|
+
} else {
|
|
52
|
+
throw new Error(`Invalid DOCTYPE`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
angleBracketsCount++;
|
|
56
|
+
exp = "";
|
|
57
|
+
} else if (xmlData[i] === '>') { //Read tag content
|
|
58
|
+
if (comment) {
|
|
59
|
+
if (xmlData[i - 1] === "-" && xmlData[i - 2] === "-") {
|
|
60
|
+
comment = false;
|
|
61
|
+
angleBracketsCount--;
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
42
64
|
angleBracketsCount--;
|
|
43
65
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
|
|
66
|
+
if (angleBracketsCount === 0) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
} else if (xmlData[i] === '[') {
|
|
70
|
+
hasBody = true;
|
|
71
|
+
} else {
|
|
72
|
+
exp += xmlData[i];
|
|
49
73
|
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (angleBracketsCount !== 0) {
|
|
77
|
+
throw new Error(`Unclosed DOCTYPE`);
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
throw new Error(`Invalid Tag instead of DOCTYPE`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return { entities, i };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
readEntityExp(xmlData, i) {
|
|
87
|
+
//External entities are not supported
|
|
88
|
+
// <!ENTITY ext SYSTEM "http://normal-website.com" >
|
|
89
|
+
|
|
90
|
+
//Parameter entities are not supported
|
|
91
|
+
// <!ENTITY entityname "&anotherElement;">
|
|
92
|
+
|
|
93
|
+
//Internal entities are supported
|
|
94
|
+
// <!ENTITY entityname "replacement text">
|
|
95
|
+
|
|
96
|
+
// Skip leading whitespace after <!ENTITY
|
|
97
|
+
i = skipWhitespace(xmlData, i);
|
|
98
|
+
|
|
99
|
+
// Read entity name
|
|
100
|
+
let entityName = "";
|
|
101
|
+
while (i < xmlData.length && !/\s/.test(xmlData[i]) && xmlData[i] !== '"' && xmlData[i] !== "'") {
|
|
102
|
+
entityName += xmlData[i];
|
|
103
|
+
i++;
|
|
104
|
+
}
|
|
105
|
+
validateEntityName(entityName);
|
|
106
|
+
|
|
107
|
+
// Skip whitespace after entity name
|
|
108
|
+
i = skipWhitespace(xmlData, i);
|
|
109
|
+
|
|
110
|
+
// Check for unsupported constructs (external entities or parameter entities)
|
|
111
|
+
if (!this.suppressValidationErr) {
|
|
112
|
+
if (xmlData.substring(i, i + 6).toUpperCase() === "SYSTEM") {
|
|
113
|
+
throw new Error("External entities are not supported");
|
|
114
|
+
} else if (xmlData[i] === "%") {
|
|
115
|
+
throw new Error("Parameter entities are not supported");
|
|
54
116
|
}
|
|
55
117
|
}
|
|
56
|
-
|
|
57
|
-
|
|
118
|
+
|
|
119
|
+
// Read entity value (internal entity)
|
|
120
|
+
let entityValue = "";
|
|
121
|
+
[i, entityValue] = this.readIdentifierVal(xmlData, i, "entity");
|
|
122
|
+
|
|
123
|
+
// Validate entity size
|
|
124
|
+
if (this.options.enabled !== false &&
|
|
125
|
+
this.options.maxEntitySize &&
|
|
126
|
+
entityValue.length > this.options.maxEntitySize) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`Entity "${entityName}" size (${entityValue.length}) exceeds maximum allowed size (${this.options.maxEntitySize})`
|
|
129
|
+
);
|
|
58
130
|
}
|
|
59
|
-
|
|
60
|
-
|
|
131
|
+
|
|
132
|
+
i--;
|
|
133
|
+
return [entityName, entityValue, i];
|
|
61
134
|
}
|
|
62
|
-
return {entities, i};
|
|
63
|
-
}
|
|
64
135
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
136
|
+
readNotationExp(xmlData, i) {
|
|
137
|
+
// Skip leading whitespace after <!NOTATION
|
|
138
|
+
i = skipWhitespace(xmlData, i);
|
|
139
|
+
|
|
140
|
+
// Read notation name
|
|
141
|
+
let notationName = "";
|
|
142
|
+
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
|
|
143
|
+
notationName += xmlData[i];
|
|
144
|
+
i++;
|
|
145
|
+
}
|
|
146
|
+
!this.suppressValidationErr && validateEntityName(notationName);
|
|
147
|
+
|
|
148
|
+
// Skip whitespace after notation name
|
|
149
|
+
i = skipWhitespace(xmlData, i);
|
|
150
|
+
|
|
151
|
+
// Check identifier type (SYSTEM or PUBLIC)
|
|
152
|
+
const identifierType = xmlData.substring(i, i + 6).toUpperCase();
|
|
153
|
+
if (!this.suppressValidationErr && identifierType !== "SYSTEM" && identifierType !== "PUBLIC") {
|
|
154
|
+
throw new Error(`Expected SYSTEM or PUBLIC, found "${identifierType}"`);
|
|
155
|
+
}
|
|
156
|
+
i += identifierType.length;
|
|
157
|
+
|
|
158
|
+
// Skip whitespace after identifier type
|
|
159
|
+
i = skipWhitespace(xmlData, i);
|
|
160
|
+
|
|
161
|
+
// Read public identifier (if PUBLIC)
|
|
162
|
+
let publicIdentifier = null;
|
|
163
|
+
let systemIdentifier = null;
|
|
164
|
+
|
|
165
|
+
if (identifierType === "PUBLIC") {
|
|
166
|
+
[i, publicIdentifier] = this.readIdentifierVal(xmlData, i, "publicIdentifier");
|
|
167
|
+
|
|
168
|
+
// Skip whitespace after public identifier
|
|
169
|
+
i = skipWhitespace(xmlData, i);
|
|
170
|
+
|
|
171
|
+
// Optionally read system identifier
|
|
172
|
+
if (xmlData[i] === '"' || xmlData[i] === "'") {
|
|
173
|
+
[i, systemIdentifier] = this.readIdentifierVal(xmlData, i, "systemIdentifier");
|
|
174
|
+
}
|
|
175
|
+
} else if (identifierType === "SYSTEM") {
|
|
176
|
+
// Read system identifier (mandatory for SYSTEM)
|
|
177
|
+
[i, systemIdentifier] = this.readIdentifierVal(xmlData, i, "systemIdentifier");
|
|
178
|
+
|
|
179
|
+
if (!this.suppressValidationErr && !systemIdentifier) {
|
|
180
|
+
throw new Error("Missing mandatory system identifier for SYSTEM notation");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return { notationName, publicIdentifier, systemIdentifier, index: --i };
|
|
81
185
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
186
|
+
|
|
187
|
+
readIdentifierVal(xmlData, i, type) {
|
|
188
|
+
let identifierVal = "";
|
|
189
|
+
const startChar = xmlData[i];
|
|
190
|
+
if (startChar !== '"' && startChar !== "'") {
|
|
191
|
+
throw new Error(`Expected quoted string, found "${startChar}"`);
|
|
192
|
+
}
|
|
193
|
+
i++;
|
|
194
|
+
|
|
195
|
+
while (i < xmlData.length && xmlData[i] !== startChar) {
|
|
196
|
+
identifierVal += xmlData[i];
|
|
197
|
+
i++;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (xmlData[i] !== startChar) {
|
|
201
|
+
throw new Error(`Unterminated ${type} value`);
|
|
202
|
+
}
|
|
203
|
+
i++;
|
|
204
|
+
return [i, identifierVal];
|
|
90
205
|
}
|
|
91
|
-
return [entityName, val, i];
|
|
92
|
-
}
|
|
93
206
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
207
|
+
readElementExp(xmlData, i) {
|
|
208
|
+
// <!ELEMENT br EMPTY>
|
|
209
|
+
// <!ELEMENT div ANY>
|
|
210
|
+
// <!ELEMENT title (#PCDATA)>
|
|
211
|
+
// <!ELEMENT book (title, author+)>
|
|
212
|
+
// <!ELEMENT name (content-model)>
|
|
213
|
+
|
|
214
|
+
// Skip leading whitespace after <!ELEMENT
|
|
215
|
+
i = skipWhitespace(xmlData, i);
|
|
216
|
+
|
|
217
|
+
// Read element name
|
|
218
|
+
let elementName = "";
|
|
219
|
+
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
|
|
220
|
+
elementName += xmlData[i];
|
|
221
|
+
i++;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Validate element name
|
|
225
|
+
if (!this.suppressValidationErr && !util.isName(elementName)) {
|
|
226
|
+
throw new Error(`Invalid element name: "${elementName}"`);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Skip whitespace after element name
|
|
230
|
+
i = skipWhitespace(xmlData, i);
|
|
231
|
+
let contentModel = "";
|
|
232
|
+
|
|
233
|
+
// Expect '(' to start content model
|
|
234
|
+
if (xmlData[i] === "E" && hasSeq(xmlData, "MPTY", i)) {
|
|
235
|
+
i += 4;
|
|
236
|
+
} else if (xmlData[i] === "A" && hasSeq(xmlData, "NY", i)) {
|
|
237
|
+
i += 2;
|
|
238
|
+
} else if (xmlData[i] === "(") {
|
|
239
|
+
i++; // Move past '('
|
|
240
|
+
|
|
241
|
+
// Read content model
|
|
242
|
+
while (i < xmlData.length && xmlData[i] !== ")") {
|
|
243
|
+
contentModel += xmlData[i];
|
|
244
|
+
i++;
|
|
245
|
+
}
|
|
246
|
+
if (xmlData[i] !== ")") {
|
|
247
|
+
throw new Error("Unterminated content model");
|
|
248
|
+
}
|
|
249
|
+
} else if (!this.suppressValidationErr) {
|
|
250
|
+
throw new Error(`Invalid Element Expression, found "${xmlData[i]}"`);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
elementName,
|
|
255
|
+
contentModel: contentModel.trim(),
|
|
256
|
+
index: i
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
readAttlistExp(xmlData, i) {
|
|
261
|
+
// Skip leading whitespace after <!ATTLIST
|
|
262
|
+
i = skipWhitespace(xmlData, i);
|
|
263
|
+
|
|
264
|
+
// Read element name
|
|
265
|
+
let elementName = "";
|
|
266
|
+
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
|
|
267
|
+
elementName += xmlData[i];
|
|
268
|
+
i++;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Validate element name
|
|
272
|
+
validateEntityName(elementName);
|
|
273
|
+
|
|
274
|
+
// Skip whitespace after element name
|
|
275
|
+
i = skipWhitespace(xmlData, i);
|
|
276
|
+
|
|
277
|
+
// Read attribute name
|
|
278
|
+
let attributeName = "";
|
|
279
|
+
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
|
|
280
|
+
attributeName += xmlData[i];
|
|
281
|
+
i++;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Validate attribute name
|
|
285
|
+
if (!validateEntityName(attributeName)) {
|
|
286
|
+
throw new Error(`Invalid attribute name: "${attributeName}"`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Skip whitespace after attribute name
|
|
290
|
+
i = skipWhitespace(xmlData, i);
|
|
291
|
+
|
|
292
|
+
// Read attribute type
|
|
293
|
+
let attributeType = "";
|
|
294
|
+
if (xmlData.substring(i, i + 8).toUpperCase() === "NOTATION") {
|
|
295
|
+
attributeType = "NOTATION";
|
|
296
|
+
i += 8; // Move past "NOTATION"
|
|
297
|
+
|
|
298
|
+
// Skip whitespace after "NOTATION"
|
|
299
|
+
i = skipWhitespace(xmlData, i);
|
|
121
300
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
301
|
+
// Expect '(' to start the list of notations
|
|
302
|
+
if (xmlData[i] !== "(") {
|
|
303
|
+
throw new Error(`Expected '(', found "${xmlData[i]}"`);
|
|
304
|
+
}
|
|
305
|
+
i++; // Move past '('
|
|
306
|
+
|
|
307
|
+
// Read the list of allowed notations
|
|
308
|
+
let allowedNotations = [];
|
|
309
|
+
while (i < xmlData.length && xmlData[i] !== ")") {
|
|
310
|
+
let notation = "";
|
|
311
|
+
while (i < xmlData.length && xmlData[i] !== "|" && xmlData[i] !== ")") {
|
|
312
|
+
notation += xmlData[i];
|
|
313
|
+
i++;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Validate notation name
|
|
317
|
+
notation = notation.trim();
|
|
318
|
+
if (!validateEntityName(notation)) {
|
|
319
|
+
throw new Error(`Invalid notation name: "${notation}"`);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
allowedNotations.push(notation);
|
|
323
|
+
|
|
324
|
+
// Skip '|' separator or exit loop
|
|
325
|
+
if (xmlData[i] === "|") {
|
|
326
|
+
i++; // Move past '|'
|
|
327
|
+
i = skipWhitespace(xmlData, i); // Skip optional whitespace after '|'
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (xmlData[i] !== ")") {
|
|
332
|
+
throw new Error("Unterminated list of notations");
|
|
333
|
+
}
|
|
334
|
+
i++; // Move past ')'
|
|
335
|
+
|
|
336
|
+
// Store the allowed notations as part of the attribute type
|
|
337
|
+
attributeType += " (" + allowedNotations.join("|") + ")";
|
|
338
|
+
} else {
|
|
339
|
+
// Handle simple types (e.g., CDATA, ID, IDREF, etc.)
|
|
340
|
+
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
|
|
341
|
+
attributeType += xmlData[i];
|
|
342
|
+
i++;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Validate simple attribute type
|
|
346
|
+
const validTypes = ["CDATA", "ID", "IDREF", "IDREFS", "ENTITY", "ENTITIES", "NMTOKEN", "NMTOKENS"];
|
|
347
|
+
if (!this.suppressValidationErr && !validTypes.includes(attributeType.toUpperCase())) {
|
|
348
|
+
throw new Error(`Invalid attribute type: "${attributeType}"`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Skip whitespace after attribute type
|
|
353
|
+
i = skipWhitespace(xmlData, i);
|
|
354
|
+
|
|
355
|
+
// Read default value
|
|
356
|
+
let defaultValue = "";
|
|
357
|
+
if (xmlData.substring(i, i + 8).toUpperCase() === "#REQUIRED") {
|
|
358
|
+
defaultValue = "#REQUIRED";
|
|
359
|
+
i += 8;
|
|
360
|
+
} else if (xmlData.substring(i, i + 7).toUpperCase() === "#IMPLIED") {
|
|
361
|
+
defaultValue = "#IMPLIED";
|
|
362
|
+
i += 7;
|
|
363
|
+
} else {
|
|
364
|
+
[i, defaultValue] = this.readIdentifierVal(xmlData, i, "ATTLIST");
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return {
|
|
368
|
+
elementName,
|
|
369
|
+
attributeName,
|
|
370
|
+
attributeType,
|
|
371
|
+
defaultValue,
|
|
372
|
+
index: i
|
|
373
|
+
};
|
|
374
|
+
}
|
|
132
375
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
376
|
+
|
|
377
|
+
// Helper functions
|
|
378
|
+
const skipWhitespace = (data, index) => {
|
|
379
|
+
while (index < data.length && /\s/.test(data[index])) {
|
|
380
|
+
index++;
|
|
381
|
+
}
|
|
382
|
+
return index;
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
function hasSeq(data, seq, i) {
|
|
386
|
+
for (let j = 0; j < seq.length; j++) {
|
|
387
|
+
if (seq[j] !== data[i + j + 1]) return false;
|
|
388
|
+
}
|
|
389
|
+
return true;
|
|
144
390
|
}
|
|
145
391
|
|
|
146
|
-
function validateEntityName(name){
|
|
392
|
+
function validateEntityName(name) {
|
|
147
393
|
if (util.isName(name))
|
|
148
|
-
|
|
394
|
+
return name;
|
|
149
395
|
else
|
|
150
396
|
throw new Error(`Invalid entity name ${name}`);
|
|
151
397
|
}
|
|
152
398
|
|
|
153
|
-
module.exports =
|
|
399
|
+
module.exports = DocTypeReader;
|
|
@@ -1,47 +1,91 @@
|
|
|
1
1
|
|
|
2
2
|
const defaultOptions = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
3
|
+
preserveOrder: false,
|
|
4
|
+
attributeNamePrefix: '@_',
|
|
5
|
+
attributesGroupName: false,
|
|
6
|
+
textNodeName: '#text',
|
|
7
|
+
ignoreAttributes: true,
|
|
8
|
+
removeNSPrefix: false, // remove NS from tag name or attribute name if true
|
|
9
|
+
allowBooleanAttributes: false, //a tag can have attributes without any value
|
|
10
|
+
//ignoreRootElement : false,
|
|
11
|
+
parseTagValue: true,
|
|
12
|
+
parseAttributeValue: false,
|
|
13
|
+
trimValues: true, //Trim string values of tag and attributes
|
|
14
|
+
cdataPropName: false,
|
|
15
|
+
numberParseOptions: {
|
|
16
|
+
hex: true,
|
|
17
|
+
leadingZeros: true,
|
|
18
|
+
eNotation: true
|
|
19
|
+
},
|
|
20
|
+
tagValueProcessor: function (tagName, val) {
|
|
21
|
+
return val;
|
|
22
|
+
},
|
|
23
|
+
attributeValueProcessor: function (attrName, val) {
|
|
24
|
+
return val;
|
|
25
|
+
},
|
|
26
|
+
stopNodes: [], //nested tags will not be parsed even for errors
|
|
27
|
+
alwaysCreateTextNode: false,
|
|
28
|
+
isArray: () => false,
|
|
29
|
+
commentPropName: false,
|
|
30
|
+
unpairedTags: [],
|
|
31
|
+
processEntities: true,
|
|
32
|
+
htmlEntities: false,
|
|
33
|
+
ignoreDeclaration: false,
|
|
34
|
+
ignorePiTags: false,
|
|
35
|
+
transformTagName: false,
|
|
36
|
+
transformAttributeName: false,
|
|
37
|
+
updateTag: function (tagName, jPath, attrs) {
|
|
38
|
+
return tagName
|
|
39
|
+
},
|
|
40
|
+
// skipEmptyListItem: false
|
|
41
|
+
captureMetaData: false,
|
|
42
|
+
maxNestedTags: 100,
|
|
43
|
+
strictReservedNames: true,
|
|
41
44
|
};
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Normalizes processEntities option for backward compatibility
|
|
48
|
+
* @param {boolean|object} value
|
|
49
|
+
* @returns {object} Always returns normalized object
|
|
50
|
+
*/
|
|
51
|
+
function normalizeProcessEntities(value) {
|
|
52
|
+
// Boolean backward compatibility
|
|
53
|
+
if (typeof value === 'boolean') {
|
|
54
|
+
return {
|
|
55
|
+
enabled: value, // true or false
|
|
56
|
+
maxEntitySize: 10000,
|
|
57
|
+
maxExpansionDepth: 10,
|
|
58
|
+
maxTotalExpansions: 1000,
|
|
59
|
+
maxExpandedLength: 100000,
|
|
60
|
+
allowedTags: null,
|
|
61
|
+
tagFilter: null
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Object config - merge with defaults
|
|
66
|
+
if (typeof value === 'object' && value !== null) {
|
|
67
|
+
return {
|
|
68
|
+
enabled: value.enabled !== false, // default true if not specified
|
|
69
|
+
maxEntitySize: value.maxEntitySize ?? 10000,
|
|
70
|
+
maxExpansionDepth: value.maxExpansionDepth ?? 10,
|
|
71
|
+
maxTotalExpansions: value.maxTotalExpansions ?? 1000,
|
|
72
|
+
maxExpandedLength: value.maxExpandedLength ?? 100000,
|
|
73
|
+
allowedTags: value.allowedTags ?? null,
|
|
74
|
+
tagFilter: value.tagFilter ?? null
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Default to enabled with limits
|
|
79
|
+
return normalizeProcessEntities(true);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const buildOptions = function (options) {
|
|
83
|
+
const built = Object.assign({}, defaultOptions, options);
|
|
84
|
+
|
|
85
|
+
// Always normalize processEntities for backward compatibility and validation
|
|
86
|
+
built.processEntities = normalizeProcessEntities(built.processEntities);
|
|
87
|
+
//console.debug(built.processEntities)
|
|
88
|
+
return built;
|
|
45
89
|
};
|
|
46
90
|
|
|
47
91
|
exports.buildOptions = buildOptions;
|