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 {