1
+ using AngleSharp . Dom ;
2
+ using System ;
3
+ using System . Collections . Generic ;
4
+ using System . Diagnostics ;
5
+ using System . Xml ;
6
+ using System . Xml . XPath ;
7
+
1
8
namespace AngleSharp . XPath
2
9
{
3
- using AngleSharp . Dom ;
4
- using System ;
5
- using System . Collections . Generic ;
6
- using System . Diagnostics ;
7
- using System . Xml ;
8
- using System . Xml . XPath ;
9
-
10
10
/// <summary>
11
11
/// Hosts the extension methods for XPath parsing.
12
12
/// </summary>
13
13
public static class Extensions
14
- {
14
+ {
15
15
/// <summary>
16
16
/// Creates a new navigator for the given document.
17
17
/// </summary>
18
18
/// <param name="document">The document to extend.</param>
19
19
/// <returns>The navigator for XPath expressions.</returns>
20
- public static XPathNavigator CreateNavigator ( this IDocument document )
21
- {
20
+ public static XPathNavigator CreateNavigator ( this IDocument document )
21
+ {
22
22
var doc = document ?? throw new ArgumentNullException ( nameof ( document ) ) ;
23
23
return new HtmlDocumentNavigator ( doc , doc . DocumentElement ) ;
24
- }
25
-
26
- [ DebuggerStepThrough ]
27
- internal static String GetOrAdd ( this XmlNameTable table , String array )
28
- {
29
- var s = table . Get ( array ) ;
24
+ }
30
25
31
- if ( s == null )
32
- {
33
- return table . Add ( array ) ;
34
- }
35
-
36
- return s ;
37
- }
26
+ [ DebuggerStepThrough ]
27
+ internal static string GetOrAdd ( this XmlNameTable table , string array )
28
+ {
29
+ var s = table . Get ( array ) ;
30
+ return s ?? table . Add ( array ) ;
31
+ }
38
32
39
33
/// <summary>
40
34
/// Selects a single node (or returns null) matching the <see cref="XPath"/> expression.
@@ -43,21 +37,14 @@ internal static String GetOrAdd(this XmlNameTable table, String array)
43
37
/// <param name="xpath">The XPath expression.</param>
44
38
/// <returns>The node matching <paramref name="xpath"/> query, if any.</returns>
45
39
/// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
46
- public static INode SelectSingleNode ( this IElement element , String xpath )
47
- {
40
+ public static INode SelectSingleNode ( this IElement element , string xpath )
41
+ {
48
42
var el = element ?? throw new ArgumentNullException ( nameof ( element ) ) ;
49
43
var xp = xpath ?? throw new ArgumentNullException ( nameof ( xpath ) ) ;
50
- var nav = new HtmlDocumentNavigator ( el . Owner , el ) ;
51
- var it = nav . Select ( xp ) ;
52
-
53
- if ( it . MoveNext ( ) )
54
- {
55
- var node = ( HtmlDocumentNavigator ) it . Current ;
56
- return node . CurrentNode ;
57
- }
58
-
59
- return null ;
60
- }
44
+ var nav = new HtmlDocumentNavigator ( el . Owner , el ) ;
45
+ var it = nav . SelectSingleNode ( xp ) ;
46
+ return ( ( HtmlDocumentNavigator ) it ) ? . CurrentNode ;
47
+ }
61
48
62
49
/// <summary>
63
50
/// Selects a list of nodes matching the <see cref="XPath"/> expression.
@@ -66,22 +53,24 @@ public static INode SelectSingleNode(this IElement element, String xpath)
66
53
/// <param name="xpath">The XPath expression.</param>
67
54
/// <returns>List of nodes matching <paramref name="xpath"/> query.</returns>
68
55
/// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
69
- public static List < INode > SelectNodes ( this IElement element , String xpath )
56
+ public static List < INode > SelectNodes ( this IElement element , string xpath )
70
57
{
71
58
var el = element ?? throw new ArgumentNullException ( nameof ( element ) ) ;
72
59
var xp = xpath ?? throw new ArgumentNullException ( nameof ( xpath ) ) ;
73
60
var nav = new HtmlDocumentNavigator ( el . Owner , el ) ;
74
- var it = nav . Select ( xp ) ;
75
- var result = new List < INode > ( ) ;
61
+ var it = nav . Select ( xp ) ;
62
+ var result = new List < INode > ( ) ;
76
63
77
- while ( it . MoveNext ( ) )
78
- {
79
- var naviagtor = ( HtmlDocumentNavigator ) it . Current ;
80
- var e = naviagtor . CurrentNode ;
81
- result . Add ( e ) ;
82
- }
64
+ while ( it . MoveNext ( ) )
65
+ {
66
+ // ReSharper disable once IdentifierTypo
67
+ var naviagtor = ( HtmlDocumentNavigator ) it . Current ;
68
+ // ReSharper disable once PossibleNullReferenceException
69
+ var e = naviagtor . CurrentNode ;
70
+ result . Add ( e ) ;
71
+ }
83
72
84
- return result ;
85
- }
86
- }
73
+ return result ;
74
+ }
75
+ }
87
76
}
0 commit comments