fix(span): prevent panic on rendering out of y bounds (#1257)

This commit is contained in:
EdJoPaTo 2024-08-02 05:17:42 +02:00 committed by GitHub
parent b344f95b7c
commit 3ca920e881
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -370,11 +370,15 @@ impl Widget for Span<'_> {
impl WidgetRef for Span<'_> {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
let Rect { mut x, y, .. } = area.intersection(buf.area);
let area = area.intersection(buf.area);
if area.is_empty() {
return;
}
let Rect { mut x, y, .. } = area;
for (i, grapheme) in self.styled_graphemes(Style::default()).enumerate() {
let symbol_width = grapheme.symbol.width();
let next_x = x.saturating_add(symbol_width as u16);
if next_x > area.intersection(buf.area).right() {
if next_x > area.right() {
break;
}
@ -629,8 +633,11 @@ mod tests {
}
#[rstest]
fn render_out_of_bounds(mut small_buf: Buffer) {
let out_of_bounds = Rect::new(20, 20, 10, 1);
#[case::x(20, 0)]
#[case::y(0, 20)]
#[case::both(20, 20)]
fn render_out_of_bounds(mut small_buf: Buffer, #[case] x: u16, #[case] y: u16) {
let out_of_bounds = Rect::new(x, y, 10, 1);
Span::raw("Hello, World!").render(out_of_bounds, &mut small_buf);
assert_eq!(small_buf, Buffer::empty(small_buf.area));
}