Fix CommonMark HTML block start character classes

This commit is contained in:
Alexandre Mutel
2026-10-15 21:48:26 +02:00
parent e068f060a4
commit d5462ae54f
3 changed files with 63 additions and 4 deletions

View File

@@ -242,6 +242,11 @@ 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.
Declaration blocks start with `<!` followed by an uppercase or lowercase ASCII
letter. Whitespace separators after known block tag names, and trailing whitespace
after complete standalone tags, must be spaces or tabs. Nonbreaking spaces and
other Unicode whitespace do not satisfy these start conditions.
## Inline HTML
HTML tags can appear within inline content:

View File

@@ -33,6 +33,60 @@ public class TestHtmlCodeBlocks
Assert.AreEqual(10 + tag.Length, htmlBlocks[0].Span.Length);
}
[Test]
public void HtmlDeclarationsStartWithAsciiLetters([Values('A', 'Z', 'a', 'z')] char firstLetter)
{
var markdown = $"<!{firstLetter}\n\n*foo*\n>\n*bar*";
var expected = $"<!{firstLetter}\n\n*foo*\n>\n<p><em>bar</em></p>\n";
TestParser.TestSpec(markdown, expected);
TestRoundtrip.RoundTrip(markdown);
}
[Test]
public void HtmlDeclarationsRequireAsciiLetters([Values('0', '@', '[', '`', '{', 'é', 'Ω')] char firstCharacter)
{
TestParser.TestSpec($"<!{firstCharacter}\n*foo*", $"<p>&lt;!{firstCharacter}\n<em>foo</em></p>\n");
}
[Test]
public void KnownHtmlTagNamesRejectOtherWhitespace(
[Values("<div", "</div", "<script")] string opening,
[Values('\f', '\v', '\u0085', '\u00a0', '\u2003', '\u202f', '\u2028', '\u2029')] char whitespace)
{
var markdown = $"{opening}{whitespace}\n*foo*";
var expected = $"<p>&lt;{opening[1..]}{whitespace}\n<em>foo</em></p>\n";
TestParser.TestSpec(markdown, expected);
}
[Test]
public void CompleteHtmlTagsRejectOtherTrailingWhitespace(
[Values("<del>", "</del>", "<custom/>")] string tag,
[Values('\f', '\v', '\u0085', '\u00a0', '\u2003', '\u202f', '\u2028', '\u2029')] char whitespace)
{
TestParser.TestSpec($"{tag}{whitespace}\n*foo*", $"<p>{tag}{whitespace}\n<em>foo</em></p>\n");
}
[TestCase("<div")]
[TestCase("</DIV")]
[TestCase("<div attribute")]
[TestCase("<div\tattribute")]
[TestCase("<div/>")]
[TestCase("</DiV>")]
[TestCase("<script")]
[TestCase("<del>")]
[TestCase("<del> \t")]
[TestCase("</del>\t")]
[TestCase("<custom/> \t")]
[TestCase("<del\tattr='x'>")]
[TestCase("<del attr='\u00a0\f'>")]
[TestCase("<div>\u00a0")]
public void ValidHtmlBlockStartConditionsArePreserved(string opening)
{
TestParser.TestSpec(opening + "\n*foo*", opening + "\n*foo*\n");
}
// 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")]

View File

@@ -72,7 +72,7 @@ public class HtmlBlockParser : BlockParser
var result = BlockState.None;
if ((c == '/' && HtmlHelper.TryParseHtmlCloseTag(ref line, ref builder)) || HtmlHelper.TryParseHtmlTagOpenTag(ref line, ref builder))
{
// Must be followed by whitespace only
// Must be followed by spaces or tabs only
bool hasOnlySpaces = true;
c = line.CurrentChar;
while (true)
@@ -81,7 +81,7 @@ public class HtmlBlockParser : BlockParser
{
break;
}
if (!c.IsWhitespace())
if (!c.IsSpaceOrTab())
{
hasOnlySpaces = false;
break;
@@ -110,7 +110,7 @@ public class HtmlBlockParser : BlockParser
{
return CreateHtmlBlock(state, HtmlBlockType.Comment, startColumn, startPosition); // group 2
}
if (c.IsAlphaUpper())
if (c.IsAlpha())
{
return CreateHtmlBlock(state, HtmlBlockType.DocumentType, startColumn, startPosition); // group 4
}
@@ -145,7 +145,7 @@ public class HtmlBlockParser : BlockParser
c = line.NextChar();
}
if (!(c == '>' || (!hasLeadingClose && c == '/' && line.PeekChar() == '>') || c.IsWhiteSpaceOrZero()))
if (!(c == '>' || (!hasLeadingClose && c == '/' && line.PeekChar() == '>') || c.IsSpaceOrTab() || c == '\0'))
{
return BlockState.None;
}