• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NaturalIntelligence / fast-xml-parser / 23146869289

16 Mar 2026 01:45PM UTC coverage: 97.709% (-0.1%) from 97.837%
23146869289

push

github

amitguptagwl
refactor: performance improvement

1144 of 1190 branches covered (96.13%)

2 of 2 new or added lines in 1 file covered. (100.0%)

44 existing lines in 4 files now uncovered.

9382 of 9602 relevant lines covered (97.71%)

464759.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.43
/src/xmlparser/DocTypeReader.js
1
import { isName } from '../util.js';
5✔
2

5✔
3
export default class DocTypeReader {
5✔
4
    constructor(options) {
5✔
5
        this.suppressValidationErr = !options;
1,690✔
6
        this.options = options;
1,690✔
7
    }
1,690✔
8

5✔
9
    readDocType(xmlData, i) {
5✔
10
        const entities = Object.create(null);
220✔
11
        let entityCount = 0;
220✔
12

220✔
13
        if (xmlData[i + 3] === 'O' &&
220✔
14
            xmlData[i + 4] === 'C' &&
220✔
15
            xmlData[i + 5] === 'T' &&
220✔
16
            xmlData[i + 6] === 'Y' &&
220✔
17
            xmlData[i + 7] === 'P' &&
220✔
18
            xmlData[i + 8] === 'E') {
220✔
19
            i = i + 9;
220✔
20
            let angleBracketsCount = 1;
220✔
21
            let hasBody = false, comment = false;
220✔
22
            let exp = "";
220✔
23
            for (; i < xmlData.length; i++) {
220✔
24
                if (xmlData[i] === '<' && !comment) { //Determine the tag type
5,885✔
25
                    if (hasBody && hasSeq(xmlData, "!ENTITY", i)) {
290✔
26
                        i += 7;
230✔
27
                        let entityName, val;
230✔
28
                        [entityName, val, i] = this.readEntityExp(xmlData, i + 1, this.suppressValidationErr);
230✔
29
                        if (val.indexOf("&") === -1) { //Parameter entities are not supported
230✔
30
                            if (this.options.enabled !== false &&
205✔
31
                                this.options.maxEntityCount &&
205✔
32
                                entityCount >= this.options.maxEntityCount) {
205!
33
                                throw new Error(
×
34
                                    `Entity count (${entityCount + 1}) exceeds maximum allowed (${this.options.maxEntityCount})`
×
35
                                );
×
36
                            }
×
37
                            //const escaped = entityName.replace(/[.\-+*:]/g, '\\.');
205✔
38
                            const escaped = entityName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
205✔
39
                            entities[entityName] = {
205✔
40
                                regx: RegExp(`&${escaped};`, "g"),
205✔
41
                                val: val
205✔
42
                            };
205✔
43
                            entityCount++;
205✔
44
                        }
205✔
45
                    }
230✔
46
                    else if (hasBody && hasSeq(xmlData, "!ELEMENT", i)) {
60✔
47
                        i += 8;//Not supported
20✔
48
                        const { index } = this.readElementExp(xmlData, i + 1);
20✔
49
                        i = index;
20✔
50
                    } else if (hasBody && hasSeq(xmlData, "!ATTLIST", i)) {
60✔
51
                        i += 8;//Not supported
5✔
52
                        // const {index} = this.readAttlistExp(xmlData,i+1);
5✔
53
                        // i = index;
5✔
54
                    } else if (hasBody && hasSeq(xmlData, "!NOTATION", i)) {
40✔
55
                        i += 9;//Not supported
10✔
56
                        const { index } = this.readNotationExp(xmlData, i + 1, this.suppressValidationErr);
10✔
57
                        i = index;
10✔
58
                    } else if (hasSeq(xmlData, "!--", i)) comment = true;
35✔
UNCOV
59
                    else throw new Error(`Invalid DOCTYPE`);
×
60

270✔
61
                    angleBracketsCount++;
270✔
62
                    exp = "";
270✔
63
                } else if (xmlData[i] === '>') { //Read tag content
5,885✔
64
                    if (comment) {
470✔
65
                        if (xmlData[i - 1] === "-" && xmlData[i - 2] === "-") {
30✔
66
                            comment = false;
25✔
67
                            angleBracketsCount--;
25✔
68
                        }
25✔
69
                    } else {
470✔
70
                        angleBracketsCount--;
440✔
71
                    }
440✔
72
                    if (angleBracketsCount === 0) {
470✔
73
                        break;
195✔
74
                    }
195✔
75
                } else if (xmlData[i] === '[') {
5,595✔
76
                    hasBody = true;
205✔
77
                } else {
5,125✔
78
                    exp += xmlData[i];
4,920✔
79
                }
4,920✔
80
            }
5,885✔
81
            if (angleBracketsCount !== 0) {
220✔
82
                throw new Error(`Unclosed DOCTYPE`);
5✔
83
            }
5✔
84
        } else {
220!
85
            throw new Error(`Invalid Tag instead of DOCTYPE`);
×
UNCOV
86
        }
×
87
        return { entities, i };
195✔
88
    }
220✔
89
    readEntityExp(xmlData, i) {
5✔
90
        //External entities are not supported
230✔
91
        //    <!ENTITY ext SYSTEM "http://normal-website.com" >
230✔
92

230✔
93
        //Parameter entities are not supported
230✔
94
        //    <!ENTITY entityname "&anotherElement;">
230✔
95

230✔
96
        //Internal entities are supported
230✔
97
        //    <!ENTITY entityname "replacement text">
230✔
98

230✔
99
        // Skip leading whitespace after <!ENTITY
230✔
100
        i = skipWhitespace(xmlData, i);
230✔
101

230✔
102
        // Read entity name
230✔
103
        let entityName = "";
230✔
104
        while (i < xmlData.length && !/\s/.test(xmlData[i]) && xmlData[i] !== '"' && xmlData[i] !== "'") {
230✔
105
            entityName += xmlData[i];
805✔
106
            i++;
805✔
107
        }
805✔
108
        validateEntityName(entityName);
230✔
109

230✔
110
        // Skip whitespace after entity name
230✔
111
        i = skipWhitespace(xmlData, i);
230✔
112

230✔
113
        // Check for unsupported constructs (external entities or parameter entities)
230✔
114
        if (!this.suppressValidationErr) {
230✔
115
            if (xmlData.substring(i, i + 6).toUpperCase() === "SYSTEM") {
225!
UNCOV
116
                throw new Error("External entities are not supported");
×
117
            } else if (xmlData[i] === "%") {
225!
118
                throw new Error("Parameter entities are not supported");
×
UNCOV
119
            }
×
120
        }
225✔
121

225✔
122
        // Read entity value (internal entity)
225✔
123
        let entityValue = "";
225✔
124
        [i, entityValue] = this.readIdentifierVal(xmlData, i, "entity");
225✔
125

225✔
126
        // Validate entity size
225✔
127
        if (this.options.enabled !== false &&
230✔
128
            this.options.maxEntitySize &&
230✔
129
            entityValue.length > this.options.maxEntitySize) {
230✔
130
            throw new Error(
15✔
131
                `Entity "${entityName}" size (${entityValue.length}) exceeds maximum allowed size (${this.options.maxEntitySize})`
15✔
132
            );
15✔
133
        }
15✔
134

210✔
135
        i--;
210✔
136
        return [entityName, entityValue, i];
210✔
137
    }
230✔
138

5✔
139
    readNotationExp(xmlData, i) {
5✔
140
        // Skip leading whitespace after <!NOTATION
10✔
141
        i = skipWhitespace(xmlData, i);
10✔
142

10✔
143
        // Read notation name
10✔
144
        let notationName = "";
10✔
145
        while (i < xmlData.length && !/\s/.test(xmlData[i])) {
10✔
146
            notationName += xmlData[i];
45✔
147
            i++;
45✔
148
        }
45✔
149
        !this.suppressValidationErr && validateEntityName(notationName);
10✔
150

10✔
151
        // Skip whitespace after notation name
10✔
152
        i = skipWhitespace(xmlData, i);
10✔
153

10✔
154
        // Check identifier type (SYSTEM or PUBLIC)
10✔
155
        const identifierType = xmlData.substring(i, i + 6).toUpperCase();
10✔
156
        if (!this.suppressValidationErr && identifierType !== "SYSTEM" && identifierType !== "PUBLIC") {
10!
157
            throw new Error(`Expected SYSTEM or PUBLIC, found "${identifierType}"`);
×
UNCOV
158
        }
×
159
        i += identifierType.length;
10✔
160

10✔
161
        // Skip whitespace after identifier type
10✔
162
        i = skipWhitespace(xmlData, i);
10✔
163

10✔
164
        // Read public identifier (if PUBLIC)
10✔
165
        let publicIdentifier = null;
10✔
166
        let systemIdentifier = null;
10✔
167

10✔
168
        if (identifierType === "PUBLIC") {
10✔
169
            [i, publicIdentifier] = this.readIdentifierVal(xmlData, i, "publicIdentifier");
10✔
170

10✔
171
            // Skip whitespace after public identifier
10✔
172
            i = skipWhitespace(xmlData, i);
10✔
173

10✔
174
            // Optionally read system identifier
10✔
175
            if (xmlData[i] === '"' || xmlData[i] === "'") {
10✔
176
                [i, systemIdentifier] = this.readIdentifierVal(xmlData, i, "systemIdentifier");
5✔
177
            }
5✔
178
        } else if (identifierType === "SYSTEM") {
10!
179
            // Read system identifier (mandatory for SYSTEM)
×
180
            [i, systemIdentifier] = this.readIdentifierVal(xmlData, i, "systemIdentifier");
×
181

×
182
            if (!this.suppressValidationErr && !systemIdentifier) {
×
183
                throw new Error("Missing mandatory system identifier for SYSTEM notation");
×
184
            }
×
UNCOV
185
        }
×
186

10✔
187
        return { notationName, publicIdentifier, systemIdentifier, index: --i };
10✔
188
    }
10✔
189

5✔
190
    readIdentifierVal(xmlData, i, type) {
5✔
191
        let identifierVal = "";
240✔
192
        const startChar = xmlData[i];
240✔
193
        if (startChar !== '"' && startChar !== "'") {
240!
194
            throw new Error(`Expected quoted string, found "${startChar}"`);
×
UNCOV
195
        }
×
196
        i++;
240✔
197

240✔
198
        const startIndex = i;
240✔
199
        while (i < xmlData.length && xmlData[i] !== startChar) {
240✔
200
            i++;
441,490✔
201
        }
441,490✔
202
        identifierVal = xmlData.substring(startIndex, i);
240✔
203

240✔
204
        if (xmlData[i] !== startChar) {
240!
205
            throw new Error(`Unterminated ${type} value`);
×
UNCOV
206
        }
×
207
        i++;
240✔
208
        return [i, identifierVal];
240✔
209
    }
240✔
210

5✔
211
    readElementExp(xmlData, i) {
5✔
212
        // <!ELEMENT br EMPTY>
20✔
213
        // <!ELEMENT div ANY>
20✔
214
        // <!ELEMENT title (#PCDATA)>
20✔
215
        // <!ELEMENT book (title, author+)>
20✔
216
        // <!ELEMENT name (content-model)>
20✔
217

20✔
218
        // Skip leading whitespace after <!ELEMENT
20✔
219
        i = skipWhitespace(xmlData, i);
20✔
220

20✔
221
        // Read element name
20✔
222
        let elementName = "";
20✔
223
        while (i < xmlData.length && !/\s/.test(xmlData[i])) {
20✔
224
            elementName += xmlData[i];
65✔
225
            i++;
65✔
226
        }
65✔
227

20✔
228
        // Validate element name
20✔
229
        if (!this.suppressValidationErr && !isName(elementName)) {
20!
230
            throw new Error(`Invalid element name: "${elementName}"`);
×
UNCOV
231
        }
×
232

20✔
233
        // Skip whitespace after element name
20✔
234
        i = skipWhitespace(xmlData, i);
20✔
235
        let contentModel = "";
20✔
236
        // Expect '(' to start content model
20✔
237
        if (xmlData[i] === "E" && hasSeq(xmlData, "MPTY", i)) i += 4;
20✔
238
        else if (xmlData[i] === "A" && hasSeq(xmlData, "NY", i)) i += 2;
15✔
239
        else if (xmlData[i] === "(") {
10✔
240
            i++; // Move past '('
10✔
241

10✔
242
            // Read content model
10✔
243
            while (i < xmlData.length && xmlData[i] !== ")") {
10✔
244
                contentModel += xmlData[i];
70✔
245
                i++;
70✔
246
            }
70✔
247
            if (xmlData[i] !== ")") {
10!
248
                throw new Error("Unterminated content model");
×
UNCOV
249
            }
×
250

10✔
251
        } else if (!this.suppressValidationErr) {
10!
252
            throw new Error(`Invalid Element Expression, found "${xmlData[i]}"`);
×
UNCOV
253
        }
×
254

20✔
255
        return {
20✔
256
            elementName,
20✔
257
            contentModel: contentModel.trim(),
20✔
258
            index: i
20✔
259
        };
20✔
260
    }
20✔
261

5✔
262
    readAttlistExp(xmlData, i) {
5✔
263
        // Skip leading whitespace after <!ATTLIST
×
264
        i = skipWhitespace(xmlData, i);
×
265

×
266
        // Read element name
×
267
        let elementName = "";
×
268
        while (i < xmlData.length && !/\s/.test(xmlData[i])) {
×
269
            elementName += xmlData[i];
×
270
            i++;
×
271
        }
×
272

×
273
        // Validate element name
×
274
        validateEntityName(elementName)
×
275

×
276
        // Skip whitespace after element name
×
277
        i = skipWhitespace(xmlData, i);
×
278

×
279
        // Read attribute name
×
280
        let attributeName = "";
×
281
        while (i < xmlData.length && !/\s/.test(xmlData[i])) {
×
282
            attributeName += xmlData[i];
×
283
            i++;
×
284
        }
×
285

×
286
        // Validate attribute name
×
287
        if (!validateEntityName(attributeName)) {
×
288
            throw new Error(`Invalid attribute name: "${attributeName}"`);
×
289
        }
×
290

×
291
        // Skip whitespace after attribute name
×
292
        i = skipWhitespace(xmlData, i);
×
293

×
294
        // Read attribute type
×
295
        let attributeType = "";
×
296
        if (xmlData.substring(i, i + 8).toUpperCase() === "NOTATION") {
×
297
            attributeType = "NOTATION";
×
298
            i += 8; // Move past "NOTATION"
×
299

×
300
            // Skip whitespace after "NOTATION"
×
301
            i = skipWhitespace(xmlData, i);
×
302

×
303
            // Expect '(' to start the list of notations
×
304
            if (xmlData[i] !== "(") {
×
305
                throw new Error(`Expected '(', found "${xmlData[i]}"`);
×
306
            }
×
307
            i++; // Move past '('
×
308

×
309
            // Read the list of allowed notations
×
310
            let allowedNotations = [];
×
311
            while (i < xmlData.length && xmlData[i] !== ")") {
×
312
                let notation = "";
×
313
                while (i < xmlData.length && xmlData[i] !== "|" && xmlData[i] !== ")") {
×
314
                    notation += xmlData[i];
×
315
                    i++;
×
316
                }
×
317

×
318
                // Validate notation name
×
319
                notation = notation.trim();
×
320
                if (!validateEntityName(notation)) {
×
321
                    throw new Error(`Invalid notation name: "${notation}"`);
×
322
                }
×
323

×
324
                allowedNotations.push(notation);
×
325

×
326
                // Skip '|' separator or exit loop
×
327
                if (xmlData[i] === "|") {
×
328
                    i++; // Move past '|'
×
329
                    i = skipWhitespace(xmlData, i); // Skip optional whitespace after '|'
×
330
                }
×
331
            }
×
332

×
333
            if (xmlData[i] !== ")") {
×
334
                throw new Error("Unterminated list of notations");
×
335
            }
×
336
            i++; // Move past ')'
×
337

×
338
            // Store the allowed notations as part of the attribute type
×
339
            attributeType += " (" + allowedNotations.join("|") + ")";
×
340
        } else {
×
341
            // Handle simple types (e.g., CDATA, ID, IDREF, etc.)
×
342
            while (i < xmlData.length && !/\s/.test(xmlData[i])) {
×
343
                attributeType += xmlData[i];
×
344
                i++;
×
345
            }
×
346

×
347
            // Validate simple attribute type
×
348
            const validTypes = ["CDATA", "ID", "IDREF", "IDREFS", "ENTITY", "ENTITIES", "NMTOKEN", "NMTOKENS"];
×
349
            if (!this.suppressValidationErr && !validTypes.includes(attributeType.toUpperCase())) {
×
350
                throw new Error(`Invalid attribute type: "${attributeType}"`);
×
351
            }
×
352
        }
×
353

×
354
        // Skip whitespace after attribute type
×
355
        i = skipWhitespace(xmlData, i);
×
356

×
357
        // Read default value
×
358
        let defaultValue = "";
×
359
        if (xmlData.substring(i, i + 8).toUpperCase() === "#REQUIRED") {
×
360
            defaultValue = "#REQUIRED";
×
361
            i += 8;
×
362
        } else if (xmlData.substring(i, i + 7).toUpperCase() === "#IMPLIED") {
×
363
            defaultValue = "#IMPLIED";
×
364
            i += 7;
×
365
        } else {
×
366
            [i, defaultValue] = this.readIdentifierVal(xmlData, i, "ATTLIST");
×
367
        }
×
368

×
369
        return {
×
370
            elementName,
×
371
            attributeName,
×
372
            attributeType,
×
373
            defaultValue,
×
374
            index: i
×
375
        }
×
UNCOV
376
    }
×
377
}
5✔
378

5✔
379

5✔
380

5✔
381
const skipWhitespace = (data, index) => {
5✔
382
    while (index < data.length && /\s/.test(data[index])) {
535✔
383
        index++;
540✔
384
    }
540✔
385
    return index;
535✔
386
};
5✔
387

5✔
388

5✔
389

5✔
390
function hasSeq(data, seq, i) {
460✔
391
    for (let j = 0; j < seq.length; j++) {
460✔
392
        if (seq[j] !== data[i + j + 1]) return false;
2,345✔
393
    }
2,345✔
394
    return true;
300✔
395
}
460✔
396

5✔
397
function validateEntityName(name) {
240✔
398
    if (isName(name))
240✔
399
        return name;
240✔
400
    else
5✔
401
        throw new Error(`Invalid entity name ${name}`);
5✔
402
}
240✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc