Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix source span calculation #733

Merged
merged 2 commits into from
Aug 26, 2023
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
32 changes: 29 additions & 3 deletions src/Markdig.Tests/MiscTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Text.RegularExpressions;

using Markdig.Extensions.AutoLinks;

using Markdig.Extensions.Tables;
using Markdig.Syntax;
using NUnit.Framework;

namespace Markdig.Tests;
Expand Down Expand Up @@ -198,9 +199,9 @@ public void VisualizeMathExpressions()
<div class=""math"">
\begin{align}
\sqrt{37} & = \sqrt{\frac{73^2-1}{12^2}} \\
& = \sqrt{\frac{73^2}{12^2}\cdot\frac{73^2-1}{73^2}} \\
& = \sqrt{\frac{73^2}{12^2}\cdot\frac{73^2-1}{73^2}} \\
& = \sqrt{\frac{73^2}{12^2}}\sqrt{\frac{73^2-1}{73^2}} \\
& = \frac{73}{12}\sqrt{1 - \frac{1}{73^2}} \\
& = \frac{73}{12}\sqrt{1 - \frac{1}{73^2}} \\
& \approx \frac{73}{12}\left(1 - \frac{1}{2\cdot73^2}\right)
\end{align}
</div>
Expand Down Expand Up @@ -291,4 +292,29 @@ public void CanUseHttpsPrefixForWWWAutoLinks()
TestParser.TestSpec("www.foo.bar", "<p><a href=\"http://www.foo.bar\">www.foo.bar</a></p>", pipeline);
TestParser.TestSpec("www.foo.bar", "<p><a href=\"https://www.foo.bar\">www.foo.bar</a></p>", httpsPipeline);
}

[Test]
public void RootInlineHasCorrectSourceSpan()
{
var pipeline = new MarkdownPipelineBuilder().UsePreciseSourceLocation().Build();
pipeline.TrackTrivia = true;

var document = Markdown.Parse("0123456789\n", pipeline);

var expectedSourceSpan = new SourceSpan(0, 10);
Assert.That(((LeafBlock)document.LastChild).Inline.Span == expectedSourceSpan);
}

[Test]
public void RootInlineInTableCellHasCorrectSourceSpan()
{
var pipeline = new MarkdownPipelineBuilder().UsePreciseSourceLocation().UseAdvancedExtensions().Build();
pipeline.TrackTrivia = true;

var document = Markdown.Parse("| a | b |\n| --- | --- |\n| <span id=\"dest\"></span><span id=\"DEST\"></span>*dest*<br/> | \\[in\\] The address of the result of the operation.<br/> |", pipeline);

var paragraph = (ParagraphBlock)((TableCell)((TableRow)((Table)document.LastChild).LastChild).First()).LastChild;
Assert.That(paragraph.Inline.Span.Start == paragraph.Inline.FirstChild.Span.Start);
Assert.That(paragraph.Inline.Span.End == paragraph.Inline.LastChild.Span.End);
}
}
25 changes: 24 additions & 1 deletion src/Markdig.Tests/TestSourcePosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ public void TestParagraph2()
");
}

[Test]
public void TestParagraphWithEndNewLine()
{
Check("0123456789\n", @"
paragraph ( 0, 0) 0-10
literal ( 0, 0) 0-9
linebreak ( 0,10) 10-10
", trackTrivia: true);

Check("0123456789\r", @"
paragraph ( 0, 0) 0-10
literal ( 0, 0) 0-9
linebreak ( 0,10) 10-10
", trackTrivia: true);

Check("0123456789\r\n", @"
paragraph ( 0, 0) 0-11
literal ( 0, 0) 0-9
linebreak ( 0,10) 10-11
", trackTrivia: true);
}

[Test]
public void TestEmphasis()
{
Expand Down Expand Up @@ -825,9 +847,10 @@ public void TestDocument()
");
}

private static void Check(string text, string expectedResult, string extensions = null)
private static void Check(string text, string expectedResult, string extensions = null, bool trackTrivia = false)
{
var pipelineBuilder = new MarkdownPipelineBuilder().UsePreciseSourceLocation();
pipelineBuilder.TrackTrivia = trackTrivia;
if (extensions != null)
{
pipelineBuilder.Configure(extensions);
Expand Down
7 changes: 6 additions & 1 deletion src/Markdig/Extensions/Tables/PipeTableParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ public bool PostProcess(InlineProcessor state, Inline? root, Inline? lastChild,
{
var paragraph = (ParagraphBlock) cell[0];
state.PostProcessInlines(postInlineProcessorIndex + 1, paragraph.Inline, null, true);
if (paragraph.Inline?.LastChild is not null)
{
paragraph.Inline.Span.End = paragraph.Inline.LastChild.Span.End;
paragraph.UpdateSpanEnd(paragraph.Inline.LastChild.Span.End);
}
}

// Clear cells when we are done
Expand Down Expand Up @@ -520,7 +525,7 @@ private static bool ParseHeaderString(Inline? inline, out TableColumnAlign? alig
// Create aligns until we may have a header row

aligns ??= new List<TableColumnDefinition>();

aligns.Add(new TableColumnDefinition() { Alignment = align });

// If this is the last delimiter, we need to check the right side of the `|` delimiter
Expand Down
10 changes: 9 additions & 1 deletion src/Markdig/Parsers/InlineProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public void ProcessInlineLeaf(LeafBlock leafBlock)
previousLineIndexForSliceOffset = 0;
lineOffsets.Clear();
var text = leafBlock.Lines.ToSlice(lineOffsets);
var textEnd = text.Length;
leafBlock.Lines.Release();
int previousStart = -1;

Expand Down Expand Up @@ -319,7 +320,8 @@ public void ProcessInlineLeaf(LeafBlock leafBlock)
var newLine = leafBlock.NewLine;
if (newLine != NewLine.None)
{
leafBlock.Inline.AppendChild(new LineBreakInline { NewLine = newLine });
var position = GetSourcePosition(textEnd, out int line, out int column);
leafBlock.Inline.AppendChild(new LineBreakInline { NewLine = newLine, Line = line, Column = column, Span = { Start = position, End = position + (newLine == NewLine.CarriageReturnLineFeed ? 1 : 0) } });
}
}
}
Expand All @@ -342,6 +344,12 @@ public void ProcessInlineLeaf(LeafBlock leafBlock)
// DebugLog.WriteLine("** Dump after Emphasis:");
// leafBlock.Inline.DumpTo(DebugLog);
//}

if (leafBlock.Inline.LastChild is not null)
{
leafBlock.Inline.Span.End = leafBlock.Inline.LastChild.Span.End;
leafBlock.UpdateSpanEnd(leafBlock.Inline.Span.End);
}
}

public void PostProcessInlines(int startingIndex, Inline? root, Inline? lastChild, bool isFinalProcessing)
Expand Down