Commit 72c40b4e46
Unregistered key
Layout: unified · split
src/message/html.rs +35 −1
| @@ -73,7 +73,9 @@ pub enum ListStyle { | |||
| 73 | impl ListStyle { | 73 | impl ListStyle { |
| 74 | fn bullets(&self, len: usize) -> BulletIterator { | 74 | fn bullets(&self, len: usize) -> BulletIterator { |
| 75 | let pos = match self { | 75 | let pos = match self { |
| 76 | Self::Ordered(start) => *start, | 76 | // Restart at 1 if the last number would not fit in a `usize`. |
| 77 | Self::Ordered(start) if start.checked_add(len).is_some() => *start, | ||
| 78 | Self::Ordered(_) => 1, | ||
| 77 | Self::Unordered => 0, | 79 | Self::Unordered => 0, |
| 78 | }; | 80 | }; |
| 79 | 81 | ||
| @@ -1535,6 +1537,38 @@ pub mod tests { | |||
| 1535 | ); | 1537 | ); |
| 1536 | } | 1538 | } |
| 1537 | 1539 | ||
| 1540 | fn list_bullets(start: usize) -> Vec<Option<String>> { | ||
| 1541 | let info = mock_room(); | ||
| 1542 | let settings = mock_settings(); | ||
| 1543 | let s = format!("<ol start=\"{start}\"><li>a</li><li>b</li></ol>"); | ||
| 1544 | let tree = parse_matrix_html(&s); | ||
| 1545 | let text = tree.to_text(40, Style::default(), &settings, &info); | ||
| 1546 | assert_eq!(text.lines.len(), 2); | ||
| 1547 | |||
| 1548 | text.lines | ||
| 1549 | .iter() | ||
| 1550 | .map(|line| line.spans.first().map(|span| span.content.to_string())) | ||
| 1551 | .collect() | ||
| 1552 | } | ||
| 1553 | |||
| 1554 | #[test] | ||
| 1555 | fn test_list_ordered_start_overflow() { | ||
| 1556 | // Numbering that would overflow a `usize` restarts at 1. | ||
| 1557 | for start in [usize::MAX, usize::MAX - 1] { | ||
| 1558 | assert_eq!(list_bullets(start), vec![Some("1. ".to_string()), Some("2. ".to_string())]); | ||
| 1559 | } | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | #[test] | ||
| 1563 | fn test_list_ordered_start_near_max() { | ||
| 1564 | // The largest start for which the numbering still fits is left alone. | ||
| 1565 | let start = usize::MAX - 2; | ||
| 1566 | assert_eq!(list_bullets(start), vec![ | ||
| 1567 | Some(format!("{}. ", start)), | ||
| 1568 | Some(format!("{}. ", start + 1)) | ||
| 1569 | ]); | ||
| 1570 | } | ||
| 1571 | |||
| 1538 | #[test] | 1572 | #[test] |
| 1539 | fn test_list_ordered() { | 1573 | fn test_list_ordered() { |
| 1540 | let info = mock_room(); | 1574 | let info = mock_room(); |