Skip to content

Commit 7089a14

Browse files
committed
Fix source position tracking for HTML blocks. Fixes #87
1 parent ad52c2b commit 7089a14

4 files changed

Lines changed: 70 additions & 4 deletions

File tree

CommonMark.NET.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2011/10/nuspec.xsd">
33
<metadata>
44
<id>CommonMark.NET</id>
5-
<version>0.13.2</version>
5+
<version>0.13.3</version>
66
<title>CommonMark.NET</title>
77
<authors>Kārlis Gaņģis</authors>
88
<licenseUrl>https://raw.githubusercontent.com/Knagis/CommonMark.NET/master/LICENSE.md</licenseUrl>
@@ -11,7 +11,7 @@
1111
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1212
<summary>Fastest .NET library for converting Markdown documents to HTML.</summary>
1313
<description>.NET library for converting Markdown documents to HTML according to the CommonMark specification. Optimized for maximum performance and targets every .NET version since 2.0, including PCL, Mono and .NET Core.</description>
14-
<releaseNotes>Fix unhandled exception.</releaseNotes>
14+
<releaseNotes>Improves source position tracking for HTML blocks.</releaseNotes>
1515
<copyright>Copyright © Kārlis Gaņģis 2014-2016</copyright>
1616
<tags>CommonMark Markdown</tags>
1717
</metadata>

CommonMark.NETCore/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.13.2",
2+
"version": "0.13.3",
33
"frameworks": {
44
"dnxcore50": {
55
"compilationOptions": {

CommonMark.Tests/SourcePositionTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,5 +655,65 @@ public void SourcePositionRemovingReferences()
655655
Assert.IsNotNull(inline);
656656
Assert.AreEqual("*asd*", data.Substring(inline.Inline.SourcePosition, inline.Inline.SourceLength));
657657
}
658+
659+
[TestMethod]
660+
[TestCategory("SourcePosition - HTML Blocks")]
661+
public void SourcePositionHtmlBlock1()
662+
{
663+
var data = "<pre>\n</pre>\nfoo";
664+
var doc = Helpers.ParseDocument(data, Settings);
665+
666+
var block = doc.AsEnumerable()
667+
.FirstOrDefault(o => o.Block != null && o.Block.Tag == Syntax.BlockTag.HtmlBlock);
668+
669+
Assert.IsNotNull(block);
670+
Assert.AreEqual("<pre>\n</pre>\n",
671+
data.Substring(block.Block.SourcePosition, block.Block.SourceLength));
672+
}
673+
674+
[TestMethod]
675+
[TestCategory("SourcePosition - HTML Blocks")]
676+
public void SourcePositionHtmlBlock2()
677+
{
678+
var data = "<!--\n-->\nfoo";
679+
var doc = Helpers.ParseDocument(data, Settings);
680+
681+
var block = doc.AsEnumerable()
682+
.FirstOrDefault(o => o.Block != null && o.Block.Tag == Syntax.BlockTag.HtmlBlock);
683+
684+
Assert.IsNotNull(block);
685+
Assert.AreEqual("<!--\n-->\n",
686+
data.Substring(block.Block.SourcePosition, block.Block.SourceLength));
687+
}
688+
689+
[TestMethod]
690+
[TestCategory("SourcePosition - HTML Blocks")]
691+
public void SourcePositionHtmlBlock6()
692+
{
693+
var data = "<div id='foo'>\r\nbar\r\n</div>\r\n\r\n";
694+
var doc = Helpers.ParseDocument(data, Settings);
695+
696+
var block = doc.AsEnumerable()
697+
.FirstOrDefault(o => o.Block != null && o.Block.Tag == Syntax.BlockTag.HtmlBlock);
698+
699+
Assert.IsNotNull(block);
700+
Assert.AreEqual("<div id='foo'>\r\nbar\r\n</div>\r\n",
701+
data.Substring(block.Block.SourcePosition, block.Block.SourceLength));
702+
}
703+
704+
[TestMethod]
705+
[TestCategory("SourcePosition - HTML Blocks")]
706+
public void SourcePositionHtmlBlock7()
707+
{
708+
var data = "<fooobar>\r\nbar\r\n\r\nbar";
709+
var doc = Helpers.ParseDocument(data, Settings);
710+
711+
var block = doc.AsEnumerable()
712+
.FirstOrDefault(o => o.Block != null && o.Block.Tag == Syntax.BlockTag.HtmlBlock);
713+
714+
Assert.IsNotNull(block);
715+
Assert.AreEqual("<fooobar>\r\nbar\r\n",
716+
data.Substring(block.Block.SourcePosition, block.Block.SourceLength));
717+
}
658718
}
659719
}

CommonMark/Parser/BlockMethods.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,14 @@ public static void Finalize(Block b, LineInfo line)
111111

112112
if (line.IsTrackingPositions)
113113
{
114+
// HTML Blocks other than type 7 call Finalize when the last line is encountered.
115+
// Block types 6 and 7 calls Finalize once it finds the next empty row but that empty row is no longer considered to be part of the block.
116+
var includesThisLine = b.HtmlBlockType != HtmlBlockType.None && b.HtmlBlockType != HtmlBlockType.InterruptingBlock && b.HtmlBlockType != HtmlBlockType.NonInterruptingBlock;
117+
114118
// (b.SourcePosition >= line.LineOffset) determines if the block started on this line.
115-
if (b.SourcePosition >= line.LineOffset && line.Line != null)
119+
includesThisLine = includesThisLine || b.SourcePosition >= line.LineOffset && line.Line != null;
120+
121+
if (includesThisLine)
116122
b.SourceLastPosition = line.CalculateOrigin(line.Line.Length, false);
117123
else
118124
b.SourceLastPosition = line.CalculateOrigin(0, false);

0 commit comments

Comments
 (0)