{"record":{"id":"ababb5d3bc4dc006","repo":"siyuan-note/siyuan","slug":"svg-contains-multiple-root-elements","errorCode":null,"errorMessage":"svg contains multiple root elements","messagePattern":"svg contains multiple root elements","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/misc.go","lineNumber":373,"sourceCode":"\t\t\tbreak\n\t\t}\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"parse svg failed: %w\", err)\n\t\t}\n\t\ttokenCount++\n\t\tif tokenCount > maxSVGTokens {\n\t\t\treturn \"\", fmt.Errorf(\"svg contains too many tokens\")\n\t\t}\n\n\t\tswitch typed := token.(type) {\n\t\tcase xml.StartElement:\n\t\t\telementStack = append(elementStack, typed.Name)\n\t\t\tdepth++\n\t\t\tif depth > maxSVGDepth {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg nesting depth exceeds %d\", maxSVGDepth)\n\t\t\t}\n\t\t\tif rootClosed {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg contains multiple root elements\")\n\t\t\t}\n\t\t\tif !rootSeen {\n\t\t\t\tif !strings.EqualFold(typed.Name.Local, \"svg\") {\n\t\t\t\t\treturn \"\", fmt.Errorf(\"root element is not svg\")\n\t\t\t\t}\n\t\t\t\trootSeen = true\n\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)","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L355-L391","documentation":"SanitizeSVG requires exactly one root element. Once the root <svg> element is closed, any further start element triggers this error — the input contains multiple top-level elements. Well-formed XML allows exactly one root; extra siblings indicate malformed or smuggled content, so the sanitizer refuses it.","triggerScenarios":"SanitizeSVG gets input like '<svg>...</svg><svg>...</svg>' or '<svg/><!--comment--><svg/>', i.e. concatenated SVG fragments, or content where HTML-style parsing would hide sibling roots.","commonSituations":"Users paste several copied SVGs at once into a custom-emoji upload, clipper concatenates multiple captures, or tooling appends a second <svg> (e.g. sprite sheets pasted whole).","solutions":["Provide a single SVG document — wrap multiple icons in one root <svg> with <symbol>/<use> if needed","Remove trailing duplicate <svg> fragments after the root closes","Fix the upstream tooling/clipper that concatenates SVG documents"],"exampleFix":"// before\n<svg viewBox=\"0 0 10 10\"></svg><svg viewBox=\"0 0 10 10\"></svg>\n// after\n<svg viewBox=\"0 0 10 10\"><symbol id=\"a\">...</symbol></svg>","handlingStrategy":"validation","validationCode":"func singleRoot(svg string) bool {\n    roots := 0\n    d := xml.NewDecoder(strings.NewReader(svg))\n    depth := 0\n    for {\n        t, err := d.RawToken()\n        if err == io.EOF { break }\n        if err != nil { break }\n        switch t.(type) {\n        case xml.StartElement:\n            depth++\n            if depth == 1 { roots++ }\n        case xml.EndElement:\n            depth--\n        }\n    }\n    return roots == 1\n}","typeGuard":null,"tryCatchPattern":"clean, err := util.SanitizeSVG(input)\nif err != nil && err.Error() == \"svg contains multiple root elements\" {\n    return fmt.Errorf(\"paste one SVG document at a time: %w\", err)\n}","preventionTips":["Never concatenate multiple SVG files into one upload","Wrap multi-icon content in a single root <svg> with <symbol>","Trim trailing junk after the root </svg> close","Check clipper tools for multi-capture concatenation bugs"],"tags":["svg","xml","sanitization","validation"],"backgroundTag":"schema-validation-failed","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"}