siyuan-note/siyuan · warning
parse svg failed: %w
Error message
parse svg failed: %w
What it means
SanitizeSVG runs an XML decoder in Strict mode and streams tokens via RawToken. Any XML well-formedness error (invalid syntax, undeclared entity, bad encoding, mismatched tag) is wrapped with %w and returned. SVGs that are valid HTML but not valid XML (unquoted attributes, HTML entities like , unclosed tags) will trip this.
Source
Thrown at kernel/util/misc.go:358
decoder := xml.NewDecoder(strings.NewReader(svgInput))
decoder.Strict = true
var buf bytes.Buffer
encoder := xml.NewEncoder(&buf)
rootSeen := false
rootClosed := false
depth := 0
skipDepth := 0
tokenCount := 0
var elementStack []xml.Name
for {
token, err := decoder.RawToken()
if err == io.EOF {
break
}
if err != nil {
return "", fmt.Errorf("parse svg failed: %w", err)
}
tokenCount++
if tokenCount > maxSVGTokens {
return "", fmt.Errorf("svg contains too many tokens")
}
switch typed := token.(type) {
case xml.StartElement:
elementStack = append(elementStack, typed.Name)
depth++
if depth > maxSVGDepth {
return "", fmt.Errorf("svg nesting depth exceeds %d", maxSVGDepth)
}
if rootClosed {
return "", fmt.Errorf("svg contains multiple root elements")
}
if !rootSeen {
if !strings.EqualFold(typed.Name.Local, "svg") {View on GitHub (pinned to 251596fc0d)
Solutions
- Open the SVG in a strict XML validator and fix well-formedness errors (quote all attributes, escape &, close all tags).
- Replace HTML entities ( ) with numeric XML entities ( ) or literal characters.
- Re-save from an editor that emits well-formed XML (e.g. Inkscape 'Optimized SVG').
- Ensure UTF-8 bytes match the <?xml encoding="utf-8"?> declaration, or omit the declaration.
Example fix
<!-- before --> <svg><rect width=100 height=50 fill="red">©</svg> <!-- after --> <svg xmlns="http://www.w3.org/2000/svg"><rect width="100" height="50" fill="red"/>©</svg>
Defensive patterns
Strategy: try-catch
Validate before calling
dec := xml.NewDecoder(strings.NewReader(svg))
dec.Strict = true
if _, err := dec.Token(); err != nil {
return errors.New("svg is not well-formed XML: " + err.Error())
} Try / catch
clean, err := util.SanitizeSVG(raw)
if err != nil {
logging.LogWarnf("skip non-well-formed SVG: %s", err)
return fallback // raw <img src> or placeholder
} Prevention
- Treat SVG as XML, not HTML
- Quote every attribute and self-close every element
- Avoid HTML entities; use numeric character references
- Validate SVGs in a strict XML parser before importing
When it happens
Trigger: Inserting/previewing an SVG asset that is not well-formed XML: unquoted attributes, HTML entities ( ©), missing closing tags, BOM or <?xml encoding mismatch, namespace prefixes without declaration, stray < or & in text.
Common situations: SVG exported from design tools with HTML quirks; SVGs copied from web pages with HTML entities; hand-authored SVG with typos; SVG whose encoding declaration disagrees with the file bytes.
Related errors
- copy unused asset [%s] to history failed: %w
- remove unused asset [%s] failed: %w
- Tesseract OCR is not installed or configured, please refer t
- SVG images are not accepted by multimodal models
- image attachment request limit exceeded: at most %d images a
AI-assisted analysis of siyuan-note/siyuan@251596fc0d (2026-08-12).
Data as JSON: /api/errors/aee9595fac752222.
Report an issue: GitHub.