@@ -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,15 +47,28 @@ 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
- public override string Name =>
58
- _attrIndex != - 1
59
- ? NameTable . GetOrAdd ( CurrentElement . Attributes [ _attrIndex ] . Name )
60
- : NameTable . GetOrAdd ( _currentNode . NodeName ) ;
55
+ public override string Name
56
+ {
57
+ get
58
+ {
59
+ if ( CurrentNode is IAttr attr )
60
+ {
61
+ return NameTable . GetOrAdd ( attr . Name ) ;
62
+ }
63
+
64
+ if ( CurrentElement != null )
65
+ {
66
+ return NameTable . GetOrAdd ( CurrentElement . LocalName ) ;
67
+ }
68
+
69
+ return NameTable . GetOrAdd ( _currentNode . NodeName ) ;
70
+ }
71
+ }
61
72
62
73
/// <inheritdoc />
63
74
public override string NamespaceURI
@@ -69,16 +80,16 @@ public override string NamespaceURI
69
80
return string . Empty ;
70
81
}
71
82
72
- return _attrIndex != - 1
73
- ? NameTable . GetOrAdd ( CurrentElement . Attributes [ _attrIndex ] . NamespaceUri ?? string . Empty )
83
+ return CurrentNode is IAttr attr
84
+ ? NameTable . GetOrAdd ( attr . NamespaceUri ?? string . Empty )
74
85
: NameTable . GetOrAdd ( CurrentElement ? . NamespaceUri ?? string . Empty ) ;
75
86
}
76
87
}
77
88
78
89
/// <inheritdoc />
79
90
public override string Prefix =>
80
- _attrIndex != 1
81
- ? NameTable . GetOrAdd ( CurrentElement . Attributes [ _attrIndex ] . Prefix ?? string . Empty )
91
+ CurrentNode is IAttr attr
92
+ ? NameTable . GetOrAdd ( attr . Prefix ?? string . Empty )
82
93
: NameTable . GetOrAdd ( CurrentElement ? . Prefix ?? string . Empty ) ;
83
94
84
95
/// <inheritdoc />
@@ -107,7 +118,7 @@ public override XPathNodeType NodeType
107
118
return XPathNodeType . Element ;
108
119
109
120
case Dom . NodeType . Element :
110
- return _attrIndex != - 1 ? XPathNodeType . Attribute : XPathNodeType . Element ;
121
+ return XPathNodeType . Element ;
111
122
112
123
case Dom . NodeType . ProcessingInstruction :
113
124
return XPathNodeType . ProcessingInstruction ;
@@ -155,7 +166,7 @@ public override string Value
155
166
return documentType . Name ;
156
167
157
168
case Dom . NodeType . Element :
158
- return _attrIndex != - 1 ? CurrentElement . Attributes [ _attrIndex ] . Value : _currentNode . TextContent ;
169
+ return _currentNode . TextContent ;
159
170
160
171
case Dom . NodeType . Entity :
161
172
return _currentNode . TextContent ;
@@ -207,7 +218,6 @@ public override bool MoveTo(XPathNavigator other)
207
218
if ( navigator . _document == _document )
208
219
{
209
220
_currentNode = navigator . _currentNode ;
210
- _attrIndex = navigator . _attrIndex ;
211
221
return true ;
212
222
}
213
223
@@ -218,8 +228,8 @@ public override bool MoveTo(XPathNavigator other)
218
228
public override bool MoveToFirstAttribute ( )
219
229
{
220
230
if ( HasAttributes )
221
- {
222
- _attrIndex = 0 ;
231
+ {
232
+ _currentNode = CurrentElement . Attributes [ 0 ] ;
223
233
return true ;
224
234
}
225
235
@@ -278,12 +288,24 @@ public override bool MoveToNextAttribute()
278
288
return false ;
279
289
}
280
290
281
- if ( _attrIndex >= CurrentElement . Attributes . Length - 1 )
291
+ if ( ! ( CurrentNode is IAttr attr ) )
292
+ {
293
+ return false ;
294
+ }
295
+
296
+ if ( attr . OwnerElement == null )
297
+ {
298
+ return false ;
299
+ }
300
+
301
+ var attrIndex = attr . OwnerElement . Attributes . Index ( attr ) ;
302
+
303
+ if ( attrIndex >= CurrentElement . Attributes . Length - 1 )
282
304
{
283
- return false ;
305
+ return false ;
284
306
}
285
307
286
- _attrIndex ++ ;
308
+ _currentNode = attr . OwnerElement . Attributes [ attrIndex + 1 ] ;
287
309
return true ;
288
310
}
289
311
@@ -296,6 +318,12 @@ public override bool MoveToNextNamespace(XPathNamespaceScope namespaceScope)
296
318
/// <inheritdoc />
297
319
public override bool MoveToParent ( )
298
320
{
321
+ if ( CurrentNode is IAttr attr )
322
+ {
323
+ _currentNode = attr . OwnerElement ;
324
+ return true ;
325
+ }
326
+
299
327
if ( _currentNode . Parent == null )
300
328
{
301
329
return false ;
0 commit comments