Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
email_notifications: Build HTML elements correctly.
Anders Kaseorg905de1fdbaa5
diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.pyindex de46d612f49..8b5ff7d07d9 100644--- a/zerver/lib/email_notifications.py+++ b/zerver/lib/email_notifications.py@@ -16,6 +16,7 @@ from django.contrib.auth import get_backendsfrom django.utils.timezone import now as timezone_nowfrom django.utils.translation import gettext as _from django.utils.translation import override as override_language+from lxml.html import builder as Efrom confirmation.models import one_click_unsubscribe_linkfrom zerver.decorator import statsd_increment@@ -79,13 +80,8 @@ def relative_to_full_url(base_url: str, content: str) -> str:if fragment.get("class") == "message_inline_image":image_link = fragment.find("a").get("href")image_title = fragment.find("a").get("title")- fragment = lxml.html.Element("p")- a = lxml.html.Element("a")- a.set("href", image_link)- a.set("target", "_blank")- a.set("title", image_title)- a.text = image_link- fragment.append(a)+ title_attr = {} if image_title is None else {"title": image_title}+ fragment = E.P(E.A(image_link, href=image_link, target="_blank", **title_attr))fragment.make_links_absolute(base_url)content = lxml.html.tostring(fragment, encoding="unicode")@@ -106,10 +102,7 @@ def fix_emojis(content: str, base_url: str, emojiset: str) -> str:emoji_name = emoji_span_elem.get("title")alt_code = emoji_span_elem.textimage_url = base_url + f"/static/generated/emoji/images-{emojiset}-64/{emoji_code}.png"- img_elem = lxml.html.fromstring(- f'<img alt="{alt_code}" src="{image_url}" title="{emoji_name}">'- )- img_elem.set("style", "height: 20px;")+ img_elem = E.IMG(alt=alt_code, src=image_url, title=emoji_name, style="height: 20px;")img_elem.tail = emoji_span_elem.tailreturn img_elem@@ -138,16 +131,13 @@ def fix_spoilers_in_html(content: str, language: str) -> str:header_content = header.find("p")if header_content is None:# Create a new element to append the spoiler to)- header_content = lxml.html.fromstring("<p></p>")+ header_content = E.P()header.append(header_content)else:- # Add a space. Its simpler to append a new span element than- # inserting text after the last node ends since neither .text- # and .tail do the right thing for us.- header_content.append(lxml.html.fromstring("<span> </span>"))- span_elem = lxml.html.fromstring(- f'<span class="spoiler-title" title="{spoiler_title}">({spoiler_title})</span'- )+ # Add a space.+ rear = header_content[-1] if len(header_content) else header_content+ rear.tail = (rear.tail or "") + " "+ span_elem = E.SPAN(f"({spoiler_title})", **E.CLASS("spoiler-title"), title=spoiler_title)header_content.append(span_elem)header.drop_tag()spoiler_content.drop_tree()diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.pyindex 45a22e01a4e..de07652e58d 100644--- a/zerver/tests/test_email_notifications.py+++ b/zerver/tests/test_email_notifications.py@@ -1309,7 +1309,7 @@ class TestMissedMessages(ZulipTestCase):def test_spoilers_in_html_emails(self) -> None:test_data = '<div class="spoiler-block"><div class="spoiler-header">\n\n<p><a>header</a> text</p>\n</div><div class="spoiler-content" aria-hidden="true">\n\n<p>content</p>\n</div></div>\n\n<p>outside spoiler</p>'actual_output = fix_spoilers_in_html(test_data, "en")- expected_output = '<div><div class="spoiler-block">\n\n<p><a>header</a> text<span> </span><span class="spoiler-title" title="Open Zulip to see the spoiler content">(Open Zulip to see the spoiler content)</span></p>\n</div>\n\n<p>outside spoiler</p></div>'+ expected_output = '<div><div class="spoiler-block">\n\n<p><a>header</a> text <span class="spoiler-title" title="Open Zulip to see the spoiler content">(Open Zulip to see the spoiler content)</span></p>\n</div>\n\n<p>outside spoiler</p></div>'self.assertEqual(actual_output, expected_output)# test against our markdown_test_cases so these features do not get out of sync.