Commit 711297d5ba

711297d5ba97476b8491ec01d7a1da28d160c8a5

parent: 9af28dd0f0

Unregistered key

Steve Jason <145211128+56steve@users.noreply.github.com> · 2026-11-16 12:22 UTC
committer: <noreply@github.com>

Fix division by zero when rendering a table without cells (#862)

Layout: unified · split

src/message/html.rs +23 −1
@@ -152,7 +152,7 @@ impl Table {
152 let mut text = Text::default(); 152 let mut text = Text::default();
153 let columns = self.columns(); 153 let columns = self.columns();
154 let cell_total = width.saturating_sub(columns).saturating_sub(1); 154 let cell_total = width.saturating_sub(columns).saturating_sub(1);
155 let cell_min = cell_total / columns; 155 let cell_min = cell_total.checked_div(columns).unwrap_or(0);
156 let mut cell_slop = cell_total - cell_min * columns; 156 let mut cell_slop = cell_total - cell_min * columns;
157 let cell_widths = (0..columns) 157 let cell_widths = (0..columns)
158 .map(|_| { 158 .map(|_| {
@@ -180,6 +180,11 @@ impl Table {
180 } 180 }
181 } 181 }
182 182
183 if columns == 0 {
184 // There are no cells to draw a grid around.
185 return text;
186 }
187
183 for section in self.sections.iter() { 188 for section in self.sections.iter() {
184 for row in section.rows.iter() { 189 for row in section.rows.iter() {
185 let mut ruler = String::new(); 190 let mut ruler = String::new();
@@ -1497,6 +1502,23 @@ pub mod tests {
1497 assert_eq!(text.lines[10].spans, vec![Span::raw("└────┴────┴───┘")]); 1502 assert_eq!(text.lines[10].spans, vec![Span::raw("└────┴────┴───┘")]);
1498 } 1503 }
1499 1504
1505 #[test]
1506 fn test_table_without_cells() {
1507 let info = mock_room();
1508 let settings = mock_settings();
1509
1510 for s in ["<table></table>", "<table><tbody><tr></tr></tbody></table>"] {
1511 let tree = parse_matrix_html(s);
1512 let text = tree.to_text(15, Style::default(), &settings, &info);
1513 assert!(text.lines.is_empty());
1514 }
1515
1516 let s = "<table><caption>Title</caption></table>";
1517 let tree = parse_matrix_html(s);
1518 let text = tree.to_text(15, Style::default(), &settings, &info);
1519 assert_eq!(text.lines.len(), 1);
1520 }
1521
1500 #[test] 1522 #[test]
1501 fn test_matrix_reply_stripped() { 1523 fn test_matrix_reply_stripped() {
1502 let info = mock_room(); 1524 let info = mock_room();