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.
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.
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.
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.
26 #ifndef NodeTraversal_h
27 #define NodeTraversal_h
29 #include "core/dom/Node.h"
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); }
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); }
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);
54 // Like previous, but skips children and starts with the next sibling.
55 static Node* previousSkippingChildren(const Node&, const Node* stayWithin = 0);
57 // Like next, but visits parents after their children.
58 static Node* nextPostOrder(const Node&, const Node* stayWithin = 0);
60 // Like previous, but visits parents before their children.
61 static Node* previousPostOrder(const Node&, const Node* stayWithin = 0);
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);
68 static Node* nextAncestorSibling(const Node&);
69 static Node* nextAncestorSibling(const Node&, const Node* stayWithin);
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);
82 template <class NodeType>
83 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current)
85 if (current.firstChild())
86 return current.firstChild();
87 if (current.nextSibling())
88 return current.nextSibling();
89 return nextAncestorSibling(current);
92 template <class NodeType>
93 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current, const Node* stayWithin)
95 if (current.firstChild())
96 return current.firstChild();
97 if (current == stayWithin)
99 if (current.nextSibling())
100 return current.nextSibling();
101 return nextAncestorSibling(current, stayWithin);
104 template <class NodeType>
105 inline Node* NodeTraversal::traverseNextSkippingChildrenTemplate(NodeType& current)
107 if (current.nextSibling())
108 return current.nextSibling();
109 return nextAncestorSibling(current);
112 template <class NodeType>
113 inline Node* NodeTraversal::traverseNextSkippingChildrenTemplate(NodeType& current, const Node* stayWithin)
115 if (current == stayWithin)
117 if (current.nextSibling())
118 return current.nextSibling();
119 return nextAncestorSibling(current, stayWithin);
122 } // namespace WebCore