Knagis/CommonMark.NET

AST may be reporting the wrong length of HTML block

Closed this issue · 8 comments

I don't know enough about the nuances of Markdown and the AST to know if this a bug so I'll just report it.

Given at the beginning of a document:

<pre>
</pre

The AST will report the HTML block begins at offset 0 with a length of 12

Close the tag however:

<pre>
</pre>

And the AST reports the block begins at offset 0 with a length of 7.

Reported in https://github.com/mike-ward/Markdown-Edit/issues/165

This is because the incomplete </pre does not close the HTML block so it will continue on the following lines (including any newlines). </pre> however does close it.

try.commonmark.org

<pre>, <style> and <script> tags are special this way. See HTML Block Type 1 from commonmark specification

I'm not sure I follow. The block gets shorter when the tag is closed. Shouldn't it be longer?

From the spec:

It ends with the first subsequent line that meets a matching end condition, or the last line of the document, if no line is encountered that meets the end condition.

OK, I see now - the problem is that the length of 7 is not inclusive of the last line.

Yes, that is a bug, I will see to fix is asap.

I should have made that clear in the original report. Too close to the problem to describe correctly.

0.13.3 has been pushed to nuget. Hopefully it fixes the issue.

Tested it out. It does fix the problem but introduces another issue. If I start with

<pre>
</pre>

and backspace over the last closing > an exception is thrown.

CommonMark.CommonMarkException was unhandled by user code
  HResult=-2146233088
  Message=An error occurred while finalizing open containers.
  Source=CommonMark
  StackTrace:
       at CommonMark.CommonMarkConverter.ProcessStage1(TextReader source, CommonMarkSettings settings) in E:\OneDrive\Programming\CommonMark\CommonMark\CommonMarkConverter.cs:line 145
       at MarkdownEdit.Models.Markdown.GenerateAbstractSyntaxTree(String text) in C:\Users\mike\GitHub\Markdown-Edit\src\MarkdownEdit\Models\Markdown.cs:line 198
       at MarkdownEdit.Models.Markdown.ToHtml(String markdown) in C:\Users\mike\GitHub\Markdown-Edit\src\MarkdownEdit\Models\Markdown.cs:line 58
       at MarkdownEdit.Controls.Preview.<>c__DisplayClass32_1.<Update>b__0() in C:\Users\mike\GitHub\Markdown-Edit\src\MarkdownEdit\Controls\Preview.xaml.cs:line 102
       at System.Threading.Tasks.Task.Execute()
  InnerException: 
       HResult=-2147467261
       Message=Object reference not set to an instance of an object.
       Source=CommonMark
       StackTrace:
            at CommonMark.Parser.BlockMethods.Finalize(Block b, LineInfo line) in E:\OneDrive\Programming\CommonMark\CommonMark\Parser\BlockMethods.cs:line 122
            at CommonMark.CommonMarkConverter.ProcessStage1(TextReader source, CommonMarkSettings settings) in E:\OneDrive\Programming\CommonMark\CommonMark\CommonMarkConverter.cs:line 135
       InnerException: 

This should be fixed now in 0.13.4

Yep, that fixes it. Thanks!