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

NaturalIntelligence / fast-xml-parser / 23148287765

16 Mar 2026 02:16PM UTC coverage: 97.68% (-0.03%) from 97.709%
23148287765

push

github

amitguptagwl
performance improvement of reading DOCTYPE

1183 of 1232 branches covered (96.02%)

12 of 22 new or added lines in 1 file covered. (54.55%)

9387 of 9610 relevant lines covered (97.68%)

464372.67 hits per line

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

64.39
/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✔
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`);
×
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
        const startIndex = i;
230✔
104
        while (i < xmlData.length && !/\s/.test(xmlData[i]) && xmlData[i] !== '"' && xmlData[i] !== "'") {
230✔
105
            i++;
805✔
106
        }
805✔
107
        let entityName = xmlData.substring(startIndex, i);
230✔
108

230✔
109
        validateEntityName(entityName);
230✔
110

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

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

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

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

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

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

10✔
144
        // Read notation name
10✔
145

10✔
146
        const startIndex = i;
10✔
147
        while (i < xmlData.length && !/\s/.test(xmlData[i])) {
10✔
148
            i++;
45✔
149
        }
45✔
150
        let notationName = xmlData.substring(startIndex, i);
10✔
151

10✔
152
        !this.suppressValidationErr && validateEntityName(notationName);
10✔
153

10✔
154
        // Skip whitespace after notation name
10✔
155
        i = skipWhitespace(xmlData, i);
10✔
156

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

10✔
164
        // Skip whitespace after identifier type
10✔
165
        i = skipWhitespace(xmlData, i);
10✔
166

10✔
167
        // Read public identifier (if PUBLIC)
10✔
168
        let publicIdentifier = null;
10✔
169
        let systemIdentifier = null;
10✔
170

10✔
171
        if (identifierType === "PUBLIC") {
10✔
172
            [i, publicIdentifier] = this.readIdentifierVal(xmlData, i, "publicIdentifier");
10✔
173

10✔
174
            // Skip whitespace after public identifier
10✔
175
            i = skipWhitespace(xmlData, i);
10✔
176

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

×
185
            if (!this.suppressValidationErr && !systemIdentifier) {
×
186
                throw new Error("Missing mandatory system identifier for SYSTEM notation");
×
187
            }
×
188
        }
×
189

10✔
190
        return { notationName, publicIdentifier, systemIdentifier, index: --i };
10✔
191
    }
10✔
192

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

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

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

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

20✔
221
        // Skip leading whitespace after <!ELEMENT
20✔
222
        i = skipWhitespace(xmlData, i);
20✔
223

20✔
224
        // Read element name
20✔
225
        const startIndex = i;
20✔
226
        while (i < xmlData.length && !/\s/.test(xmlData[i])) {
20✔
227
            i++;
65✔
228
        }
65✔
229
        let elementName = xmlData.substring(startIndex, i);
20✔
230

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

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

10✔
245
            // Read content model
10✔
246
            const startIndex = i;
10✔
247
            while (i < xmlData.length && xmlData[i] !== ")") {
10✔
248
                i++;
70✔
249
            }
70✔
250
            contentModel = xmlData.substring(startIndex, i);
10✔
251

10✔
252
            if (xmlData[i] !== ")") {
10!
253
                throw new Error("Unterminated content model");
×
254
            }
×
255

10✔
256
        } else if (!this.suppressValidationErr) {
10!
257
            throw new Error(`Invalid Element Expression, found "${xmlData[i]}"`);
×
258
        }
×
259

20✔
260
        return {
20✔
261
            elementName,
20✔
262
            contentModel: contentModel.trim(),
20✔
263
            index: i
20✔
264
        };
20✔
265
    }
20✔
266

5✔
267
    readAttlistExp(xmlData, i) {
5✔
268
        // Skip leading whitespace after <!ATTLIST
×
269
        i = skipWhitespace(xmlData, i);
×
270

×
271
        // Read element name
×
NEW
272
        let startIndex = i;
×
273
        while (i < xmlData.length && !/\s/.test(xmlData[i])) {
×
274
            i++;
×
275
        }
×
NEW
276
        let elementName = xmlData.substring(startIndex, i);
×
277

×
278
        // Validate element name
×
279
        validateEntityName(elementName)
×
280

×
281
        // Skip whitespace after element name
×
282
        i = skipWhitespace(xmlData, i);
×
283

×
284
        // Read attribute name
×
NEW
285
        startIndex = i;
×
286
        while (i < xmlData.length && !/\s/.test(xmlData[i])) {
×
287
            i++;
×
288
        }
×
NEW
289
        let attributeName = xmlData.substring(startIndex, i);
×
290

×
291
        // Validate attribute name
×
292
        if (!validateEntityName(attributeName)) {
×
293
            throw new Error(`Invalid attribute name: "${attributeName}"`);
×
294
        }
×
295

×
296
        // Skip whitespace after attribute name
×
297
        i = skipWhitespace(xmlData, i);
×
298

×
299
        // Read attribute type
×
300
        let attributeType = "";
×
301
        if (xmlData.substring(i, i + 8).toUpperCase() === "NOTATION") {
×
302
            attributeType = "NOTATION";
×
303
            i += 8; // Move past "NOTATION"
×
304

×
305
            // Skip whitespace after "NOTATION"
×
306
            i = skipWhitespace(xmlData, i);
×
307

×
308
            // Expect '(' to start the list of notations
×
309
            if (xmlData[i] !== "(") {
×
310
                throw new Error(`Expected '(', found "${xmlData[i]}"`);
×
311
            }
×
312
            i++; // Move past '('
×
313

×
314
            // Read the list of allowed notations
×
315
            let allowedNotations = [];
×
316
            while (i < xmlData.length && xmlData[i] !== ")") {
×
NEW
317

×
NEW
318

×
NEW
319
                const startIndex = i;
×
320
                while (i < xmlData.length && xmlData[i] !== "|" && xmlData[i] !== ")") {
×
321
                    i++;
×
322
                }
×
NEW
323
                let notation = xmlData.substring(startIndex, i);
×
324

×
325
                // Validate notation name
×
326
                notation = notation.trim();
×
327
                if (!validateEntityName(notation)) {
×
328
                    throw new Error(`Invalid notation name: "${notation}"`);
×
329
                }
×
330

×
331
                allowedNotations.push(notation);
×
332

×
333
                // Skip '|' separator or exit loop
×
334
                if (xmlData[i] === "|") {
×
335
                    i++; // Move past '|'
×
336
                    i = skipWhitespace(xmlData, i); // Skip optional whitespace after '|'
×
337
                }
×
338
            }
×
339

×
340
            if (xmlData[i] !== ")") {
×
341
                throw new Error("Unterminated list of notations");
×
342
            }
×
343
            i++; // Move past ')'
×
344

×
345
            // Store the allowed notations as part of the attribute type
×
346
            attributeType += " (" + allowedNotations.join("|") + ")";
×
347
        } else {
×
348
            // Handle simple types (e.g., CDATA, ID, IDREF, etc.)
×
NEW
349
            const startIndex = i;
×
350
            while (i < xmlData.length && !/\s/.test(xmlData[i])) {
×
351
                i++;
×
352
            }
×
NEW
353
            attributeType += xmlData.substring(startIndex, i);
×
354

×
355
            // Validate simple attribute type
×
356
            const validTypes = ["CDATA", "ID", "IDREF", "IDREFS", "ENTITY", "ENTITIES", "NMTOKEN", "NMTOKENS"];
×
357
            if (!this.suppressValidationErr && !validTypes.includes(attributeType.toUpperCase())) {
×
358
                throw new Error(`Invalid attribute type: "${attributeType}"`);
×
359
            }
×
360
        }
×
361

×
362
        // Skip whitespace after attribute type
×
363
        i = skipWhitespace(xmlData, i);
×
364

×
365
        // Read default value
×
366
        let defaultValue = "";
×
367
        if (xmlData.substring(i, i + 8).toUpperCase() === "#REQUIRED") {
×
368
            defaultValue = "#REQUIRED";
×
369
            i += 8;
×
370
        } else if (xmlData.substring(i, i + 7).toUpperCase() === "#IMPLIED") {
×
371
            defaultValue = "#IMPLIED";
×
372
            i += 7;
×
373
        } else {
×
374
            [i, defaultValue] = this.readIdentifierVal(xmlData, i, "ATTLIST");
×
375
        }
×
376

×
377
        return {
×
378
            elementName,
×
379
            attributeName,
×
380
            attributeType,
×
381
            defaultValue,
×
382
            index: i
×
383
        }
×
384
    }
×
385
}
5✔
386

5✔
387

5✔
388

5✔
389
const skipWhitespace = (data, index) => {
5✔
390
    while (index < data.length && /\s/.test(data[index])) {
535✔
391
        index++;
540✔
392
    }
540✔
393
    return index;
535✔
394
};
5✔
395

5✔
396

5✔
397

5✔
398
function hasSeq(data, seq, i) {
460✔
399
    for (let j = 0; j < seq.length; j++) {
460✔
400
        if (seq[j] !== data[i + j + 1]) return false;
2,345✔
401
    }
2,345✔
402
    return true;
300✔
403
}
460✔
404

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

© 2026 Coveralls, Inc