siyuan-note/siyuan · error

root element is not svg

Error message

root element is not svg

What it means

SanitizeSVG checks that the first (root) start element is <svg> (case-insensitive). If the document's root is any other element, it fails with this error. The sanitizer is dedicated to SVG and will not pass through other XML/HTML documents, since sanitization rules only apply to SVG content.

Solutions

  1. Verify the file actually is an SVG: the first element must be <svg ...>
  2. Re-export or re-save the content as a genuine SVG from the source tool
  3. If serving images generically, route non-SVG files to a different handler instead of SanitizeSVG

Example fix

// before (root is not svg)
<div><svg viewBox="0 0 10 10"></svg></div>
// after
<svg viewBox="0 0 10 10"></svg>
Defensive patterns

Strategy: validation

Validate before calling

func hasSVGRoot(svg string) bool {
    d := xml.NewDecoder(strings.NewReader(svg))
    for {
        t, err := d.RawToken()
        if err == io.EOF || err != nil { return false }
        if se, ok := t.(xml.StartElement); ok {
            return strings.EqualFold(se.Name.Local, "svg")
        }
    }
}

Try / catch

if !hasSVGRoot(input) {
    return errors.New("file is not an SVG document")
}
clean, err := util.SanitizeSVG(input)

Prevention

When it happens

Trigger: Passing non-SVG content to SanitizeSVG — e.g. an HTML fragment rooted at <div>, an XML document rooted at <?xml?><g>, a renamed .svg file that actually holds PNG/HTML data, or content whose XML declaration/prolog is followed by a non-svg element.

Common situations: Users upload an HTML page or plain XML renamed to .svg, clipper saves an HTML snippet as .svg, or tooling misroutes file types so a non-SVG document reaches the emoji/asset sanitizer.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/a8304a6159b8b69f. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/misc.go:377

		}
		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") {
					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)
			}

View on GitHub (pinned to 9f775e8a12)