diff --git a/contrib/limited_html_renderer.py b/contrib/limited_html_renderer.py new file mode 100644 index 00000000..2ff49a93 --- /dev/null +++ b/contrib/limited_html_renderer.py @@ -0,0 +1,15 @@ +import sys +if sys.version_info < (3, 4): + from mistletoe import _html as html +else: + import html +from mistletoe.html_renderer import HTMLRenderer + +class LimitedHTMLRenderer(HTMLRenderer): + @staticmethod + def render_html_block(token): + return html.escape(token.content) + + @staticmethod + def render_html_span(token): + return html.escape(token.content) diff --git a/test/test_contrib/test_limited_html_renderer.py b/test/test_contrib/test_limited_html_renderer.py new file mode 100644 index 00000000..49c25151 --- /dev/null +++ b/test/test_contrib/test_limited_html_renderer.py @@ -0,0 +1,38 @@ +from contrib.limited_html_renderer import LimitedHTMLRenderer +from mistletoe.block_token import Document +import random +import string +from unittest import TestCase + +class TestLimitedHTMLRenderer(TestCase): + def setUp(self): + self.renderer = LimitedHTMLRenderer() + self.renderer.__enter__() + self.addCleanup(self.renderer.__exit__, None, None, None) + + def check_render(self, inputString, expected, errormsg): + output = self.renderer.render(Document(inputString)) + output = output.strip() + self.assertEqual(output, expected, errormsg) + + def test_render_inline_div(self): + input = '
<span>hello</span>
' + self.check_render(input, output, 'One line span is not escaped') + + def test_render_embedded_markdown(self): + input = 'hello
\n</div>' + self.check_render(input, output, 'Markdown inside div is unexpectedly escaped') + + def test_block_quotes_not_escaped(self): + input = ''' + > Blockquote + > End of quote'''.replace(' ', '') + output = self.renderer.render(Document(input)) + self.assertNotIn('&', output, 'Blockquotes are unexpectedly escaped')