Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

email_notifications: Fix inline-ing of image-URL-only messages.

Alex Vandiver039b869ca58b
diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.py
index 88080932d98..94c0eb919f7 100644
--- a/zerver/lib/email_notifications.py
+++ b/zerver/lib/email_notifications.py
@@ -61,26 +61,30 @@ def relative_to_full_url(fragment: lxml.html.HtmlElement, base_url: str) -> None
if elem.get("title") is not None:
elem.set("title", link)
- # Inline images can't be displayed in the emails as the request
- # from the mail server can't be authenticated because it has no
- # user_profile object linked to it. So we scrub the inline image
- # container.
- inline_image_containers = fragment.find_class("message_inline_image")
- for container in inline_image_containers:
- container.drop_tree()
-
- # The previous block handles most inline images, but for messages
- # where the entire Markdown input was just the URL of an image
- # (i.e. the entire body is a message_inline_image object), the
- # entire message body will be that image element; here, we need a
- # more drastic edit to the content.
- if fragment.get("class") == "message_inline_image":
- image_link = fragment.find("a").get("href")
- image_title = fragment.find("a").get("title")
+ # Because we were parsed with fragment_fromstring, we are
+ # guaranteed there is a top-level <div>, and the original
+ # top-level contents are within that.
+ if len(fragment) == 1 and fragment[0].get("class") == "message_inline_image":
+ # The next block handles most inline images, but for messages
+ # where the entire Markdown input was just the URL of an image
+ # (i.e. the entire body is a message_inline_image object), the
+ # entire message body will be that image element; here, we need a
+ # more drastic edit to the content.
+ inner = fragment[0]
+ image_link = inner.find("a").get("href")
+ image_title = inner.find("a").get("title")
title_attr = {} if image_title is None else {"title": image_title}
- fragment.clear()
- fragment.tag = "p"
- fragment.append(E.A(image_link, href=image_link, target="_blank", **title_attr))
+ inner.clear()
+ inner.tag = "p"
+ inner.append(E.A(image_link, href=image_link, target="_blank", **title_attr))
+ else:
+ # Inline images can't be displayed in the emails as the request
+ # from the mail server can't be authenticated because it has no
+ # user_profile object linked to it. So we scrub the inline image
+ # container.
+ inline_image_containers = fragment.find_class("message_inline_image")
+ for container in inline_image_containers:
+ container.drop_tree()
fragment.make_links_absolute(base_url)
diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.py
index 02979ea894a..5454271c7c9 100644
--- a/zerver/tests/test_email_notifications.py
+++ b/zerver/tests/test_email_notifications.py
@@ -1257,7 +1257,7 @@ class TestMissedMessages(ZulipTestCase):
def test_relative_to_full_url(self) -> None:
def convert(test_data: str) -> str:
- fragment = lxml.html.fromstring(test_data)
+ fragment = lxml.html.fragment_fromstring(test_data, create_parent=True)
relative_to_full_url(fragment, "http://example.com")
return lxml.html.tostring(fragment, encoding="unicode")
@@ -1287,7 +1287,9 @@ class TestMissedMessages(ZulipTestCase):
# A path similar to our emoji path, but not in a link:
test_data = "<p>Check out the file at: '/static/generated/emoji/images/emoji/'</p>"
actual_output = convert(test_data)
- expected_output = "<p>Check out the file at: '/static/generated/emoji/images/emoji/'</p>"
+ expected_output = (
+ "<div><p>Check out the file at: '/static/generated/emoji/images/emoji/'</p></div>"
+ )
self.assertEqual(actual_output, expected_output)
# An uploaded file
@@ -1295,8 +1297,8 @@ class TestMissedMessages(ZulipTestCase):
test_data = test_data.format(realm_id=zephyr_realm.id)
actual_output = convert(test_data)
expected_output = (
- '<a href="http://example.com/user_uploads/{realm_id}/1f/some_random_value">'
- + "/user_uploads/{realm_id}/1f/some_random_value</a>"
+ '<div><a href="http://example.com/user_uploads/{realm_id}/1f/some_random_value">'
+ + "/user_uploads/{realm_id}/1f/some_random_value</a></div>"
)
expected_output = expected_output.format(realm_id=zephyr_realm.id)
self.assertEqual(actual_output, expected_output)
@@ -1304,7 +1306,7 @@ class TestMissedMessages(ZulipTestCase):
# A profile picture like syntax, but not actually in an HTML tag
test_data = '<p>Set src="/avatar/[email protected]?s=30"</p>'
actual_output = convert(test_data)
- expected_output = '<p>Set src="/avatar/[email protected]?s=30"</p>'
+ expected_output = '<div><p>Set src="/avatar/[email protected]?s=30"</p></div>'
self.assertEqual(actual_output, expected_output)
# A narrow URL which begins with a '#'.
@@ -1314,8 +1316,8 @@ class TestMissedMessages(ZulipTestCase):
)
actual_output = convert(test_data)
expected_output = (
- '<p><a href="http://example.com/#narrow/stream/test/topic/test.20topic/near/142" '
- + 'title="http://example.com/#narrow/stream/test/topic/test.20topic/near/142">Conversation</a></p>'
+ '<div><p><a href="http://example.com/#narrow/stream/test/topic/test.20topic/near/142" '
+ + 'title="http://example.com/#narrow/stream/test/topic/test.20topic/near/142">Conversation</a></p></div>'
)
self.assertEqual(actual_output, expected_output)
@@ -1345,9 +1347,9 @@ class TestMissedMessages(ZulipTestCase):
)
actual_output = convert(test_data)
expected_output = (
- '<p><a href="https://www.google.com/images/srpr/logo4w.png" '
+ '<div><p><a href="https://www.google.com/images/srpr/logo4w.png" '
+ 'target="_blank" title="https://www.google.com/images/srpr/logo4w.png">'
- + "https://www.google.com/images/srpr/logo4w.png</a></p>"
+ + "https://www.google.com/images/srpr/logo4w.png</a></p></div>"
)
self.assertEqual(actual_output, expected_output)