( content: string, )
| 175 | * @returns Validation result with parsed values or error message |
| 176 | */ |
| 177 | export 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) { |
no test coverage detected