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

Don't allocate AttributesCollection in HtmlNode unless needed #558

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,20 +254,7 @@ public HtmlNode FirstChild
/// </summary>
public bool HasAttributes
{
get
{
if (_attributes == null)
{
return false;
}

if (_attributes.Count <= 0)
{
return false;
}

return true;
}
get { return _attributes != null && _attributes.Count > 0; }
}

/// <summary>
Expand Down Expand Up @@ -1041,7 +1028,7 @@ public void AppendChildren(HtmlNodeCollection newChildren)
/// <returns></returns>
public IEnumerable<HtmlAttribute> ChildAttributes(string name)
{
return Attributes.AttributesWithName(name);
return HasAttributes ? Attributes.AttributesWithName(name) : Enumerable.Empty<HtmlAttribute>();
}

/// <summary>
Expand Down Expand Up @@ -2324,7 +2311,7 @@ internal void CloseNode(HtmlNode endnode, int level = 0)

internal string GetId()
{
HtmlAttribute att = Attributes["id"];
HtmlAttribute att = HasAttributes ? Attributes["id"] : null;
return att == null ? string.Empty : att.Value;
}

Expand Down
2 changes: 1 addition & 1 deletion src/HtmlAgilityPack.Shared/HtmlNodeNavigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ public override string GetAttribute(string localName, string namespaceURI)
#if TRACE_NAVIGATOR
InternalTrace("localName=" + localName + ", namespaceURI=" + namespaceURI);
#endif
HtmlAttribute att = _currentnode.Attributes[localName];
HtmlAttribute att = _currentnode.HasAttributes ? _currentnode.Attributes[localName] : null;
if (att == null)
{
#if TRACE_NAVIGATOR
Expand Down