diff --git a/docs/change_log/release-3.4.md b/docs/change_log/release-3.4.md index b38831cbf..a79573484 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 `
` handling of `PrettifyTreeprocessor` (#1261, #1263).
diff --git a/markdown/treeprocessors.py b/markdown/treeprocessors.py
index f634ff53c..99490bf44 100644
--- a/markdown/treeprocessors.py
+++ b/markdown/treeprocessors.py
@@ -432,5 +432,8 @@ def run(self, root):
# 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 7a3c73550..efdc38370 100644
--- a/tests/test_apis.py
+++ b/tests/test_apis.py
@@ -525,6 +525,43 @@ def testBrTailNoNewline(self):
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 = "
"
+ expected = "
\n"
+ self.assertEqual(expected, self.prettify(xml))
+
+ def testPreCodeWithChildren(self):
+ xml = "
"
+ expected = "
\n"
+ self.assertEqual(expected, self.prettify(xml))
+
+ def testPreCodeWithSpaceOnly(self):
+ xml = "
"
+ expected = "\n
\n"
+ self.assertEqual(expected, self.prettify(xml))
+
+ def testPreCodeWithText(self):
+ xml = " hello
"
+ expected = " hello\n
\n"
+ self.assertEqual(expected, self.prettify(xml))
+
+ def testPreCodeWithTrailingSpace(self):
+ xml = " hello
"
+ expected = " hello\n
\n"
+ self.assertEqual(expected, self.prettify(xml))
+
+
class testSerializers(unittest.TestCase):
""" Test the html and xhtml serializers. """