#4215·rich

[BUG] `export_svg()` generates a dangling clip-path reference for the last line

Author: yayong3Created Sep 3, 2026Updated Sep 5, 2026
LabelsNeeds triage

Describe the bug

Console.export_svg() generates a clip-path reference for each line of text, but does not emit the corresponding <clipPath> definition for the final line. For example, a three-line recording contains references to line-0, line-1, and line-2, while the <defs> section only defines line-0 and line-1. This might be due to Console.export_svg(). After iterating over N lines, y is N - 1, but line_offsets = [line_no * line_height + 1.5 for line_no in range(y)](in \rich\console.py line 2540) stops at N - 2.

Reproduction

import re
from io import StringIO

from rich.console import Console

console = Console(
    file=StringIO(),
    record=True,
    width=40,
    height=10,
    force_terminal=True,
    color_system=None,
    highlight=False,
    markup=False,
)
console.print("alpha")
console.print("bravo")
console.print("charlie")
svg = console.export_svg(clear=False, title="poc")

ids = re.findall(r'<clipPath id="([^"]+-line-\d+)"', svg)
refs = sorted(set(re.findall(r'clip-path="url\(#([^"]+-line-\d+)\)"', svg)))
print("clipPath ids:", ids)
print("clip-path refs:", refs)
print("dangling:", sorted(set(refs) - set(ids)))

Output:

clipPath ids: ['terminal-3078692677-line-0', 'terminal-3078692677-line-1']
clip-path refs: ['terminal-3078692677-line-0', 'terminal-3078692677-line-1', 'terminal-3078692677-line-2']
dangling: ['terminal-3078692677-line-2']

Platform

  • Python version: 3.12.3
  • Rich version: 15.0.0 (9d8f9a37)
  • Linux x86_64