1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)/*
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *           (C) 2001 Dirk Mueller (mueller@kde.org)
5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
7d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Copyright (C) 2014 Samsung Electronics. All rights reserved.
8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * This library is free software; you can redistribute it and/or
10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * modify it under the terms of the GNU Library General Public
11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * License as published by the Free Software Foundation; either
12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * version 2 of the License, or (at your option) any later version.
13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * This library is distributed in the hope that it will be useful,
15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * but WITHOUT ANY WARRANTY; without even the implied warranty of
16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Library General Public License for more details.
18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * You should have received a copy of the GNU Library General Public License
20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * along with this library; see the file COPYING.LIB.  If not, write to
21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Boston, MA 02110-1301, USA.
23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) */
25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#ifndef NodeTraversal_h
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#define NodeTraversal_h
28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
29c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "core/dom/ContainerNode.h"
30e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)#include "core/dom/Node.h"
31926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
32c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
33926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
34d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class NodeTraversal {
35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)public:
36d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Does a pre-order traversal of the tree to find the next node after this one.
37d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // This uses the same order that tags appear in the source file. If the stayWithin
38d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // argument is non-null, the traversal will stop once the specified node is reached.
39d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // This can be used to restrict traversal to a particular sub-tree.
40d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* next(const Node& current) { return traverseNextTemplate(current); }
41d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* next(const ContainerNode& current) { return traverseNextTemplate(current); }
42d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* next(const Node& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); }
43d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* next(const ContainerNode& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); }
44d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
45d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Like next, but skips children and starts with the next sibling.
46c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    static Node* nextSkippingChildren(const Node&);
47c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    static Node* nextSkippingChildren(const Node&, const Node* stayWithin);
48d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
496f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    static Node* lastWithin(const ContainerNode&);
50c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    static Node& lastWithinOrSelf(Node&);
51c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
52c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // Does a reverse pre-order traversal to find the node that comes before the current one in document order
53d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* previous(const Node&, const Node* stayWithin = 0);
54d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
55d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Like previous, but skips children and starts with the next sibling.
56d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* previousSkippingChildren(const Node&, const Node* stayWithin = 0);
57d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
58d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Like next, but visits parents after their children.
59d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* nextPostOrder(const Node&, const Node* stayWithin = 0);
60d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Like previous, but visits parents before their children.
62d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* previousPostOrder(const Node&, const Node* stayWithin = 0);
63d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
64d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Pre-order traversal including the pseudo-elements.
65d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* previousIncludingPseudo(const Node&, const Node* stayWithin = 0);
66d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* nextIncludingPseudo(const Node&, const Node* stayWithin = 0);
67d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* nextIncludingPseudoSkippingChildren(const Node&, const Node* stayWithin = 0);
68d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
69d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* nextAncestorSibling(const Node&);
70d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* nextAncestorSibling(const Node&, const Node* stayWithin);
71c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    static Node& highestAncestorOrSelf(Node&);
72c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
73c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // Children traversal.
74c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    static Node* childAt(const Node& parent, unsigned index) { return childAtTemplate(parent, index); }
75c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    static Node* childAt(const ContainerNode& parent, unsigned index) { return childAtTemplate(parent, index); }
76d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
77d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)private:
78d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    template <class NodeType>
79d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* traverseNextTemplate(NodeType&);
80d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    template <class NodeType>
81d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static Node* traverseNextTemplate(NodeType&, const Node* stayWithin);
82d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    template <class NodeType>
83c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    static Node* childAtTemplate(NodeType&, unsigned);
84d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)};
85926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
86926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)template <class NodeType>
87d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)inline Node* NodeTraversal::traverseNextTemplate(NodeType& current)
88926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
89c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (current.hasChildren())
9051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return current.firstChild();
9151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (current.nextSibling())
9251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return current.nextSibling();
93926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return nextAncestorSibling(current);
94926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
9502772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
96926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)template <class NodeType>
97d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)inline Node* NodeTraversal::traverseNextTemplate(NodeType& current, const Node* stayWithin)
98926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
99c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (current.hasChildren())
10051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return current.firstChild();
101926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (current == stayWithin)
102926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return 0;
10351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (current.nextSibling())
10451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return current.nextSibling();
105926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return nextAncestorSibling(current, stayWithin);
106926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
107926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
108c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)inline Node* NodeTraversal::nextSkippingChildren(const Node& current)
109926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
11051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (current.nextSibling())
11151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return current.nextSibling();
112926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return nextAncestorSibling(current);
113926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
114926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
115c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)inline Node* NodeTraversal::nextSkippingChildren(const Node& current, const Node* stayWithin)
116926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
117926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (current == stayWithin)
118926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return 0;
11951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (current.nextSibling())
12051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return current.nextSibling();
121926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return nextAncestorSibling(current, stayWithin);
122926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
123926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
124c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)inline Node& NodeTraversal::highestAncestorOrSelf(Node& current)
125c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles){
126c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    Node* highest = &current;
127c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    while (highest->parentNode())
128c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        highest = highest->parentNode();
129c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    return *highest;
130c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}
131c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
132c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)template <class NodeType>
133c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)inline Node* NodeTraversal::childAtTemplate(NodeType& parent, unsigned index)
134c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles){
135c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    Node* child = parent.firstChild();
136c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    while (child && index--)
137c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        child = child->nextSibling();
138c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    return child;
139c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}
140c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
141c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
142926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
143926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#endif
144