Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check on null for Openednodes dictionary, add unit test #331

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Expand Up @@ -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
{
Expand Down
21 changes: 21 additions & 0 deletions src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs
Expand Up @@ -945,6 +945,27 @@ public void GetClasses_WhereClassWithWhitespacePassed_ShouldNotBeEmpty()
Assert.IsNotEmpty(aTag.GetClasses());
}

[Test]
public void LoadHtml_WhenHtmlHasUnclosedTags_AndOptionCheckSyntaxFalse_ShouldNotThrowException()
{
var html = "<div>Some simple <p> html</div>";
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
{
Expand Down