Skip to content

Commit

Permalink
Merge pull request #666 from meziantou/issues/665
Browse files Browse the repository at this point in the history
Fix ANE when parsing empty documents with trackTrivia enabled
  • Loading branch information
xoofx committed Sep 27, 2022
2 parents bce4b70 + 8e4a732 commit 98c687b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Markdig.Tests/TestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Text.RegularExpressions;
using Markdig.Extensions.JiraLinks;
using Markdig.Renderers.Roundtrip;
using Markdig.Syntax;
using NUnit.Framework;

Expand Down Expand Up @@ -67,6 +68,15 @@ public void EnsureSpecsAreUpToDate()
TestDescendantsOrder.TestSchemas(specsSyntaxTrees);
}

[Test]
public void ParseEmptyDocumentWithTrackTriviaEnabled()
{
var document = Markdown.Parse("", trackTrivia: true);
using var sw = new StringWriter();
new RoundtripRenderer(sw).Render(document);
Assert.AreEqual("", sw.ToString());
}

public static void TestSpec(string inputText, string expectedOutputText, string extensions = null, bool plainText = false, string context = null)
{
context ??= string.Empty;
Expand Down
6 changes: 5 additions & 1 deletion src/Markdig/Parsers/MarkdownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public static MarkdownDocument Parse(string text, MarkdownPipeline? pipeline = n
var noBlocksFoundBlock = new EmptyBlock(null);
List<StringSlice> linesBefore = blockProcessor.UseLinesBefore();
noBlocksFoundBlock.LinesAfter = new List<StringSlice>();
noBlocksFoundBlock.LinesAfter.AddRange(linesBefore);
if (linesBefore != null)
{
noBlocksFoundBlock.LinesAfter.AddRange(linesBefore);
}

document.Add(noBlocksFoundBlock);
}
else if (lastBlock != null && blockProcessor.LinesBefore != null)
Expand Down

0 comments on commit 98c687b

Please sign in to comment.