Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Fix invalid mention bug for user mention.
m-e-l-u-h-a-nc8979a5100eb
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex 3fbaf43ddcf..67df4ea4f67 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -1796,8 +1796,10 @@ class RealmFilterPattern(markdown.inlinepatterns.Pattern):)-class UserMentionPattern(markdown.inlinepatterns.Pattern):- def handleMatch(self, m: Match[str]) -> Optional[Element]:+class UserMentionPattern(markdown.inlinepatterns.InlineProcessor):+ def handleMatch( # type: ignore[override] # supertype incompatible with supersupertype+ self, m: Match[str], data: str+ ) -> Union[Tuple[None, None, None], Tuple[Element, int, int]]:match = m.group("match")silent = m.group("silent") == "_"@@ -1806,7 +1808,7 @@ class UserMentionPattern(markdown.inlinepatterns.Pattern):if match.startswith("**") and match.endswith("**"):name = match[2:-2]else:- return None+ return None, None, Nonewildcard = mention.user_mention_matches_wildcard(name)@@ -1827,7 +1829,7 @@ class UserMentionPattern(markdown.inlinepatterns.Pattern):user_id = str(user["id"])else:# Don't highlight @mentions that don't refer to a valid user- return None+ return None, None, Noneel = Element("span")el.set("data-user-id", user_id)@@ -1838,8 +1840,8 @@ class UserMentionPattern(markdown.inlinepatterns.Pattern):el.set("class", "user-mention")text = f"@{text}"el.text = markdown.util.AtomicString(text)- return el- return None+ return el, m.start(), m.end()+ return None, None, Noneclass UserGroupMentionPattern(markdown.inlinepatterns.Pattern):diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex c23661f6099..09a59deae54 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -1845,6 +1845,36 @@ class MarkdownTest(ZulipTestCase):)self.assertEqual(msg.mentions_user_ids, set())+ def test_mention_invalid_followed_by_valid(self) -> None:+ sender_user_profile = self.example_user("othello")+ user_profile = self.example_user("hamlet")+ msg = Message(sender=sender_user_profile, sending_client=get_client("test"))+ user_id = user_profile.id++ content = "@**Invalid user** and @**King Hamlet**"+ self.assertEqual(+ render_markdown(msg, content),+ '<p>@<strong>Invalid user</strong> and <span class="user-mention" '+ f'data-user-id="{user_id}">'+ "@King Hamlet</span></p>",+ )+ self.assertEqual(msg.mentions_user_ids, {user_profile.id})++ def test_silent_mention_invalid_followed_by_valid(self) -> None:+ sender_user_profile = self.example_user("othello")+ user_profile = self.example_user("hamlet")+ msg = Message(sender=sender_user_profile, sending_client=get_client("test"))+ user_id = user_profile.id++ content = "@_**Invalid user** and @_**King Hamlet**"+ self.assertEqual(+ render_markdown(msg, content),+ '<p>@_<strong>Invalid user</strong> and <span class="user-mention silent" '+ f'data-user-id="{user_id}">'+ "King Hamlet</span></p>",+ )+ self.assertEqual(msg.mentions_user_ids, set())+def test_possible_mentions(self) -> None:def assert_mentions(content: str, names: Set[str], has_wildcards: bool = False) -> None:self.assertEqual(possible_mentions(content), (names, has_wildcards))