{"record":{"id":"83a08a44cb00c026","repo":"siyuan-note/siyuan","slug":"svg-contains-text-outside-the-root-element","errorCode":null,"errorMessage":"svg contains text outside the root element","messagePattern":"svg contains text outside the root element","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/misc.go","lineNumber":426,"sourceCode":"\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}\n\t\t\tdepth--\n\t\t\tif depth == 0 {\n\t\t\t\trootClosed = true\n\t\t\t}\n\t\tcase xml.CharData:\n\t\t\tif skipDepth > 0 {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (!rootSeen || rootClosed) && strings.TrimSpace(string(typed)) != \"\" {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg contains text outside the root element\")\n\t\t\t}\n\t\t\tif rootSeen && !rootClosed {\n\t\t\t\tif err = encoder.EncodeToken(typed); err != nil {\n\t\t\t\t\treturn \"\", fmt.Errorf(\"render svg failed: %w\", err)\n\t\t\t\t}\n\t\t\t}\n\t\tcase xml.Comment:\n\t\t\tif skipDepth == 0 && rootSeen && !rootClosed {\n\t\t\t\tif err = encoder.EncodeToken(typed); err != nil {\n\t\t\t\t\treturn \"\", fmt.Errorf(\"render svg failed: %w\", err)\n\t\t\t\t}\n\t\t\t}\n\t\tcase xml.Directive:\n\t\t\tif !isBenignSVGDoctype(string(typed)) {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg directives are not allowed\")\n\t\t\t}\n\t\t\t// 良性 DOCTYPE 声明不写入输出，与 XML 声明（ProcInst）的处理方式一致，不影响浏览器渲染\n\t\tcase xml.ProcInst:","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L408-L444","documentation":"SanitizeSVG only allows character data between the opening and closing of the root <svg> element. Non-whitespace text before the root starts or after it closes makes the document invalid as an SVG image, so the function rejects it. Whitespace-only text outside the root is tolerated and dropped.","triggerScenarios":"Calling SanitizeSVG with input like `hello<svg>...</svg>` or `<svg>...</svg>trailing text`, typically when the payload embeds the SVG inside other text or when an HTML wrapper was pasted instead of raw SVG.","commonSituations":"Users pasting SVG copied from an HTML page (with surrounding text), template engines that interpolate text around the SVG, corrupted emoji/asset files that lost their boundaries.","solutions":["Trim the input so it contains exactly one SVG document with no leading/trailing non-whitespace text","Strip any surrounding HTML/text and extract the <svg>...</svg> fragment before sanitizing","Validate with an XML parser that errors on content outside the root before calling the API"],"exampleFix":"// before\nsanitize(`note: <svg>...</svg>`)\n// after\nsanitize(`<svg xmlns=\"http://www.w3.org/2000/svg\">...</svg>`)","handlingStrategy":"validation","validationCode":"trimmed := strings.TrimSpace(svgText)\nif !strings.HasPrefix(trimmed, \"<svg\") || !strings.HasSuffix(trimmed, \">\") {\n    return errors.New(\"payload is not a standalone SVG document\")\n}","typeGuard":null,"tryCatchPattern":"sanitized, err := util.SanitizeSVG(raw)\nif err != nil {\n    if strings.Contains(err.Error(), \"outside the root\") {\n        raw = extractSVGFragment(raw) // pull out <svg>...</svg>\n        sanitized, err = util.SanitizeSVG(raw)\n    }\n}","preventionTips":["Store SVGs as standalone documents, never embedded in HTML/text","Extract the <svg>...</svg> fragment when accepting pasted content","Validate that the payload starts with the svg element"],"tags":["xml","svg","sanitization","validation"],"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"}