diff --git a/quotequail/_html.py b/quotequail/_html.py index e58c8ae..29a3bd2 100644 --- a/quotequail/_html.py +++ b/quotequail/_html.py @@ -34,6 +34,8 @@ "th", ] +IMAGE_LINE_TEXT = "\ufffc" + def trim_tree_after(element: Element, include_element: bool = True): """ @@ -389,6 +391,9 @@ def _trim_spaces(text: str) -> str: # Buffer for the current line. line = "" + # Whether the current line contains an image. + has_image = False + # The reference tuple (element, position) for the start of the line. start_ref = None @@ -416,6 +421,9 @@ def _trim_spaces(text: str) -> str: if is_block or line_break: line = _trim_spaces(line) + if not line and has_image: + line = IMAGE_LINE_TEXT + if line or line_break or is_forward: end_ref = (el, state) yield start_ref, end_ref, start_indentation_level, line @@ -423,6 +431,7 @@ def _trim_spaces(text: str) -> str: if max_lines is not None and counter > max_lines: return line = "" + has_image = False if is_forward: # Simulate forward @@ -440,6 +449,12 @@ def _trim_spaces(text: str) -> str: start_ref = (el, state) start_indentation_level = indentation_level + # Flag the image after any pending line was flushed above, so that + # the image starts a line of its own (start_ref now points at it) + # and gets flushed at its end tag. + if tag_name == "img" and state is Position.Begin: + has_image = True + elif isinstance(token, str): line += token diff --git a/tests/test_unwrap_html.py b/tests/test_unwrap_html.py index 6ac5233..da8e2b5 100644 --- a/tests/test_unwrap_html.py +++ b/tests/test_unwrap_html.py @@ -22,6 +22,41 @@ def test_unwrap_html_simple(html, expected): assert unwrap_html(html) == expected +def test_unwrap_html_image_only_quoted_body(): + # The quoted message consists of an inline image only, with no text. + html = ( + "
Top text
" + "From: someone@example.com
To: anyone@example.com

Top text
", + "html": '
Top text
" + "From: someone@example.com
To: anyone@example.com

Quoted text
Top text
", + "html": '
Quoted text