{"record":{"id":"15e1b661a37e7aef","repo":"swc-project/swc","slug":"failed-to-parse-input-as-document-15e1b6","errorCode":null,"errorMessage":"failed to parse input as document","messagePattern":"failed to parse input as document","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"bindings/binding_html_wasm/src/lib.rs","lineNumber":632,"sourceCode":"                    &fm,\n                    swc_html::parser::parser::ParserConfig {\n                        scripting_enabled,\n                        iframe_srcdoc: opts.iframe_srcdoc,\n                        ..Default::default()\n                    },\n                    &mut errors,\n                );\n\n                let document = match document {\n                    Ok(v) => v,\n                    Err(err) => {\n                        err.to_diagnostics(handler).emit();\n\n                        for err in errors {\n                            err.to_diagnostics(handler).emit();\n                        }\n\n                        bail!(\"failed to parse input as document\")\n                    }\n                };\n\n                (DocumentOrDocumentFragment::Document(document), None)\n            };\n\n            let mut returned_errors = None;\n\n            if !errors.is_empty() {\n                returned_errors = Some(Vec::with_capacity(errors.len()));\n\n                for err in errors {\n                    let mut buf = vec![];\n\n                    err.to_diagnostics(handler).buffer(&mut buf);\n\n                    for i in buf {\n                        returned_errors.as_mut().unwrap().push(Diagnostic {","sourceCodeStart":614,"sourceCodeEnd":650,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/bindings/binding_html_wasm/src/lib.rs#L614-L650","documentation":"The WebAssembly HTML binding's document mode: parse_file_as_document returned Err, so the binding emits the parser error plus all recoverable errors as diagnostics and then bails with this generic message, which crosses the wasm boundary as a JS exception. The diagnostics printed before it are the real diagnosis. Mirrors the Node binding exactly; fatal document parse failures are rare because HTML parsing recovers from most errors.","triggerScenarios":"Calling the wasm HTML parse API without a context element with input that fatally breaks document parsing (structurally impossible markup, truncated or non-HTML payloads).","commonSituations":"Browser-side pipelines passing user-supplied or scraped HTML without pre-validation; content that begins like HTML but degrades mid-stream; swc_html version regressions.","solutions":["Read the emitted diagnostics for exact positions and messages","Reduce the document around the first diagnostic to a minimal repro","Pre-validate untrusted input with DOMParser and reject failures before invoking the binding","File an swc_html issue if the input is valid HTML"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"function preflightDocument(input) {\n  if (typeof input !== 'string' || input.trim().length === 0) {\n    throw new TypeError('document input must be a non-empty string');\n  }\n  if (typeof DOMParser !== 'undefined') {\n    const doc = new DOMParser().parseFromString(input, 'text/html');\n    if (doc.querySelector('parsererror')) return false;\n  }\n  return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n  const res = await parse(src);\n} catch (e) {\n  if (String(e?.message).includes('failed to parse input as document')) {\n    return { ok: false, reason: 'invalid-html', raw: src };\n  }\n  throw e;\n}","preventionTips":["Never feed unvalidated scraped or user HTML straight into the wasm parser","Surface the emitted diagnostics in error reports - the bail message alone is not diagnosable","Version-lock the wasm binding so parser behavior is reproducible"],"tags":["swc","html","parser","document","wasm-bindings"],"backgroundTag":"html-parse-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"}