1use std::io::BufRead;
6use std::sync::LazyLock;
7
8use quick_xml::{
9 Reader,
10 errors::Error as QuickXmlError,
11 events::{BytesEnd, BytesStart, BytesText},
12};
13
14use crate::simplify::{SimplifiedText, simplify_quote};
15
16#[derive(Default)]
17struct Dehtml {
18 strbuilder: String,
19 quote: String,
20 add_text: AddText,
21 last_href: Option<String>,
22 divs_since_quote_div: u32,
26 divs_since_quoted_content_div: u32,
29 divs_since_hp_legacy_display: u32,
32 blockquotes_since_blockquote: u32,
35}
36
37impl Dehtml {
38 fn is_quote(&self) -> bool {
40 self.divs_since_quoted_content_div > 0 || self.blockquotes_since_blockquote > 0
41 }
42
43 fn get_buf(&mut self) -> &mut String {
47 if self.is_quote() {
48 &mut self.quote
49 } else {
50 &mut self.strbuilder
51 }
52 }
53
54 fn get_add_text(&self) -> AddText {
55 if self.divs_since_quote_div > 0 && self.divs_since_quoted_content_div == 0
58 || self.divs_since_hp_legacy_display > 0
59 {
60 AddText::No
61 } else {
62 self.add_text
63 }
64 }
65}
66
67#[derive(Debug, Default, PartialEq, Clone, Copy)]
68enum AddText {
69 No,
72
73 #[default]
74 YesRemoveLineEnds,
75
76 YesPreserveLineEnds,
78}
79
80pub(crate) fn dehtml(buf: &str) -> Option<SimplifiedText> {
81 let (s, quote) = dehtml_quick_xml(buf);
82 if !s.trim().is_empty() {
83 let text = dehtml_cleanup(s);
84 let top_quote = if !quote.trim().is_empty() {
85 Some(dehtml_cleanup(simplify_quote("e).0))
86 } else {
87 None
88 };
89 return Some(SimplifiedText {
90 text,
91 top_quote,
92 ..Default::default()
93 });
94 }
95 let s = dehtml_manually(buf);
96 if !s.trim().is_empty() {
97 let text = dehtml_cleanup(s);
98 return Some(SimplifiedText {
99 text,
100 ..Default::default()
101 });
102 }
103 None
104}
105
106fn dehtml_cleanup(mut text: String) -> String {
107 text.retain(|c| c != '\r');
108 let lines = text.trim().split('\n');
109 let mut text = String::new();
110 let mut linebreak = false;
111 for line in lines {
112 if line.chars().all(char::is_whitespace) {
113 linebreak = true;
114 } else {
115 if !text.is_empty() {
116 text += "\n";
117 if linebreak {
118 text += "\n";
119 }
120 }
121 text += line.trim_end();
122 linebreak = false;
123 }
124 }
125 text
126}
127
128fn dehtml_quick_xml(buf: &str) -> (String, String) {
129 let buf = buf.trim().trim_start_matches("<!doctype html>");
130
131 let mut dehtml = Dehtml {
132 strbuilder: String::with_capacity(buf.len()),
133 ..Default::default()
134 };
135
136 let mut reader = quick_xml::Reader::from_str(buf);
137 reader.config_mut().check_end_names = false;
138
139 let mut buf = Vec::new();
140 let mut char_buf = String::with_capacity(4);
141
142 loop {
143 match reader.read_event_into(&mut buf) {
144 Ok(quick_xml::events::Event::Start(ref e)) => {
145 dehtml_starttag_cb(e, &mut dehtml, &reader)
146 }
147 Ok(quick_xml::events::Event::End(ref e)) => dehtml_endtag_cb(e, &mut dehtml),
148 Ok(quick_xml::events::Event::Text(ref e)) => dehtml_text_cb(e, &mut dehtml),
149 Ok(quick_xml::events::Event::CData(e)) => {
150 str_cb(&String::from_utf8_lossy(&e as &[_]), &mut dehtml)
151 }
152 Ok(quick_xml::events::Event::Empty(ref e)) => {
153 dehtml_starttag_cb(e, &mut dehtml, &reader);
156 dehtml_endtag_cb(
157 &BytesEnd::new(String::from_utf8_lossy(e.name().as_ref())),
158 &mut dehtml,
159 );
160 }
161 Ok(quick_xml::events::Event::GeneralRef(ref e)) => {
162 match e.resolve_char_ref() {
163 Err(err) => eprintln!(
164 "resolve_char_ref() error at position {}: {:?}",
165 reader.buffer_position(),
166 err,
167 ),
168 Ok(Some(ch)) => {
169 char_buf.clear();
170 char_buf.push(ch);
171 str_cb(&char_buf, &mut dehtml);
172 }
173 Ok(None) => {
174 let event_str = String::from_utf8_lossy(e);
175 if let Some(s) = quick_xml::escape::resolve_html5_entity(&event_str) {
176 str_cb(s, &mut dehtml);
177 } else {
178 str_cb(&format!("&{event_str};"), &mut dehtml);
180 }
181 }
182 }
183 }
184 Err(QuickXmlError::IllFormed(_)) => {
185 str_cb(&String::from_utf8_lossy(&buf), &mut dehtml);
187 }
188 Err(e) => {
189 eprintln!(
190 "Parse html error: Error at position {}: {:?}",
191 reader.buffer_position(),
192 e
193 );
194 }
195 Ok(quick_xml::events::Event::Eof) => break,
196 _ => (),
197 }
198 buf.clear();
199 }
200
201 (dehtml.strbuilder, dehtml.quote)
202}
203
204fn dehtml_text_cb(event: &BytesText, dehtml: &mut Dehtml) {
205 if dehtml.get_add_text() == AddText::YesPreserveLineEnds
206 || dehtml.get_add_text() == AddText::YesRemoveLineEnds
207 {
208 let event = event as &[_];
209 let event_str = std::str::from_utf8(event).unwrap_or_default();
210 str_cb(event_str, dehtml);
211 }
212}
213
214fn str_cb(event_str: &str, dehtml: &mut Dehtml) {
215 static LINE_RE: LazyLock<regex::Regex> =
216 LazyLock::new(|| regex::Regex::new(r"(\r?\n)+").unwrap());
217
218 let add_text = dehtml.get_add_text();
219 if add_text == AddText::YesRemoveLineEnds {
220 let event_str = LINE_RE.replace_all(event_str, " ");
223
224 let buf = dehtml.get_buf();
228 if !buf.ends_with(' ') && !buf.ends_with('\n') && event_str.starts_with(' ') {
229 *buf += " ";
230 }
231
232 *buf += event_str.trim_start();
233 } else if add_text == AddText::YesPreserveLineEnds {
234 *dehtml.get_buf() += LINE_RE.replace_all(event_str, "\n").as_ref();
235 }
236}
237
238fn dehtml_endtag_cb(event: &BytesEnd, dehtml: &mut Dehtml) {
239 let tag = String::from_utf8_lossy(event.name().as_ref())
240 .trim()
241 .to_lowercase();
242
243 match tag.as_str() {
244 "style" | "script" | "title" | "pre" => {
245 *dehtml.get_buf() += "\n\n";
246 dehtml.add_text = AddText::YesRemoveLineEnds;
247 }
248 "div" => {
249 pop_tag(&mut dehtml.divs_since_quote_div);
250 pop_tag(&mut dehtml.divs_since_quoted_content_div);
251 pop_tag(&mut dehtml.divs_since_hp_legacy_display);
252
253 *dehtml.get_buf() += "\n\n";
254 dehtml.add_text = AddText::YesRemoveLineEnds;
255 }
256 "a" => {
257 if let Some(ref last_href) = dehtml.last_href.take() {
258 let buf = dehtml.get_buf();
259 if buf.ends_with('[') {
260 buf.truncate(buf.len() - 1);
261 } else {
262 *buf += "](";
263 *buf += last_href;
264 *buf += ")";
265 }
266 }
267 }
268 "b" | "strong" => {
269 if dehtml.get_add_text() != AddText::No {
270 *dehtml.get_buf() += "*";
271 }
272 }
273 "i" | "em" => {
274 if dehtml.get_add_text() != AddText::No {
275 *dehtml.get_buf() += "_";
276 }
277 }
278 "blockquote" => pop_tag(&mut dehtml.blockquotes_since_blockquote),
279 _ => {}
280 }
281}
282
283fn dehtml_starttag_cb<B: std::io::BufRead>(
284 event: &BytesStart,
285 dehtml: &mut Dehtml,
286 reader: &quick_xml::Reader<B>,
287) {
288 let tag = String::from_utf8_lossy(event.name().as_ref())
289 .trim()
290 .to_lowercase();
291
292 match tag.as_str() {
293 "p" | "table" | "td" => {
294 if !dehtml.strbuilder.is_empty() {
295 *dehtml.get_buf() += "\n\n";
296 }
297 dehtml.add_text = AddText::YesRemoveLineEnds;
298 }
299 #[rustfmt::skip]
300 "div" => {
301 maybe_push_tag(event, reader, "quote", &mut dehtml.divs_since_quote_div);
302 maybe_push_tag(event, reader, "quoted-content", &mut dehtml.divs_since_quoted_content_div);
303 maybe_push_tag(event, reader, "header-protection-legacy-display",
304 &mut dehtml.divs_since_hp_legacy_display);
305
306 *dehtml.get_buf() += "\n\n";
307 dehtml.add_text = AddText::YesRemoveLineEnds;
308 }
309 "br" => {
310 *dehtml.get_buf() += "\n";
311 dehtml.add_text = AddText::YesRemoveLineEnds;
312 }
313 "style" | "script" | "title" => {
314 dehtml.add_text = AddText::No;
315 }
316 "pre" => {
317 *dehtml.get_buf() += "\n\n";
318 dehtml.add_text = AddText::YesPreserveLineEnds;
319 }
320 "a" => {
321 if let Some(href) = event
322 .html_attributes()
323 .filter_map(|attr| attr.ok())
324 .find(|attr| {
325 String::from_utf8_lossy(attr.key.as_ref())
326 .trim()
327 .to_lowercase()
328 == "href"
329 })
330 {
331 let href = href
332 .decode_and_unescape_value(reader.decoder())
333 .unwrap_or_default()
334 .to_string();
335
336 if !href.is_empty() {
337 dehtml.last_href = Some(href);
338 *dehtml.get_buf() += "[";
339 }
340 }
341 }
342 "b" | "strong" => {
343 if dehtml.get_add_text() != AddText::No {
344 *dehtml.get_buf() += "*";
345 }
346 }
347 "i" | "em" => {
348 if dehtml.get_add_text() != AddText::No {
349 *dehtml.get_buf() += "_";
350 }
351 }
352 "blockquote" => dehtml.blockquotes_since_blockquote += 1,
353 _ => {}
354 }
355}
356
357fn pop_tag(count: &mut u32) {
360 if *count > 0 {
361 *count -= 1;
362 }
363}
364
365fn maybe_push_tag(
368 event: &BytesStart,
369 reader: &Reader<impl BufRead>,
370 tag_name: &str,
371 count: &mut u32,
372) {
373 if *count > 0 || tag_contains_attr(event, reader, tag_name) {
374 *count += 1;
375 }
376}
377
378fn tag_contains_attr(event: &BytesStart, reader: &Reader<impl BufRead>, name: &str) -> bool {
379 event.attributes().any(|r| {
380 r.map(|a| {
381 a.decode_and_unescape_value(reader.decoder())
382 .map(|v| v == name)
383 .unwrap_or(false)
384 })
385 .unwrap_or(false)
386 })
387}
388
389pub fn dehtml_manually(buf: &str) -> String {
390 let mut strbuilder = String::new();
392 let mut show_next_chars = true;
393 for c in buf.chars() {
394 match c {
395 '<' => show_next_chars = false,
396 '>' => show_next_chars = true,
397 _ => {
398 if show_next_chars {
399 strbuilder.push(c)
400 }
401 }
402 }
403 }
404 strbuilder
405}
406
407#[cfg(test)]
408mod tests {
409 use super::*;
410
411 #[test]
412 fn test_dehtml() {
413 let cases = vec",
417 ),
418 ("<b> bar </b>", "* bar *"),
419 ("<i>foo</i>", "_foo_"),
420 ("<b> bar <i> foo", "* bar _ foo"),
421 ("& bar", "& bar"),
422 ("<a href='/foo.png>Hi</a> ", "Hi"),
424 ("No link: <a href='https://get.delta.chat/'/>", "No link:"),
425 (
426 "No link: <a href='https://get.delta.chat/'></a>",
427 "No link:",
428 ),
429 ("<!doctype html>\n<b>fat text</b>", "*fat text*"),
430 ("<!some invalid html code>\n<b>some text</b>", "some text"),
432 ];
433 for (input, output) in cases {
434 assert_eq!(dehtml(input).unwrap().text, output);
435 }
436 let none_cases = vec!["<html> </html>", ""];
437 for input in none_cases {
438 assert_eq!(dehtml(input), None);
439 }
440 }
441
442 #[test]
443 fn test_dehtml_parse_br() {
444 let html = "line1<br>line2";
445 let plain = dehtml(html).unwrap().text;
446 assert_eq!(plain, "line1\nline2");
447
448 let html = "line1<br> line2";
449 let plain = dehtml(html).unwrap().text;
450 assert_eq!(plain, "line1\nline2");
451
452 let html = "line1 <br><br> line2";
453 let plain = dehtml(html).unwrap().text;
454 assert_eq!(plain, "line1\n\nline2");
455
456 let html = "\r\r\nline1<br>\r\n\r\n\r\rline2<br/>line3\n\r";
457 let plain = dehtml(html).unwrap().text;
458 assert_eq!(plain, "line1\nline2\nline3");
459 }
460
461 #[test]
462 fn test_dehtml_parse_span() {
463 assert_eq!(dehtml("<span>Foo</span>bar").unwrap().text, "Foobar");
464 assert_eq!(dehtml("<span>Foo</span> bar").unwrap().text, "Foo bar");
465 assert_eq!(dehtml("<span>Foo </span>bar").unwrap().text, "Foo bar");
466 assert_eq!(dehtml("<span>Foo</span>\nbar").unwrap().text, "Foo bar");
467 assert_eq!(dehtml("\n<span>Foo</span> bar").unwrap().text, "Foo bar");
468 assert_eq!(dehtml("<span>Foo</span>\n\nbar").unwrap().text, "Foo bar");
469 assert_eq!(dehtml("Foo\n<span>bar</span>").unwrap().text, "Foo bar");
470 assert_eq!(dehtml("Foo<span>\nbar</span>").unwrap().text, "Foo bar");
471 }
472
473 #[test]
474 fn test_dehtml_parse_p() {
475 let html = "<p>Foo</p><p>Bar</p>";
476 let plain = dehtml(html).unwrap().text;
477 assert_eq!(plain, "Foo\n\nBar");
478
479 let html = "<p>Foo<p>Bar";
480 let plain = dehtml(html).unwrap().text;
481 assert_eq!(plain, "Foo\n\nBar");
482
483 let html = "<p>Foo</p><p>Bar<p>Baz";
484 let plain = dehtml(html).unwrap().text;
485 assert_eq!(plain, "Foo\n\nBar\n\nBaz");
486 }
487
488 #[test]
489 fn test_dehtml_parse_href() {
490 let html = "<a href=url>text</a>";
491 let plain = dehtml(html).unwrap().text;
492
493 assert_eq!(plain, "[text](url)");
494 }
495
496 #[test]
497 fn test_dehtml_case_sensitive_link() {
498 let html = "<html><A HrEf=\"https://foo.bar/Data\">case in URLs matter</A></html>";
499 let plain = dehtml(html).unwrap().text;
500 assert_eq!(plain, "[case in URLs matter](https://foo.bar/Data)");
501 }
502
503 #[test]
504 fn test_dehtml_bold_text() {
505 let html = "<!DOCTYPE name [<!DOCTYPE ...>]><!-- comment -->text <b><?php echo ... ?>bold</b><![CDATA[<>]]>";
506 let plain = dehtml(html).unwrap().text;
507
508 assert_eq!(plain, "text *bold*<>");
509 }
510
511 #[test]
512 fn test_dehtml_html_encoded() {
513 let html = "<>"'& äÄöÖüÜß fooÆçÇ ♦‎‏‌&noent;‍";
514
515 let plain = dehtml(html).unwrap().text;
516
517 assert_eq!(
518 plain,
519 "<>\"\'& äÄöÖüÜß fooÆçÇ \u{2666}\u{200e}\u{200f}\u{200c}&noent;\u{200d}"
520 );
521 }
522
523 #[test]
524 fn test_unclosed_tags() {
525 let input = r##"
526 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
527 'http://www.w3.org/TR/html4/loose.dtd'>
528 <html>
529 <head>
530 <title>Hi</title>
531 <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
532 </head>
533 <body>
534 lots of text
535 </body>
536 </html>
537 "##;
538 let txt = dehtml(input).unwrap();
539 assert_eq!(txt.text.trim(), "lots of text");
540 }
541
542 #[test]
543 fn test_pre_tag() {
544 let input = "<html><pre>\ntwo\nlines\n</pre></html>";
545 let txt = dehtml(input).unwrap();
546 assert_eq!(txt.text.trim(), "two\nlines");
547 }
548
549 #[test]
550 fn test_hp_legacy_display() {
551 let input = r#"
552<html><head><title></title></head><body>
553<div class="header-protection-legacy-display">
554<pre>Subject: Dinner plans</pre>
555</div>
556<p>
557Let's meet at Rama's Roti Shop at 8pm and go to the park
558from there.
559</p>
560</body>
561</html>
562 "#;
563 let txt = dehtml(input).unwrap();
564 assert_eq!(
565 txt.text.trim(),
566 "Let's meet at Rama's Roti Shop at 8pm and go to the park from there."
567 );
568 }
569
570 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
571 async fn test_quote_div() {
572 let input = include_str!("../test-data/message/gmx-quote-body.eml");
573 let dehtml = dehtml(input).unwrap();
574 let SimplifiedText {
575 text,
576 is_forwarded,
577 is_cut,
578 top_quote,
579 footer,
580 } = dehtml;
581 assert_eq!(text, "Test");
582 assert_eq!(is_forwarded, false);
583 assert_eq!(is_cut, false);
584 assert_eq!(top_quote.as_deref(), Some("test"));
585 assert_eq!(footer, None);
586 }
587
588 #[test]
589 fn test_spaces() {
590 let input = include_str!("../test-data/spaces.html");
591 let txt = dehtml(input).unwrap();
592 assert_eq!(
593 txt.text,
594 "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)"
595 );
596 }
597}