siyuan-note/siyuan · error

svg contains text outside the root element

Error message

svg contains text outside the root element

What it means

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.

Solutions

  1. Trim the input so it contains exactly one SVG document with no leading/trailing non-whitespace text
  2. Strip any surrounding HTML/text and extract the <svg>...</svg> fragment before sanitizing
  3. Validate with an XML parser that errors on content outside the root before calling the API

Example fix

// before
sanitize(`note: <svg>...</svg>`)
// after
sanitize(`<svg xmlns="http://www.w3.org/2000/svg">...</svg>`)
Defensive patterns

Strategy: validation

Validate before calling

trimmed := strings.TrimSpace(svgText)
if !strings.HasPrefix(trimmed, "<svg") || !strings.HasSuffix(trimmed, ">") {
    return errors.New("payload is not a standalone SVG document")
}

Try / catch

sanitized, err := util.SanitizeSVG(raw)
if err != nil {
    if strings.Contains(err.Error(), "outside the root") {
        raw = extractSVGFragment(raw) // pull out <svg>...</svg>
        sanitized, err = util.SanitizeSVG(raw)
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at kernel/util/misc.go:426

				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
			}
		case xml.CharData:
			if skipDepth > 0 {
				continue
			}
			if (!rootSeen || rootClosed) && strings.TrimSpace(string(typed)) != "" {
				return "", fmt.Errorf("svg contains text outside the root element")
			}
			if rootSeen && !rootClosed {
				if err = encoder.EncodeToken(typed); err != nil {
					return "", fmt.Errorf("render svg failed: %w", err)
				}
			}
		case xml.Comment:
			if skipDepth == 0 && rootSeen && !rootClosed {
				if err = encoder.EncodeToken(typed); err != nil {
					return "", fmt.Errorf("render svg failed: %w", err)
				}
			}
		case xml.Directive:
			if !isBenignSVGDoctype(string(typed)) {
				return "", fmt.Errorf("svg directives are not allowed")
			}
			// 良性 DOCTYPE 声明不写入输出,与 XML 声明(ProcInst)的处理方式一致,不影响浏览器渲染
		case xml.ProcInst:

View on GitHub (pinned to 9f775e8a12)