{"record":{"id":"4dc2ac4e15996ffd","repo":"siyuan-note/siyuan","slug":"render-svg-failed-w","errorCode":null,"errorMessage":"render svg failed: %w","messagePattern":"render svg failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/misc.go","lineNumber":394,"sourceCode":"\t\t\t\tif !strings.EqualFold(typed.Name.Local, \"svg\") {\n\t\t\t\t\treturn \"\", fmt.Errorf(\"root element is not svg\")\n\t\t\t\t}\n\t\t\t\trootSeen = true\n\t\t\t}\n\n\t\t\tif skipDepth > 0 {\n\t\t\t\tskipDepth++\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif _, unsafe := unsafeSVGElements[strings.ToLower(typed.Name.Local)]; unsafe {\n\t\t\t\tskipDepth = 1\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\ttyped.Name = preserveXMLName(typed.Name)\n\t\t\ttyped.Attr = sanitizeSVGAttributes(typed.Attr)\n\t\t\tif err = encoder.EncodeToken(typed); err != nil {\n\t\t\t\treturn \"\", fmt.Errorf(\"render svg failed: %w\", err)\n\t\t\t}\n\t\tcase xml.EndElement:\n\t\t\tif depth <= 0 || len(elementStack) == 0 {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg contains an unexpected closing element\")\n\t\t\t}\n\t\t\tstartName := elementStack[len(elementStack)-1]\n\t\t\tif startName != typed.Name {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg closing element %q does not match %q\", typed.Name.Local, startName.Local)\n\t\t\t}\n\t\t\telementStack = elementStack[:len(elementStack)-1]\n\t\t\tif skipDepth > 0 {\n\t\t\t\tskipDepth--\n\t\t\t\tdepth--\n\t\t\t\tif depth == 0 {\n\t\t\t\t\trootClosed = true\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/util/misc.go#L376-L412","documentation":"While encoding a sanitized StartElement token back out, Go's xml.Encoder rejected the token. SanitizeSVG builds its output with encoding/xml, so any element whose name or attributes the encoder considers malformed (e.g. an invalid qualified name after preserveXMLName joins Space and Local with ':') cannot be rendered. This wraps the underlying encoder error.","triggerScenarios":"An element name containing characters illegal in an XML name after namespace-prefix preservation (Space+':'+Local), producing names like 'ns:foo' with invalid prefix characters; encoding a token that conflicts with the encoder's internal state.","commonSituations":"Hand-crafted or adversarial SVG with odd namespace declarations; inputs where the declared prefix contains ':' or the local name contains characters Go's encoder refuses; rarely hit with well-formed editor-produced SVG.","solutions":["Inspect the wrapped %w error to see the encoder's exact complaint","Fix or strip the offending element/attribute name in the source SVG so it is a valid XML qualified name","Re-export or re-save the SVG from a standard tool so namespaces are declared normally","If you control the input pipeline, validate XML well-formedness (e.g. xml.Unmarshal into a probe doc) before SanitizeSVG"],"exampleFix":"// before\nSanitizeSVG(\"<svg><a:b:c/></svg>\") // invalid qualified name -> render svg failed\n// after\nSanitizeSVG(\"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\"><valid-name/></svg>\")","handlingStrategy":"try-catch","validationCode":"var probe interface{}\nif err := xml.Unmarshal([]byte(input), &probe); err != nil {\n\treturn fmt.Errorf(\"not well-formed XML: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"clean, err := util.SanitizeSVG(input)\nif err != nil {\n\tvar inner error\n\tif errors.As(err, &inner) && strings.Contains(err.Error(), \"render svg failed\") {\n\t\treturn fmt.Errorf(\"svg element/attr name rejected by encoder: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Keep element and attribute names valid per XML 1.0 (no repeated colons, valid start chars)","Avoid hand-crafted namespace prefixes; declare prefixes properly with xmlns","Round-trip the SVG through xml.Marshal/Unmarshal once before storing it","Regenerate icons from design tools instead of editing raw markup"],"tags":["svg","xml","encoding","go-stdlib"],"backgroundTag":"json-marshal-failed","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}