siyuan-note/siyuan · error

svg contains an unexpected closing element

Error message

svg contains an unexpected closing element

What it means

SanitizeSVG tracks an element stack of open start elements. An EndElement arriving when the stack is empty or depth is zero means the input closes more elements than it opens — malformed XML nesting. Because encoding/xml's RawToken does not enforce balanced tags, the sanitizer performs this explicit check and rejects the document.

Solutions

  1. Run the SVG through xmllint or any XML validator to find the unbalanced closing tag and remove it
  2. Re-export the SVG cleanly rather than hand-editing tags
  3. Check for copy-paste truncation that dropped opening elements
  4. Fix upstream generators that emit unmatched close tags

Example fix

// before
<svg><g></g></g></svg>
// after
<svg><g></g></svg>
Defensive patterns

Strategy: validation

Validate before calling

func balancedTags(svg string) bool {
    var stack []string
    d := xml.NewDecoder(strings.NewReader(svg))
    for {
        t, err := d.RawToken()
        if err == io.EOF { return len(stack) == 0 }
        if err != nil { return len(stack) == 0 }
        switch v := t.(type) {
        case xml.StartElement:
            stack = append(stack, v.Name.Local)
        case xml.EndElement:
            if len(stack) == 0 { return false }
            stack = stack[:len(stack)-1]
        }
    }
}

Try / catch

clean, err := util.SanitizeSVG(input)
if err != nil && err.Error() == "svg contains an unexpected closing element" {
    return fmt.Errorf("svg has unmatched closing tags; validate with xmllint: %w", err)
}

Prevention

When it happens

Trigger: Input like '<svg><g></g></g></svg>' (extra closing tags) or stray '</div>' fragments reaches SanitizeSVG; RawToken emits the unmatched EndElement and the stack check fails.

Common situations: Hand-edited SVG with deleted opening tags but leftover closing tags, truncation/corruption during copy-paste, or HTML-tolerant sources where implicit closing differs from XML.

Related errors


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

Appendix: source

Thrown at kernel/util/misc.go:398

			}

			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
			}
			typed.Name = preserveXMLName(typed.Name)
			if err = encoder.EncodeToken(typed); err != nil {
				return "", fmt.Errorf("render svg failed: %w", err)
			}

View on GitHub (pinned to 9f775e8a12)