swc-project/swc · error

failed to parse input as document

Error message

failed to parse input as document

What it means

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.

Source

Thrown at bindings/binding_html_wasm/src/lib.rs:632

                    &fm,
                    swc_html::parser::parser::ParserConfig {
                        scripting_enabled,
                        iframe_srcdoc: opts.iframe_srcdoc,
                        ..Default::default()
                    },
                    &mut errors,
                );

                let document = match document {
                    Ok(v) => v,
                    Err(err) => {
                        err.to_diagnostics(handler).emit();

                        for err in errors {
                            err.to_diagnostics(handler).emit();
                        }

                        bail!("failed to parse input as document")
                    }
                };

                (DocumentOrDocumentFragment::Document(document), None)
            };

            let mut returned_errors = None;

            if !errors.is_empty() {
                returned_errors = Some(Vec::with_capacity(errors.len()));

                for err in errors {
                    let mut buf = vec![];

                    err.to_diagnostics(handler).buffer(&mut buf);

                    for i in buf {
                        returned_errors.as_mut().unwrap().push(Diagnostic {

View on GitHub (pinned to 5176682b65)

Solutions

  1. Read the emitted diagnostics for exact positions and messages
  2. Reduce the document around the first diagnostic to a minimal repro
  3. Pre-validate untrusted input with DOMParser and reject failures before invoking the binding
  4. File an swc_html issue if the input is valid HTML
Defensive patterns

Strategy: try-catch

Validate before calling

function preflightDocument(input) {
  if (typeof input !== 'string' || input.trim().length === 0) {
    throw new TypeError('document input must be a non-empty string');
  }
  if (typeof DOMParser !== 'undefined') {
    const doc = new DOMParser().parseFromString(input, 'text/html');
    if (doc.querySelector('parsererror')) return false;
  }
  return true;
}

Try / catch

try {
  const res = await parse(src);
} catch (e) {
  if (String(e?.message).includes('failed to parse input as document')) {
    return { ok: false, reason: 'invalid-html', raw: src };
  }
  throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/15e1b661a37e7aef. Report an issue: GitHub.