Skip to content

Commit 6e0f82e

Browse files
committed
Fix MoveToParent for Attr.
1 parent cbf85d2 commit 6e0f82e

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/AngleSharp.XPath.Tests/HtmlDocumentNavigatorTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using AngleSharp.Html.Parser;
44
using NUnit.Framework;
55
using System.Threading.Tasks;
6+
using System.Xml.XPath;
67
using AngleSharp.Dom;
78

89
namespace AngleSharp.XPath.Tests
@@ -146,5 +147,31 @@ public void TestNameXPathFunctionOnHTMLDoc()
146147
// Assert
147148
Assert.AreEqual(TagNames.Html, htmlNav.Evaluate("name()"));
148149
}
150+
151+
[Test]
152+
public void MoveToParent_CallWhenCurrentNodeIsAttr_ShouldBeMovedToAttrOwnerElement()
153+
{
154+
// Arrange
155+
var xml = @"<root att1='value 1' att2='value 2'><child>foo</child></root>";
156+
var parser = new XmlParser();
157+
var doc = parser.ParseDocument(xml);
158+
var nav = doc.CreateNavigator(false);
159+
nav.MoveToChild("root", "");
160+
161+
// Act
162+
163+
if (nav.MoveToFirstAttribute())
164+
{
165+
do
166+
{
167+
Assert.AreEqual(nav.NodeType, XPathNodeType.Attribute);
168+
}
169+
while (nav.MoveToNextAttribute());
170+
nav.MoveToParent();
171+
}
172+
173+
// Assert
174+
Assert.AreEqual(nav.Name, "root");
175+
}
149176
}
150177
}

src/AngleSharp.XPath/AngleSharp.XPath.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<Version>2.0.0-alpha-2</Version>
3+
<Version>2.0.0-alpha-3</Version>
44
<AssemblyVersion>2.0.0</AssemblyVersion>
5-
<FileVersion>2.0.0-alpha-2</FileVersion>
5+
<FileVersion>2.0.0-alpha-3</FileVersion>
66
<Authors>Denis Ivanov</Authors>
77
<PackageId>AngleSharp.XPath</PackageId>
88
<AssemblyName>AngleSharp.XPath</AssemblyName>

src/AngleSharp.XPath/HtmlDocumentNavigator.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,16 @@ public override bool MoveToNextAttribute()
293293
return false;
294294
}
295295

296+
if (attr.OwnerElement == null)
297+
{
298+
return false;
299+
}
300+
296301
var attrIndex = attr.OwnerElement.Attributes.Index(attr);
297302

298303
if (attrIndex >= CurrentElement.Attributes.Length - 1)
299304
{
300-
return false;
305+
return false;
301306
}
302307

303308
_currentNode = attr.OwnerElement.Attributes[attrIndex + 1];
@@ -313,6 +318,12 @@ public override bool MoveToNextNamespace(XPathNamespaceScope namespaceScope)
313318
/// <inheritdoc />
314319
public override bool MoveToParent()
315320
{
321+
if (CurrentNode is IAttr attr)
322+
{
323+
_currentNode = attr.OwnerElement;
324+
return true;
325+
}
326+
316327
if (_currentNode.Parent == null)
317328
{
318329
return false;

0 commit comments

Comments
 (0)