siyuan-note/siyuan · error

render svg failed: %w

Error message

render svg failed: %w

What it means

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.

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 8641553a1f)

Solutions

  1. Inspect the wrapped %w error to see the encoder's exact complaint
  2. Fix or strip the offending element/attribute name in the source SVG so it is a valid XML qualified name
  3. Re-export or re-save the SVG from a standard tool so namespaces are declared normally
  4. If you control the input pipeline, validate XML well-formedness (e.g. xml.Unmarshal into a probe doc) before SanitizeSVG

Example fix

// before
SanitizeSVG("<svg><a:b:c/></svg>") // invalid qualified name -> render svg failed
// after
SanitizeSVG("<svg xmlns=\"http://www.w3.org/2000/svg\"><valid-name/></svg>")
Defensive patterns

Strategy: try-catch

Validate before calling

var probe interface{}
if err := xml.Unmarshal([]byte(input), &probe); err != nil {
	return fmt.Errorf("not well-formed XML: %w", err)
}

Try / catch

clean, err := util.SanitizeSVG(input)
if err != nil {
	var inner error
	if errors.As(err, &inner) && strings.Contains(err.Error(), "render svg failed") {
		return fmt.Errorf("svg element/attr name rejected by encoder: %w", err)
	}
	return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/4dc2ac4e15996ffd. Report an issue: GitHub.