MCPcopy Create free account
hub / github.com/denoland/std / validateXmlDeclaration

Function validateXmlDeclaration

xml/_common.ts:177–381  ·  view source on GitHub ↗
(
  content: string,
)

Source from the content-addressed store, hash-verified

175 * @returns Validation result with parsed values or error message
176 */
177export function validateXmlDeclaration(
178 content: string,
179): XmlDeclarationValidationResult | XmlDeclarationValidationError {
180 // Fast-path: version is required and must be first, quick check before full parse
181 const trimmed = content.trimStart();
182 if (!trimmed.startsWith("version")) {
183 // Check for common errors: case variants
184 if (
185 trimmed.startsWith("VERSION") || trimmed.startsWith("Version")
186 ) {
187 const name = trimmed.slice(0, 7);
188 return {
189 valid: false,
190 error: `'${name}' must be lowercase in XML declaration`,
191 };
192 }
193 return {
194 valid: false,
195 error: "Missing required 'version' attribute in XML declaration",
196 };
197 }
198
199 let pos = 0;
200 const len = content.length;
201
202 let version: string | undefined;
203 let encoding: string | undefined;
204 let standalone: "yes" | "no" | undefined;
205 let foundVersion = false;
206 let foundEncoding = false;
207 let foundStandalone = false;
208
209 // Skip leading whitespace
210 while (pos < len && isXmlWhitespace(content.charCodeAt(pos))) {
211 pos++;
212 }
213
214 // Parse attributes
215 while (pos < len) {
216 // Read attribute name
217 const nameStart = pos;
218 while (
219 pos < len &&
220 !isXmlWhitespace(content.charCodeAt(pos)) &&
221 content.charCodeAt(pos) !== 0x3D // =
222 ) {
223 pos++;
224 }
225 const name = content.slice(nameStart, pos);
226
227 if (name === "") {
228 // Trailing whitespace only
229 break;
230 }
231
232 // Check for valid attribute names and order
233 if (name === "version") {
234 if (foundVersion) {

Callers 3

#emitDeclarationMethod · 0.90
_common_test.tsFile · 0.90
parseSyncFunction · 0.90

Calls 5

isXmlWhitespaceFunction · 0.85
isValidVersionNumFunction · 0.85
isValidEncNameFunction · 0.85
startsWithMethod · 0.80
sliceMethod · 0.45

Tested by

no test coverage detected