Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Markdig.Tests/TestCodeInline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public class TestCodeInline
[Test]
public void UnpairedCodeInlineWithTrailingChars()
{
TestParser.TestSpec("*`\n\f", "<p>*`</p>");
TestParser.TestSpec("*`\n\f", "<p>*`\n\f</p>");
}
}
2 changes: 2 additions & 0 deletions src/Markdig.Tests/TestFencedCodeBlocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class TestFencedCodeBlocks
[TestCase(" c# \t fOo \t", "c#", "fOo")]
[TestCase("in\\%fo arg\\%ument", "in%fo", "arg%ument")]
[TestCase("info&#9; arg&acute;ument", "info\t", "arg\u00B4ument")]
[TestCase("\u00A0c#\u00A0", "\u00A0c#\u00A0", "")]
[TestCase(" c# \u00A0foo\u00A0 ", "c#", "\u00A0foo\u00A0")]
public void TestInfoAndArguments(string infoString, string expectedInfo, string expectedArguments)
{
Test('`');
Expand Down
22 changes: 22 additions & 0 deletions src/Markdig.Tests/TestUnicodeWhitespaceTrimming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Markdig.Tests;

public class TestUnicodeWhitespaceTrimming
{
// Paragraph and heading contents are trimmed of spaces and tabs only,
// other Unicode whitespace such as U+00A0 or U+3000 is part of the text.
// Compared exactly, as TestParser.TestSpec normalizes whitespace around list items and applies NFKD.
[Test]
[TestCase("a ", "<p>a </p>\n")]
[TestCase(" a", "<p> a</p>\n")]
[TestCase("a ", "<p>a </p>\n")]
[TestCase("a \n b", "<p>a \n b</p>\n")]
[TestCase("a \t", "<p>a</p>\n")]
[TestCase("# h ", "<h1>h </h1>\n")]
[TestCase("h \n===", "<h1>h </h1>\n")]
[TestCase("- a ", "<ul>\n<li>a </li>\n</ul>\n")]
[TestCase("> a ", "<blockquote>\n<p>a </p>\n</blockquote>\n")]
public void KeepsUnicodeWhitespaceThatIsNotASpaceOrTab(string markdown, string expected)
{
Assert.That(Markdown.ToHtml(markdown), Is.EqualTo(expected));
}
}
43 changes: 43 additions & 0 deletions src/Markdig/Helpers/StringSlice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,49 @@ public bool TrimEnd()
return start > i;
}

/// <summary>
/// Trims spaces and tabs at the beginning of this slice, keeping other Unicode whitespace such as U+00A0.
/// </summary>
/// <returns><c>true</c> if it has reaches the end of the slice</returns>
internal bool TrimStartSpaceOrTab()
{
string text = Text;
int end = End;
int i = Start;

while (i <= end && (uint)i < (uint)text.Length && text[i].IsSpaceOrTab())
i++;

Start = i;
return i > end;
}

/// <summary>
/// Trims spaces and tabs at the end of this slice, keeping other Unicode whitespace such as U+00A0.
/// </summary>
/// <returns><c>true</c> if the slice is empty afterwards</returns>
internal bool TrimEndSpaceOrTab()
{
string text = Text;
int start = Start;
int i = End;

while (start <= i && (uint)i < (uint)text.Length && text[i].IsSpaceOrTab())
i--;

End = i;
return start > i;
}

/// <summary>
/// Trims spaces and tabs from both the start and end of this slice.
/// </summary>
internal void TrimSpaceOrTab()
{
TrimStartSpaceOrTab();
TrimEndSpaceOrTab();
}

/// <summary>
/// Trims whitespaces from both the start and end of this slice.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Markdig/Parsers/FencedBlockParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static bool DefaultInfoParser(BlockProcessor state, ref StringSlice line,
infoStringSlice = new StringSlice(line.Text, line.Start, firstSpace - 1);

var argStringSlice = new StringSlice(line.Text, firstSpace, line.End);
argStringSlice.Trim();
argStringSlice.TrimSpaceOrTab();
fenced.Arguments = s_argumentsStringCache.Get(argStringSlice.AsSpan());
}
else
Expand All @@ -245,7 +245,7 @@ public static bool DefaultInfoParser(BlockProcessor state, ref StringSlice line,
fenced.Arguments = string.Empty;
}

infoStringSlice.Trim();
infoStringSlice.TrimSpaceOrTab();

fenced.Info = s_infoStringCache.Get(infoStringSlice.AsSpan());

Expand Down Expand Up @@ -277,7 +277,7 @@ public override BlockState TryOpen(BlockProcessor processor)
// specs spaces: Is space and tabs? or only spaces? Use space and tab for this case
if (!processor.TrackTrivia)
{
line.TrimStart();
line.TrimStartSpaceOrTab();
}

var fenced = CreateFencedBlock(processor);
Expand Down
5 changes: 4 additions & 1 deletion src/Markdig/Parsers/HeadingBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ public override bool Close(BlockProcessor processor, Block block)
if (!processor.TrackTrivia)
{
var heading = (HeadingBlock)block;
heading.Lines.Trim();
for (int i = 0; i < heading.Lines.Count; i++)
{
heading.Lines.Lines[i].Slice.TrimSpaceOrTab();
}
}
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Markdig/Parsers/ParagraphBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public override bool Close(BlockProcessor processor, Block block)
{
for (int i = 0; i < lineCount; i++)
{
lines.Lines[i].Slice.TrimStart();
lines.Lines[i].Slice.TrimStartSpaceOrTab();
}
lines.Lines[lineCount - 1].Slice.TrimEnd();
lines.Lines[lineCount - 1].Slice.TrimEndSpaceOrTab();
}
}

Expand Down Expand Up @@ -167,9 +167,9 @@ private BlockState TryParseSetexHeading(BlockProcessor state, Block block)
var lines = heading.Lines.Lines;
for (int i = 0; i < heading.Lines.Count; i++)
{
lines[i].Slice.TrimStart();
lines[i].Slice.TrimStartSpaceOrTab();
}
lines[heading.Lines.Count - 1].Slice.TrimEnd();
lines[heading.Lines.Count - 1].Slice.TrimEndSpaceOrTab();
}

// Remove the paragraph as a pending block
Expand Down
Loading