baynezy/Html2Markdown

Empty lists throw an exception

Closed this issue · 6 comments

An empty UL or OL in the HTML will throw an uncaught exception.

Code:

var html = "<ul></ul>";  // Note: zero LI child elements in the UL
var converter = new Html2Markdown.Converter();
var md = converter.Convert(html);

Exception:

InvalidOperationException: Sequence contains no elements
at System.Linq.Enumerable.Aggregate[TSource](IEnumerable`1 source, Func`3 func)    
at Html2Markdown.Replacement.HtmlParser.ReplaceList(String html)    
at Html2Markdown.Replacement.HtmlParser.ReplaceLists(String html)    
at Html2Markdown.Replacement.CustomReplacer.Replace(String html)    
at Html2Markdown.Converter.<>c.<Convert>b__5_0(String current, IReplacer element)    
at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)    
at Html2Markdown.Converter.Convert(String html)

I was processing thousands of HTML fragments, and ran into this several dozen times. It's very repeatable.

I've just happened to add a test case and fix this in my PR for non-closed li tags #89

This should be resolved in the latest version @baynezy

Thanks @stevetemple. @deanebarker can you please confirm?

Confirmed. My code snippet from above completes without error now (and returns an empty string, as expected).

An empty UL or li in the HTML will throw an uncaught exception.

Code:
string HtmlContent = "<ul><li></li></ul>"; // Note :UL contains LI child elements string mdContent = new Converter().Convert(HtmlContent);

Exception:
System.InvalidOperationException: Sequence contains no elements at System.Linq.Enumerable.Aggregate[TSource](IEnumerable1 source, Func3 func) at Html2Markdown.Replacement.HtmlParser.ReplaceList(String html) at Html2Markdown.Replacement.HtmlParser.ReplaceLists(String html) at Html2Markdown.Replacement.CustomReplacer.Replace(String html) at Html2Markdown.Converter.<>c.<Convert>b__5_0(String current, IReplacer element) at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable1 source, TAccumulate seed, Func3 func) at Html2Markdown.Converter.Convert(String html) at SyncProductsandRolesListToWorkitem.unit.UnitTest1.htmltomd(String filePath) in E:\a\SourceCode\SyncProductsandRolesListToWorkitem\SyncProductsandRolesListToWorkitem.unit\UnitTest1.cs:line 29

@Chaohuiguo - can you please raise this as a new issue and I will take a look at it?