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

NaturalIntelligence / fast-xml-parser / 22607952071

03 Mar 2026 04:10AM UTC coverage: 97.689% (-0.2%) from 97.87%
22607952071

push

github

amitguptagwl
update release info

1029 of 1065 branches covered (96.62%)

8454 of 8654 relevant lines covered (97.69%)

515532.84 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

5✔
260
    readAttlistExp(xmlData, i) {
5✔
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);
×
300

×
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
    }
×
375
}
5✔
376

5✔
377

5✔
378

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

5✔
386

5✔
387

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

5✔
395
function validateEntityName(name) {
240✔
396
    if (isName(name))
240✔
397
        return name;
240✔
398
    else
5✔
399
        throw new Error(`Invalid entity name ${name}`);
5✔
400
}
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