Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Escape HTML entities in inline code blocks.
Chris Heald42f239915526
diff --git a/zerver/lib/bugdown/__init__.py b/zerver/lib/bugdown/__init__.pyindex 36537882cb0..9654abe8514 100644--- a/zerver/lib/bugdown/__init__.py+++ b/zerver/lib/bugdown/__init__.py@@ -522,22 +522,18 @@ class InlineHttpsProcessor(markdown.treeprocessors.Treeprocessor):continueimg.set("src", get_camo_url(url))-class BacktickPattern(markdown.inlinepatterns.Pattern):+class BacktickInlineProcessor(markdown.inlinepatterns.BacktickInlineProcessor):""" Return a `<code>` element containing the matching text. """-- def __init__(self, pattern: str) -> None:- markdown.inlinepatterns.Pattern.__init__(self, pattern)- self.ESCAPED_BSLASH = '{}{}{}'.format(markdown.util.STX, ord('\\'), markdown.util.ETX)- self.tag = 'code'-- def handleMatch(self, m: Match[str]) -> Union[str, Element]:- if m.group(4):- el = Element(self.tag)- # Modified to not strip whitespace- el.text = markdown.util.AtomicString(m.group(4))- return el- else:- return m.group(2).replace('\\\\', self.ESCAPED_BSLASH)+ def handleMatch(self, m: Match[str], data: str) -> Tuple[Union[None, Element], int, int]:+ # Let upstream's implementation do its job as it is, we'll+ # just replace the text to not strip the group because it+ # makes it impossible to put leading/trailing whitespace in+ # an inline code block.+ el, start, end = super().handleMatch(m, data)+ if m.group(3):+ # upstream's code here is: m.group(3).strip() rather than m.group(3).+ el.text = markdown.util.AtomicString(markdown.util.code_escape(m.group(3)))+ return el, start, endclass InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):TWITTER_MAX_IMAGE_HEIGHT = 400@@ -1928,14 +1924,12 @@ class Bugdown(markdown.Markdown):EMPHASIS_RE = r'(\*)(?!\s+)([^\*^\n]+)(?<!\s)\*'ENTITY_RE = markdown.inlinepatterns.ENTITY_RESTRONG_EM_RE = r'(\*\*\*)(?!\s+)([^\*^\n]+)(?<!\s)\*\*\*'- # Inline code block without whitespace stripping- BACKTICK_RE = r'(?:(?<!\\)((?:\\{2})+)(?=`+)|(?<!\\)(`+)(.+?)(?<!`)\3(?!`))'# Add Inline Patterns. We use a custom numbering of the# rules, that preserves the order from upstream but leaves# space for us to add our own.reg = markdown.util.Registry()- reg.register(BacktickPattern(BACKTICK_RE), 'backtick', 105)+ reg.register(BacktickInlineProcessor(markdown.inlinepatterns.BACKTICK_RE), 'backtick', 105)reg.register(markdown.inlinepatterns.DoubleTagPattern(STRONG_EM_RE, 'strong,em'), 'strong_em', 100)reg.register(UserMentionPattern(mention.find_mentions, self), 'usermention', 95)reg.register(Tex(r'\B(?<!\$)\$\$(?P<body>[^\n_$](\\\$|[^$\n])*)\$\$(?!\$)\B'), 'tex', 90)diff --git a/zerver/tests/fixtures/markdown_test_cases.json b/zerver/tests/fixtures/markdown_test_cases.jsonindex 9f794df1b7c..9b71255c073 100644--- a/zerver/tests/fixtures/markdown_test_cases.json+++ b/zerver/tests/fixtures/markdown_test_cases.json@@ -41,6 +41,12 @@"expected_output": "<p><code> outer </code> <code> space </code></p>","text_content": " outer space "},+ {+ "name": "inline_code_html_entities",+ "input": "`< >`",+ "expected_output": "<p><code><&nbsp;></code></p>",+ "text_content": "< >"+ },{"name": "codeblock_backticks","input": "\n```\nfenced code\n```\n\n```inline code```\n",diff --git a/zerver/tests/test_bugdown.py b/zerver/tests/test_bugdown.pyindex e08238f3d05..1dbf6da4179 100644--- a/zerver/tests/test_bugdown.py+++ b/zerver/tests/test_bugdown.py@@ -1,6 +1,7 @@import copyimport osimport re+from textwrap import dedentfrom typing import Any, Dict, List, Optional, Set, Tuplefrom unittest import mock@@ -2008,6 +2009,57 @@ class BugdownTest(ZulipTestCase):'<p><a href="#narrow/stream/999-hello">hello</a></p>',)+ def test_html_entity_conversion(self) -> None:+ msg = """\+ Test raw: Hello, ©+ Test inline code: `©`++ Test fenced code:+ ```+ ©+ ©+ ```++ Test quote:+ ~~~quote+ ©+ ~~~++ Test a list:+ * ©+ * `©`+ * ```©```++ Test an indented block:++ ©"""++ expected_output = """\+ <p>Test raw: Hello, ©<br>+ Test inline code: <code>&copy;</code></p>+ <p>Test fenced code:</p>+ <div class="codehilite"><pre><span></span><code>&copy;+ &copy;+ </code></pre></div>+++ <p>Test quote:</p>+ <blockquote>+ <p>©</p>+ </blockquote>+ <p>Test a list:</p>+ <ul>+ <li>©</li>+ <li><code>&copy;</code></li>+ <li><code>&copy;</code></li>+ </ul>+ <p>Test an indented block:</p>+ <div class="codehilite"><pre><span></span><code>&copy;+ </code></pre></div>"""++ converted = bugdown_convert(dedent(msg))+ self.assertEqual(converted, dedent(expected_output))+class BugdownApiTests(ZulipTestCase):def test_render_message_api(self) -> None:content = 'That is a **bold** statement'