{"record":{"id":"b0b0b32c4e35cca9","repo":"swc-project/swc","slug":"failed-to-parse-namespace-of-context-element-b0b0b3","errorCode":null,"errorMessage":"failed to parse namespace of context element","messagePattern":"failed to parse namespace of context element","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"bindings/binding_html_wasm/src/lib.rs","lineNumber":278,"sourceCode":"const fn default_collapse_whitespaces() -> CollapseWhitespaces {\n    CollapseWhitespaces::OnlyMetadata\n}\n\nenum DocumentOrDocumentFragment {\n    Document(Document),\n    DocumentFragment(DocumentFragment),\n}\n\nfn create_namespace(namespace: &str) -> anyhow::Result<Namespace> {\n    match &*namespace.to_lowercase() {\n        \"http://www.w3.org/1999/xhtml\" => Ok(Namespace::HTML),\n        \"http://www.w3.org/1998/math/mathml\" => Ok(Namespace::MATHML),\n        \"http://www.w3.org/2000/svg\" => Ok(Namespace::SVG),\n        \"http://www.w3.org/1999/xlink\" => Ok(Namespace::XLINK),\n        \"http://www.w3.org/xml/1998/namespace\" => Ok(Namespace::XML),\n        \"http://www.w3.org/2000/xmlns/\" => Ok(Namespace::XMLNS),\n        _ => {\n            bail!(\"failed to parse namespace of context element\")\n        }\n    }\n}\n\nfn create_element(context_element: Element) -> anyhow::Result<swc_html_ast::Element> {\n    let mut attributes = Vec::with_capacity(context_element.attributes.len());\n\n    for attribute in context_element.attributes.into_iter() {\n        let namespace = match attribute.namespace {\n            Some(namespace) => Some(create_namespace(&namespace)?),\n            _ => None,\n        };\n\n        attributes.push(swc_html_ast::Attribute {\n            span: DUMMY_SP,\n            namespace,\n            prefix: attribute.prefix.map(|value| value.into()),\n            name: attribute.name.into(),","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/bindings/binding_html_wasm/src/lib.rs#L260-L296","documentation":"Same logic as the Node binding, compiled into the WebAssembly HTML binding: create_namespace accepts exactly six well-known namespace URIs (XHTML, MathML, SVG, XLINK, XML, XMLNS), compared lowercased, when converting the JS-side context element for fragment parsing. Any other namespace string on the element or its attributes fails before parsing begins; the error then crosses the wasm boundary as a JS exception.","triggerScenarios":"Calling the wasm HTML parse API in fragment mode with options.contextElement (or an attribute on it) whose namespace is not one of the six exact URIs, e.g. 'http://www.w3.org/2000/svg/' with a trailing slash or a custom namespace URI.","commonSituations":"Browser or bundler-integrated tooling reusing context elements parsed by other libraries (DOMParser, parse5) that carry extra namespaces; typos or trailing slashes in hardcoded URIs; assuming the wasm build accepts any namespace because the TS type is string.","solutions":["Normalize namespaces to one of the six exact URIs before calling the API (only 'http://www.w3.org/2000/xmlns/' ends with a slash)","Omit namespace for un-namespaced elements and attributes","Filter or map unknown namespaces from context elements produced by upstream parsers"],"exampleFix":"// before\ncontextElement = { tagName: 'svg', namespace: el.namespaceURI };\n// el.namespaceURI could be anything DOMParser produced\n\n// after\nconst KNOWN = new Set([\n  'http://www.w3.org/1999/xhtml',\n  'http://www.w3.org/1998/math/mathml',\n  'http://www.w3.org/2000/svg',\n  'http://www.w3.org/1999/xlink',\n  'http://www.w3.org/xml/1998/namespace',\n  'http://www.w3.org/2000/xmlns/',\n]);\ncontextElement = {\n  tagName: 'svg',\n  namespace: KNOWN.has(el.namespaceURI) ? el.namespaceURI : undefined,\n};","handlingStrategy":"validation","validationCode":"const KNOWN = new Set([\n  'http://www.w3.org/1999/xhtml',\n  'http://www.w3.org/1998/math/mathml',\n  'http://www.w3.org/2000/svg',\n  'http://www.w3.org/1999/xlink',\n  'http://www.w3.org/xml/1998/namespace',\n  'http://www.w3.org/2000/xmlns/',\n]);\nfunction sanitizeContextElement(el) {\n  const ns = el.namespace?.toLowerCase();\n  if (ns && !KNOWN.has(ns)) delete el.namespace;\n  el.attributes = (el.attributes ?? []).map((a) => {\n    if (a.namespace && !KNOWN.has(a.namespace.toLowerCase())) {\n      const { namespace, ...rest } = a;\n      return rest;\n    }\n    return a;\n  });\n  return el;\n}","typeGuard":"const isKnownNamespace = (ns: string): boolean =>\n  [\n    'http://www.w3.org/1999/xhtml',\n    'http://www.w3.org/1998/math/mathml',\n    'http://www.w3.org/2000/svg',\n    'http://www.w3.org/1999/xlink',\n    'http://www.w3.org/xml/1998/namespace',\n    'http://www.w3.org/2000/xmlns/',\n  ].includes(ns.toLowerCase());","tryCatchPattern":"try {\n  const res = await parse(src, { contextElement });\n} catch (e) {\n  if (String(e?.message).includes('namespace of context element')) {\n    return parse(src, { contextElement: sanitizeContextElement(contextElement) });\n  }\n  throw e;\n}","preventionTips":["Sanitize DOMParser-derived context elements before passing them to the wasm binding","Keep namespace constants in one shared module for both Node and wasm paths","Add a unit test that feeds each of the six URIs through your context element builder"],"tags":["swc","html","namespace","context-element","wasm-bindings"],"backgroundTag":"unknown-namespace-uri","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"}