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

NaturalIntelligence / fast-xml-parser / 24500125020

16 Apr 2026 08:26AM UTC coverage: 97.64% (+0.02%) from 97.617%
24500125020

push

github

amitguptagwl
remove unwanted tests

1135 of 1182 branches covered (96.02%)

9392 of 9619 relevant lines covered (97.64%)

463944.63 hits per line

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

64.13
/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,700✔
6
        this.options = options;
1,700✔
7
    }
1,700✔
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 != null &&
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] = val;
205✔
40
                            entityCount++;
205✔
41
                        }
205✔
42
                    }
230✔
43
                    else if (hasBody && hasSeq(xmlData, "!ELEMENT", i)) {
60✔
44
                        i += 8;//Not supported
20✔
45
                        const { index } = this.readElementExp(xmlData, i + 1);
20✔
46
                        i = index;
20✔
47
                    } else if (hasBody && hasSeq(xmlData, "!ATTLIST", i)) {
60✔
48
                        i += 8;//Not supported
5✔
49
                        // const {index} = this.readAttlistExp(xmlData,i+1);
5✔
50
                        // i = index;
5✔
51
                    } else if (hasBody && hasSeq(xmlData, "!NOTATION", i)) {
40✔
52
                        i += 9;//Not supported
10✔
53
                        const { index } = this.readNotationExp(xmlData, i + 1, this.suppressValidationErr);
10✔
54
                        i = index;
10✔
55
                    } else if (hasSeq(xmlData, "!--", i)) comment = true;
35✔
56
                    else throw new Error(`Invalid DOCTYPE`);
×
57

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

230✔
90
        //Parameter entities are not supported
230✔
91
        //    <!ENTITY entityname "&anotherElement;">
230✔
92

230✔
93
        //Internal entities are supported
230✔
94
        //    <!ENTITY entityname "replacement text">
230✔
95

230✔
96
        // Skip leading whitespace after <!ENTITY
230✔
97
        i = skipWhitespace(xmlData, i);
230✔
98

230✔
99
        // Read entity name
230✔
100
        const startIndex = i;
230✔
101
        while (i < xmlData.length && !/\s/.test(xmlData[i]) && xmlData[i] !== '"' && xmlData[i] !== "'") {
230✔
102
            i++;
805✔
103
        }
805✔
104
        let entityName = xmlData.substring(startIndex, i);
230✔
105

230✔
106
        validateEntityName(entityName);
230✔
107

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

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

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

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

210✔
133
        i--;
210✔
134
        return [entityName, entityValue, i];
210✔
135
    }
230✔
136

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

10✔
141
        // Read notation name
10✔
142

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

10✔
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}"`);
×
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
            }
×
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}"`);
×
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`);
×
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
        const startIndex = i;
20✔
223
        while (i < xmlData.length && !/\s/.test(xmlData[i])) {
20✔
224
            i++;
65✔
225
        }
65✔
226
        let elementName = xmlData.substring(startIndex, i);
20✔
227

20✔
228
        // Validate element name
20✔
229
        if (!this.suppressValidationErr && !isName(elementName)) {
20!
230
            throw new Error(`Invalid element name: "${elementName}"`);
×
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
            const startIndex = i;
10✔
244
            while (i < xmlData.length && xmlData[i] !== ")") {
10✔
245
                i++;
70✔
246
            }
70✔
247
            contentModel = xmlData.substring(startIndex, i);
10✔
248

10✔
249
            if (xmlData[i] !== ")") {
10!
250
                throw new Error("Unterminated content model");
×
251
            }
×
252

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

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

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

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

×
275
        // Validate element name
×
276
        validateEntityName(elementName)
×
277

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

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

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

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

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

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

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

×
311
            // Read the list of allowed notations
×
312
            let allowedNotations = [];
×
313
            while (i < xmlData.length && xmlData[i] !== ")") {
×
314

×
315

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

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

×
328
                allowedNotations.push(notation);
×
329

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

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

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

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

×
359
        // Skip whitespace after attribute type
×
360
        i = skipWhitespace(xmlData, i);
×
361

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

×
374
        return {
×
375
            elementName,
×
376
            attributeName,
×
377
            attributeType,
×
378
            defaultValue,
×
379
            index: i
×
380
        }
×
381
    }
×
382
}
5✔
383

5✔
384

5✔
385

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

5✔
393

5✔
394

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

5✔
402
function validateEntityName(name) {
240✔
403
    if (isName(name))
240✔
404
        return name;
240✔
405
    else
5✔
406
        throw new Error(`Invalid entity name ${name}`);
5✔
407
}
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