Skip to content

Commit

Permalink
Fix "Name" property
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanMagnan committed Jun 19, 2023
1 parent 08aae94 commit a22812c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/HtmlAgilityPack.Shared/HtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public HtmlNode CreateElement(string name)
}

HtmlNode node = CreateNode(HtmlNodeType.Element);
node.Name = name;
node.SetName(name);
return node;
}

Expand Down Expand Up @@ -1885,6 +1885,7 @@ private bool IsParentImplicitEnd()
|| nodeName == "h6"
|| nodeName == "header"
|| nodeName == "hr"
|| nodeName == "li"
|| nodeName == "menu"
|| nodeName == "nav"
|| nodeName == "ol"
Expand Down
33 changes: 19 additions & 14 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ public HtmlNode(HtmlNodeType type, HtmlDocument ownerdocument, int index)
switch (type)
{
case HtmlNodeType.Comment:
Name = HtmlNodeTypeNameComment;
SetName(HtmlNodeTypeNameComment);
_endnode = this;
break;

case HtmlNodeType.Document:
Name = HtmlNodeTypeNameDocument;
SetName(HtmlNodeTypeNameDocument);
_endnode = this;
break;

case HtmlNodeType.Text:
Name = HtmlNodeTypeNameText;
SetName(HtmlNodeTypeNameText);
_endnode = this;
break;
}
Expand Down Expand Up @@ -600,7 +600,7 @@ public string Name
if (_optimizedName == null)
{
if (_name == null)
Name = _ownerdocument.Text.Substring(_namestartindex, _namelength);
SetName(_ownerdocument.Text.Substring(_namestartindex, _namelength));

if (_name == null)
_optimizedName = string.Empty;
Expand All @@ -612,12 +612,18 @@ public string Name

return _optimizedName;
}
set
{
_name = value;
_optimizedName = null;
set
{
SetName(value);
SetChanged();
}
}
}

internal void SetName(string value)
{
_name = value;
_optimizedName = null;
}

/// <summary>
/// Gets the HTML node immediately following this element.
Expand Down Expand Up @@ -1046,7 +1052,7 @@ public HtmlNode CloneNode(string newName, bool deep)
}

HtmlNode node = CloneNode(deep);
node.Name = newName;
node.SetName(newName);
return node;
}

Expand All @@ -1058,7 +1064,7 @@ public HtmlNode CloneNode(string newName, bool deep)
public HtmlNode CloneNode(bool deep)
{
HtmlNode node = _ownerdocument.CreateNode(_nodetype);
node.Name = OriginalName;
node.SetName(OriginalName);

switch (_nodetype)
{
Expand Down Expand Up @@ -2159,10 +2165,9 @@ public void SetParent(HtmlNode parent)
}
}

#endregion

#region Internal Methods
#endregion

#region Internal Methods
internal void SetChanged()
{
_changed = true;
Expand Down

0 comments on commit a22812c

Please sign in to comment.