siyuan-note/siyuan · error
svg root element is incomplete
Error message
svg root element is incomplete
What it means
After token processing, SanitizeSVG verifies structural invariants: the root element must have been seen, closed, depth must be zero, and no elements may remain skipped or on the stack. If any invariant fails, the document is truncated or only partially an SVG, so sanitization fails with 'svg root element is incomplete'. This guards against serving half-parsed SVG to browsers.
Solutions
- Check that the input ends with a closing `</svg>` matching the opening tag
- Repair truncation by re-uploading or re-exporting the complete SVG file
- Ensure the payload is a single complete SVG document, not a fragment, before calling SanitizeSVG
Example fix
// before <svg xmlns="..."><rect .../> // after <svg xmlns="..."><rect .../></svg>
Defensive patterns
Strategy: validation
Validate before calling
func looksCompleteSVG(data []byte) bool {
return bytes.HasPrefix(bytes.TrimSpace(data), []byte("<svg")) &&
bytes.Contains(data, []byte("</svg>"))
} Try / catch
sanitized, err := util.SanitizeSVG(raw)
if err != nil && strings.Contains(err.Error(), "incomplete") {
return errors.New("uploaded SVG is truncated; please re-upload")
} Prevention
- Verify upload completeness (size/etag) before processing
- Ensure file transfer is atomic; avoid partial writes to asset storage
- Reject SVG fragments lacking a closing root tag
When it happens
Trigger: SanitizeSVG receives input where the root <svg> never closes (truncated file), never opens, closes early leaving elements unbalanced, or where skipped subtrees never terminate.
Common situations: Network-truncated SVG uploads, users saving only part of an SVG file, string operations that cut off the closing `</svg>`, SVG fragments pasted without the root element.
Related errors
- parse svg failed
- render svg failed
- svg closing element does not match
- svg contains an unexpected closing element
- svg contains multiple root elements
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/3d6483e9c460bd66.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/util/misc.go:450
}
case xml.Comment:
if skipDepth == 0 && rootSeen && !rootClosed {
if err = encoder.EncodeToken(typed); err != nil {
return "", fmt.Errorf("render svg failed: %w", err)
}
}
case xml.Directive:
if !isBenignSVGDoctype(string(typed)) {
return "", fmt.Errorf("svg directives are not allowed")
}
// 良性 DOCTYPE 声明不写入输出,与 XML 声明(ProcInst)的处理方式一致,不影响浏览器渲染
case xml.ProcInst:
// XML 声明和处理指令不影响 SVG 图像内容,输出时统一省略。
}
}
if !rootSeen || !rootClosed || depth != 0 || skipDepth != 0 || len(elementStack) != 0 {
return "", fmt.Errorf("svg root element is incomplete")
}
if err := encoder.Close(); err != nil {
return "", fmt.Errorf("render svg failed: %w", err)
}
return buf.String(), nil
}
func preserveXMLName(name xml.Name) xml.Name {
if name.Space != "" {
name.Local = name.Space + ":" + name.Local
name.Space = ""
}
return name
}
// isBenignSVGDoctype 判断 DOCTYPE 是否仅为 svg 根元素的无内部子集声明。
// Go 的 xml.Directive 是 <! 与 > 之间的内容;内部子集 [...] 内可声明实体,存在 XXE 风险,一律拒绝。
func isBenignSVGDoctype(d string) bool {View on GitHub (pinned to 9f775e8a12)