Skip to content

Commit

Permalink
Unescape any backslash escaped inline raw HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
waylan committed Jul 19, 2023
1 parent 93054dd commit d60c16f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/change_log/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Python-Markdown Change Log
*under development*: version 3.4.4 (a bug-fix release).

* Add a special case for initial 's to smarty extension (#1305).
* Unescape any backslash escaped inline raw HTML (#1358).

March 23, 2023: version 3.4.3 (a bug-fix release).

Expand Down
14 changes: 13 additions & 1 deletion markdown/inlinepatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def handleMatch(self, m, data): # pragma: no cover
class HtmlInlineProcessor(InlineProcessor):
""" Store raw inline html and return a placeholder. """
def handleMatch(self, m, data):
rawhtml = self.unescape(m.group(1))
rawhtml = self.backslash_unescape(self.unescape(m.group(1)))
place_holder = self.md.htmlStash.store(rawhtml)
return place_holder, m.start(0), m.end(0)

Expand All @@ -438,6 +438,18 @@ def get_stash(m):

return util.INLINE_PLACEHOLDER_RE.sub(get_stash, text)

def backslash_unescape(self, text):
""" Return text with backslash escapes undone (backslashes are restored). """
try:
RE = self.md.treeprocessors['unescape'].RE
except KeyError: # pragma: no cover
return text

def _unescape(m):
return chr(int(m.group(1)))

return RE.sub(_unescape, text)


class AsteriskProcessor(InlineProcessor):
"""Emphasis processor for handling strong and em matches inside asterisks."""
Expand Down
3 changes: 3 additions & 0 deletions tests/test_syntax/inline/test_raw_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ def test_inline_html_angle_brackets(self):
self.assertMarkdownRenders("<span>e>c</span>", "<p><span>e&gt;c</span></p>")
self.assertMarkdownRenders("<span>e < c</span>", "<p><span>e &lt; c</span></p>")
self.assertMarkdownRenders("<span>e > c</span>", "<p><span>e &gt; c</span></p>")

def test_inline_html_backslashes(self):
self.assertMarkdownRenders('<img src="..\\..\\foo.png">', '<p><img src="..\\..\\foo.png"></p>')

0 comments on commit d60c16f

Please sign in to comment.