Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions quotequail/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"th",
]

IMAGE_LINE_TEXT = "\ufffc"


def trim_tree_after(element: Element, include_element: bool = True):
"""
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -416,13 +421,17 @@ 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
counter += 1
if max_lines is not None and counter > max_lines:
return
line = ""
has_image = False

if is_forward:
# Simulate forward
Expand All @@ -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

Expand Down
35 changes: 35 additions & 0 deletions tests/test_unwrap_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
"<p>Top text</p>"
"<p>From: someone@example.com<br>To: anyone@example.com</p>"
'<p><img src="cid:image001.jpg"></p>'
)

assert unwrap_html(html) == {
"type": "forward",
"from": "someone@example.com",
"to": "anyone@example.com",
"html_top": "<p>Top text</p>",
"html": '<p><img src="cid:image001.jpg"></p>',
}


def test_unwrap_html_image_at_start_of_quoted_body():
# An image at the start of the quoted message, followed by text, used to
# be dropped from the quote (closeio/quotequail#22).
html = (
"<p>Top text</p>"
"<p>From: someone@example.com<br>To: anyone@example.com</p>"
'<p><img src="cid:image001.jpg"><br>Quoted text</p>'
)

assert unwrap_html(html) == {
"type": "forward",
"from": "someone@example.com",
"to": "anyone@example.com",
"html_top": "<p>Top text</p>",
"html": '<p><img src="cid:image001.jpg"><br>Quoted text</p>',
}


@pytest.mark.parametrize(
("file", "expected"),
[
Expand Down