{"record":{"id":"92fb32dac8997571","repo":"siyuan-note/siyuan","slug":"svg-contains-an-unexpected-closing-element","errorCode":null,"errorMessage":"svg contains an unexpected closing element","messagePattern":"svg contains an unexpected closing element","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/misc.go","lineNumber":398,"sourceCode":"\t\t\t}\n\n\t\t\tif skipDepth > 0 {\n\t\t\t\tskipDepth++\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif _, unsafe := unsafeSVGElements[strings.ToLower(typed.Name.Local)]; unsafe {\n\t\t\t\tskipDepth = 1\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\ttyped.Name = preserveXMLName(typed.Name)\n\t\t\ttyped.Attr = sanitizeSVGAttributes(typed.Attr)\n\t\t\tif err = encoder.EncodeToken(typed); err != nil {\n\t\t\t\treturn \"\", fmt.Errorf(\"render svg failed: %w\", err)\n\t\t\t}\n\t\tcase xml.EndElement:\n\t\t\tif depth <= 0 || len(elementStack) == 0 {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg contains an unexpected closing element\")\n\t\t\t}\n\t\t\tstartName := elementStack[len(elementStack)-1]\n\t\t\tif startName != typed.Name {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg closing element %q does not match %q\", typed.Name.Local, startName.Local)\n\t\t\t}\n\t\t\telementStack = elementStack[:len(elementStack)-1]\n\t\t\tif skipDepth > 0 {\n\t\t\t\tskipDepth--\n\t\t\t\tdepth--\n\t\t\t\tif depth == 0 {\n\t\t\t\t\trootClosed = true\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\ttyped.Name = preserveXMLName(typed.Name)\n\t\t\tif err = encoder.EncodeToken(typed); err != nil {\n\t\t\t\treturn \"\", fmt.Errorf(\"render svg failed: %w\", err)\n\t\t\t}","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L380-L416","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run the SVG through xmllint or any XML validator to find the unbalanced closing tag and remove it","Re-export the SVG cleanly rather than hand-editing tags","Check for copy-paste truncation that dropped opening elements","Fix upstream generators that emit unmatched close tags"],"exampleFix":"// before\n<svg><g></g></g></svg>\n// after\n<svg><g></g></svg>","handlingStrategy":"validation","validationCode":"func balancedTags(svg string) bool {\n    var stack []string\n    d := xml.NewDecoder(strings.NewReader(svg))\n    for {\n        t, err := d.RawToken()\n        if err == io.EOF { return len(stack) == 0 }\n        if err != nil { return len(stack) == 0 }\n        switch v := t.(type) {\n        case xml.StartElement:\n            stack = append(stack, v.Name.Local)\n        case xml.EndElement:\n            if len(stack) == 0 { return false }\n            stack = stack[:len(stack)-1]\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"clean, err := util.SanitizeSVG(input)\nif err != nil && err.Error() == \"svg contains an unexpected closing element\" {\n    return fmt.Errorf(\"svg has unmatched closing tags; validate with xmllint: %w\", err)\n}","preventionTips":["Validate SVG with xmllint before ingestion","Avoid hand-deleting opening tags without removing their closers","Check for truncation after copy-paste or download","Prefer re-exporting SVG over manual tag editing"],"tags":["svg","xml","parsing","sanitization"],"backgroundTag":"xml-parse-error","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}