siyuan-note/siyuan · error

svg closing element does not match

Error message

svg closing element %q does not match %q

What it means

SanitizeSVG parses an SVG document token-by-token with encoding/xml and re-encodes only whitelisted content. When an end element arrives whose local name differs from the innermost start element on the stack, the document is malformed (mismatched nesting), so sanitization is aborted and this error is returned. The library refuses to guess at a repair because output SVG is served back to browsers.

Solutions

  1. Validate the SVG with a strict XML parser (encoding/xml Decoder with input processed to EOF) before calling SanitizeSVG
  2. Locate the mismatched tag: compare each closing tag against the open-element stack; fix or regenerate the SVG source
  3. If the SVG comes from a third party, re-export it from a vector editor (Inkscape/Illustrator) which writes well-formed XML

Example fix

// before
decoder.Token() sees </circle> while stack top is <path> -> error
// after
<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0"/></svg>
Defensive patterns

Strategy: validation

Validate before calling

func isWellFormedXML(data []byte) error {
    dec := xml.NewDecoder(bytes.NewReader(data))
    for {
        _, err := dec.Token()
        if err == io.EOF {
            return nil
        }
        if err != nil {
            return err
        }
    }
}

Try / catch

sanitized, err := util.SanitizeSVG(raw)
if err != nil {
    log.Warnf("SVG rejected: %v", err)
    return http.StatusBadRequest
}

Prevention

When it happens

Trigger: Calling SanitizeSVG (directly, via serveSVG, or via normalizeCustomEmojiData) with SVG whose tags are unbalanced, e.g. `<svg><path></circle></svg>` or a self-closing tag written as `<g></g></svg>`.

Common situations: Hand-edited SVG assets, SVG pasted from editors that emit non-well-formed markup, truncated uploads, or custom emoji data produced by string concatenation instead of an XML writer.

Related errors


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

Appendix: source

Thrown at kernel/util/misc.go:402

				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)
			}
			depth--
			if depth == 0 {
				rootClosed = true
			}

View on GitHub (pinned to 9f775e8a12)