Skip to content

Commit 38d0234

Browse files
committed
refactored code of DOCTYPE
1 parent 7c6cba4 commit 38d0234

File tree

1 file changed

+9
-30
lines changed

1 file changed

+9
-30
lines changed

src/xmlparser/DocTypeReader.js

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ function readEntityExp(xmlData, i) {
9191
entityName += xmlData[i];
9292
i++;
9393
}
94-
95-
// Validate entity name
96-
if (!validateEntityName(entityName)) {
97-
throw new Error(`Invalid entity name: "${entityName}"`);
98-
}
94+
validateEntityName(entityName);
9995

10096
// Skip whitespace after entity name
10197
i = skipWhitespace(xmlData, i);
@@ -108,22 +104,9 @@ function readEntityExp(xmlData, i) {
108104
}
109105

110106
// Read entity value (internal entity)
111-
const quoteChar = xmlData[i];
112-
if (quoteChar !== '"' && quoteChar !== "'") {
113-
throw new Error(`Expected quoted string, found "${quoteChar}"`);
114-
}
115-
i++;
116-
117107
let entityValue = "";
118-
while (i < xmlData.length && xmlData[i] !== quoteChar) {
119-
entityValue += xmlData[i];
120-
i++;
121-
}
122-
123-
if (xmlData[i] !== quoteChar) {
124-
throw new Error("Unterminated entity value");
125-
}
126-
108+
[i, entityValue] = readIdentifierVal(xmlData, i, "entity");
109+
i--;
127110
return [entityName, entityValue, i ];
128111
}
129112

@@ -137,11 +120,7 @@ function readNotationExp(xmlData, i) {
137120
notationName += xmlData[i];
138121
i++;
139122
}
140-
141-
// Validate notation name
142-
if (!validateEntityName(notationName)) {
143-
throw new Error(`Invalid notation name: "${notationName}"`);
144-
}
123+
validateEntityName(notationName);
145124

146125
// Skip whitespace after notation name
147126
i = skipWhitespace(xmlData, i);
@@ -161,18 +140,18 @@ function readNotationExp(xmlData, i) {
161140
let systemIdentifier = null;
162141

163142
if (identifierType === "PUBLIC") {
164-
[i, publicIdentifier ] = readIdentifierVal(xmlData, i);
143+
[i, publicIdentifier ] = readIdentifierVal(xmlData, i, "publicIdentifier");
165144

166145
// Skip whitespace after public identifier
167146
i = skipWhitespace(xmlData, i);
168147

169148
// Optionally read system identifier
170149
if (xmlData[i] === '"' || xmlData[i] === "'") {
171-
[i, systemIdentifier ] = readIdentifierVal(xmlData, i);
150+
[i, systemIdentifier ] = readIdentifierVal(xmlData, i,"systemIdentifier");
172151
}
173152
} else if (identifierType === "SYSTEM") {
174153
// Read system identifier (mandatory for SYSTEM)
175-
[i, systemIdentifier ] = readIdentifierVal(xmlData, i);
154+
[i, systemIdentifier ] = readIdentifierVal(xmlData, i, "systemIdentifier");
176155

177156
if (!systemIdentifier) {
178157
throw new Error("Missing mandatory system identifier for SYSTEM notation");
@@ -182,7 +161,7 @@ function readNotationExp(xmlData, i) {
182161
return {notationName, publicIdentifier, systemIdentifier, index: --i};
183162
}
184163

185-
function readIdentifierVal(xmlData, i) {
164+
function readIdentifierVal(xmlData, i, type) {
186165
let identifierVal = "";
187166
const startChar = xmlData[i];
188167
if (startChar !== '"' && startChar !== "'") {
@@ -196,7 +175,7 @@ function readIdentifierVal(xmlData, i) {
196175
}
197176

198177
if (xmlData[i] !== startChar) {
199-
throw new Error("Unterminated identifier");
178+
throw new Error(`Unterminated ${type} value`);
200179
}
201180
i++;
202181
return [i, identifierVal];

0 commit comments

Comments
 (0)