{"record":{"id":"5e11f50de57b8df3","repo":"swc-project/swc","slug":"failed-to-parse-input-as-document-fragment-5e11f5","errorCode":null,"errorMessage":"failed to parse input as document fragment","messagePattern":"failed to parse input as document fragment","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"bindings/binding_html_wasm/src/lib.rs","lineNumber":604,"sourceCode":"                    form_element.as_ref(),\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_fragment = match document_fragment {\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 fragment\")\n                    }\n                };\n\n                (\n                    DocumentOrDocumentFragment::DocumentFragment(document_fragment),\n                    Some(context_element),\n                )\n            } else {\n                let document = parse_file_as_document(\n                    &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","sourceCodeStart":586,"sourceCodeEnd":622,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/bindings/binding_html_wasm/src/lib.rs#L586-L622","documentation":"The WebAssembly HTML binding's fragment mode (parse with context element): when parse_file_as_document_fragment returns a fatal error, the binding emits the parser error and buffered recoverable errors as diagnostics, then bails with this generic message that surfaces as a JS exception from the wasm module. The diagnostics carry the actual cause. Identical semantics to the Node binding, so the same input fails in both builds.","triggerScenarios":"Calling the wasm fragment parse API (input plus contextElement) with markup that the HTML parser cannot recover from under the given context.","commonSituations":"In-browser HTML tooling (template linters, sanitizers) feeding fragments with context-sensitive rules broken; passing payloads that are not HTML at all; wasm build used to pre-validate untrusted input without a pre-check.","solutions":["Surface the emitted diagnostics to the user instead of only the generic message","Minimize the fragment and retry with the same contextElement to locate the fatal construct","Validate/repair input before the call (e.g. with DOMParser) if input is untrusted","Report an swc_html issue with the repro if diagnostics contradict the HTML spec"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"function preflight(input) {\n  if (typeof input !== 'string' || input.length === 0) {\n    throw new TypeError('fragment 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, { contextElement });\n} catch (e) {\n  if (String(e?.message).includes('failed to parse input as document fragment')) {\n    return { ok: false, reason: 'invalid-fragment', raw: src };\n  }\n  throw e;\n}","preventionTips":["In browsers, pre-validate fragments with DOMParser before invoking the wasm parser","Return structured failure objects instead of letting generic exceptions bubble to UI","Keep a fixture corpus of known-good fragments for regression testing the wasm build"],"tags":["swc","html","parser","document-fragment","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-14T05:17:10.506Z"}