Skip to content

Commit 41abd66

Browse files
committed
performance improvement of reading DOCTYPE
1 parent 3dfcd20 commit 41abd66

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/xmlparser/DocTypeReader.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ export default class DocTypeReader {
100100
i = skipWhitespace(xmlData, i);
101101

102102
// Read entity name
103-
let entityName = "";
103+
const startIndex = i;
104104
while (i < xmlData.length && !/\s/.test(xmlData[i]) && xmlData[i] !== '"' && xmlData[i] !== "'") {
105-
entityName += xmlData[i];
106105
i++;
107106
}
107+
let entityName = xmlData.substring(startIndex, i);
108+
108109
validateEntityName(entityName);
109110

110111
// Skip whitespace after entity name
@@ -141,11 +142,13 @@ export default class DocTypeReader {
141142
i = skipWhitespace(xmlData, i);
142143

143144
// Read notation name
144-
let notationName = "";
145+
146+
const startIndex = i;
145147
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
146-
notationName += xmlData[i];
147148
i++;
148149
}
150+
let notationName = xmlData.substring(startIndex, i);
151+
149152
!this.suppressValidationErr && validateEntityName(notationName);
150153

151154
// Skip whitespace after notation name
@@ -219,11 +222,11 @@ export default class DocTypeReader {
219222
i = skipWhitespace(xmlData, i);
220223

221224
// Read element name
222-
let elementName = "";
225+
const startIndex = i;
223226
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
224-
elementName += xmlData[i];
225227
i++;
226228
}
229+
let elementName = xmlData.substring(startIndex, i);
227230

228231
// Validate element name
229232
if (!this.suppressValidationErr && !isName(elementName)) {
@@ -240,10 +243,12 @@ export default class DocTypeReader {
240243
i++; // Move past '('
241244

242245
// Read content model
246+
const startIndex = i;
243247
while (i < xmlData.length && xmlData[i] !== ")") {
244-
contentModel += xmlData[i];
245248
i++;
246249
}
250+
contentModel = xmlData.substring(startIndex, i);
251+
247252
if (xmlData[i] !== ")") {
248253
throw new Error("Unterminated content model");
249254
}
@@ -264,11 +269,11 @@ export default class DocTypeReader {
264269
i = skipWhitespace(xmlData, i);
265270

266271
// Read element name
267-
let elementName = "";
272+
let startIndex = i;
268273
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
269-
elementName += xmlData[i];
270274
i++;
271275
}
276+
let elementName = xmlData.substring(startIndex, i);
272277

273278
// Validate element name
274279
validateEntityName(elementName)
@@ -277,11 +282,11 @@ export default class DocTypeReader {
277282
i = skipWhitespace(xmlData, i);
278283

279284
// Read attribute name
280-
let attributeName = "";
285+
startIndex = i;
281286
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
282-
attributeName += xmlData[i];
283287
i++;
284288
}
289+
let attributeName = xmlData.substring(startIndex, i);
285290

286291
// Validate attribute name
287292
if (!validateEntityName(attributeName)) {
@@ -309,11 +314,13 @@ export default class DocTypeReader {
309314
// Read the list of allowed notations
310315
let allowedNotations = [];
311316
while (i < xmlData.length && xmlData[i] !== ")") {
312-
let notation = "";
317+
318+
319+
const startIndex = i;
313320
while (i < xmlData.length && xmlData[i] !== "|" && xmlData[i] !== ")") {
314-
notation += xmlData[i];
315321
i++;
316322
}
323+
let notation = xmlData.substring(startIndex, i);
317324

318325
// Validate notation name
319326
notation = notation.trim();
@@ -339,10 +346,11 @@ export default class DocTypeReader {
339346
attributeType += " (" + allowedNotations.join("|") + ")";
340347
} else {
341348
// Handle simple types (e.g., CDATA, ID, IDREF, etc.)
349+
const startIndex = i;
342350
while (i < xmlData.length && !/\s/.test(xmlData[i])) {
343-
attributeType += xmlData[i];
344351
i++;
345352
}
353+
attributeType += xmlData.substring(startIndex, i);
346354

347355
// Validate simple attribute type
348356
const validTypes = ["CDATA", "ID", "IDREF", "IDREFS", "ENTITY", "ENTITIES", "NMTOKEN", "NMTOKENS"];

0 commit comments

Comments
 (0)