siyuan-note/siyuan · error
render svg failed
Error message
render svg failed: %w
What it means
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.
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
Defensive patterns
Strategy: try-catch
Validate before calling
// normalize exotic namespaces/prefixes before sanitizing
if strings.Contains(svg, "xlink:") {
svg = strings.ReplaceAll(svg, "xlink:href", "href")
} Try / catch
clean, err := util.SanitizeSVG(input)
if err != nil && strings.HasPrefix(err.Error(), "render svg failed") {
return fmt.Errorf("could not re-encode svg (%v); simplify namespaces and retry", errors.Unwrap(err))
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- parse svg failed
- svg closing element does not match
- svg contains an unexpected closing element
- svg contains multiple root elements
- svg contains text outside the root element
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/4dc2ac4e15996ffd.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/util/misc.go:394
if !strings.EqualFold(typed.Name.Local, "svg") {
return "", fmt.Errorf("root element is not svg")
}
rootSeen = true
}
if skipDepth > 0 {
skipDepth++
continue
}
if _, unsafe := unsafeSVGElements[strings.ToLower(typed.Name.Local)]; unsafe {
skipDepth = 1
continue
}
typed.Name = preserveXMLName(typed.Name)
typed.Attr = sanitizeSVGAttributes(typed.Attr)
if err = encoder.EncodeToken(typed); err != nil {
return "", fmt.Errorf("render svg failed: %w", err)
}
case xml.EndElement:
if depth <= 0 || len(elementStack) == 0 {
return "", fmt.Errorf("svg contains an unexpected closing element")
}
startName := elementStack[len(elementStack)-1]
if startName != typed.Name {
return "", fmt.Errorf("svg closing element %q does not match %q", typed.Name.Local, startName.Local)
}
elementStack = elementStack[:len(elementStack)-1]
if skipDepth > 0 {
skipDepth--
depth--
if depth == 0 {
rootClosed = true
}
continue
}View on GitHub (pinned to 9f775e8a12)