Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XPathNodeHelper.cs « Cache « Xml « System « System.Xml « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d41386dc0567985990c1182846971c658148437a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//------------------------------------------------------------------------------
// <copyright file="XPathNodeHelper.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;

namespace MS.Internal.Xml.Cache {

    /// <summary>
    /// Library of XPathNode helper routines.
    /// </summary>
    internal abstract class XPathNodeHelper {

        /// <summary>
        /// Return chain of namespace nodes.  If specified node has no local namespaces, then 0 will be
        /// returned.  Otherwise, the first node in the chain is guaranteed to be a local namespace (its
        /// parent is this node).  Subsequent nodes may not have the same node as parent, so the caller will
        /// need to test the parent in order to terminate a search that processes only local namespaces.
        /// </summary>
        public static int GetLocalNamespaces(XPathNode[] pageElem, int idxElem, out XPathNode[] pageNmsp) {
            if (pageElem[idxElem].HasNamespaceDecls) {
                // Only elements have namespace nodes
                Debug.Assert(pageElem[idxElem].NodeType == XPathNodeType.Element);
                return pageElem[idxElem].Document.LookupNamespaces(pageElem, idxElem, out pageNmsp);
            }
            pageNmsp = null;
            return 0;
        }

        /// <summary>
        /// Return chain of in-scope namespace nodes for nodes of type Element.  Nodes in the chain might not
        /// have this element as their parent.  Since the xmlns:xml namespace node is always in scope, this
        /// method will never return 0 if the specified node is an element.
        /// </summary>
        public static int GetInScopeNamespaces(XPathNode[] pageElem, int idxElem, out XPathNode[] pageNmsp) {
            XPathDocument doc;

            // Only elements have namespace nodes
            if (pageElem[idxElem].NodeType == XPathNodeType.Element) {
                doc = pageElem[idxElem].Document;

                // Walk ancestors, looking for an ancestor that has at least one namespace declaration
                while (!pageElem[idxElem].HasNamespaceDecls) {
                    idxElem = pageElem[idxElem].GetParent(out pageElem);
                    if (idxElem == 0) {
                        // There are no namespace nodes declared on ancestors, so return xmlns:xml node
                        return doc.GetXmlNamespaceNode(out pageNmsp);
                    }
                }
                // Return chain of in-scope namespace nodes
                return doc.LookupNamespaces(pageElem, idxElem, out pageNmsp);
            }
            pageNmsp = null;
            return 0;
        }

        /// <summary>
        /// Return the first attribute of the specified node.  If no attribute exist, do not
        /// set pageNode or idxNode and return false.
        /// </summary>
        public static bool GetFirstAttribute(ref XPathNode[] pageNode, ref int idxNode) {
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            if (pageNode[idxNode].HasAttribute) {
                GetChild(ref pageNode, ref idxNode);
                Debug.Assert(pageNode[idxNode].NodeType == XPathNodeType.Attribute);
                return true;
            }
            return false;
        }

        /// <summary>
        /// Return the next attribute sibling of the specified node.  If the node is not itself an
        /// attribute, or if there are no siblings, then do not set pageNode or idxNode and return false.
        /// </summary>
        public static bool GetNextAttribute(ref XPathNode[] pageNode, ref int idxNode) {
            XPathNode[] page;
            int idx;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            idx = pageNode[idxNode].GetSibling(out page);
            if (idx != 0 && page[idx].NodeType == XPathNodeType.Attribute) {
                pageNode = page;
                idxNode = idx;
                return true;
            }
            return false;
        }

        /// <summary>
        /// Return the first content-typed child of the specified node.  If the node has no children, or
        /// if the node is not content-typed, then do not set pageNode or idxNode and return false.
        /// </summary>
        public static bool GetContentChild(ref XPathNode[] pageNode, ref int idxNode) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            if (page[idx].HasContentChild) {
                GetChild(ref page, ref idx);

                // Skip past attribute children
                while (page[idx].NodeType == XPathNodeType.Attribute) {
                    idx = page[idx].GetSibling(out page);
                    Debug.Assert(idx != 0);
                }

                pageNode = page;
                idxNode = idx;
                return true;
            }
            return false;
        }

        /// <summary>
        /// Return the next content-typed sibling of the specified node.  If the node has no siblings, or
        /// if the node is not content-typed, then do not set pageNode or idxNode and return false.
        /// </summary>
        public static bool GetContentSibling(ref XPathNode[] pageNode, ref int idxNode) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            if (!page[idx].IsAttrNmsp) {
                idx = page[idx].GetSibling(out page);
                if (idx != 0) {
                    pageNode = page;
                    idxNode = idx;
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Return the parent of the specified node.  If the node has no parent, do not set pageNode
        /// or idxNode and return false.
        /// </summary>
        public static bool GetParent(ref XPathNode[] pageNode, ref int idxNode) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            idx = page[idx].GetParent(out page);
            if (idx != 0) {
                pageNode = page;
                idxNode = idx;
                return true;
            }
            return false;
        }

        /// <summary>
        /// Return a location integer that can be easily compared with other locations from the same document
        /// in order to determine the relative document order of two nodes.
        /// </summary>
        public static int GetLocation(XPathNode[] pageNode, int idxNode) {
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");
            Debug.Assert(idxNode <= UInt16.MaxValue);
            Debug.Assert(pageNode[0].PageInfo.PageNumber <= Int16.MaxValue);
            return (pageNode[0].PageInfo.PageNumber << 16) | idxNode;
        }

        /// <summary>
        /// Return the first element child of the specified node that has the specified name.  If no such child exists,
        /// then do not set pageNode or idxNode and return false.  Assume that the localName has been atomized with respect
        /// to this document's name table, but not the namespaceName.
        /// </summary>
        public static bool GetElementChild(ref XPathNode[] pageNode, ref int idxNode, string localName, string namespaceName) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            // Only check children if at least one element child exists
            if (page[idx].HasElementChild) {
                GetChild(ref page, ref idx);
                Debug.Assert(idx != 0);

                // Find element with specified localName and namespaceName
                do {
                    if (page[idx].ElementMatch(localName, namespaceName)) {
                        pageNode = page;
                        idxNode = idx;
                        return true;
                    }
                    idx = page[idx].GetSibling(out page);
                }
                while (idx != 0);
            }
            return false;
        }

        /// <summary>
        /// Return a following sibling element of the specified node that has the specified name.  If no such
        /// sibling exists, or if the node is not content-typed, then do not set pageNode or idxNode and
        /// return false.  Assume that the localName has been atomized with respect to this document's name table,
        /// but not the namespaceName.
        /// </summary>
        public static bool GetElementSibling(ref XPathNode[] pageNode, ref int idxNode, string localName, string namespaceName) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            // Elements should not be returned as "siblings" of attributes (namespaces don't link to elements, so don't need to check them)
            if (page[idx].NodeType != XPathNodeType.Attribute) {
                while (true) {
                    idx = page[idx].GetSibling(out page);

                    if (idx == 0)
                        break;

                    if (page[idx].ElementMatch(localName, namespaceName)) {
                        pageNode = page;
                        idxNode = idx;
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Return the first child of the specified node that has the specified type (must be a content type).  If no such
        /// child exists, then do not set pageNode or idxNode and return false.
        /// </summary>
        public static bool GetContentChild(ref XPathNode[] pageNode, ref int idxNode, XPathNodeType typ) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            int mask;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            // Only check children if at least one content-typed child exists
            if (page[idx].HasContentChild) {
                mask = XPathNavigator.GetContentKindMask(typ);

                GetChild(ref page, ref idx);
                do {
                    if (((1 << (int) page[idx].NodeType) & mask) != 0) {
                        // Never return attributes, as Attribute is not a content type
                        if (typ == XPathNodeType.Attribute)
                            return false;

                        pageNode = page;
                        idxNode = idx;
                        return true;
                    }

                    idx = page[idx].GetSibling(out page);
                }
                while (idx != 0);
            }

            return false;
        }

        /// <summary>
        /// Return a following sibling of the specified node that has the specified type.  If no such
        /// sibling exists, then do not set pageNode or idxNode and return false.
        /// </summary>
        public static bool GetContentSibling(ref XPathNode[] pageNode, ref int idxNode, XPathNodeType typ) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            int mask = XPathNavigator.GetContentKindMask(typ);
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            if (page[idx].NodeType != XPathNodeType.Attribute) {
                while (true) {
                    idx = page[idx].GetSibling(out page);

                    if (idx == 0)
                        break;

                    if (((1 << (int) page[idx].NodeType) & mask) != 0) {
                        Debug.Assert(typ != XPathNodeType.Attribute && typ != XPathNodeType.Namespace);
                        pageNode = page;
                        idxNode = idx;
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Return the first preceding sibling of the specified node.  If no such sibling exists, then do not set
        /// pageNode or idxNode and return false.
        /// </summary>
        public static bool GetPreviousContentSibling(ref XPathNode[] pageNode, ref int idxNode) {
            XPathNode[] pageParent = pageNode, pagePrec, pageAnc;
            int idxParent = idxNode, idxPrec, idxAnc;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");
            Debug.Assert(pageNode[idxNode].NodeType != XPathNodeType.Attribute);

            // Since nodes are laid out in document order on pages, the algorithm is:
            //   1. Get parent of current node
            //   2. If no parent, then there is no previous sibling, so return false
            //   3. Get node that immediately precedes the current node in document order
            //   4. If preceding node is parent, then there is no previous sibling, so return false
            //   5. Walk ancestors of preceding node, until parent of current node is found
            idxParent = pageParent[idxParent].GetParent(out pageParent);
            if (idxParent != 0) {
                idxPrec = idxNode - 1;
                if (idxPrec == 0) {
                    // Need to get previous page
                    pagePrec = pageNode[0].PageInfo.PreviousPage;
                    idxPrec = pagePrec.Length - 1;
                }
                else {
                    // Previous node is on the same page
                    pagePrec = pageNode;
                }

                // If parent node is previous node, then no previous sibling
                if (idxParent == idxPrec && pageParent == pagePrec)
                    return false;

                // Find child of parent node by walking ancestor chain
                pageAnc = pagePrec;
                idxAnc = idxPrec;
                do {
                    pagePrec = pageAnc;
                    idxPrec = idxAnc;
                    idxAnc = pageAnc[idxAnc].GetParent(out pageAnc);
                    Debug.Assert(idxAnc != 0 && pageAnc != null);
                }
                while (idxAnc != idxParent || pageAnc != pageParent);

                // We found the previous sibling, but if it's an attribute node, then return false
                if (pagePrec[idxPrec].NodeType != XPathNodeType.Attribute) {
                    pageNode = pagePrec;
                    idxNode = idxPrec;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Return a previous sibling element of the specified node that has the specified name.  If no such
        /// sibling exists, or if the node is not content-typed, then do not set pageNode or idxNode and
        /// return false.  Assume that the localName has been atomized with respect to this document's name table,
        /// but not the namespaceName.
        /// </summary>
        public static bool GetPreviousElementSibling(ref XPathNode[] pageNode, ref int idxNode, string localName, string namespaceName) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            if (page[idx].NodeType != XPathNodeType.Attribute) {
                while (true) {
                    if (!GetPreviousContentSibling(ref page, ref idx))
                        break;

                    if (page[idx].ElementMatch(localName, namespaceName)) {
                        pageNode = page;
                        idxNode = idx;
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Return a previous sibling of the specified node that has the specified type.  If no such
        /// sibling exists, then do not set pageNode or idxNode and return false.
        /// </summary>
        public static bool GetPreviousContentSibling(ref XPathNode[] pageNode, ref int idxNode, XPathNodeType typ) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            int mask = XPathNavigator.GetContentKindMask(typ);
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            while (true) {
                if (!GetPreviousContentSibling(ref page, ref idx))
                    break;

                if (((1 << (int) page[idx].NodeType) & mask) != 0) {
                    pageNode = page;
                    idxNode = idx;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Return the attribute of the specified node that has the specified name.  If no such attribute exists,
        /// then do not set pageNode or idxNode and return false.  Assume that the localName has been atomized with respect
        /// to this document's name table, but not the namespaceName.
        /// </summary>
        public static bool GetAttribute(ref XPathNode[] pageNode, ref int idxNode, string localName, string namespaceName) {
            XPathNode[] page = pageNode;
            int idx = idxNode;
            Debug.Assert(pageNode != null && idxNode != 0, "Cannot pass null argument(s)");

            // Find attribute with specified localName and namespaceName
            if (page[idx].HasAttribute) {
                GetChild(ref page, ref idx);
                do {
                    if (page[idx].NameMatch(localName, namespaceName)) {
                        pageNode = page;
                        idxNode = idx;
                        return true;
                    }
                    idx = page[idx].GetSibling(out page);
                }
                while (idx != 0 && page[idx].NodeType == XPathNodeType.Attribute);
            }

            return false;
        }

        /// <summary>
        /// Get the next non-virtual (not collapsed text, not namespaces) node that follows the specified node in document order.
        /// If no such node exists, then do not set pageNode or idxNode and return false.
        /// </summary>
        public static bool GetFollowing(ref XPathNode[] pageNode, ref int idxNode) {
            XPathNode[] page = pageNode;
            int idx = idxNode;

            do {
                // Next non-virtual node is in next slot within the page
                if (++idx < page[0].PageInfo.NodeCount) {
                    pageNode = page;
                    idxNode = idx;
                    return true;
                }

                // Otherwise, start at the beginning of the next page
                page = page[0].PageInfo.NextPage;
                idx = 0;
            }
            while (page != null);

            return false;
        }

        /// <summary>
        /// Get the next element node that:
        ///   1. Follows the current node in document order (includes descendants, unlike XPath following axis)
        ///   2. Precedes the ending node in document order (if pageEnd is null, then all following nodes in the document are considered)
        ///   3. Has the specified QName
        /// If no such element exists, then do not set pageCurrent or idxCurrent and return false.
        /// Assume that the localName has been atomized with respect to this document's name table, but not the namespaceName.
        /// </summary>
        public static bool GetElementFollowing(ref XPathNode[] pageCurrent, ref int idxCurrent, XPathNode[] pageEnd, int idxEnd, string localName, string namespaceName) {
            XPathNode[] page = pageCurrent;
            int idx = idxCurrent;
            Debug.Assert(pageCurrent != null && idxCurrent != 0, "Cannot pass null argument(s)");

            // If current node is an element having a matching name,
            if (page[idx].NodeType == XPathNodeType.Element && (object) page[idx].LocalName == (object) localName) {
                // Then follow similar element name pointers
                int idxPageEnd = 0;
                int idxPageCurrent;

                if (pageEnd != null) {
                    idxPageEnd = pageEnd[0].PageInfo.PageNumber;
                    idxPageCurrent = page[0].PageInfo.PageNumber;

                    // If ending node is <= starting node in document order, then scan to end of document
                    if (idxPageCurrent > idxPageEnd || (idxPageCurrent == idxPageEnd && idx >= idxEnd))
                        pageEnd = null;
                }

                while (true) {
                    idx = page[idx].GetSimilarElement(out page);

                    if (idx == 0)
                        break;

                    // Only scan to ending node
                    if (pageEnd != null) {
                        idxPageCurrent = page[0].PageInfo.PageNumber;
                        if (idxPageCurrent > idxPageEnd)
                            break;

                        if (idxPageCurrent == idxPageEnd && idx >= idxEnd)
                            break;
                    }

                    if ((object) page[idx].LocalName == (object) localName && page[idx].NamespaceUri == namespaceName)
                        goto FoundNode;
                }

                return false;
            }

            // Since nodes are laid out in document order on pages, scan them sequentially
            // rather than following links.
            idx++;
            do {
                if ((object) page == (object) pageEnd && idx <= idxEnd) {
                    // Only scan to termination point
                    while (idx != idxEnd) {
                        if (page[idx].ElementMatch(localName, namespaceName))
                            goto FoundNode;
                        idx++;
                    }
                    break;
                }
                else {
                    // Scan all nodes in the page
                    while (idx < page[0].PageInfo.NodeCount) {
                        if (page[idx].ElementMatch(localName, namespaceName))
                            goto FoundNode;
                        idx++;
                    }
                }

                page = page[0].PageInfo.NextPage;
                idx = 1;
            }
            while (page != null);

            return false;

        FoundNode:
            // Found match
            pageCurrent = page;
            idxCurrent = idx;
            return true;
        }

        /// <summary>
        /// Get the next node that:
        ///   1. Follows the current node in document order (includes descendants, unlike XPath following axis)
        ///   2. Precedes the ending node in document order (if pageEnd is null, then all following nodes in the document are considered)
        ///   3. Has the specified XPathNodeType (but Attributes and Namespaces never match)
        /// If no such node exists, then do not set pageCurrent or idxCurrent and return false.
        /// </summary>
        public static bool GetContentFollowing(ref XPathNode[] pageCurrent, ref int idxCurrent, XPathNode[] pageEnd, int idxEnd, XPathNodeType typ) {
            XPathNode[] page = pageCurrent;
            int idx = idxCurrent;
            int mask = XPathNavigator.GetContentKindMask(typ);
            Debug.Assert(pageCurrent != null && idxCurrent != 0, "Cannot pass null argument(s)");
            Debug.Assert(typ != XPathNodeType.Text, "Text should be handled by GetTextFollowing in order to take into account collapsed text.");
            Debug.Assert(page[idx].NodeType != XPathNodeType.Attribute, "Current node should never be an attribute or namespace--caller should handle this case.");

            // Since nodes are laid out in document order on pages, scan them sequentially
            // rather than following sibling/child/parent links.
            idx++;
            do {
                if ((object) page == (object) pageEnd && idx <= idxEnd) {
                    // Only scan to termination point
                    while (idx != idxEnd) {
                        if (((1 << (int) page[idx].NodeType) & mask) != 0)
                            goto FoundNode;
                        idx++;
                    }
                    break;
                }
                else {
                    // Scan all nodes in the page
                    while (idx < page[0].PageInfo.NodeCount) {
                        if (((1 << (int) page[idx].NodeType) & mask) != 0)
                            goto FoundNode;
                        idx++;
                    }
                }

                page = page[0].PageInfo.NextPage;
                idx = 1;
            }
            while (page != null);

            return false;

        FoundNode:
            Debug.Assert(!page[idx].IsAttrNmsp, "GetContentFollowing should never return attributes or namespaces.");

            // Found match
            pageCurrent = page;
            idxCurrent = idx;
            return true;
        }

        /// <summary>
        /// Scan all nodes that follow the current node in document order, but precede the ending node in document order.
        /// Return two types of nodes with non-null text:
        ///   1. Element parents of collapsed text nodes (since it is the element parent that has the collapsed text)
        ///   2. Non-collapsed text nodes
        /// If no such node exists, then do not set pageCurrent or idxCurrent and return false.
        /// </summary>
        public static bool GetTextFollowing(ref XPathNode[] pageCurrent, ref int idxCurrent, XPathNode[] pageEnd, int idxEnd) {
            XPathNode[] page = pageCurrent;
            int idx = idxCurrent;
            Debug.Assert(pageCurrent != null && idxCurrent != 0, "Cannot pass null argument(s)");
            Debug.Assert(!page[idx].IsAttrNmsp, "Current node should never be an attribute or namespace--caller should handle this case.");

            // Since nodes are laid out in document order on pages, scan them sequentially
            // rather than following sibling/child/parent links.
            idx++;
            do {
                if ((object) page == (object) pageEnd && idx <= idxEnd) {
                    // Only scan to termination point
                    while (idx != idxEnd) {
                        if (page[idx].IsText || (page[idx].NodeType == XPathNodeType.Element && page[idx].HasCollapsedText))
                            goto FoundNode;
                        idx++;
                    }
                    break;
                }
                else {
                    // Scan all nodes in the page
                    while (idx < page[0].PageInfo.NodeCount) {
                        if (page[idx].IsText || (page[idx].NodeType == XPathNodeType.Element && page[idx].HasCollapsedText))
                            goto FoundNode;
                        idx++;
                    }
                }

                page = page[0].PageInfo.NextPage;
                idx = 1;
            }
            while (page != null);

            return false;

        FoundNode:
            // Found match
            pageCurrent = page;
            idxCurrent = idx;
            return true;
        }

        /// <summary>
        /// Get the next non-virtual (not collapsed text, not namespaces) node that follows the specified node in document order,
        /// but is not a descendant.  If no such node exists, then do not set pageNode or idxNode and return false.
        /// </summary>
        public static bool GetNonDescendant(ref XPathNode[] pageNode, ref int idxNode) {
            XPathNode[] page = pageNode;
            int idx = idxNode;

            // Get page, idx at which to end sequential scan of nodes
            do {
                // If the current node has a sibling,
                if (page[idx].HasSibling) {
                    // Then that is the first non-descendant
                    pageNode = page;
                    idxNode = page[idx].GetSibling(out pageNode);
                    return true;
                }

                // Otherwise, try finding a sibling at the parent level
                idx = page[idx].GetParent(out page);
            }
            while (idx != 0);

            return false;
        }

        /// <summary>
        /// Return the page and index of the first child (attribute or content) of the specified node.
        /// </summary>
        private static void GetChild(ref XPathNode[] pageNode, ref int idxNode) {
            Debug.Assert(pageNode[idxNode].HasAttribute || pageNode[idxNode].HasContentChild, "Caller must check HasAttribute/HasContentChild on parent before calling GetChild.");
            Debug.Assert(pageNode[idxNode].HasAttribute || !pageNode[idxNode].HasCollapsedText, "Text child is virtualized and therefore is not present in the physical node page.");

            if (++idxNode >= pageNode.Length) {
                // Child is first node on next page
                pageNode = pageNode[0].PageInfo.NextPage;
                idxNode = 1;
            }
            // Else child is next node on this page
        }
    }
}