Skip to content

Commit

Permalink
fix: conversion html_to_text hr tags rstrip non string object (#1162)
Browse files Browse the repository at this point in the history
Co-authored-by: Fabio Lucattini <f.lucattini@desys.it>
  • Loading branch information
fabiottini and flucdesys committed Jul 17, 2024
1 parent ddb5b52 commit e30adb4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
4 changes: 3 additions & 1 deletion apprise/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ def handle_starttag(self, tag, attrs):
self._result.append('\n')

elif tag == 'hr':
if self._result:
if self._result and isinstance(self._result[-1], str):
self._result[-1] = self._result[-1].rstrip(' ')
else:
pass

self._result.append('\n---\n')

Expand Down
64 changes: 64 additions & 0 deletions test/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,70 @@ def to_html(body):
# If you give nothing, you get nothing in return
assert to_html("") == ""

# Special case on HR tag
assert to_html("""
<html>
<head></head>
<body>
<p><b>FROM: </b>apprise-test@mydomain.yyy
<apprise-test@mydomain.yyy></p>
Hi!<br/>
How are you?<br/>
<font color=3D"#FF0000">red font</font>
<a href=3D"http://www.python.org">link</a> you wanted.<br/>
</body>
</html>
""") == "FROM: apprise-test@mydomain.yyy\nHi!\n How are you?\n \
red font link you wanted."

assert to_html("""
<html>
<head></head>
<body>
<p><b>FROM: </b>apprise-test@mydomain.yyy
<apprise-test@mydomain.yyy><hr></p>
Hi!<br/>
How are you?<br/>
<font color=3D"#FF0000">red font</font>
<a href=3D"http://www.python.org">link</a> you wanted.<br/>
</body>
</html>
""") == "FROM: apprise-test@mydomain.yyy\n---\nHi!\n \
How are you?\n red font link you wanted."

# Special case on HR if text is sorrunded by HR tags
# its created a dict element
assert to_html("""
<html>
<head></head>
<body>
<p><hr><b>FROM: </b>apprise-test@mydomain.yyy
<apprise-test@mydomain.yyy><hr></p>
Hi!<br/>
How are you?<br/>
<font color=3D"#FF0000">red font</font>
<a href=3D"http://www.python.org">link</a> you wanted.<br/>
</body>
</html>
""") == "---\nFROM: apprise-test@mydomain.yyy\n---\nHi!\n \
How are you?\n red font link you wanted."

assert to_html("""
<html>
<head></head>
<body>
<p>
<hr><b>TEST</b><hr>
</p>
Hi!<br/>
How are you?<br/>
<font color=3D"#FF0000">red font</font>
<a href=3D"http://www.python.org">link</a> you wanted.<br/>
</body>
</html>
""") == "---\nTEST\n---\nHi!\n How are you?\n red font link you \
wanted."

with pytest.raises(TypeError):
# Invalid input
assert to_html(None)
Expand Down

0 comments on commit e30adb4

Please sign in to comment.