{"record":{"id":"2d84834b2a142c56","repo":"swc-project/swc","slug":"failed-to-parse-input-as-document-fragment","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_node/src/lib.rs","lineNumber":560,"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":542,"sourceCodeEnd":578,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/bindings/binding_html_node/src/lib.rs#L542-L578","documentation":"The Node HTML binding's fragment mode (parse with a context element) calls parse_file_as_document_fragment; if that returns a fatal error, the binding first emits the parser error plus every accumulated recoverable error as diagnostics and then bails with this generic message. The actionable detail lives in the emitted diagnostics, not in this string. It means the fragment was invalid in a way the error-tolerant HTML parser could not recover from.","triggerScenarios":"Calling the fragment parse path (input plus contextElement) with markup that fatally violates parsing rules for the given context, e.g. content that is illegal inside the chosen context element or structurally broken input the parser cannot recover from.","commonSituations":"Parsing template fragments with the wrong context element (e.g. table-fragment rules under a div context); passing empty or non-HTML payloads; regressions in swc_html parser versions that turn previously-recovered errors into fatal ones.","solutions":["Read the diagnostics emitted immediately before the bail - they carry file, line, column and the real parser message","Reproduce with a minimal fragment and the same contextElement to isolate the offending markup","Drop the contextElement and try document mode to see whether the fragment context itself is the problem","If diagnostics flag valid HTML, file an swc_html issue with the minimal repro and both parser errors"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"function preflightFragment(input, contextElement) {\n  if (typeof input !== 'string' || input.length === 0) {\n    throw new TypeError('fragment input must be a non-empty string');\n  }\n  // cheap DOM-based check for untrusted input\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 = parseSync(src, { contextElement });\n} catch (e) {\n  if (String(e?.message).includes('failed to parse input as document fragment')) {\n    // the diagnostics emitted to stderr carry the real positions; collect them if the binding exposes an errors return\n    return { ok: false, reason: 'invalid-fragment', raw: src };\n  }\n  throw e;\n}","preventionTips":["Validate untrusted HTML with a tolerant parser before handing it to the binding","Keep the context element consistent with the fragment kind (svg fragments under an svg context)","Log emitted diagnostics, not just the generic bail message"],"tags":["swc","html","parser","document-fragment","node-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"}