From d7e0f99babd33700aa02edd4760deab800c8b5b1 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Mon, 25 Aug 2014 10:17:28 -0400 Subject: Add test of unsafe HTML in stash with safe_mode='excape' This should give us 100% coverage of postprocessors.py. --- tests/test_apis.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_apis.py b/tests/test_apis.py index 5117ccd..e99f606 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -135,6 +135,26 @@ class TestHtmlStash(unittest.TestCase): self.assertEqual(self.stash.html_counter, 0) self.assertEqual(self.stash.rawHtmlBlocks, []) + def testUnsafeHtmlInSafeMode(self): + """ Test that unsafe HTML gets escaped in safe_mode. """ + output = markdown.markdown('foo', extensions=[self.build_extension()], safe_mode='escape') + self.assertEqual(output, '

<script>print("evil")</script>

') + + def build_extension(self): + """ Build an extention that addes unsafe html to Stash in same_mode. """ + class Unsafe(markdown.treeprocessors.Treeprocessor): + def run(self, root): + el = root.find('p') + el.text = self.markdown.htmlStash.store('', safe=False) + return root + + class StoreUnsafeHtml(markdown.extensions.Extension): + def extendMarkdown(self, md, md_globals): + md.treeprocessors.add('unsafe', Unsafe(md), '_end') + + return StoreUnsafeHtml() + + class TestOrderedDict(unittest.TestCase): """ Test OrderedDict storage class. """ -- cgit v1.2.3