/*
 * Copyright (C) 2013 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ElementAncestorIterator_h
#define ElementAncestorIterator_h

#include "ElementIterator.h"

namespace WebCore {

template <typename ElementType>
class ElementAncestorIterator : public ElementIterator<ElementType> {
public:
    ElementAncestorIterator();
    ElementAncestorIterator(ElementType* current);
    ElementAncestorIterator& operator++();
};

template <typename ElementType>
class ElementAncestorConstIterator : public ElementConstIterator<ElementType> {
public:
    ElementAncestorConstIterator();
    ElementAncestorConstIterator(const ElementType* current);
    ElementAncestorConstIterator& operator++();
};

template <typename ElementType>
class ElementAncestorIteratorAdapter {
public:
    ElementAncestorIteratorAdapter(ElementType* descendant);
    ElementAncestorIterator<ElementType> begin();
    ElementAncestorIterator<ElementType> end();
    ElementType* first() { return m_first; }

private:
    ElementType* m_first;
};

template <typename ElementType>
class ElementAncestorConstIteratorAdapter {
public:
    ElementAncestorConstIteratorAdapter(const ElementType* descendant);
    ElementAncestorConstIterator<ElementType> begin() const;
    ElementAncestorConstIterator<ElementType> end() const;
    const ElementType* first() const { return m_first; }

private:
    const ElementType* m_first;
};

ElementAncestorIteratorAdapter<Element> elementLineage(Element* first);
ElementAncestorConstIteratorAdapter<Element> elementLineage(const Element* first);
ElementAncestorIteratorAdapter<Element> elementAncestors(Element* descendant);
ElementAncestorConstIteratorAdapter<Element> elementAncestors(const Element* descendant);
template <typename ElementType> ElementAncestorIteratorAdapter<ElementType> lineageOfType(Element* first);
template <typename ElementType> ElementAncestorConstIteratorAdapter<ElementType> lineageOfType(const Element* first);
template <typename ElementType> ElementAncestorIteratorAdapter<ElementType> ancestorsOfType(Element* descendant);
template <typename ElementType> ElementAncestorConstIteratorAdapter<ElementType> ancestorsOfType(const Element* descendant);

// ElementAncestorIterator

template <typename ElementType>
inline ElementAncestorIterator<ElementType>::ElementAncestorIterator()
    : ElementIterator<ElementType>(0)
{
}

template <typename ElementType>
inline ElementAncestorIterator<ElementType>::ElementAncestorIterator(ElementType* current)
    : ElementIterator<ElementType>(0, current)
{
}

template <typename ElementType>
inline ElementAncestorIterator<ElementType>& ElementAncestorIterator<ElementType>::operator++()
{
    return static_cast<ElementAncestorIterator<ElementType>&>(ElementIterator<ElementType>::traverseAncestor());
}

// ElementAncestorConstIterator

template <typename ElementType>
inline ElementAncestorConstIterator<ElementType>::ElementAncestorConstIterator()
    : ElementConstIterator<ElementType>(0)
{
}

template <typename ElementType>
inline ElementAncestorConstIterator<ElementType>::ElementAncestorConstIterator(const ElementType* current)
    : ElementConstIterator<ElementType>(0, current)
{
}

template <typename ElementType>
inline ElementAncestorConstIterator<ElementType>& ElementAncestorConstIterator<ElementType>::operator++()
{
    return static_cast<ElementAncestorConstIterator<ElementType>&>(ElementConstIterator<ElementType>::traverseAncestor());
}

// ElementAncestorIteratorAdapter

template <typename ElementType>
inline ElementAncestorIteratorAdapter<ElementType>::ElementAncestorIteratorAdapter(ElementType* first)
    : m_first(first)
{
}

template <typename ElementType>
inline ElementAncestorIterator<ElementType> ElementAncestorIteratorAdapter<ElementType>::begin()
{
    return ElementAncestorIterator<ElementType>(m_first);
}

template <typename ElementType>
inline ElementAncestorIterator<ElementType> ElementAncestorIteratorAdapter<ElementType>::end()
{
    return ElementAncestorIterator<ElementType>();
}

// ElementAncestorConstIteratorAdapter

template <typename ElementType>
inline ElementAncestorConstIteratorAdapter<ElementType>::ElementAncestorConstIteratorAdapter(const ElementType* first)
    : m_first(first)
{
}

template <typename ElementType>
inline ElementAncestorConstIterator<ElementType> ElementAncestorConstIteratorAdapter<ElementType>::begin() const
{
    return ElementAncestorConstIterator<ElementType>(m_first);
}

template <typename ElementType>
inline ElementAncestorConstIterator<ElementType> ElementAncestorConstIteratorAdapter<ElementType>::end() const
{
    return ElementAncestorConstIterator<ElementType>();
}

// Standalone functions

inline ElementAncestorIteratorAdapter<Element> elementLineage(Element* first)
{
    return ElementAncestorIteratorAdapter<Element>(first);
}

inline ElementAncestorConstIteratorAdapter<Element> elementLineage(const Element* first)
{
    return ElementAncestorConstIteratorAdapter<Element>(first);
}

inline ElementAncestorIteratorAdapter<Element> elementAncestors(Element* descendant)
{
    return ElementAncestorIteratorAdapter<Element>(descendant->parentElement());
}

inline ElementAncestorConstIteratorAdapter<Element> elementAncestors(const Element* descendant)
{
    return ElementAncestorConstIteratorAdapter<Element>(descendant->parentElement());
}

template <typename ElementType>
inline ElementAncestorIteratorAdapter<ElementType> lineageOfType(ElementType* first)
{
    return ElementAncestorIteratorAdapter<ElementType>(first);
}

template <typename ElementType>
inline ElementAncestorConstIteratorAdapter<ElementType> lineageOfType(const ElementType* first)
{
    return ElementAncestorConstIteratorAdapter<ElementType>(first);
}

template <typename ElementType>
inline ElementAncestorIteratorAdapter<ElementType> ancestorsOfType(Element* descendant)
{
    ElementType* first = findElementAncestorOfType<ElementType>(*descendant);
    return ElementAncestorIteratorAdapter<ElementType>(first);
}

template <typename ElementType>
inline ElementAncestorConstIteratorAdapter<ElementType> ancestorsOfType(const Element* descendant)
{
    const ElementType* first = findElementAncestorOfType<const ElementType>(*descendant);
    return ElementAncestorConstIteratorAdapter<ElementType>(first);
}

}

#endif
