diff --git a/src/HtmlAgilityPack.Shared/HtmlNode.cs b/src/HtmlAgilityPack.Shared/HtmlNode.cs index 9927f1c..69f2529 100644 --- a/src/HtmlAgilityPack.Shared/HtmlNode.cs +++ b/src/HtmlAgilityPack.Shared/HtmlNode.cs @@ -2011,20 +2011,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 6adb6ef..ec08cb5 100644 --- a/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs +++ b/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs @@ -1206,6 +1206,27 @@ public void AttributeSerialization() } + [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 {