Skip to content

Commit

Permalink
Merge pull request #114 from jugglerchris/panic_word_wrap
Browse files Browse the repository at this point in the history
Fix a panic on unlucky word wrapping.
  • Loading branch information
jugglerchris authored Jan 12, 2024
2 parents ee981eb + 1de8ce8 commit 216be3d
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "html2text"
version = "0.10.2"
version = "0.10.3"
authors = ["Chris Emerson <[email protected]>"]
description = "Render HTML as plain text."
repository = "https://github.com/jugglerchris/rust-html2text/"
Expand All @@ -27,7 +27,7 @@ dashmap = "~5.4"

[features]
html_trace = []
html_trace_bt = ["backtrace"]
html_trace_bt = ["dep:backtrace"]
default = []
css = ["dep:lightningcss"]

Expand Down
2 changes: 1 addition & 1 deletion src/render/text_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl<T: Clone + Eq + Debug + Default> WrappedBlock<T> {
// 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;
Expand Down
119 changes: 119 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ fn test_colour_map(annotations: &[RichAnnotation], s: &str) -> String
} => {
tags = ("<R>", "</R>");
}
crate::Colour{
r: 0xff,
g: 0xff,
b: 0xff
} => {
tags = ("<W>", "</W>");
}
crate::Colour{
r: 0,
g: 0xff,
Expand Down Expand Up @@ -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 <em>there</em> boo"#,
"Hello *there* boo\n",
20);
test_html(br#"Hello <em>there</em> boo"#,
"Hello *there*\nboo\n",
15);
test_html(br#"Hello <em>there</em> boo"#,
"Hello *there*\nboo\n",
14);
test_html(br#"Hello <em>there</em> boo"#,
"Hello *there*\nboo\n",
13);
test_html(br#"Hello <em>there</em> boo"#,
"Hello\n*there* boo\n",
12);
test_html(br#"Hello <em>there</em> boo"#,
"Hello\n*there* boo\n",
11);
test_html(br#"Hello <em>there</em> boo"#,
"Hello\n*there*\nboo\n",
10);
test_html(br#"Hello <em>there</em> boo"#,
"Hello\n*there\n* boo\n",
6);
test_html(br#"Hello <em>there</em> boo"#,
"Hello\n*ther\ne*\nboo\n",
5);
test_html(br#"Hello <em>there</em> boo"#,
"Hell\no\n*the\nre*\nboo\n",
4);
test_html(br#"Hello <em>there</em> boo"#,
"H\ne\nl\nl\no\n*\nt\nh\ne\nr\ne\n*\nb\no\no\n",
1);
}

#[test]
fn test_div() {
test_html(
Expand Down Expand Up @@ -1940,4 +2017,46 @@ text
"#);
}

#[test]
fn test_wrap_word_boundaries() {
let html = br#"<head><style>em { color: white; }</style></head>
<body>
Hello *<em>there</em>* boo"#;
test_html_coloured(html,
"Hello *<W>there</W>* boo\n",
20);
test_html_coloured(html,
"Hello *<W>there</W>*\nboo\n",
15);
test_html_coloured(html,
"Hello *<W>there</W>*\nboo\n",
14);
test_html_coloured(html,
"Hello *<W>there</W>*\nboo\n",
13);
test_html_coloured(html,
"Hello\n*<W>there</W>* boo\n",
12);
test_html_coloured(html,
"Hello\n*<W>there</W>* boo\n",
11);
test_html_coloured(html,
"Hello\n*<W>there</W>*\nboo\n",
10);
test_html_coloured(html,
"Hello\n*<W>there</W>*\nboo\n",
7);
test_html_coloured(html,
"Hello\n*<W>there</W>\n* boo\n",
6);
test_html_coloured(html,
"Hello\n*<W>ther</W>\n<W>e</W>*\nboo\n",
5);
test_html_coloured(html,
"Hell\no\n*<W>the</W>\n<W>re</W>*\nboo\n",
4);
test_html_coloured(html,
"H\ne\nl\nl\no\n*<W></W>\n<W>t</W>\n<W>h</W>\n<W>e</W>\n<W>r</W>\n<W>e</W>\n*\nb\no\no\n",
1);
}
}

0 comments on commit 216be3d

Please sign in to comment.