diff --git a/CHANGELOG.md b/CHANGELOG.md index d56ea8f..3205492 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ Possible log types: - `[fixed]` for any bug fixes. - `[security]` to invite users to upgrade in case of vulnerabilities. +### 0.10.3 + +- [fixed] A panic on some unlucky text wrapping coincidences. +- [fixed] Use dep:backtrace in Cargo.toml to avoid implicit feature. + ### 0.10.2 - [fixed] CSS: Ignore transparent colours. diff --git a/Cargo.toml b/Cargo.toml index 34d16fd..b08be9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "html2text" -version = "0.10.2" +version = "0.10.3" authors = ["Chris Emerson "] description = "Render HTML as plain text." repository = "https://github.com/jugglerchris/rust-html2text/" @@ -27,7 +27,7 @@ dashmap = "~5.4" [features] html_trace = [] -html_trace_bt = ["backtrace"] +html_trace_bt = ["dep:backtrace"] default = [] css = ["dep:lightningcss"] diff --git a/src/render/text_renderer.rs b/src/render/text_renderer.rs index c49b6fd..332f0d4 100644 --- a/src/render/text_renderer.rs +++ b/src/render/text_renderer.rs @@ -386,7 +386,7 @@ impl WrappedBlock { // Check if we've made no progress, for example // if the first character is 2 cells wide and we // only have a width of 1. - if idx == 0 { + if idx == 0 && self.line.width() == 0 { return Err(Error::TooNarrow); } split_idx = idx; diff --git a/src/tests.rs b/src/tests.rs index 28d9a4d..0462aaa 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -45,6 +45,13 @@ fn test_colour_map(annotations: &[RichAnnotation], s: &str) -> String } => { tags = ("", ""); } + crate::Colour{ + r: 0xff, + g: 0xff, + b: 0xff + } => { + tags = ("", ""); + } crate::Colour{ r: 0, g: 0xff, @@ -736,6 +743,76 @@ full screen width "#, 80, 17); } +#[test] +fn test_wrap_word_boundaries() { + test_html(br#"Hello there boo"#, + "Hello there boo\n", + 20); + test_html(br#"Hello there boo"#, + "Hello there boo\n", + 15); + test_html(br#"Hello there boo"#, + "Hello there\nboo\n", + 14); + test_html(br#"Hello there boo"#, + "Hello there\nboo\n", + 13); + test_html(br#"Hello there boo"#, + "Hello there\nboo\n", + 12); + test_html(br#"Hello there boo"#, + "Hello there\nboo\n", + 11); + test_html(br#"Hello there boo"#, + "Hello\nthere boo\n", + 10); + test_html(br#"Hello there boo"#, + "Hello\nthere\nboo\n", + 6); + test_html(br#"Hello there boo"#, + "Hello\nthere\nboo\n", + 5); + test_html(br#"Hello there boo"#, + "Hell\no\nther\ne\nboo\n", + 4); + test_html(br#"Hello there boo"#, + "H\ne\nl\nl\no\nt\nh\ne\nr\ne\nb\no\no\n", + 1); + test_html(br#"Hello there boo"#, + "Hello *there* boo\n", + 20); + test_html(br#"Hello there boo"#, + "Hello *there*\nboo\n", + 15); + test_html(br#"Hello there boo"#, + "Hello *there*\nboo\n", + 14); + test_html(br#"Hello there boo"#, + "Hello *there*\nboo\n", + 13); + test_html(br#"Hello there boo"#, + "Hello\n*there* boo\n", + 12); + test_html(br#"Hello there boo"#, + "Hello\n*there* boo\n", + 11); + test_html(br#"Hello there boo"#, + "Hello\n*there*\nboo\n", + 10); + test_html(br#"Hello there boo"#, + "Hello\n*there\n* boo\n", + 6); + test_html(br#"Hello there boo"#, + "Hello\n*ther\ne*\nboo\n", + 5); + test_html(br#"Hello there boo"#, + "Hell\no\n*the\nre*\nboo\n", + 4); + test_html(br#"Hello there boo"#, + "H\ne\nl\nl\no\n*\nt\nh\ne\nr\ne\n*\nb\no\no\n", + 1); +} + #[test] fn test_div() { test_html( @@ -1940,4 +2017,46 @@ text "#); } + #[test] + fn test_wrap_word_boundaries() { + let html = br#" + + Hello *there* boo"#; + test_html_coloured(html, + "Hello *there* boo\n", + 20); + test_html_coloured(html, + "Hello *there*\nboo\n", + 15); + test_html_coloured(html, + "Hello *there*\nboo\n", + 14); + test_html_coloured(html, + "Hello *there*\nboo\n", + 13); + test_html_coloured(html, + "Hello\n*there* boo\n", + 12); + test_html_coloured(html, + "Hello\n*there* boo\n", + 11); + test_html_coloured(html, + "Hello\n*there*\nboo\n", + 10); + test_html_coloured(html, + "Hello\n*there*\nboo\n", + 7); + test_html_coloured(html, + "Hello\n*there\n* boo\n", + 6); + test_html_coloured(html, + "Hello\n*ther\ne*\nboo\n", + 5); + test_html_coloured(html, + "Hell\no\n*the\nre*\nboo\n", + 4); + test_html_coloured(html, + "H\ne\nl\nl\no\n*\nt\nh\ne\nr\ne\n*\nb\no\no\n", + 1); + } }