Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix emails forwarded from Thunderbird + slice_tree problem when the s…
…tart element is the same as the end element.
  • Loading branch information
thomasst committed May 25, 2016
1 parent 3d1c921 commit be9e691
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 5 deletions.
21 changes: 16 additions & 5 deletions quotequail/_html.py
Expand Up @@ -6,7 +6,7 @@
from ._patterns import FORWARD_LINE, FORWARD_STYLES, MULTIPLE_WHITESPACE_RE

INLINE_TAGS = ['a', 'b', 'em', 'i', 'strong', 'span', 'font', 'q',
'object', 'bdo', 'sub', 'sup', 'center']
'object', 'bdo', 'sub', 'sup', 'center', 'td', 'th']

This comment has been minimized.

Copy link
@philfreo

philfreo May 25, 2016

Member

There are more inline tags. Should you add them all? https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements#Elements


BEGIN = 'begin'
END = 'end'
Expand Down Expand Up @@ -151,11 +151,22 @@ def slice_tree(tree, start_refs, end_refs, slice_tuple, html_copy=None):
new_tree = tree

if start_ref:
trim_tree_before(start_ref[0],
include_element=(start_ref[1] == BEGIN))
include_start = (start_ref[1] == BEGIN)
if end_ref:
trim_tree_after(end_ref[0],
include_element=(end_ref[1] == END))
include_end = (end_ref[1] == END)

# If start_ref is the same as end_ref, and we don't include the element,
# we are removing the entire tree. We need to handle this separately,
# otherwise trim_tree_after won't work because it can't find the already
# removed reference.
if start_ref and end_ref and start_ref[0] == end_ref[0]:
if not include_start or not include_end:
return get_html_tree('')

if start_ref:
trim_tree_before(start_ref[0], include_element=include_start)
if end_ref:
trim_tree_after(end_ref[0], include_element=include_end)

return new_tree

Expand Down
26 changes: 26 additions & 0 deletions tests/__init__.py
Expand Up @@ -703,6 +703,20 @@ def test_outlook_forward(self):
'outlook_forward_unwrapped.html')
self.assert_equal_to_file(result['html_top'],
'outlook_forward_unwrapped_top.html')
self.assertNotIn('html_bottom', result)

def test_thunderbird_forward(self):
data = self.read_file('thunderbird_forward.html')
result = unwrap_html(data)
self.assertEqual(result['type'], 'forward')
self.assertEqual(result['from'], 'John Doe <johndoe@example.com>')
self.assertEqual(result['to'], 'Foo Bar <foobar@example.com>')
self.assertEqual(result['date'], 'Tue, 3 May 2016 14:54:27 +0200 (CEST)')
self.assertEqual(result['subject'], 'Re: Example subject')
self.assertNotIn('html_top', result)
self.assert_equal_to_file(result['html'],
'thunderbird_forward_unwrapped.html')
self.assertNotIn('html_bottom', result)

class InternalTestCase(unittest.TestCase):
def test_parse_reply(self):
Expand Down Expand Up @@ -844,6 +858,18 @@ def test_tree_line_generator(self):
((blockquote, 'end'), (div, 'end'), 0, 'world'),
])

tree = _html.get_html_tree('''
<table>
<tr><td>Subject: </td><td>the subject</td></tr>
<tr><td>From: </td><td>from line</td></tr>
</table>''')
data = [result for result in _html.tree_line_generator(tree)]
tr1, tr2 = tree.xpath('table/tr')
self.assertEqual(data, [
((tr1, 'begin'), (tr1, 'end'), 0, 'Subject: the subject'),
((tr2, 'begin'), (tr2, 'end'), 0, 'From: from line'),
])

def test_trim_after(self):
from quotequail import _html

Expand Down
45 changes: 45 additions & 0 deletions tests/files/thunderbird_forward.html
@@ -0,0 +1,45 @@
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p><br>
</p>
<div class="moz-forward-container"><br>
<br>
-------- Forwarded Message --------
<table class="moz-email-headers-table" border="0" cellpadding="0"
cellspacing="0">
<tbody>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">Subject:
</th>
<td>Re: Example subject</td>
</tr>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">Date: </th>
<td>Tue, 3 May 2016 14:54:27 +0200 (CEST)</td>
</tr>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">From: </th>
<td>John Doe <a class="moz-txt-link-rfc2396E" href="mailto:johndoe@example.com">&lt;johndoe@example.com&gt;</a></td>
</tr>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">To: </th>
<td>Foo Bar <a class="moz-txt-link-rfc2396E" href="mailto:foobar@example.com">&lt;foobar@example.com&gt;</a></td>
</tr>
</tbody>
</table>
<br>
<br>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<div>Dear John,</div>
<div><br>
</div>
<div>This is a test.</div>
</div>
</body>
</html>


11 changes: 11 additions & 0 deletions tests/files/thunderbird_forward_unwrapped.html
@@ -0,0 +1,11 @@
<html><head>

<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000"><div class="moz-forward-container"><div>Dear John,</div>
<div><br>
</div>
<div>This is a test.</div>
</div>
</body>
</html>

0 comments on commit be9e691

Please sign in to comment.