Skip to content

Commit e29f7af

Browse files
committed
Don't allocate AttributesCollection in HtmlNode unless needed
1 parent 9703643 commit e29f7af

File tree

2 files changed

+4
-17
lines changed

2 files changed

+4
-17
lines changed

src/HtmlAgilityPack.Shared/HtmlNode.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,7 @@ public HtmlNode FirstChild
254254
/// </summary>
255255
public bool HasAttributes
256256
{
257-
get
258-
{
259-
if (_attributes == null)
260-
{
261-
return false;
262-
}
263-
264-
if (_attributes.Count <= 0)
265-
{
266-
return false;
267-
}
268-
269-
return true;
270-
}
257+
get { return _attributes != null && _attributes.Count > 0; }
271258
}
272259

273260
/// <summary>
@@ -1053,7 +1040,7 @@ public void AppendChildren(HtmlNodeCollection newChildren)
10531040
/// <returns></returns>
10541041
public IEnumerable<HtmlAttribute> ChildAttributes(string name)
10551042
{
1056-
return Attributes.AttributesWithName(name);
1043+
return HasAttributes ? Attributes.AttributesWithName(name) : Enumerable.Empty<HtmlAttribute>();
10571044
}
10581045

10591046
/// <summary>
@@ -2402,7 +2389,7 @@ internal void CloseNode(HtmlNode endnode, int level = 0)
24022389

24032390
internal string GetId()
24042391
{
2405-
HtmlAttribute att = Attributes["id"];
2392+
HtmlAttribute att = HasAttributes ? Attributes["id"] : null;
24062393
return att == null ? string.Empty : att.Value;
24072394
}
24082395

src/HtmlAgilityPack.Shared/HtmlNodeNavigator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ public override
539539
#if TRACE_NAVIGATOR
540540
InternalTrace("localName=" + localName + ", namespaceURI=" + namespaceURI);
541541
#endif
542-
HtmlAttribute att = _currentnode.Attributes[localName];
542+
HtmlAttribute att = _currentnode.HasAttributes ? _currentnode.Attributes[localName] : null;
543543
if (att == null)
544544
{
545545
#if TRACE_NAVIGATOR

0 commit comments

Comments
 (0)