{"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/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L376-L412","documentation":"While re-encoding the sanitized SVG tokens, xml.Encoder.EncodeToken returned an error, wrapped as 'render svg failed'. This happens when the encoder cannot serialize a token that the decoder accepted — most commonly a start element appearing after the root was conceptually mismatched, invalid XML Names after namespace preservation (preserveXMLName), or encoder state violations. It indicates an internal serialization problem rather than bad input parsing.","triggerScenarios":"EncodeToken fails on a start element or chardata/comment after attribute sanitization — e.g. element or attribute names that become invalid XML names after preserveXMLName/sanitizeSVGAttributes processing, or encoding after an underlying buffer error.","commonSituations":"SVG using exotic namespace prefixes or element names that survive decoding but fail re-encoding; extremely rare in practice since both decoder and encoder use strict XML rules — often signals an edge case worth reporting.","solutions":["Inspect the wrapped inner error (%w) to see the encoder's specific complaint","Identify the unusual element/attribute names or namespaces in the input SVG and normalize them to standard names","Validate the input with a strict XML linter and simplify exotic namespaces/prefixes","If reproducible with valid SVG, file a bug — this is likely an internal edge case in preserveXMLName/sanitizeSVGAttributes"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// normalize exotic namespaces/prefixes before sanitizing\nif strings.Contains(svg, \"xlink:\") {\n    svg = strings.ReplaceAll(svg, \"xlink:href\", \"href\")\n}","typeGuard":null,"tryCatchPattern":"clean, err := util.SanitizeSVG(input)\nif err != nil && strings.HasPrefix(err.Error(), \"render svg failed\") {\n    return fmt.Errorf(\"could not re-encode svg (%v); simplify namespaces and retry\", errors.Unwrap(err))\n}","preventionTips":["Keep SVG namespaces standard (svg, xlink)","Avoid exotic element/attribute names from unknown generators","If reproducible on valid SVG, report it — it indicates an internal edge case","Always log the wrapped inner error for diagnosis"],"tags":["svg","xml","encoding","sanitization"],"backgroundTag":"xml-parse-error","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}