mirror of
https://github.com/xoofx/markdig.git
synced 2026-11-16 09:20:40 +00:00
Fix HTML block whitespace terminators and roundtripping (#960)
* Fix HTML blocks not ending at a line with only spaces or tabs A blank line ends HTML blocks of type 6 and 7, and CommonMark counts a line with only spaces or tabs as blank. MatchEnd checked IsBlankLine after going back over the indent, which made such a line non-empty, so the HTML block went on and the Markdown after it was output as raw text. Check the line before going back. * Fix HTML block terminator trivia and add roundtrip coverage --------- Co-authored-by: Alexandre Mutel <alexandre_mutel@live.com>
This commit is contained in:
committed by
GitHub
parent
55edc078e6
commit
cdf8aedd83
@@ -232,6 +232,11 @@ Raw HTML can be included directly:
|
||||
</div>
|
||||
```
|
||||
|
||||
HTML blocks that end at a blank line, such as `<div>` blocks and complete tags
|
||||
on their own line, also end at a line containing only spaces or tabs. Markdown
|
||||
after that line is parsed normally. Blocks such as `<script>` and HTML comments
|
||||
instead continue until their matching end condition.
|
||||
|
||||
## Inline HTML
|
||||
|
||||
HTML tags can appear within inline content:
|
||||
|
||||
@@ -32,4 +32,41 @@ public class TestHtmlCodeBlocks
|
||||
Assert.AreEqual(7, htmlBlocks[0].Span.Start);
|
||||
Assert.AreEqual(10 + tag.Length, htmlBlocks[0].Span.Length);
|
||||
}
|
||||
}
|
||||
|
||||
// A line containing only spaces or tabs is a blank line, which ends HTML blocks of type 6 and 7.
|
||||
// https://spec.commonmark.org/0.31.2/#blank-line
|
||||
[TestCase("<div>\n \nfoo", "<div>\n<p>foo</p>\n")]
|
||||
[TestCase("<div>\n\t\nfoo", "<div>\n<p>foo</p>\n")]
|
||||
[TestCase("<del>\n \n*foo*", "<del>\n<p><em>foo</em></p>\n")]
|
||||
public void WhitespaceOnlyLineEndsHtmlBlock(string markdown, string expected)
|
||||
{
|
||||
TestParser.TestSpec(markdown, expected);
|
||||
}
|
||||
|
||||
[TestCase("<div>\n \nfoo")]
|
||||
[TestCase("<div>\n\t\nfoo")]
|
||||
[TestCase("<del>\n \t \n*foo*")]
|
||||
[TestCase("<div>\r\n \r\nfoo")]
|
||||
[TestCase("<del>\r\n\t\r\n*foo*")]
|
||||
[TestCase("<div>\r \t\rfoo")]
|
||||
[TestCase("> <div>\n> \n> foo")]
|
||||
[TestCase("> <div>\n> \n> foo")]
|
||||
[TestCase("> > <div>\n> > \n> > foo")]
|
||||
[TestCase("- > <div>\n > \n > foo")]
|
||||
[TestCase("- <div>\n \t\n foo")]
|
||||
[TestCase("<div>\n \n")]
|
||||
public void WhitespaceOnlyHtmlBlockTerminatorRoundtrips(string markdown)
|
||||
{
|
||||
TestRoundtrip.RoundTrip(markdown);
|
||||
}
|
||||
|
||||
[TestCase("<script>\n \t\n*foo*\n</script>")]
|
||||
[TestCase("<!--\n \t\n*foo*\n-->")]
|
||||
[TestCase("<div>\n\u00a0\n*foo*")]
|
||||
[TestCase("<del>\n\u00a0\n*foo*")]
|
||||
public void NonTerminatingHtmlBlockLinesArePreserved(string markdown)
|
||||
{
|
||||
TestParser.TestSpec(markdown, markdown + "\n");
|
||||
TestRoundtrip.RoundTrip(markdown);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,6 +181,12 @@ public class HtmlBlockParser : BlockParser
|
||||
|
||||
private BlockState MatchEnd(BlockProcessor state, HtmlBlock htmlBlock)
|
||||
{
|
||||
// Keep blank-line indentation consumed so BlockProcessor can preserve it as trivia.
|
||||
if (state.IsBlankLine && (htmlBlock.Type is HtmlBlockType.InterruptingBlock or HtmlBlockType.NonInterruptingBlock))
|
||||
{
|
||||
return BlockState.BreakDiscard;
|
||||
}
|
||||
|
||||
state.GoToColumn(state.ColumnBeforeIndent);
|
||||
|
||||
// Early exit if it is not starting by an HTML tag
|
||||
@@ -256,18 +262,6 @@ public class HtmlBlockParser : BlockParser
|
||||
}
|
||||
}
|
||||
break;
|
||||
case HtmlBlockType.InterruptingBlock:
|
||||
if (state.IsBlankLine)
|
||||
{
|
||||
result = BlockState.BreakDiscard;
|
||||
}
|
||||
break;
|
||||
case HtmlBlockType.NonInterruptingBlock:
|
||||
if (state.IsBlankLine)
|
||||
{
|
||||
result = BlockState.BreakDiscard;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Update only if we don't have a break discard
|
||||
|
||||
Reference in New Issue
Block a user