Skip to content

Commit

Permalink
Update PrettifyTreeprocessor <pre><code> handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fourpoints authored May 27, 2022
1 parent 96d27f1 commit dc434df
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/change_log/release-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
7 changes: 5 additions & 2 deletions markdown/treeprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
37 changes: 37 additions & 0 deletions tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<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. """

Expand Down

0 comments on commit dc434df

Please sign in to comment.