Skip to content

Commit

Permalink
Solution for Python-Markdown#1045 case and add additional test for mo…
Browse files Browse the repository at this point in the history
…re coverage
  • Loading branch information
facelessuser committed Oct 15, 2020
1 parent 0827498 commit 1acef7e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
26 changes: 20 additions & 6 deletions markdown/extensions/md_in_html.py
Expand Up @@ -90,8 +90,10 @@ def at_line_start(self):
"""At line start."""

value = super().at_line_start()
if not value and self.cleandoc and self.cleandoc[-1] == '\n\n':
value = True
if not value and self.cleandoc:
last = self.cleandoc[-1]
if isinstance(last, str) and last.endswith('\n'):
value = True
return value

def handle_starttag(self, tag, attrs):
Expand Down Expand Up @@ -119,7 +121,10 @@ def handle_starttag(self, tag, attrs):
super().handle_starttag(tag, attrs)
else:
text = self.get_starttag_text()
self.handle_data(text)
if self.mdstate and self.mdstate[-1] == "off":
self.handle_data(self.md.htmlStash.store(text))
else:
self.handle_data(text)

def handle_endtag(self, tag):
if tag in block_level_tags:
Expand Down Expand Up @@ -150,14 +155,20 @@ def handle_endtag(self, tag):
else:
# Treat orphan closing tag as a span level tag.
text = self.get_endtag_text(tag)
self.handle_data(text)
if self.mdstate and self.mdstate[-1] == "off":
self.handle_data(self.md.htmlStash.store(text))
else:
self.handle_data(text)
else:
# Span level tag
if self.inraw:
super().handle_endtag(tag)
else:
text = self.get_endtag_text(tag)
self.handle_data(text)
if self.mdstate and self.mdstate[-1] == "off":
self.handle_data(self.md.htmlStash.store(text))
else:
self.handle_data(text)

def handle_data(self, data):
if self.inraw or not self.mdstack:
Expand All @@ -172,7 +183,10 @@ def handle_empty_tag(self, data, is_block):
if self.at_line_start() and is_block:
self.handle_data('\n' + self.md.htmlStash.store(data) + '\n\n')
else:
self.handle_data(data)
if self.mdstate and self.mdstate[-1] == "off":
self.handle_data(self.md.htmlStash.store(data))
else:
self.handle_data(data)


class HtmlBlockPreprocessor(Preprocessor):
Expand Down
47 changes: 47 additions & 0 deletions tests/test_syntax/extensions/test_md_in_html.py
Expand Up @@ -390,6 +390,53 @@ def test_md1_nested_empty_block(self):
)
)

def test_complex_nested_case(self):
self.assertMarkdownRenders(
self.dedent(
"""
<div markdown="1">
**test**
<div>
**test**
<img src=""/>
<code>Test</code>
<span>**test**</span>
<p>Test 2</p>
</div>
</div>
"""
),
self.dedent(
"""
<div>
<p><strong>test</strong></p>
<div>
**test**
<img src=""/>
<code>Test</code>
<span>**test**</span>
<p>Test 2</p>
</div>
</div>
"""
)
)

def test_md1_intail_md1(self):
self.assertMarkdownRenders(
'<div markdown="1">*foo*</div><div markdown="1">*bar*</div>',
self.dedent(
"""
<div>
<p><em>foo</em></p>
</div>
<div>
<p><em>bar</em></p>
</div>
"""
)
)

def test_md1_no_blank_line_before(self):
self.assertMarkdownRenders(
self.dedent(
Expand Down

0 comments on commit 1acef7e

Please sign in to comment.