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 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25#pragma once
26
27#include "ContainerNode.h"
28#include "Text.h"
29
30namespace WebCore {
31namespace NodeTraversal {
32
33// Does a pre-order traversal of the tree to find the next node after this one.
34// This uses the same order that tags appear in the source file. If the stayWithin
35// argument is non-null, the traversal will stop once the specified node is reached.
36// This can be used to restrict traversal to a particular sub-tree.
37Node* next(const Node&);
38Node* next(const Node&, const Node* stayWithin);
39Node* next(const ContainerNode&);
40Node* next(const ContainerNode&, const Node* stayWithin);
41Node* next(const Text&);
42Node* next(const Text&, const Node* stayWithin);
43
44// Like next, but skips children and starts with the next sibling.
45Node* nextSkippingChildren(const Node&);
46Node* nextSkippingChildren(const Node&, const Node* stayWithin);
47
48// Does a reverse pre-order traversal to find the node that comes before the current one in document order
49WEBCORE_EXPORT Node* last(const ContainerNode&);
50Node* previous(const Node&, const Node* stayWithin = nullptr);
51
52// Like previous, but skips children and starts with the next sibling.
53Node* previousSkippingChildren(const Node&, const Node* stayWithin = nullptr);
54
55// Like next, but visits parents after their children.
56Node* nextPostOrder(const Node&, const Node* stayWithin = nullptr);
57
58// Like previous/previousSkippingChildren, but visits parents before their children.
59Node* previousPostOrder(const Node&, const Node* stayWithin = nullptr);
60Node* previousSkippingChildrenPostOrder(const Node&, const Node* stayWithin = nullptr);
61
62// Pre-order traversal including the pseudo-elements.
63Node* previousIncludingPseudo(const Node&, const Node* = nullptr);
64Node* nextIncludingPseudo(const Node&, const Node* = nullptr);
65Node* nextIncludingPseudoSkippingChildren(const Node&, const Node* = nullptr);
66
67}
68
69namespace NodeTraversal {
70
71WEBCORE_EXPORT Node* nextAncestorSibling(const Node&);
72WEBCORE_EXPORT Node* nextAncestorSibling(const Node&, const Node* stayWithin);
73WEBCORE_EXPORT Node* deepLastChild(Node&);
74
75template <class NodeType>
76inline Node* traverseNextTemplate(NodeType& current)
77{
78 if (current.firstChild())
79 return current.firstChild();
80 if (current.nextSibling())
81 return current.nextSibling();
82 return nextAncestorSibling(current);
83}
84inline Node* next(const Node& current) { return traverseNextTemplate(current); }
85inline Node* next(const ContainerNode& current) { return traverseNextTemplate(current); }
86
87template <class NodeType>
88inline Node* traverseNextTemplate(NodeType& current, const Node* stayWithin)
89{
90 if (current.firstChild())
91 return current.firstChild();
92 if (&current == stayWithin)
93 return nullptr;
94 if (current.nextSibling())
95 return current.nextSibling();
96 return nextAncestorSibling(current, stayWithin);
97}
98inline Node* next(const Node& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); }
99inline Node* next(const ContainerNode& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); }
100
101inline Node* nextSkippingChildren(const Node& current)
102{
103 if (current.nextSibling())
104 return current.nextSibling();
105 return nextAncestorSibling(current);
106}
107
108inline Node* nextSkippingChildren(const Node& current, const Node* stayWithin)
109{
110 if (&current == stayWithin)
111 return nullptr;
112 if (current.nextSibling())
113 return current.nextSibling();
114 return nextAncestorSibling(current, stayWithin);
115}
116
117inline Node* next(const Text& current) { return nextSkippingChildren(current); }
118inline Node* next(const Text& current, const Node* stayWithin) { return nextSkippingChildren(current, stayWithin); }
119
120inline Node* previous(const Node& current, const Node* stayWithin)
121{
122 if (Node* previous = current.previousSibling())
123 return deepLastChild(*previous);
124 if (current.parentNode() == stayWithin)
125 return nullptr;
126 return current.parentNode();
127}
128
129}
130}
131