From 34cc37541326e193e50955528bc39620946b0423 Mon Sep 17 00:00:00 2001 From: Evgenii Zakharov Date: Tue, 17 Sep 2019 21:25:07 +0300 Subject: [PATCH] add check on null for Openednodes dictionary, add unit test --- src/HtmlAgilityPack.Shared/HtmlNode.cs | 31 ++++++++++--------- .../HtmlDocumentTests.cs | 21 +++++++++++++ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlNode.cs b/src/HtmlAgilityPack.Shared/HtmlNode.cs index 92c41c9..5f81847 100644 --- a/src/HtmlAgilityPack.Shared/HtmlNode.cs +++ b/src/HtmlAgilityPack.Shared/HtmlNode.cs @@ -2006,20 +2006,23 @@ internal void UpdateLastNode() HtmlNode newLast = null; if (_prevwithsamename == null || !_prevwithsamename._starttag) { - foreach (var openNode in _ownerdocument.Openednodes) - { - if ((openNode.Key < _outerstartindex || openNode.Key > (_outerstartindex + _outerlength)) && openNode.Value._name == _name) - { - if (newLast == null && openNode.Value._starttag) - { - newLast = openNode.Value; - } - else if (newLast !=null && newLast.InnerStartIndex < openNode.Key && openNode.Value._starttag) - { - newLast = openNode.Value; - } - } - } + if (_ownerdocument.Openednodes != null) + { + foreach (var openNode in _ownerdocument.Openednodes) + { + if ((openNode.Key < _outerstartindex || openNode.Key > (_outerstartindex + _outerlength)) && openNode.Value._name == _name) + { + if (newLast == null && openNode.Value._starttag) + { + newLast = openNode.Value; + } + else if (newLast != null && newLast.InnerStartIndex < openNode.Key && openNode.Value._starttag) + { + newLast = openNode.Value; + } + } + } + } } else { diff --git a/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs b/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs index 8a61559..81c4ac6 100644 --- a/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs +++ b/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs @@ -945,6 +945,27 @@ public void GetClasses_WhereClassWithWhitespacePassed_ShouldNotBeEmpty() Assert.IsNotEmpty(aTag.GetClasses()); } + [Test] + public void LoadHtml_WhenHtmlHasUnclosedTags_AndOptionCheckSyntaxFalse_ShouldNotThrowException() + { + var html = "
Some simple

html

"; + var htmlDoc = new HtmlDocument { OptionCheckSyntax = false }; + + htmlDoc.LoadHtml(html); + + using (var memoryStream = new MemoryStream()) + { + htmlDoc.Save(memoryStream); + memoryStream.Seek(0, SeekOrigin.Begin); + using (var streamReader = new StreamReader(memoryStream)) + { + string parsedHtml = streamReader.ReadToEnd(); + Assert.IsNotEmpty(parsedHtml); + } + } + Assert.Zero(htmlDoc.ParseErrors.Count()); + } + [HasXPath] public class StackOverflowPage {