Skip to content

Fails to parse html that contains empty list element #161

@can-oezkan

Description

@can-oezkan

Describe the bug
When trying to parse html that contains an empty list element:

<ul>
  <li>not empty</li>
  <li></li>
</ul>

an System.InvalidOperationException is thrown.

Stacktrace:

Unhandled exception. System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at HtmlToOpenXml.Expressions.ListExpression.<Interpret>d__4.MoveNext()
   at HtmlToOpenXml.Expressions.BlockElementExpression.ComposeChildren(ParsingContext context, IEnumerable`1 childNodes, ParagraphProperties paragraphProperties, Action`1 preAction, Action`1 postAction)
   at HtmlToOpenXml.Expressions.BlockElementExpression.Interpret(ParsingContext context, IEnumerable`1 childNodes)
   at HtmlToOpenXml.Expressions.PhrasingElementExpression.Interpret(ParsingContext context)
   at HtmlToOpenXml.Expressions.BlockElementExpression.Interpret(ParsingContext context)
   at HtmlToOpenXml.Expressions.BodyExpression.Interpret(ParsingContext context)
   at HtmlToOpenXml.HtmlConverter.<Parse>d__8.MoveNext()
   at HtmlToOpenXml.HtmlConverter.Parse(String html)
   at Program.<Main>$(String[] args) in C:\git\MyApp\MyApp\Program.cs:line 10

Expected behavior
An exception should not be thrown when the html contains an empty list element.

Repro

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using HtmlToOpenXml;

using var stream = new MemoryStream();
using var document = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);

var converter = new HtmlConverter(document.AddMainDocumentPart());
    
converter.Parse("""
                      <ul>
                       <li>not empty</li>
                       <li></li>
                      </ul>
                      """);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions