Skip to content

Commit

Permalink
Add tests for inline code
Browse files Browse the repository at this point in the history
Fix CDATA pattern
  • Loading branch information
facelessuser committed Jan 25, 2025
1 parent 0cc9aa4 commit 494f321
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 4 deletions.
8 changes: 4 additions & 4 deletions markdown/inlinepatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ def build_inlinepatterns(md: Markdown, **kwargs: Any) -> util.Registry[InlinePro
""" Match an automatic email link (`<[email protected]>`). """

HTML_RE = (
r'(<(\/?[a-zA-Z][^<>@ ]*( [^<>]*)?|' # Tag
r'!--(?:(?!<!--|-->).)*--|' # Comment
r'[?](?:(?!<[?]|[?]>).)*[?]|' # Processing instruction
r'<!\[CDATA\[(?:(?!<!\[CDATA\[|\]\]).)*\]\]|' # `CDATA`
r'(<(\/?[a-zA-Z][^<>@ ]*( [^<>]*)?|' # Tag
r'!--(?:(?!<!--|-->).)*--|' # Comment
r'[?](?:(?!<[?]|[?]>).)*[?]|' # Processing instruction
r'!\[CDATA\[(?:(?!<!\[CDATA\[|\]\]>).)*\]\]' # `CDATA`
')>)'
)
""" Match an HTML tag (`<...>`). """
Expand Down
109 changes: 109 additions & 0 deletions tests/test_syntax/extensions/test_md_in_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,115 @@ def test_md1_nested_footnote_ref(self):
extensions=['md_in_html', 'footnotes']
)

def test_md1_code_void_tag(self):

# https://github.com/Python-Markdown/markdown/issues/1075
self.assertMarkdownRenders(
self.dedent(
"""
<div class="outer" markdown="1">
Code: `<label></input></label>`
</div>
<div class="outer" markdown="1">
HTML: <label></input></label>
</div>
"""
),
'<div class="outer">\n'
'<p>Code: <code>&lt;label&gt;&lt;/input&gt;&lt;/label&gt;</code></p>\n'
'</div>\n'
'<div class="outer">\n'
'<p>HTML: <label></input></label></p>\n'
'</div>',
extensions=['md_in_html']
)

def test_md1_code_comment(self):

self.assertMarkdownRenders(
self.dedent(
"""
<div class="outer" markdown="1">
Code: `<label><!-- **comment** --></label>`
</div>
<div class="outer" markdown="1">
HTML: <label><!-- **comment** --></label>
</div>
"""
),
'<div class="outer">\n'
'<p>Code: <code>&lt;label&gt;&lt;!-- **comment** --&gt;&lt;/label&gt;</code></p>\n'
'</div>\n'
'<div class="outer">\n'
'<p>HTML: <label><!-- **comment** --></label></p>\n'
'</div>',
extensions=['md_in_html']
)

def test_md1_code_pi(self):

self.assertMarkdownRenders(
self.dedent(
"""
<div class="outer" markdown="1">
Code: `<label><?php # echo '**simple**';?></label>`
</div>
<div class="outer" markdown="1">
HTML: <label><?php # echo '**simple**';?></label>
</div>
"""
),
'<div class="outer">\n'
'<p>Code: <code>&lt;label&gt;&lt;?php # echo \'**simple**\';?&gt;&lt;/label&gt;</code></p>\n'
'</div>\n'
'<div class="outer">\n'
'<p>HTML: <label><?php # echo \'**simple**\';?></label></p>\n'
'</div>',
extensions=['md_in_html']
)

def test_md1_code_cdata(self):

self.assertMarkdownRenders(
self.dedent(
"""
<div class="outer" markdown="1">
Code: `<label><![CDATA[some stuff]]></label>`
</div>
<div class="outer" markdown="1">
HTML: <label><![CDATA[some stuff]]></label>
</div>
"""
),
'<div class="outer">\n'
'<p>Code: <code>&lt;label&gt;&lt;![CDATA[some stuff]]&gt;&lt;/label&gt;</code></p>\n'
'</div>\n'
'<div class="outer">\n'
'<p>HTML: <label><![CDATA[some stuff]]></label></p>\n'
'</div>',
extensions=['md_in_html']
)


def load_tests(loader, tests, pattern):
""" Ensure `TestHTMLBlocks` doesn't get run twice by excluding it here. """
Expand Down

0 comments on commit 494f321

Please sign in to comment.