@@ -10,7 +10,6 @@ public class HtmlDocumentNavigator : XPathNavigator
1010 {
1111 private readonly IDocument _document ;
1212 private INode _currentNode ;
13- private int _attrIndex ;
1413 private readonly bool _ignoreNamespaces ;
1514
1615 /// <summary>
@@ -24,7 +23,6 @@ public HtmlDocumentNavigator(IDocument document, INode currentNode, bool ignoreN
2423 _document = document ?? throw new ArgumentNullException ( nameof ( document ) ) ;
2524 NameTable = new NameTable ( ) ;
2625 _currentNode = currentNode ?? throw new ArgumentNullException ( nameof ( currentNode ) ) ;
27- _attrIndex = - 1 ;
2826 _ignoreNamespaces = ignoreNamespaces ;
2927 }
3028
@@ -49,14 +47,14 @@ public HtmlDocumentNavigator(IDocument document, INode currentNode, bool ignoreN
4947
5048 /// <inheritdoc />
5149 public override string LocalName =>
52- _attrIndex != - 1
53- ? NameTable . GetOrAdd ( CurrentElement . Attributes [ _attrIndex ] . LocalName )
50+ CurrentNode is IAttr attr
51+ ? attr . LocalName
5452 : NameTable . GetOrAdd ( CurrentNode is IElement e ? e . LocalName : string . Empty ) ;
5553
5654 /// <inheritdoc />
5755 public override string Name =>
58- _attrIndex != - 1
59- ? NameTable . GetOrAdd ( CurrentElement . Attributes [ _attrIndex ] . Name )
56+ CurrentNode is IAttr attr
57+ ? NameTable . GetOrAdd ( attr . Name )
6058 : NameTable . GetOrAdd ( _currentNode . NodeName ) ;
6159
6260 /// <inheritdoc />
@@ -69,16 +67,16 @@ public override string NamespaceURI
6967 return string . Empty ;
7068 }
7169
72- return _attrIndex != - 1
73- ? NameTable . GetOrAdd ( CurrentElement . Attributes [ _attrIndex ] . NamespaceUri ?? string . Empty )
70+ return CurrentNode is IAttr attr
71+ ? NameTable . GetOrAdd ( attr . NamespaceUri ?? string . Empty )
7472 : NameTable . GetOrAdd ( CurrentElement ? . NamespaceUri ?? string . Empty ) ;
7573 }
7674 }
7775
7876 /// <inheritdoc />
7977 public override string Prefix =>
80- _attrIndex != 1
81- ? NameTable . GetOrAdd ( CurrentElement . Attributes [ _attrIndex ] . Prefix ?? string . Empty )
78+ CurrentNode is IAttr attr
79+ ? NameTable . GetOrAdd ( attr . Prefix ?? string . Empty )
8280 : NameTable . GetOrAdd ( CurrentElement ? . Prefix ?? string . Empty ) ;
8381
8482 /// <inheritdoc />
@@ -107,7 +105,7 @@ public override XPathNodeType NodeType
107105 return XPathNodeType . Element ;
108106
109107 case Dom . NodeType . Element :
110- return _attrIndex != - 1 ? XPathNodeType . Attribute : XPathNodeType . Element ;
108+ return XPathNodeType . Element ;
111109
112110 case Dom . NodeType . ProcessingInstruction :
113111 return XPathNodeType . ProcessingInstruction ;
@@ -155,7 +153,7 @@ public override string Value
155153 return documentType . Name ;
156154
157155 case Dom . NodeType . Element :
158- return _attrIndex != - 1 ? CurrentElement . Attributes [ _attrIndex ] . Value : _currentNode . TextContent ;
156+ return _currentNode . TextContent ;
159157
160158 case Dom . NodeType . Entity :
161159 return _currentNode . TextContent ;
@@ -207,7 +205,6 @@ public override bool MoveTo(XPathNavigator other)
207205 if ( navigator . _document == _document )
208206 {
209207 _currentNode = navigator . _currentNode ;
210- _attrIndex = navigator . _attrIndex ;
211208 return true ;
212209 }
213210
@@ -218,8 +215,8 @@ public override bool MoveTo(XPathNavigator other)
218215 public override bool MoveToFirstAttribute ( )
219216 {
220217 if ( HasAttributes )
221- {
222- _attrIndex = 0 ;
218+ {
219+ _currentNode = CurrentElement . Attributes [ 0 ] ;
223220 return true ;
224221 }
225222
@@ -278,12 +275,19 @@ public override bool MoveToNextAttribute()
278275 return false ;
279276 }
280277
281- if ( _attrIndex >= CurrentElement . Attributes . Length - 1 )
278+ if ( ! ( CurrentNode is IAttr attr ) )
279+ {
280+ return false ;
281+ }
282+
283+ var attrIndex = attr . OwnerElement . Attributes . Index ( attr ) ;
284+
285+ if ( attrIndex >= CurrentElement . Attributes . Length - 1 )
282286 {
283287 return false ;
284288 }
285289
286- _attrIndex ++ ;
290+ _currentNode = attr . OwnerElement . Attributes [ attrIndex + 1 ] ;
287291 return true ;
288292 }
289293
0 commit comments