@@ -10,7 +10,6 @@ public class HtmlDocumentNavigator : XPathNavigator
10
10
{
11
11
private readonly IDocument _document ;
12
12
private INode _currentNode ;
13
- private int _attrIndex ;
14
13
private readonly bool _ignoreNamespaces ;
15
14
16
15
/// <summary>
@@ -24,7 +23,6 @@ public HtmlDocumentNavigator(IDocument document, INode currentNode, bool ignoreN
24
23
_document = document ?? throw new ArgumentNullException ( nameof ( document ) ) ;
25
24
NameTable = new NameTable ( ) ;
26
25
_currentNode = currentNode ?? throw new ArgumentNullException ( nameof ( currentNode ) ) ;
27
- _attrIndex = - 1 ;
28
26
_ignoreNamespaces = ignoreNamespaces ;
29
27
}
30
28
@@ -49,14 +47,14 @@ public HtmlDocumentNavigator(IDocument document, INode currentNode, bool ignoreN
49
47
50
48
/// <inheritdoc />
51
49
public override string LocalName =>
52
- _attrIndex != - 1
53
- ? NameTable . GetOrAdd ( CurrentElement . Attributes [ _attrIndex ] . LocalName )
50
+ CurrentNode is IAttr attr
51
+ ? attr . LocalName
54
52
: NameTable . GetOrAdd ( CurrentNode is IElement e ? e . LocalName : string . Empty ) ;
55
53
56
54
/// <inheritdoc />
57
55
public override string Name =>
58
- _attrIndex != - 1
59
- ? NameTable . GetOrAdd ( CurrentElement . Attributes [ _attrIndex ] . Name )
56
+ CurrentNode is IAttr attr
57
+ ? NameTable . GetOrAdd ( attr . Name )
60
58
: NameTable . GetOrAdd ( _currentNode . NodeName ) ;
61
59
62
60
/// <inheritdoc />
@@ -69,16 +67,16 @@ public override string NamespaceURI
69
67
return string . Empty ;
70
68
}
71
69
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 )
74
72
: NameTable . GetOrAdd ( CurrentElement ? . NamespaceUri ?? string . Empty ) ;
75
73
}
76
74
}
77
75
78
76
/// <inheritdoc />
79
77
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 )
82
80
: NameTable . GetOrAdd ( CurrentElement ? . Prefix ?? string . Empty ) ;
83
81
84
82
/// <inheritdoc />
@@ -107,7 +105,7 @@ public override XPathNodeType NodeType
107
105
return XPathNodeType . Element ;
108
106
109
107
case Dom . NodeType . Element :
110
- return _attrIndex != - 1 ? XPathNodeType . Attribute : XPathNodeType . Element ;
108
+ return XPathNodeType . Element ;
111
109
112
110
case Dom . NodeType . ProcessingInstruction :
113
111
return XPathNodeType . ProcessingInstruction ;
@@ -155,7 +153,7 @@ public override string Value
155
153
return documentType . Name ;
156
154
157
155
case Dom . NodeType . Element :
158
- return _attrIndex != - 1 ? CurrentElement . Attributes [ _attrIndex ] . Value : _currentNode . TextContent ;
156
+ return _currentNode . TextContent ;
159
157
160
158
case Dom . NodeType . Entity :
161
159
return _currentNode . TextContent ;
@@ -207,7 +205,6 @@ public override bool MoveTo(XPathNavigator other)
207
205
if ( navigator . _document == _document )
208
206
{
209
207
_currentNode = navigator . _currentNode ;
210
- _attrIndex = navigator . _attrIndex ;
211
208
return true ;
212
209
}
213
210
@@ -218,8 +215,8 @@ public override bool MoveTo(XPathNavigator other)
218
215
public override bool MoveToFirstAttribute ( )
219
216
{
220
217
if ( HasAttributes )
221
- {
222
- _attrIndex = 0 ;
218
+ {
219
+ _currentNode = CurrentElement . Attributes [ 0 ] ;
223
220
return true ;
224
221
}
225
222
@@ -278,12 +275,19 @@ public override bool MoveToNextAttribute()
278
275
return false ;
279
276
}
280
277
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 )
282
286
{
283
287
return false ;
284
288
}
285
289
286
- _attrIndex ++ ;
290
+ _currentNode = attr . OwnerElement . Attributes [ attrIndex + 1 ] ;
287
291
return true ;
288
292
}
289
293
0 commit comments