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/ContainerNode.h"
30 #include "core/dom/Node.h"
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); }
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);
49 static Node* lastWithin(const ContainerNode&);
50 static Node& lastWithinOrSelf(Node&);
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);
55 // Like previous, but skips children and starts with the next sibling.
56 static Node* previousSkippingChildren(const Node&, const Node* stayWithin = 0);
58 // Like next, but visits parents after their children.
59 static Node* nextPostOrder(const Node&, const Node* stayWithin = 0);
61 // Like previous, but visits parents before their children.
62 static Node* previousPostOrder(const Node&, const Node* stayWithin = 0);
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);
69 static Node* nextAncestorSibling(const Node&);
70 static Node* nextAncestorSibling(const Node&, const Node* stayWithin);
71 static Node& highestAncestorOrSelf(Node&);
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); }
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);
86 template <class NodeType>
87 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current)
89 if (current.hasChildren())
90 return current.firstChild();
91 if (current.nextSibling())
92 return current.nextSibling();
93 return nextAncestorSibling(current);
96 template <class NodeType>
97 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current, const Node* stayWithin)
99 if (current.hasChildren())
100 return current.firstChild();
101 if (current == stayWithin)
103 if (current.nextSibling())
104 return current.nextSibling();
105 return nextAncestorSibling(current, stayWithin);
108 inline Node* NodeTraversal::nextSkippingChildren(const Node& current)
110 if (current.nextSibling())
111 return current.nextSibling();
112 return nextAncestorSibling(current);
115 inline Node* NodeTraversal::nextSkippingChildren(const Node& current, const Node* stayWithin)
117 if (current == stayWithin)
119 if (current.nextSibling())
120 return current.nextSibling();
121 return nextAncestorSibling(current, stayWithin);
124 inline Node& NodeTraversal::highestAncestorOrSelf(Node& current)
126 Node* highest = ¤t;
127 while (highest->parentNode())
128 highest = highest->parentNode();
132 template <class NodeType>
133 inline Node* NodeTraversal::childAtTemplate(NodeType& parent, unsigned index)
135 Node* child = parent.firstChild();
136 while (child && index--)
137 child = child->nextSibling();