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/)
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.
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.
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.
26 #include "core/dom/NodeTraversal.h"
28 #include "core/dom/ContainerNode.h"
31 namespace NodeTraversal {
33 Node* previousIncludingPseudo(const Node& current, const Node* stayWithin)
35 if (current == stayWithin)
37 if (Node* previous = current.pseudoAwarePreviousSibling()) {
38 while (previous->pseudoAwareLastChild())
39 previous = previous->pseudoAwareLastChild();
42 return current.parentNode();
45 Node* nextIncludingPseudo(const Node& current, const Node* stayWithin)
47 if (Node* next = current.pseudoAwareFirstChild())
49 if (current == stayWithin)
51 if (Node* next = current.pseudoAwareNextSibling())
53 for (Node* parent = current.parentNode(); parent; parent = parent->parentNode()) {
54 if (parent == stayWithin)
56 if (Node* next = parent->pseudoAwareNextSibling())
62 Node* nextIncludingPseudoSkippingChildren(const Node& current, const Node* stayWithin)
64 if (current == stayWithin)
66 if (Node* next = current.pseudoAwareNextSibling())
68 for (Node* parent = current.parentNode(); parent; parent = parent->parentNode()) {
69 if (parent == stayWithin)
71 if (Node* next = parent->pseudoAwareNextSibling())
77 Node* nextAncestorSibling(const Node& current)
79 ASSERT(!current.nextSibling());
80 for (Node* parent = current.parentNode(); parent; parent = parent->parentNode()) {
81 if (parent->nextSibling())
82 return parent->nextSibling();
87 Node* nextAncestorSibling(const Node& current, const Node* stayWithin)
89 ASSERT(!current.nextSibling());
90 ASSERT(current != stayWithin);
91 for (Node* parent = current.parentNode(); parent; parent = parent->parentNode()) {
92 if (parent == stayWithin)
94 if (parent->nextSibling())
95 return parent->nextSibling();
100 Node* previous(const Node& current, const Node* stayWithin)
102 if (current == stayWithin)
104 if (current.previousSibling()) {
105 Node* previous = current.previousSibling();
106 while (previous->lastChild())
107 previous = previous->lastChild();
110 return current.parentNode();
113 Node* previousSkippingChildren(const Node& current, const Node* stayWithin)
115 if (current == stayWithin)
117 if (current.previousSibling())
118 return current.previousSibling();
119 for (Node* parent = current.parentNode(); parent; parent = parent->parentNode()) {
120 if (parent == stayWithin)
122 if (parent->previousSibling())
123 return parent->previousSibling();
128 Node* nextPostOrder(const Node& current, const Node* stayWithin)
130 if (current == stayWithin)
132 if (!current.nextSibling())
133 return current.parentNode();
134 Node* next = current.nextSibling();
135 while (next->firstChild())
136 next = next->firstChild();
140 static Node* previousAncestorSiblingPostOrder(const Node& current, const Node* stayWithin)
142 ASSERT(!current.previousSibling());
143 for (Node* parent = current.parentNode(); parent; parent = parent->parentNode()) {
144 if (parent == stayWithin)
146 if (parent->previousSibling())
147 return parent->previousSibling();
152 Node* previousPostOrder(const Node& current, const Node* stayWithin)
154 if (current.lastChild())
155 return current.lastChild();
156 if (current == stayWithin)
158 if (current.previousSibling())
159 return current.previousSibling();
160 return previousAncestorSiblingPostOrder(current, stayWithin);