1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
* Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#pragma once
#include "Element.h"
#include "NodeTraversal.h"
namespace WebCore {
template <typename ElementType>
class Traversal {
public:
// First or last ElementType child of the node.
static ElementType* firstChild(const Node&);
static ElementType* firstChild(const ContainerNode&);
static ElementType* lastChild(const Node&);
static ElementType* lastChild(const ContainerNode&);
// First or last ElementType descendant of the node. For Elements firstWithin is always the same as first child.
static ElementType* firstWithin(const Node&);
static ElementType* firstWithin(const ContainerNode&);
static ElementType* lastWithin(const Node&);
static ElementType* lastWithin(const ContainerNode&);
static ElementType* inclusiveFirstWithin(Node&);
static ElementType* inclusiveFirstWithin(ContainerNode&);
static ElementType* inclusiveLastWithin(Node&);
static ElementType* inclusiveLastWithin(ContainerNode&);
// Pre-order traversal skipping non-ElementType nodes.
static ElementType* next(const Node&);
static ElementType* next(const Node&, const Node* stayWithin);
static ElementType* next(const ContainerNode&);
static ElementType* next(const ContainerNode&, const Node* stayWithin);
static ElementType* previous(const Node&);
static ElementType* previous(const Node&, const Node* stayWithin);
// Next or previous ElementType sibling if there is one.
static ElementType* nextSibling(const Node&);
static ElementType* previousSibling(const Node&);
// Like next, but skips children.
static ElementType* nextSkippingChildren(const Node&);
static ElementType* nextSkippingChildren(const Node&, const Node* stayWithin);
private:
template <typename CurrentType> static ElementType* firstChildTemplate(CurrentType&);
template <typename CurrentType> static ElementType* lastChildTemplate(CurrentType&);
template <typename CurrentType> static ElementType* firstWithinTemplate(CurrentType&);
template <typename CurrentType> static ElementType* lastWithinTemplate(CurrentType&);
template <typename CurrentType> static ElementType* nextTemplate(CurrentType&);
template <typename CurrentType> static ElementType* nextTemplate(CurrentType&, const Node* stayWithin);
};
class ElementTraversal : public Traversal<Element> {
public:
// FIXME: These should go somewhere else.
// Pre-order traversal including the pseudo-elements.
static Element* previousIncludingPseudo(const Node&, const Node* = nullptr);
static Element* nextIncludingPseudo(const Node&, const Node* = nullptr);
static Element* nextIncludingPseudoSkippingChildren(const Node&, const Node* = nullptr);
// Utility function to traverse only the element and pseudo-element siblings of a node.
static Element* pseudoAwarePreviousSibling(const Node&);
};
// Specialized for pure Element to exploit the fact that Elements parent is always either another Element or the root.
template <>
template <typename CurrentType>
inline Element* Traversal<Element>::firstWithinTemplate(CurrentType& current)
{
return firstChildTemplate(current);
}
template <>
template <typename CurrentType>
inline Element* Traversal<Element>::nextTemplate(CurrentType& current)
{
for (auto* node = NodeTraversal::next(current); node; node = NodeTraversal::nextSkippingChildren(*node)) {
if (auto* element = dynamicDowncast<Element>(*node))
return element;
}
return nullptr;
}
template <>
template <typename CurrentType>
inline Element* Traversal<Element>::nextTemplate(CurrentType& current, const Node* stayWithin)
{
for (auto* node = NodeTraversal::next(current, stayWithin); node; node = NodeTraversal::nextSkippingChildren(*node, stayWithin)) {
if (auto* element = dynamicDowncast<Element>(*node))
return element;
}
return nullptr;
}
// Generic versions.
template <typename ElementType>
template <typename CurrentType>
inline ElementType* Traversal<ElementType>::firstChildTemplate(CurrentType& current)
{
for (auto* node = current.firstChild(); node; node = node->nextSibling()) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
template <typename CurrentType>
inline ElementType* Traversal<ElementType>::lastChildTemplate(CurrentType& current)
{
for (auto* node = current.lastChild(); node; node = node->previousSibling()) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
template <typename CurrentType>
inline ElementType* Traversal<ElementType>::firstWithinTemplate(CurrentType& current)
{
for (auto* node = current.firstChild(); node; node = NodeTraversal::next(*node, ¤t)) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
template <typename CurrentType>
inline ElementType* Traversal<ElementType>::lastWithinTemplate(CurrentType& current)
{
for (auto* node = NodeTraversal::last(current); node; node = NodeTraversal::previous(*node, ¤t)) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
template <typename CurrentType>
inline ElementType* Traversal<ElementType>::nextTemplate(CurrentType& current)
{
for (auto* node = NodeTraversal::next(current); node; node = NodeTraversal::next(*node)) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
template <typename CurrentType>
inline ElementType* Traversal<ElementType>::nextTemplate(CurrentType& current, const Node* stayWithin)
{
for (auto* node = NodeTraversal::next(current, stayWithin); node; node = NodeTraversal::next(*node, stayWithin)) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::previous(const Node& current)
{
for (auto* node = NodeTraversal::previous(current); node; node = NodeTraversal::previous(*node)) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::previous(const Node& current, const Node* stayWithin)
{
for (auto* node = NodeTraversal::previous(current, stayWithin); node; node = NodeTraversal::previous(*node, stayWithin)) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::nextSibling(const Node& current)
{
for (auto* node = current.nextSibling(); node; node = node->nextSibling()) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::previousSibling(const Node& current)
{
for (auto* node = current.previousSibling(); node; node = node->previousSibling()) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::nextSkippingChildren(const Node& current)
{
for (auto* node = NodeTraversal::nextSkippingChildren(current); node; node = NodeTraversal::nextSkippingChildren(*node)) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::nextSkippingChildren(const Node& current, const Node* stayWithin)
{
for (auto* node = NodeTraversal::nextSkippingChildren(current, stayWithin); node; node = NodeTraversal::nextSkippingChildren(*node, stayWithin)) {
if (auto* element = dynamicDowncast<ElementType>(*node))
return element;
}
return nullptr;
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::firstChild(const ContainerNode& current) { return firstChildTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::firstChild(const Node& current) { return firstChildTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::lastChild(const ContainerNode& current) { return lastChildTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::lastChild(const Node& current) { return lastChildTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::inclusiveFirstWithin(ContainerNode& current)
{
if (auto* element = dynamicDowncast<ElementType>(current))
return element;
return firstWithin(current);
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::inclusiveFirstWithin(Node& current)
{
if (auto* element = dynamicDowncast<ElementType>(current))
return element;
return firstWithin(current);
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::inclusiveLastWithin(ContainerNode& current)
{
if (auto* element = dynamicDowncast<ElementType>(current))
return element;
return lastWithin(current);
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::inclusiveLastWithin(Node& current)
{
if (auto* element = dynamicDowncast<ElementType>(current))
return element;
return lastWithin(current);
}
template <typename ElementType>
inline ElementType* Traversal<ElementType>::firstWithin(const ContainerNode& current) { return firstWithinTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::firstWithin(const Node& current) { return firstWithinTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::lastWithin(const ContainerNode& current) { return lastWithinTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::lastWithin(const Node& current) { return lastWithinTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::next(const ContainerNode& current) { return nextTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::next(const Node& current) { return nextTemplate(current); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::next(const ContainerNode& current, const Node* stayWithin) { return nextTemplate(current, stayWithin); }
template <typename ElementType>
inline ElementType* Traversal<ElementType>::next(const Node& current, const Node* stayWithin) { return nextTemplate(current, stayWithin); }
// FIXME: These should go somewhere else.
inline Element* ElementTraversal::previousIncludingPseudo(const Node& current, const Node* stayWithin)
{
for (auto* node = NodeTraversal::previousIncludingPseudo(current, stayWithin); node; node = NodeTraversal::previousIncludingPseudo(*node, stayWithin)) {
if (auto* element = dynamicDowncast<Element>(*node))
return element;
}
return nullptr;
}
inline Element* ElementTraversal::nextIncludingPseudo(const Node& current, const Node* stayWithin)
{
for (auto* node = NodeTraversal::nextIncludingPseudo(current, stayWithin); node; node = NodeTraversal::nextIncludingPseudo(*node, stayWithin)) {
if (auto* element = dynamicDowncast<Element>(*node))
return element;
}
return nullptr;
}
inline Element* ElementTraversal::nextIncludingPseudoSkippingChildren(const Node& current, const Node* stayWithin)
{
for (auto* node = NodeTraversal::nextIncludingPseudoSkippingChildren(current, stayWithin); node; node = NodeTraversal::nextIncludingPseudoSkippingChildren(*node, stayWithin)) {
if (auto* element = dynamicDowncast<Element>(*node))
return element;
}
return nullptr;
}
inline Element* ElementTraversal::pseudoAwarePreviousSibling(const Node& current)
{
for (auto* node = current.pseudoAwarePreviousSibling(); node; node = node->pseudoAwarePreviousSibling()) {
if (auto* element = dynamicDowncast<Element>(*node))
return element;
}
return nullptr;
}
} // namespace WebCore
|