Skip to content

Commit 4f25072

Browse files
committed
fix: dehtml: Don't just truncate text when trying to decode (#5223)
If `escaper::decode_html_buf_sloppy()` just truncates the text (which happens when it fails to html-decode it at some position), then it's probably not HTML at all and should be left as is. That's what happens with hyperlinks f.e. and there was even a test on this wrong behaviour which is fixed now. So, now hyperlinks are not truncated in messages and should work as expected.
1 parent 91c3a39 commit 4f25072

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/dehtml.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ fn dehtml_text_cb(event: &BytesText, dehtml: &mut Dehtml) {
181181
if dehtml.get_add_text() == AddText::YesPreserveLineEnds
182182
|| dehtml.get_add_text() == AddText::YesRemoveLineEnds
183183
{
184-
let last_added = escaper::decode_html_buf_sloppy(event as &[_]).unwrap_or_default();
184+
let event = event as &[_];
185+
let event_str = std::str::from_utf8(event).unwrap_or_default();
186+
let mut last_added = escaper::decode_html_buf_sloppy(event).unwrap_or_default();
187+
if event_str.starts_with(&last_added) {
188+
last_added = event_str.to_string();
189+
}
185190

186191
if dehtml.get_add_text() == AddText::YesRemoveLineEnds {
187192
// Replace all line ends with spaces.
@@ -527,6 +532,6 @@ mod tests {
527532
fn test_spaces() {
528533
let input = include_str!("../test-data/spaces.html");
529534
let txt = dehtml(input).unwrap();
530-
assert_eq!(txt.text, "Welcome back to Strolling!\n\nHey there,\n\nWelcome back! Use this link to securely sign in to your Strolling account:\n\nSign in to Strolling\n\nFor your security, the link will expire in 24 hours time.\n\nSee you soon!\n\nYou can also copy\n\nhttps://strolling.rosano.ca/members/?token=XXX\n\nIf you did not make this request, you can safely ignore this email.\n\nThis message was sent from [strolling.rosano.ca](https://strolling.rosano.ca/) to [alice@example.org](mailto:alice@example.org)");
535+
assert_eq!(txt.text, "Welcome back to Strolling!\n\nHey there,\n\nWelcome back! Use this link to securely sign in to your Strolling account:\n\nSign in to Strolling\n\nFor your security, the link will expire in 24 hours time.\n\nSee you soon!\n\nYou can also copy & paste this URL into your browser:\n\nhttps://strolling.rosano.ca/members/?token=XXX&action=signin&r=https%3A%2F%2Fstrolling.rosano.ca%2F\n\nIf you did not make this request, you can safely ignore this email.\n\nThis message was sent from [strolling.rosano.ca](https://strolling.rosano.ca/) to [alice@example.org](mailto:alice@example.org)");
531536
}
532537
}

0 commit comments

Comments
 (0)