/* * 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. * */ #ifndef ElementTraversal_h #define ElementTraversal_h #include "Element.h" #include "NodeTraversal.h" namespace WebCore { template 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&); // 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 static ElementType* firstChildTemplate(CurrentType&); template static ElementType* lastChildTemplate(CurrentType&); template static ElementType* firstWithinTemplate(CurrentType&); template static ElementType* lastWithinTemplate(CurrentType&); template static ElementType* nextTemplate(CurrentType&); template static ElementType* nextTemplate(CurrentType&, const Node* stayWithin); }; class ElementTraversal : public Traversal { 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 inline Element* Traversal::firstWithinTemplate(CurrentType& current) { return firstChildTemplate(current); } template <> template inline Element* Traversal::nextTemplate(CurrentType& current) { Node* node = NodeTraversal::next(current); while (node && !is(*node)) node = NodeTraversal::nextSkippingChildren(*node); return downcast(node); } template <> template inline Element* Traversal::nextTemplate(CurrentType& current, const Node* stayWithin) { Node* node = NodeTraversal::next(current, stayWithin); while (node && !is(*node)) node = NodeTraversal::nextSkippingChildren(*node, stayWithin); return downcast(node); } // Generic versions. template template inline ElementType* Traversal::firstChildTemplate(CurrentType& current) { Node* node = current.firstChild(); while (node && !is(*node)) node = node->nextSibling(); return downcast(node); } template template inline ElementType* Traversal::lastChildTemplate(CurrentType& current) { Node* node = current.lastChild(); while (node && !is(*node)) node = node->previousSibling(); return downcast(node); } template template inline ElementType* Traversal::firstWithinTemplate(CurrentType& current) { Node* node = current.firstChild(); while (node && !is(*node)) node = NodeTraversal::next(*node, ¤t); return downcast(node); } template template inline ElementType* Traversal::lastWithinTemplate(CurrentType& current) { Node* node = NodeTraversal::last(current); while (node && !is(*node)) node = NodeTraversal::previous(*node, ¤t); return downcast(node); } template template inline ElementType* Traversal::nextTemplate(CurrentType& current) { Node* node = NodeTraversal::next(current); while (node && !is(*node)) node = NodeTraversal::next(*node); return downcast(node); } template template inline ElementType* Traversal::nextTemplate(CurrentType& current, const Node* stayWithin) { Node* node = NodeTraversal::next(current, stayWithin); while (node && !is(*node)) node = NodeTraversal::next(*node, stayWithin); return downcast(node); } template inline ElementType* Traversal::previous(const Node& current) { Node* node = NodeTraversal::previous(current); while (node && !is(*node)) node = NodeTraversal::previous(*node); return downcast(node); } template inline ElementType* Traversal::previous(const Node& current, const Node* stayWithin) { Node* node = NodeTraversal::previous(current, stayWithin); while (node && !is(*node)) node = NodeTraversal::previous(*node, stayWithin); return downcast(node); } template inline ElementType* Traversal::nextSibling(const Node& current) { Node* node = current.nextSibling(); while (node && !is(*node)) node = node->nextSibling(); return downcast(node); } template inline ElementType* Traversal::previousSibling(const Node& current) { Node* node = current.previousSibling(); while (node && !is(*node)) node = node->previousSibling(); return downcast(node); } template inline ElementType* Traversal::nextSkippingChildren(const Node& current) { Node* node = NodeTraversal::nextSkippingChildren(current); while (node && !is(*node)) node = NodeTraversal::nextSkippingChildren(*node); return downcast(node); } template inline ElementType* Traversal::nextSkippingChildren(const Node& current, const Node* stayWithin) { Node* node = NodeTraversal::nextSkippingChildren(current, stayWithin); while (node && !is(*node)) node = NodeTraversal::nextSkippingChildren(*node, stayWithin); return downcast(node); } template inline ElementType* Traversal::firstChild(const ContainerNode& current) { return firstChildTemplate(current); } template inline ElementType* Traversal::firstChild(const Node& current) { return firstChildTemplate(current); } template inline ElementType* Traversal::lastChild(const ContainerNode& current) { return lastChildTemplate(current); } template inline ElementType* Traversal::lastChild(const Node& current) { return lastChildTemplate(current); } template inline ElementType* Traversal::firstWithin(const ContainerNode& current) { return firstWithinTemplate(current); } template inline ElementType* Traversal::firstWithin(const Node& current) { return firstWithinTemplate(current); } template inline ElementType* Traversal::lastWithin(const ContainerNode& current) { return lastWithinTemplate(current); } template inline ElementType* Traversal::lastWithin(const Node& current) { return lastWithinTemplate(current); } template inline ElementType* Traversal::next(const ContainerNode& current) { return nextTemplate(current); } template inline ElementType* Traversal::next(const Node& current) { return nextTemplate(current); } template inline ElementType* Traversal::next(const ContainerNode& current, const Node* stayWithin) { return nextTemplate(current, stayWithin); } template inline ElementType* Traversal::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) { Node* node = NodeTraversal::previousIncludingPseudo(current, stayWithin); while (node && !is(*node)) node = NodeTraversal::previousIncludingPseudo(*node, stayWithin); return downcast(node); } inline Element* ElementTraversal::nextIncludingPseudo(const Node& current, const Node* stayWithin) { Node* node = NodeTraversal::nextIncludingPseudo(current, stayWithin); while (node && !is(*node)) node = NodeTraversal::nextIncludingPseudo(*node, stayWithin); return downcast(node); } inline Element* ElementTraversal::nextIncludingPseudoSkippingChildren(const Node& current, const Node* stayWithin) { Node* node = NodeTraversal::nextIncludingPseudoSkippingChildren(current, stayWithin); while (node && !is(*node)) node = NodeTraversal::nextIncludingPseudoSkippingChildren(*node, stayWithin); return downcast(node); } inline Element* ElementTraversal::pseudoAwarePreviousSibling(const Node& current) { Node* node = current.pseudoAwarePreviousSibling(); while (node && !is(*node)) node = node->pseudoAwarePreviousSibling(); return downcast(node); } } #endif