1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Xml.XPath;
6 
7 namespace MS.Internal.Xml.XPath
8 {
9     internal class XPathDescendantIterator : XPathAxisIterator
10     {
11         private int _level = 0;
12 
XPathDescendantIterator(XPathNavigator nav, XPathNodeType type, bool matchSelf)13         public XPathDescendantIterator(XPathNavigator nav, XPathNodeType type, bool matchSelf) : base(nav, type, matchSelf) { }
XPathDescendantIterator(XPathNavigator nav, string name, string namespaceURI, bool matchSelf)14         public XPathDescendantIterator(XPathNavigator nav, string name, string namespaceURI, bool matchSelf) : base(nav, name, namespaceURI, matchSelf) { }
15 
XPathDescendantIterator(XPathDescendantIterator it)16         public XPathDescendantIterator(XPathDescendantIterator it) : base(it)
17         {
18             _level = it._level;
19         }
20 
Clone()21         public override XPathNodeIterator Clone()
22         {
23             return new XPathDescendantIterator(this);
24         }
25 
MoveNext()26         public override bool MoveNext()
27         {
28             if (first)
29             {
30                 first = false;
31                 if (matchSelf && Matches)
32                 {
33                     position = 1;
34                     return true;
35                 }
36             }
37 
38             while (true)
39             {
40                 if (nav.MoveToFirstChild())
41                 {
42                     _level++;
43                 }
44                 else
45                 {
46                     while (true)
47                     {
48                         if (_level == 0)
49                         {
50                             return false;
51                         }
52                         if (nav.MoveToNext())
53                         {
54                             break;
55                         }
56                         nav.MoveToParent();
57                         _level--;
58                     }
59                 }
60 
61                 if (Matches)
62                 {
63                     position++;
64                     return true;
65                 }
66             }
67         }
68     }
69 }
70