Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / NodeTraversal.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
6  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
7  * Copyright (C) 2014 Samsung Electronics. All rights reserved.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB.  If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25
26 #ifndef NodeTraversal_h
27 #define NodeTraversal_h
28
29 #include "core/dom/ContainerNode.h"
30 #include "core/dom/Node.h"
31
32 namespace blink {
33
34 class NodeTraversal {
35 public:
36     // Does a pre-order traversal of the tree to find the next node after this one.
37     // This uses the same order that tags appear in the source file. If the stayWithin
38     // argument is non-null, the traversal will stop once the specified node is reached.
39     // This can be used to restrict traversal to a particular sub-tree.
40     static Node* next(const Node& current) { return traverseNextTemplate(current); }
41     static Node* next(const ContainerNode& current) { return traverseNextTemplate(current); }
42     static Node* next(const Node& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); }
43     static Node* next(const ContainerNode& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); }
44
45     // Like next, but skips children and starts with the next sibling.
46     static Node* nextSkippingChildren(const Node&);
47     static Node* nextSkippingChildren(const Node&, const Node* stayWithin);
48
49     static Node* lastWithin(const ContainerNode&);
50     static Node& lastWithinOrSelf(Node&);
51
52     // Does a reverse pre-order traversal to find the node that comes before the current one in document order
53     static Node* previous(const Node&, const Node* stayWithin = 0);
54
55     // Like previous, but skips children and starts with the next sibling.
56     static Node* previousSkippingChildren(const Node&, const Node* stayWithin = 0);
57
58     // Like next, but visits parents after their children.
59     static Node* nextPostOrder(const Node&, const Node* stayWithin = 0);
60
61     // Like previous, but visits parents before their children.
62     static Node* previousPostOrder(const Node&, const Node* stayWithin = 0);
63
64     // Pre-order traversal including the pseudo-elements.
65     static Node* previousIncludingPseudo(const Node&, const Node* stayWithin = 0);
66     static Node* nextIncludingPseudo(const Node&, const Node* stayWithin = 0);
67     static Node* nextIncludingPseudoSkippingChildren(const Node&, const Node* stayWithin = 0);
68
69     static Node* nextAncestorSibling(const Node&);
70     static Node* nextAncestorSibling(const Node&, const Node* stayWithin);
71     static Node& highestAncestorOrSelf(Node&);
72
73     // Children traversal.
74     static Node* childAt(const Node& parent, unsigned index) { return childAtTemplate(parent, index); }
75     static Node* childAt(const ContainerNode& parent, unsigned index) { return childAtTemplate(parent, index); }
76
77 private:
78     template <class NodeType>
79     static Node* traverseNextTemplate(NodeType&);
80     template <class NodeType>
81     static Node* traverseNextTemplate(NodeType&, const Node* stayWithin);
82     template <class NodeType>
83     static Node* childAtTemplate(NodeType&, unsigned);
84 };
85
86 template <class NodeType>
87 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current)
88 {
89     if (current.hasChildren())
90         return current.firstChild();
91     if (current.nextSibling())
92         return current.nextSibling();
93     return nextAncestorSibling(current);
94 }
95
96 template <class NodeType>
97 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current, const Node* stayWithin)
98 {
99     if (current.hasChildren())
100         return current.firstChild();
101     if (current == stayWithin)
102         return 0;
103     if (current.nextSibling())
104         return current.nextSibling();
105     return nextAncestorSibling(current, stayWithin);
106 }
107
108 inline Node* NodeTraversal::nextSkippingChildren(const Node& current)
109 {
110     if (current.nextSibling())
111         return current.nextSibling();
112     return nextAncestorSibling(current);
113 }
114
115 inline Node* NodeTraversal::nextSkippingChildren(const Node& current, const Node* stayWithin)
116 {
117     if (current == stayWithin)
118         return 0;
119     if (current.nextSibling())
120         return current.nextSibling();
121     return nextAncestorSibling(current, stayWithin);
122 }
123
124 inline Node& NodeTraversal::highestAncestorOrSelf(Node& current)
125 {
126     Node* highest = &current;
127     while (highest->parentNode())
128         highest = highest->parentNode();
129     return *highest;
130 }
131
132 template <class NodeType>
133 inline Node* NodeTraversal::childAtTemplate(NodeType& parent, unsigned index)
134 {
135     Node* child = parent.firstChild();
136     while (child && index--)
137         child = child->nextSibling();
138     return child;
139 }
140
141 } // namespace blink
142
143 #endif