#ifndef ElementTraversal_h
#define ElementTraversal_h
#include "core/dom/Element.h"
#include "core/dom/NodeTraversal.h"
namespace WebCore {
template <class ElementType>
class Traversal {
public:
static ElementType* firstChild(const ContainerNode& current) { return firstChildTemplate(current); }
static ElementType* firstChild(const Node& current) { return firstChildTemplate(current); }
static ElementType* lastChild(const ContainerNode& current) { return lastChildTemplate(current); }
static ElementType* lastChild(const Node& current) { return lastChildTemplate(current); }
static ElementType* firstAncestor(const Node& current);
static ElementType* firstAncestorOrSelf(Node& current) { return firstAncestorOrSelfTemplate(current); }
static ElementType* firstAncestorOrSelf(Element& current) { return firstAncestorOrSelfTemplate(current); }
static ElementType* firstWithin(const ContainerNode& current) { return firstWithinTemplate(current); }
static ElementType* firstWithin(const Node& current) { return firstWithinTemplate(current); }
static ElementType* lastWithin(const ContainerNode& current) { return lastWithinTemplate(current); }
static ElementType* lastWithin(const Node& current) { return lastWithinTemplate(current); }
static ElementType* next(const ContainerNode& current) { return nextTemplate(current); }
static ElementType* next(const Node& current) { return nextTemplate(current); }
static ElementType* next(const ContainerNode& current, const Node* stayWithin) { return nextTemplate(current, stayWithin); }
static ElementType* next(const Node& current, const Node* stayWithin) { return nextTemplate(current, stayWithin); }
static ElementType* previous(const ContainerNode& current) { return previousTemplate(current); }
static ElementType* previous(const Node& current) { return previousTemplate(current); }
static ElementType* previous(const ContainerNode& current, const Node* stayWithin) { return previousTemplate(current, stayWithin); }
static ElementType* previous(const Node& current, const Node* stayWithin) { return previousTemplate(current, stayWithin); }
static ElementType* nextSkippingChildren(const ContainerNode& current) { return nextSkippingChildrenTemplate(current); }
static ElementType* nextSkippingChildren(const Node& current) { return nextSkippingChildrenTemplate(current); }
static ElementType* nextSkippingChildren(const ContainerNode& current, const Node* stayWithin) { return nextSkippingChildrenTemplate(current, stayWithin); }
static ElementType* nextSkippingChildren(const Node& current, const Node* stayWithin) { return nextSkippingChildrenTemplate(current, stayWithin); }
static ElementType* previousIncludingPseudo(const Node&, const Node* stayWithin = 0);
static ElementType* nextIncludingPseudo(const Node&, const Node* stayWithin = 0);
static ElementType* nextIncludingPseudoSkippingChildren(const Node&, const Node* stayWithin = 0);
static ElementType* pseudoAwarePreviousSibling(const Node&);
static ElementType* previousSibling(const Node&);
static ElementType* nextSibling(const Node&);
private:
template <class NodeType>
static ElementType* firstChildTemplate(NodeType&);
template <class NodeType>
static ElementType* lastChildTemplate(NodeType&);
template <class NodeType>
static ElementType* firstAncestorOrSelfTemplate(NodeType&);
template <class NodeType>
static ElementType* firstWithinTemplate(NodeType&);
template <class NodeType>
static ElementType* lastWithinTemplate(NodeType&);
template <class NodeType>
static ElementType* nextTemplate(NodeType&);
template <class NodeType>
static ElementType* nextTemplate(NodeType&, const Node* stayWithin);
template <class NodeType>
static ElementType* previousTemplate(NodeType&);
template <class NodeType>
static ElementType* previousTemplate(NodeType&, const Node* stayWithin);
template <class NodeType>
static ElementType* nextSkippingChildrenTemplate(NodeType&);
template <class NodeType>
static ElementType* nextSkippingChildrenTemplate(NodeType&, const Node* stayWithin);
};
typedef Traversal<Element> ElementTraversal;
template <>
template <class NodeType>
inline Element* Traversal<Element>::firstWithinTemplate(NodeType& current)
{
return firstChildTemplate(current);
}
template <>
template <class NodeType>
inline Element* Traversal<Element>::lastWithinTemplate(NodeType& current)
{
return lastChildTemplate(current);
}
template <>
template <class NodeType>
inline Element* Traversal<Element>::nextTemplate(NodeType& current)
{
Node* node = NodeTraversal::next(current);
while (node && !node->isElementNode())
node = NodeTraversal::nextSkippingChildren(*node);
return toElement(node);
}
template <>
template <class NodeType>
inline Element* Traversal<Element>::nextTemplate(NodeType& current, const Node* stayWithin)
{
Node* node = NodeTraversal::next(current, stayWithin);
while (node && !node->isElementNode())
node = NodeTraversal::nextSkippingChildren(*node, stayWithin);
return toElement(node);
}
template <>
template <class NodeType>
inline Element* Traversal<Element>::previousTemplate(NodeType& current)
{
Node* node = NodeTraversal::previous(current);
while (node && !node->isElementNode())
node = NodeTraversal::previous(*node);
return toElement(node);
}
template <>
template <class NodeType>
inline Element* Traversal<Element>::previousTemplate(NodeType& current, const Node* stayWithin)
{
Node* node = NodeTraversal::previous(current, stayWithin);
while (node && !node->isElementNode())
node = NodeTraversal::previous(*node, stayWithin);
return toElement(node);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::firstChildTemplate(NodeType& current)
{
Node* node = current.firstChild();
while (node && !isElementOfType<const ElementType>(*node))
node = node->nextSibling();
return toElement<ElementType>(node);
}
template <class ElementType>
inline ElementType* Traversal<ElementType>::firstAncestor(const Node& current)
{
ContainerNode* ancestor = current.parentNode();
while (ancestor && !isElementOfType<const ElementType>(*ancestor))
ancestor = ancestor->parentNode();
return toElement<ElementType>(ancestor);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::firstAncestorOrSelfTemplate(NodeType& current)
{
if (isElementOfType<const ElementType>(current))
return &toElement<ElementType>(current);
return firstAncestor(current);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::lastChildTemplate(NodeType& current)
{
Node* node = current.lastChild();
while (node && !isElementOfType<const ElementType>(*node))
node = node->previousSibling();
return toElement<ElementType>(node);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::firstWithinTemplate(NodeType& current)
{
Element* element = Traversal<Element>::firstWithin(current);
while (element && !isElementOfType<const ElementType>(*element))
element = Traversal<Element>::next(*element, ¤t);
return toElement<ElementType>(element);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::lastWithinTemplate(NodeType& current)
{
Element* element = Traversal<Element>::lastWithin(current);
while (element && !isElementOfType<const ElementType>(*element))
element = Traversal<Element>::previous(element, ¤t);
return toElement<ElementType>(element);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::nextTemplate(NodeType& current)
{
Element* element = Traversal<Element>::next(current);
while (element && !isElementOfType<const ElementType>(*element))
element = Traversal<Element>::next(*element);
return toElement<ElementType>(element);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::nextTemplate(NodeType& current, const Node* stayWithin)
{
Element* element = Traversal<Element>::next(current, stayWithin);
while (element && !isElementOfType<const ElementType>(*element))
element = Traversal<Element>::next(*element, stayWithin);
return toElement<ElementType>(element);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::previousTemplate(NodeType& current)
{
Element* element = Traversal<Element>::previous(current);
while (element && !isElementOfType<const ElementType>(*element))
element = Traversal<Element>::previous(*element);
return toElement<ElementType>(element);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::previousTemplate(NodeType& current, const Node* stayWithin)
{
Element* element = Traversal<Element>::previous(current, stayWithin);
while (element && !isElementOfType<const ElementType>(*element))
element = Traversal<Element>::previous(*element, stayWithin);
return toElement<ElementType>(element);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::nextSkippingChildrenTemplate(NodeType& current)
{
Node* node = NodeTraversal::nextSkippingChildren(current);
while (node && !isElementOfType<const ElementType>(*node))
node = NodeTraversal::nextSkippingChildren(*node);
return toElement<ElementType>(node);
}
template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::nextSkippingChildrenTemplate(NodeType& current, const Node* stayWithin)
{
Node* node = NodeTraversal::nextSkippingChildren(current, stayWithin);
while (node && !isElementOfType<const ElementType>(*node))
node = NodeTraversal::nextSkippingChildren(*node, stayWithin);
return toElement<ElementType>(node);
}
template <class ElementType>
inline ElementType* Traversal<ElementType>::previousIncludingPseudo(const Node& current, const Node* stayWithin)
{
Node* node = NodeTraversal::previousIncludingPseudo(current, stayWithin);
while (node && !isElementOfType<const ElementType>(*node))
node = NodeTraversal::previousIncludingPseudo(*node, stayWithin);
return toElement<ElementType>(node);
}
template <class ElementType>
inline ElementType* Traversal<ElementType>::nextIncludingPseudo(const Node& current, const Node* stayWithin)
{
Node* node = NodeTraversal::nextIncludingPseudo(current, stayWithin);
while (node && !isElementOfType<const ElementType>(*node))
node = NodeTraversal::nextIncludingPseudo(*node, stayWithin);
return toElement<ElementType>(node);
}
template <class ElementType>
inline ElementType* Traversal<ElementType>::nextIncludingPseudoSkippingChildren(const Node& current, const Node* stayWithin)
{
Node* node = NodeTraversal::nextIncludingPseudoSkippingChildren(current, stayWithin);
while (node && !isElementOfType<const ElementType>(*node))
node = NodeTraversal::nextIncludingPseudoSkippingChildren(*node, stayWithin);
return toElement<ElementType>(node);
}
template <class ElementType>
inline ElementType* Traversal<ElementType>::pseudoAwarePreviousSibling(const Node& current)
{
Node* node = current.pseudoAwarePreviousSibling();
while (node && !isElementOfType<const ElementType>(*node))
node = node->pseudoAwarePreviousSibling();
return toElement<ElementType>(node);
}
template <class ElementType>
inline ElementType* Traversal<ElementType>::previousSibling(const Node& current)
{
Node* node = current.previousSibling();
while (node && !isElementOfType<const ElementType>(*node))
node = node->previousSibling();
return toElement<ElementType>(node);
}
template <class ElementType>
inline ElementType* Traversal<ElementType>::nextSibling(const Node& current)
{
Node* node = current.nextSibling();
while (node && !isElementOfType<const ElementType>(*node))
node = node->nextSibling();
return toElement<ElementType>(node);
}
}
#endif