From 935d55bc1320612bf7f2e46c3510f43eb0531108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Thu, 17 Mar 2016 14:03:07 +0000 Subject: [PATCH 1/4] Add failing toc test --- tests/test_extensions.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 6d47e0ebd..a5b739e72 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -775,6 +775,21 @@ def testAnchorLink(self): '

Header 2

' ) + def testAnchorLinkWithInlineCode(self): + """ Test TOC Anchorlink with inline code. """ + md = markdown.Markdown( + extensions=[markdown.extensions.toc.TocExtension(anchorlink=True)] + ) + text = '# This is `code` and `this` too.' + self.assertEqual( + md.convert(text), + '

' # noqa + '' # noqa + 'This is code and this too.' # noqa + '' # noqa + '

' # noqa + ) + def testTitle(self): """ Test TOC Title. """ md = markdown.Markdown( From 56f491eeeeddb9651d68a0fd6d21aac139abca52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Thu, 17 Mar 2016 15:32:57 +0000 Subject: [PATCH 2/4] Add toc permalink tests --- tests/test_extensions.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_extensions.py b/tests/test_extensions.py index a5b739e72..7a18b9491 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -790,6 +790,34 @@ def testAnchorLinkWithInlineCode(self): '' # noqa ) + def testPermalink(self): + """ Test TOC Permalink. """ + md = markdown.Markdown( + extensions=[markdown.extensions.toc.TocExtension(permalink=True)] + ) + text = '# Header' + self.assertEqual( + md.convert(text), + '

' # noqa + 'Header' # noqa + '' # noqa + '

' # noqa + ) + + def testPermalinkWithInlineCode(self): + """ Test TOC Permalink with inline code. """ + md = markdown.Markdown( + extensions=[markdown.extensions.toc.TocExtension(permalink=True)] + ) + text = '# This is `code` and `this` too.' + self.assertEqual( + md.convert(text), + '

' # noqa + 'This is code and this too.' # noqa + '' # noqa + '

' # noqa + ) + def testTitle(self): """ Test TOC Title. """ md = markdown.Markdown( From 6a1b408f82d6c7e59730a4641554bdcab492eb21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Thu, 17 Mar 2016 16:00:01 +0000 Subject: [PATCH 3/4] Add single inline code toc tests --- tests/test_extensions.py | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 7a18b9491..19a138974 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -775,8 +775,23 @@ def testAnchorLink(self): '

Header 2

' ) - def testAnchorLinkWithInlineCode(self): - """ Test TOC Anchorlink with inline code. """ + def testAnchorLinkWithSingleInlineCode(self): + """ Test TOC Anchorlink with single inline code. """ + md = markdown.Markdown( + extensions=[markdown.extensions.toc.TocExtension(anchorlink=True)] + ) + text = '# This is `code`.' + self.assertEqual( + md.convert(text), + '

' # noqa + '' # noqa + 'This is code.' # noqa + '' # noqa + '

' # noqa + ) + + def testAnchorLinkWithDoubleInlineCode(self): + """ Test TOC Anchorlink with double inline code. """ md = markdown.Markdown( extensions=[markdown.extensions.toc.TocExtension(anchorlink=True)] ) @@ -804,8 +819,22 @@ def testPermalink(self): '' # noqa ) - def testPermalinkWithInlineCode(self): - """ Test TOC Permalink with inline code. """ + def testPermalinkWithSingleInlineCode(self): + """ Test TOC Permalink with single inline code. """ + md = markdown.Markdown( + extensions=[markdown.extensions.toc.TocExtension(permalink=True)] + ) + text = '# This is `code`.' + self.assertEqual( + md.convert(text), + '

' # noqa + 'This is code.' # noqa + '' # noqa + '

' # noqa + ) + + def testPermalinkWithDoubleInlineCode(self): + """ Test TOC Permalink with double inline code. """ md = markdown.Markdown( extensions=[markdown.extensions.toc.TocExtension(permalink=True)] ) From 803ab63609d6a148b01d6afc12f841501a5fd84c Mon Sep 17 00:00:00 2001 From: Dmitry Shachnev Date: Thu, 17 Mar 2016 18:14:06 +0100 Subject: [PATCH 4/4] toc: Remove children from header element after iterating Removing items while iterating can result in wrong behavior. Refs #461. --- markdown/extensions/toc.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index b3cf898f4..56db33c50 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -180,7 +180,8 @@ def add_anchor(self, c, elem_id): # @ReservedAssignment c.text = "" for elem in c: anchor.append(elem) - c.remove(elem) + while c: + c.remove(c[0]) c.append(anchor) def add_permalink(self, c, elem_id):