1- #region License
2- // MIT License
3- //
4- // Copyright (c) 2018 Denis Ivanov
5- //
6- // Permission is hereby granted, free of charge, to any person obtaining a copy
7- // of this software and associated documentation files (the "Software"), to deal
8- // in the Software without restriction, including without limitation the rights
9- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10- // copies of the Software, and to permit persons to whom the Software is
11- // furnished to do so, subject to the following conditions:
12- //
13- // The above copyright notice and this permission notice shall be included in all
14- // copies or substantial portions of the Software.
15- //
16- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22- // SOFTWARE.
23- #endregion
24-
25- using AngleSharp . Dom ;
26- using AngleSharp . Dom . Html ;
27- using System ;
28- using System . Collections . Generic ;
29- using System . Diagnostics ;
30- using System . Xml ;
31- using System . Xml . XPath ;
32-
331namespace AngleSharp . XPath
342{
35- public static class Extensions
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+ /// <summary>
11+ /// Hosts the extension methods for XPath parsing.
12+ /// </summary>
13+ public static class Extensions
3614 {
37- public static XPathNavigator CreateNavigator ( this IHtmlDocument document )
15+ /// <summary>
16+ /// Creates a new navigator for the given document.
17+ /// </summary>
18+ /// <param name="document">The document to extend.</param>
19+ /// <returns>The navigator for XPath expressions.</returns>
20+ public static XPathNavigator CreateNavigator ( this IDocument document )
3821 {
39- if ( document == null )
40- {
41- throw new ArgumentNullException ( nameof ( document ) ) ;
42- }
43-
44- return new HtmlDocumentNavigator ( document , document . DocumentElement ) ;
22+ var doc = document ?? throw new ArgumentNullException ( nameof ( document ) ) ;
23+ return new HtmlDocumentNavigator ( doc , doc . DocumentElement ) ;
4524 }
4625
4726 [ DebuggerStepThrough ]
48- public static string GetOrAdd ( this XmlNameTable table , string array )
27+ internal static String GetOrAdd ( this XmlNameTable table , String array )
4928 {
5029 var s = table . Get ( array ) ;
5130
@@ -57,51 +36,42 @@ public static string GetOrAdd(this XmlNameTable table, string array)
5736 return s ;
5837 }
5938
60- public static INode SelectSingleNode ( this IElement element , string xpath )
39+ /// <summary>
40+ /// Selects a single node (or returns null) matching the <see cref="XPath"/> expression.
41+ /// </summary>
42+ /// <param name="element">The element to start looking from.</param>
43+ /// <param name="xpath">The XPath expression.</param>
44+ /// <returns>The node matching <paramref name="xpath"/> query, if any.</returns>
45+ /// <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 )
6147 {
62- if ( element == null )
63- {
64- throw new ArgumentNullException ( nameof ( element ) ) ;
65- }
66-
67- if ( xpath == null )
68- {
69- throw new ArgumentNullException ( nameof ( xpath ) ) ;
70- }
48+ var el = element ?? throw new ArgumentNullException ( nameof ( element ) ) ;
49+ var xp = xpath ?? throw new ArgumentNullException ( nameof ( xpath ) ) ;
50+ var nav = new HtmlDocumentNavigator ( el . Owner , el ) ;
51+ var it = nav . Select ( xp ) ;
7152
72- var nav = new HtmlDocumentNavigator ( element . Owner , element ) ;
73- var it = nav . Select ( xpath ) ;
53+ if ( it . MoveNext ( ) )
54+ {
55+ var node = ( HtmlDocumentNavigator ) it . Current ;
56+ return node . CurrentNode ;
57+ }
7458
75- if ( ! it . MoveNext ( ) )
76- {
77- return null ;
78- }
79-
80- var node = ( HtmlDocumentNavigator ) it . Current ;
81- return node . CurrentNode ;
59+ return null ;
8260 }
8361
84- /// <summary>
85- /// Selects a list of nodes matching the <see cref="XPath"/> expression.
86- /// </summary>
87- /// <param name="element"></param>
88- /// <param name="xpath">The XPath expression.</param>
89- /// <returns>List of nodes matching <paramref name="xpath"/> query.</returns>
90- /// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
91- public static List < INode > SelectNodes ( this IElement element , string xpath )
92- {
93- if ( element == null )
94- {
95- throw new ArgumentNullException ( nameof ( element ) ) ;
96- }
97-
98- if ( xpath == null )
99- {
100- throw new ArgumentNullException ( nameof ( xpath ) ) ;
101- }
102-
103- var nav = new HtmlDocumentNavigator ( element . Owner , element ) ;
104- var it = nav . Select ( xpath ) ;
62+ /// <summary>
63+ /// Selects a list of nodes matching the <see cref="XPath"/> expression.
64+ /// </summary>
65+ /// <param name="element">The element to start looking from.</param>
66+ /// <param name="xpath">The XPath expression.</param>
67+ /// <returns>List of nodes matching <paramref name="xpath"/> query.</returns>
68+ /// <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 )
70+ {
71+ var el = element ?? throw new ArgumentNullException ( nameof ( element ) ) ;
72+ var xp = xpath ?? throw new ArgumentNullException ( nameof ( xpath ) ) ;
73+ var nav = new HtmlDocumentNavigator ( el . Owner , el ) ;
74+ var it = nav . Select ( xp ) ;
10575 var result = new List < INode > ( ) ;
10676
10777 while ( it . MoveNext ( ) )
0 commit comments