From 1acef7e1892cf97b6030170c40a62be3da046a1b Mon Sep 17 00:00:00 2001 From: facelessuser Date: Thu, 15 Oct 2020 13:42:33 -0600 Subject: [PATCH] Solution for #1045 case and add additional test for more coverage --- markdown/extensions/md_in_html.py | 26 +++++++--- .../test_syntax/extensions/test_md_in_html.py | 47 +++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/markdown/extensions/md_in_html.py b/markdown/extensions/md_in_html.py index 2740e14de..4a79f08ae 100644 --- a/markdown/extensions/md_in_html.py +++ b/markdown/extensions/md_in_html.py @@ -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): @@ -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: @@ -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: @@ -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): diff --git a/tests/test_syntax/extensions/test_md_in_html.py b/tests/test_syntax/extensions/test_md_in_html.py index 38020f1b4..d262bf0c3 100644 --- a/tests/test_syntax/extensions/test_md_in_html.py +++ b/tests/test_syntax/extensions/test_md_in_html.py @@ -390,6 +390,53 @@ def test_md1_nested_empty_block(self): ) ) + def test_complex_nested_case(self): + self.assertMarkdownRenders( + self.dedent( + """ +
+ **test** +
+ **test** + + Test + **test** +

Test 2

+
+
+ """ + ), + self.dedent( + """ +
+

test

+
+ **test** + + Test + **test** +

Test 2

+
+
+ """ + ) + ) + + def test_md1_intail_md1(self): + self.assertMarkdownRenders( + '
*foo*
*bar*
', + self.dedent( + """ +
+

foo

+
+
+

bar

+
+ """ + ) + ) + def test_md1_no_blank_line_before(self): self.assertMarkdownRenders( self.dedent(