summaryrefslogtreecommitdiff
path: root/tests/test_extensions.py
diff options
context:
space:
mode:
authorJim Porter <826865+jimporter@users.noreply.github.com>2020-02-12 12:28:51 -0800
committerGitHub <noreply@github.com>2020-02-12 15:28:51 -0500
commit74c0166c6903b07b3b2b9cbba36705d1414cbaf7 (patch)
tree50cb58b90d95a5b83f55646a8db2411cf42c1f72 /tests/test_extensions.py
parent1de3083d82a5245015f0f0b9471a965916134d6d (diff)
downloadpython-markdown-74c0166c6903b07b3b2b9cbba36705d1414cbaf7.tar.gz
Fix escaping of HTML special chars (<, >, &) in `.toc_tokens`
Fixes #906.
Diffstat (limited to 'tests/test_extensions.py')
-rw-r--r--tests/test_extensions.py50
1 files changed, 37 insertions, 13 deletions
diff --git a/tests/test_extensions.py b/tests/test_extensions.py
index c787886..8ecbe0d 100644
--- a/tests/test_extensions.py
+++ b/tests/test_extensions.py
@@ -827,6 +827,25 @@ class TestTOC(TestCaseWithAssertStartsWith):
{'level': 1, 'id': 'foo-bar', 'name': 'Foo &amp; bar', 'children': []},
])
+ def testHtmlSpecialChars(self):
+ """ Test Headers with HTML special characters. """
+ text = '# Foo > & bar'
+ self.assertEqual(
+ self.md.convert(text),
+ '<h1 id="foo-bar">Foo &gt; &amp; bar</h1>'
+ )
+ self.assertEqual(
+ self.md.toc,
+ '<div class="toc">\n'
+ '<ul>\n' # noqa
+ '<li><a href="#foo-bar">Foo &gt; &amp; bar</a></li>\n' # noqa
+ '</ul>\n' # noqa
+ '</div>\n'
+ )
+ self.assertEqual(self.md.toc_tokens, [
+ {'level': 1, 'id': 'foo-bar', 'name': 'Foo &gt; &amp; bar', 'children': []},
+ ])
+
def testRawHtml(self):
""" Test Headers with raw HTML. """
text = '# Foo <b>Bar</b> Baz.'
@@ -1000,27 +1019,31 @@ class TestTOC(TestCaseWithAssertStartsWith):
md = markdown.Markdown(extensions=['toc', 'attr_list'])
text = ('# Header 1\n\n'
'## Header 2 { #foo }\n\n'
- '## Header 3 { data-toc-label="Foo Bar"}\n\n'
- '# Header 4 { data-toc-label="Foo <b>Baz</b>" }')
+ '## Header 3 { data-toc-label="Foo Bar" }\n\n'
+ '# Header 4 { data-toc-label="Foo > Baz" }\n\n'
+ '# Header 5 { data-toc-label="Foo <b>Quux</b>" }')
+
self.assertEqual(
md.convert(text),
'<h1 id="header-1">Header 1</h1>\n'
'<h2 id="foo">Header 2</h2>\n'
'<h2 id="header-3">Header 3</h2>\n'
- '<h1 id="header-4">Header 4</h1>'
+ '<h1 id="header-4">Header 4</h1>\n'
+ '<h1 id="header-5">Header 5</h1>'
)
self.assertEqual(
md.toc,
'<div class="toc">\n'
- '<ul>\n' # noqa
- '<li><a href="#header-1">Header 1</a>' # noqa
- '<ul>\n' # noqa
- '<li><a href="#foo">Header 2</a></li>\n' # noqa
- '<li><a href="#header-3">Foo Bar</a></li>\n' # noqa
- '</ul>\n' # noqa
- '</li>\n' # noqa
- '<li><a href="#header-4">Foo Baz</a></li>\n' # noqa
- '</ul>\n' # noqa
+ '<ul>\n' # noqa
+ '<li><a href="#header-1">Header 1</a>' # noqa
+ '<ul>\n' # noqa
+ '<li><a href="#foo">Header 2</a></li>\n' # noqa
+ '<li><a href="#header-3">Foo Bar</a></li>\n' # noqa
+ '</ul>\n' # noqa
+ '</li>\n' # noqa
+ '<li><a href="#header-4">Foo &gt; Baz</a></li>\n' # noqa
+ '<li><a href="#header-5">Foo Quux</a></li>\n' # noqa
+ '</ul>\n' # noqa
'</div>\n'
)
self.assertEqual(md.toc_tokens, [
@@ -1028,7 +1051,8 @@ class TestTOC(TestCaseWithAssertStartsWith):
{'level': 2, 'id': 'foo', 'name': 'Header 2', 'children': []},
{'level': 2, 'id': 'header-3', 'name': 'Foo Bar', 'children': []}
]},
- {'level': 1, 'id': 'header-4', 'name': 'Foo Baz', 'children': []},
+ {'level': 1, 'id': 'header-4', 'name': 'Foo &gt; Baz', 'children': []},
+ {'level': 1, 'id': 'header-5', 'name': 'Foo Quux', 'children': []},
])
def testUniqueFunc(self):