diff options
author | fourpoints <30053843+fourpoints@users.noreply.github.com> | 2022-05-27 15:01:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-27 09:01:57 -0400 |
commit | dc434df776fe9af36fe07c4e782e51035ce30e1f (patch) | |
tree | f79d28a0b570d7d7271183adb0a91af7df839a59 | |
parent | 96d27f191bf31e76fca8c4294a3238519d13a631 (diff) | |
download | python-markdown-dc434df776fe9af36fe07c4e782e51035ce30e1f.tar.gz |
Update PrettifyTreeprocessor `<pre><code>` handling
Fixes #1263.
-rw-r--r-- | docs/change_log/release-3.4.md | 2 | ||||
-rw-r--r-- | markdown/treeprocessors.py | 7 | ||||
-rw-r--r-- | tests/test_apis.py | 37 |
3 files changed, 43 insertions, 3 deletions
diff --git a/docs/change_log/release-3.4.md b/docs/change_log/release-3.4.md index b38831c..a795734 100644 --- a/docs/change_log/release-3.4.md +++ b/docs/change_log/release-3.4.md @@ -68,4 +68,4 @@ The following new features have been included in the 3.4 release: The following bug fixes are included in the 3.4 release: * Extension entry-points are only loaded if needed (#1216). -* Added a `None` check to `PrettifyTreeprocessor` (#1261). +* Added additional checks to the `<pre><code>` handling of `PrettifyTreeprocessor` (#1261, #1263). diff --git a/markdown/treeprocessors.py b/markdown/treeprocessors.py index f634ff5..99490bf 100644 --- a/markdown/treeprocessors.py +++ b/markdown/treeprocessors.py @@ -432,5 +432,8 @@ class PrettifyTreeprocessor(Treeprocessor): # Clean up extra empty lines at end of code blocks. pres = root.iter('pre') for pre in pres: - if len(pre) and pre[0].tag == 'code' and pre[0].text is not None: - pre[0].text = util.AtomicString(pre[0].text.rstrip() + '\n') + if len(pre) and pre[0].tag == 'code': + code = pre[0] + # Only prettify code containing text only + if not len(code) and code.text is not None: + code.text = util.AtomicString(code.text.rstrip() + '\n') diff --git a/tests/test_apis.py b/tests/test_apis.py index 7a3c735..efdc383 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -525,6 +525,43 @@ class testElementTailTests(unittest.TestCase): self.assertEqual(br.tail, "\n") +class testElementPreCodeTests(unittest.TestCase): + """ Element PreCode Tests """ + def setUp(self): + md = markdown.Markdown() + self.pretty = markdown.treeprocessors.PrettifyTreeprocessor(md) + + def prettify(self, xml): + root = etree.fromstring(xml) + self.pretty.run(root) + return etree.tostring(root, encoding="unicode", short_empty_elements=False) + + def testPreCodeEmpty(self): + xml = "<pre><code></code></pre>" + expected = "<pre><code></code></pre>\n" + self.assertEqual(expected, self.prettify(xml)) + + def testPreCodeWithChildren(self): + xml = "<pre><code> <span /></code></pre>" + expected = "<pre><code> <span></span></code></pre>\n" + self.assertEqual(expected, self.prettify(xml)) + + def testPreCodeWithSpaceOnly(self): + xml = "<pre><code> </code></pre>" + expected = "<pre><code>\n</code></pre>\n" + self.assertEqual(expected, self.prettify(xml)) + + def testPreCodeWithText(self): + xml = "<pre><code> hello</code></pre>" + expected = "<pre><code> hello\n</code></pre>\n" + self.assertEqual(expected, self.prettify(xml)) + + def testPreCodeWithTrailingSpace(self): + xml = "<pre><code> hello </code></pre>" + expected = "<pre><code> hello\n</code></pre>\n" + self.assertEqual(expected, self.prettify(xml)) + + class testSerializers(unittest.TestCase): """ Test the html and xhtml serializers. """ |