Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💡 Contrib limited html renderer (#74) #75

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions contrib/limited_html_renderer.py
Original file line number Diff line number Diff line change
@@ -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)
38 changes: 38 additions & 0 deletions test/test_contrib/test_limited_html_renderer.py
Original file line number Diff line number Diff line change
@@ -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 = '<div>hello</div>'
output = '&lt;div&gt;hello&lt;/div&gt;'
self.check_render(input, output, 'One line div is not escaped')

def test_render_inline_span(self):
input = '<span>hello</span>'
output = '<p>&lt;span&gt;hello&lt;/span&gt;</p>'
self.check_render(input, output, 'One line span is not escaped')

def test_render_embedded_markdown(self):
input = '<div>\n\n*hello*\n\n</div>'
output = '&lt;div&gt;\n<p><em>hello</em></p>\n&lt;/div&gt;'
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')