Skip to content

Commit 829fa4a

Browse files
author
Denis Ivanov
committed
Add SelectNodes method
1 parent f8544bd commit 829fa4a

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/AngleSharp.XPath.Tests/HtmlDocumentNavigatorTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Threading.Tasks;
6+
using AngleSharp.Parser.Html;
67
using AngleSharp.XPath;
78

89
namespace AngleSharp.XPath.Tests
@@ -24,5 +25,25 @@ public async Task SelectSinleNodeTest()
2425
// Assert
2526
Assert.That(content, Is.Not.Null);
2627
}
28+
29+
[Test]
30+
public void SelectNodes_SelectList_ShouldReturnList()
31+
{
32+
// Arrange
33+
const string html =
34+
@"<ol>
35+
<li>First</li>
36+
<li>Second</li>
37+
<li>Third</li>
38+
</ol>";
39+
var parser = new HtmlParser();
40+
var document = parser.Parse(html);
41+
42+
// Act
43+
var nodes = document.DocumentElement.SelectNodes("//li");
44+
45+
// Assert
46+
Assert.That(nodes, Has.Count.EqualTo(3));
47+
}
2748
}
2849
}

src/AngleSharp.XPath/AngleSharp.XPath.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<Version>1.0.0</Version>
44
<Authors>Denis Ivanov</Authors>
55
<PackageId>AngleSharp.XPath</PackageId>
6-
<AssemblyVersion>1.0.0</AssemblyVersion>
7-
<FileVersion>1.0.0</FileVersion>
6+
<AssemblyVersion>1.0.1</AssemblyVersion>
7+
<FileVersion>1.0.1</FileVersion>
88
<Description>XPath support for AngleSharp</Description>
99
<PackageProjectUrl>https://github.com/denis-ivanov/AngleSharp.XPath/</PackageProjectUrl>
1010
<PackageTags>HTML XPath AngleSharp</PackageTags>

src/AngleSharp.XPath/Extensions.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using AngleSharp.Dom;
22
using AngleSharp.Dom.Html;
33
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.Specialized;
46
using System.Diagnostics;
57
using System.Xml;
68
using System.Xml.XPath;
@@ -22,7 +24,7 @@ public static XPathNavigator CreateNavigator(this IHtmlDocument document)
2224
[DebuggerStepThrough]
2325
public static string GetOrAdd(this XmlNameTable table, string array)
2426
{
25-
string s = table.Get(array);
27+
var s = table.Get(array);
2628

2729
if (s == null)
2830
{
@@ -55,5 +57,38 @@ public static INode SelectSingleNode(this IElement element, string xpath)
5557
var node = (HtmlDocumentNavigator)it.Current;
5658
return node.CurrentNode;
5759
}
60+
61+
/// <summary>
62+
/// Selects a list of nodes matching the <see cref="XPath"/> expression.
63+
/// </summary>
64+
/// <param name="element"></param>
65+
/// <param name="xpath">The XPath expression.</param>
66+
/// <returns>List of nodes matching <paramref name="xpath"/> query.</returns>
67+
/// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
68+
public static List<INode> SelectNodes(this IElement element, string xpath)
69+
{
70+
if (element == null)
71+
{
72+
throw new ArgumentNullException(nameof(element));
73+
}
74+
75+
if (xpath == null)
76+
{
77+
throw new ArgumentNullException(nameof(xpath));
78+
}
79+
80+
var nav = new HtmlDocumentNavigator(element.Owner, element);
81+
var it = nav.Select(xpath);
82+
var result = new List<INode>();
83+
84+
while (it.MoveNext())
85+
{
86+
var naviagtor = (HtmlDocumentNavigator) it.Current;
87+
var e = naviagtor.CurrentNode;
88+
result.Add(e);
89+
}
90+
91+
return result;
92+
}
5893
}
5994
}

0 commit comments

Comments
 (0)