{"record":{"id":"42643fe8fa886294","repo":"swc-project/swc","slug":"unexpectedcharacter","errorCode":"UnexpectedCharacter","errorMessage":"Unexpected character, only whitespace character allowed","messagePattern":"Unexpected character, only whitespace character allowed","errorType":"validation","errorClass":"swc_xml_parser::error::Error","httpStatus":null,"severity":"error","filePath":"crates/swc_xml_parser/src/parser/mod.rs","lineNumber":281,"sourceCode":"                    self.phase = Phase::EndPhase;\n                }\n                Token::Comment { .. } => {\n                    self.append_comment_to_doc(token_and_info)?;\n                }\n                Token::ProcessingInstruction { .. } => {\n                    self.append_processing_instruction_to_doc(token_and_info)?;\n                }\n                Token::Cdata { .. } => {\n                    self.errors.push(Error::new(\n                        token_and_info.span,\n                        ErrorKind::UnexpectedTokenInStartPhase,\n                    ));\n\n                    self.append_cdata_to_doc(token_and_info)?;\n                }\n                Token::Character { value, .. } => {\n                    if !is_whitespace(*value) {\n                        self.errors.push(Error::new(\n                            token_and_info.span,\n                            ErrorKind::UnexpectedCharacter,\n                        ));\n                    }\n                }\n                Token::Eof => {\n                    self.errors.push(Error::new(\n                        token_and_info.span,\n                        ErrorKind::UnexpectedEofInStartPhase,\n                    ));\n\n                    self.process_token(token_and_info, Some(Phase::EndPhase))?;\n                }\n                Token::Doctype { .. } => {\n                    let document_type = self.create_document_type_for_token(token_and_info);\n\n                    self.append_node(self.document.as_ref().unwrap(), document_type);\n                }","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_xml_parser/src/parser/mod.rs#L263-L299","documentation":"The XML start phase (before the root element) permits only whitespace character data. When a Character token with a non-whitespace value arrives there, `UnexpectedCharacter` is recorded at the token's span (crates/swc_xml_parser/src/parser/mod.rs:281) and the text is otherwise ignored — parsing continues waiting for the root element. This is the standard 'content before root element' well-formedness violation.","triggerScenarios":"Parsing input such as `hello<root/>` or `123<root/>` — any non-whitespace text before the first start tag reaches the Character branch of the start-phase handler.","commonSituations":"Log lines or banners prepended to XML responses; concatenation bugs that join plain text with an XML body; a BOM decoded as a character by a non-UTF-8-aware reader; response envelopes that prefix payloads with status text.","solutions":["Strip leading non-whitespace text before the root element in the producer","Locate what prepends the text (echo/log statement, banner, BOM) and remove it","Split mixed content: parse only the substring starting at the first `<` that begins the root element","Decode input as UTF-8 and drop a leading U+FEFF BOM before parsing"],"exampleFix":"<!-- before -->\nOK: <root><a/></root>\n<!-- after -->\n<root><a/></root>","handlingStrategy":"validation","validationCode":"// Verify only whitespace precedes the root element\nfunction assertPrologIsWhitespace(xml: string): void {\n  const rootIdx = xml.search(/<[a-zA-Z_:]/);\n  const prolog = (rootIdx === -1 ? xml : xml.slice(0, rootIdx))\n    .replace(/<\\?[\\s\\S]*?\\?>|<!--([\\s\\S]*?)-->|<!DOCTYPE[^>]*>/g, '');\n  if (/\\S/.test(prolog)) throw new Error('non-whitespace text before root element');\n}","typeGuard":null,"tryCatchPattern":"for err in parser.take_errors() {\n    if matches!(err.kind, ErrorKind::UnexpectedCharacter) {\n        // text before root was ignored; find and remove it at the producer\n    }\n}","preventionTips":["Never prepend banners/log text to XML responses; use headers or comments","Decode input as UTF-8 and strip a leading U+FEFF BOM before parsing","If mixing text and XML, split at the root start tag and parse only the XML part"],"tags":["xml","whitespace","prolog","well-formedness","parser"],"backgroundTag":"xml-wellformedness-error","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}