| 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 | |
| 30 | namespace WebCore { |
| 31 | namespace 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. |
| 37 | Node* next(const Node&); |
| 38 | Node* next(const Node&, const Node* stayWithin); |
| 39 | Node* next(const ContainerNode&); |
| 40 | Node* next(const ContainerNode&, const Node* stayWithin); |
| 41 | Node* next(const Text&); |
| 42 | Node* next(const Text&, const Node* stayWithin); |
| 43 | |
| 44 | // Like next, but skips children and starts with the next sibling. |
| 45 | Node* nextSkippingChildren(const Node&); |
| 46 | Node* 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 |
| 49 | WEBCORE_EXPORT Node* last(const ContainerNode&); |
| 50 | Node* previous(const Node&, const Node* stayWithin = nullptr); |
| 51 | |
| 52 | // Like previous, but skips children and starts with the next sibling. |
| 53 | Node* previousSkippingChildren(const Node&, const Node* stayWithin = nullptr); |
| 54 | |
| 55 | // Like next, but visits parents after their children. |
| 56 | Node* nextPostOrder(const Node&, const Node* stayWithin = nullptr); |
| 57 | |
| 58 | // Like previous/previousSkippingChildren, but visits parents before their children. |
| 59 | Node* previousPostOrder(const Node&, const Node* stayWithin = nullptr); |
| 60 | Node* previousSkippingChildrenPostOrder(const Node&, const Node* stayWithin = nullptr); |
| 61 | |
| 62 | // Pre-order traversal including the pseudo-elements. |
| 63 | Node* previousIncludingPseudo(const Node&, const Node* = nullptr); |
| 64 | Node* nextIncludingPseudo(const Node&, const Node* = nullptr); |
| 65 | Node* nextIncludingPseudoSkippingChildren(const Node&, const Node* = nullptr); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | namespace NodeTraversal { |
| 70 | |
| 71 | WEBCORE_EXPORT Node* nextAncestorSibling(const Node&); |
| 72 | WEBCORE_EXPORT Node* nextAncestorSibling(const Node&, const Node* stayWithin); |
| 73 | WEBCORE_EXPORT Node* deepLastChild(Node&); |
| 74 | |
| 75 | template <class NodeType> |
| 76 | inline 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 | } |
| 84 | inline Node* next(const Node& current) { return traverseNextTemplate(current); } |
| 85 | inline Node* next(const ContainerNode& current) { return traverseNextTemplate(current); } |
| 86 | |
| 87 | template <class NodeType> |
| 88 | inline Node* traverseNextTemplate(NodeType& current, const Node* stayWithin) |
| 89 | { |
| 90 | if (current.firstChild()) |
| 91 | return current.firstChild(); |
| 92 | if (¤t == stayWithin) |
| 93 | return nullptr; |
| 94 | if (current.nextSibling()) |
| 95 | return current.nextSibling(); |
| 96 | return nextAncestorSibling(current, stayWithin); |
| 97 | } |
| 98 | inline Node* next(const Node& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); } |
| 99 | inline Node* next(const ContainerNode& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); } |
| 100 | |
| 101 | inline Node* nextSkippingChildren(const Node& current) |
| 102 | { |
| 103 | if (current.nextSibling()) |
| 104 | return current.nextSibling(); |
| 105 | return nextAncestorSibling(current); |
| 106 | } |
| 107 | |
| 108 | inline Node* nextSkippingChildren(const Node& current, const Node* stayWithin) |
| 109 | { |
| 110 | if (¤t == stayWithin) |
| 111 | return nullptr; |
| 112 | if (current.nextSibling()) |
| 113 | return current.nextSibling(); |
| 114 | return nextAncestorSibling(current, stayWithin); |
| 115 | } |
| 116 | |
| 117 | inline Node* next(const Text& current) { return nextSkippingChildren(current); } |
| 118 | inline Node* next(const Text& current, const Node* stayWithin) { return nextSkippingChildren(current, stayWithin); } |
| 119 | |
| 120 | inline 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 | |