Skip to content

Commit 85a9160

Browse files
committed
Use set instead of list for block level elements
Using a set allows for better performances when checking for membership of a tag within block level elements. Issue-1507: #1507
1 parent 6347c57 commit 85a9160

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

markdown/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def __init__(self, **kwargs):
113113
]
114114
""" List of characters which get the backslash escape treatment. """
115115

116-
self.block_level_elements: list[str] = BLOCK_LEVEL_ELEMENTS.copy()
116+
self.block_level_elements: set[str] = BLOCK_LEVEL_ELEMENTS.copy()
117117

118118
self.registeredExtensions: list[Extension] = []
119119
self.docType = "" # TODO: Maybe delete this. It does not appear to be used anymore.

markdown/extensions/md_in_html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class HTMLExtractorExtra(HTMLExtractor):
4242

4343
def __init__(self, md: Markdown, *args, **kwargs):
4444
# All block-level tags.
45-
self.block_level_tags = set(md.block_level_elements.copy())
45+
self.block_level_tags = md.block_level_elements.copy()
4646
# Block-level tags in which the content only gets span level parsing
4747
self.span_tags = set(
4848
['address', 'dd', 'dt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'legend', 'li', 'p', 'summary', 'td', 'th']

markdown/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"""
4545

4646

47-
BLOCK_LEVEL_ELEMENTS: list[str] = [
47+
BLOCK_LEVEL_ELEMENTS: set[str] = {
4848
# Elements which are invalid to wrap in a `<p>` tag.
4949
# See https://w3c.github.io/html/grouping-content.html#the-p-element
5050
'address', 'article', 'aside', 'blockquote', 'details', 'div', 'dl',
@@ -56,9 +56,9 @@
5656
'math', 'map', 'noscript', 'output', 'object', 'option', 'progress', 'script',
5757
'style', 'summary', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'tr', 'video',
5858
'center'
59-
]
59+
}
6060
"""
61-
List of HTML tags which get treated as block-level elements. Same as the `block_level_elements`
61+
Set of HTML tags which get treated as block-level elements. Same as the `block_level_elements`
6262
attribute of the [`Markdown`][markdown.Markdown] class. Generally one should use the
6363
attribute on the class. This remains for compatibility with older extensions.
6464
"""

tests/test_apis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ class TestBlockAppend(unittest.TestCase):
920920
def testBlockAppend(self):
921921
""" Test that appended escapes are only in the current instance. """
922922
md = markdown.Markdown()
923-
md.block_level_elements.append('test')
923+
md.block_level_elements.add('test')
924924
self.assertEqual('test' in md.block_level_elements, True)
925925
md2 = markdown.Markdown()
926926
self.assertEqual('test' not in md2.block_level_elements, True)

0 commit comments

Comments
 (0)