Upstream version 7.36.149.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/Node.h"
30
31 namespace WebCore {
32
33 class NodeTraversal {
34 public:
35     // Does a pre-order traversal of the tree to find the next node after this one.
36     // This uses the same order that tags appear in the source file. If the stayWithin
37     // argument is non-null, the traversal will stop once the specified node is reached.
38     // This can be used to restrict traversal to a particular sub-tree.
39     static Node* next(const Node& current) { return traverseNextTemplate(current); }
40     static Node* next(const ContainerNode& current) { return traverseNextTemplate(current); }
41     static Node* next(const Node& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); }
42     static Node* next(const ContainerNode& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); }
43
44     // Like next, but skips children and starts with the next sibling.
45     static Node* nextSkippingChildren(const Node& current) { return traverseNextSkippingChildrenTemplate(current); }
46     static Node* nextSkippingChildren(const ContainerNode& current) { return traverseNextSkippingChildrenTemplate(current); }
47     static Node* nextSkippingChildren(const Node& current, const Node* stayWithin) { return traverseNextSkippingChildrenTemplate(current, stayWithin); }
48     static Node* nextSkippingChildren(const ContainerNode& current, const Node* stayWithin) { return traverseNextSkippingChildrenTemplate(current, stayWithin); }
49
50     // Does a reverse pre-order traversal to find the node that comes before the current one in document order
51     static Node* lastWithin(const ContainerNode&);
52     static Node* previous(const Node&, const Node* stayWithin = 0);
53
54     // Like previous, but skips children and starts with the next sibling.
55     static Node* previousSkippingChildren(const Node&, const Node* stayWithin = 0);
56
57     // Like next, but visits parents after their children.
58     static Node* nextPostOrder(const Node&, const Node* stayWithin = 0);
59
60     // Like previous, but visits parents before their children.
61     static Node* previousPostOrder(const Node&, const Node* stayWithin = 0);
62
63     // Pre-order traversal including the pseudo-elements.
64     static Node* previousIncludingPseudo(const Node&, const Node* stayWithin = 0);
65     static Node* nextIncludingPseudo(const Node&, const Node* stayWithin = 0);
66     static Node* nextIncludingPseudoSkippingChildren(const Node&, const Node* stayWithin = 0);
67
68     static Node* nextAncestorSibling(const Node&);
69     static Node* nextAncestorSibling(const Node&, const Node* stayWithin);
70
71 private:
72     template <class NodeType>
73     static Node* traverseNextTemplate(NodeType&);
74     template <class NodeType>
75     static Node* traverseNextTemplate(NodeType&, const Node* stayWithin);
76     template <class NodeType>
77     static Node* traverseNextSkippingChildrenTemplate(NodeType&);
78     template <class NodeType>
79     static Node* traverseNextSkippingChildrenTemplate(NodeType&, const Node* stayWithin);
80 };
81
82 template <class NodeType>
83 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current)
84 {
85     if (current.firstChild())
86         return current.firstChild();
87     if (current.nextSibling())
88         return current.nextSibling();
89     return nextAncestorSibling(current);
90 }
91
92 template <class NodeType>
93 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current, const Node* stayWithin)
94 {
95     if (current.firstChild())
96         return current.firstChild();
97     if (current == stayWithin)
98         return 0;
99     if (current.nextSibling())
100         return current.nextSibling();
101     return nextAncestorSibling(current, stayWithin);
102 }
103
104 template <class NodeType>
105 inline Node* NodeTraversal::traverseNextSkippingChildrenTemplate(NodeType& current)
106 {
107     if (current.nextSibling())
108         return current.nextSibling();
109     return nextAncestorSibling(current);
110 }
111
112 template <class NodeType>
113 inline Node* NodeTraversal::traverseNextSkippingChildrenTemplate(NodeType& current, const Node* stayWithin)
114 {
115     if (current == stayWithin)
116         return 0;
117     if (current.nextSibling())
118         return current.nextSibling();
119     return nextAncestorSibling(current, stayWithin);
120 }
121
122 } // namespace WebCore
123
124 #endif