/*
 * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
 * Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
 *
 * 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 "CSSSelector.h"
#include "CSSValueKeywords.h"
#include "CSSValueList.h"
#include <wtf/PassOwnPtr.h>
#include <wtf/text/AtomicString.h>
#include <wtf/text/AtomicStringHash.h>
#include <wtf/text/StringView.h>
#include <wtf/text/WTFString.h>

namespace WebCore {

class CSSParser;
class CSSValue;
class QualifiedName;

enum class CSSParserSelectorCombinator {
    Child,
    DescendantSpace,
    DirectAdjacent,
    IndirectAdjacent,
};

class CSSParserSelector {
    WTF_MAKE_FAST_ALLOCATED;
public:
    static PassOwnPtr<CSSParserSelector> parsePseudoClassSelectorFromStringView(StringView&);
    static PassOwnPtr<CSSParserSelector> parsePseudoElementSelectorFromStringView(StringView&);
    static PassOwnPtr<CSSParserSelector> parsePagePseudoSelector(const AtomicString&);

    CSSParserSelector();
    explicit CSSParserSelector(const QualifiedName&);
    ~CSSParserSelector();

    ALWAYS_INLINE PassOwnPtr<CSSSelector> releaseSelector() { return m_selector.release(); }

    ALWAYS_INLINE void setValue(const AtomicString& value, bool matchLowerCase = false) { m_selector->setValue(value, matchLowerCase); }

    // FIXME-NEWPARSER: These two methods can go away once old parser is gone.
    void setAttribute(const QualifiedName& value, bool isCaseInsensitive) { m_selector->setAttribute(value, isCaseInsensitive); }
    void setAttributeValueMatchingIsCaseInsensitive(bool isCaseInsensitive) { m_selector->setAttributeValueMatchingIsCaseInsensitive(isCaseInsensitive); }

    void setAttribute(const QualifiedName& value, bool convertToLowercase, CSSSelector::AttributeMatchType type) { m_selector->setAttribute(value, convertToLowercase, type); }

    void setArgument(const AtomicString& value) { m_selector->setArgument(value); }
    void setNth(int a, int b) { m_selector->setNth(a, b); }
    ALWAYS_INLINE void setMatch(CSSSelector::Match value) { m_selector->m_match = value; }
    ALWAYS_INLINE void setRelation(CSSSelector::RelationType value) { m_selector->m_relation = value; }
    ALWAYS_INLINE void setForPage() { m_selector->setForPage(); }

    CSSSelector::Match match() const { return m_selector->match(); }
    CSSSelector::PseudoElementType pseudoElementType() const { return m_selector->pseudoElementType(); }
    const CSSSelectorList* selectorList() const { return m_selector->selectorList(); }

    void setPseudoElementType(CSSSelector::PseudoElementType type) { m_selector->setPseudoElementType(type); }
    void setPseudoClassType(CSSSelector::PseudoClassType type) { m_selector->setPseudoClassType(type); }

    void adoptSelectorVector(Vector<OwnPtr<CSSParserSelector>>&& selectorVector);
    void setArgumentList(PassOwnPtr<Vector<AtomicString>>);
    void setSelectorList(PassOwnPtr<CSSSelectorList>);

    ALWAYS_INLINE CSSSelector::PseudoClassType pseudoClassType() const { return m_selector->pseudoClassType(); }
    ALWAYS_INLINE bool isCustomPseudoElement() const { return m_selector->isCustomPseudoElement(); }

    bool isPseudoElementCueFunction() const;

    bool isSimple() const;
    bool matchesPseudoElement() const;

    bool isHostPseudoSelector() const;

    // FIXME-NEWPARSER: "slotted" was removed here for now, since it leads to a combinator
    // connection of ShadowDescendant, and the current shadow DOM code doesn't expect this. When
    // we do fix this issue, make sure to patch the namespace prependTag code to remove the slotted
    // special case, since it will be covered by this function once again.
    bool needsImplicitShadowCombinatorForMatching() const;

    ALWAYS_INLINE CSSParserSelector* tagHistory() const { return m_tagHistory.get(); }
    ALWAYS_INLINE void setTagHistory(PassOwnPtr<CSSParserSelector> selector) { m_tagHistory = selector; }
    void insertTagHistory(CSSSelector::RelationType before, PassOwnPtr<CSSParserSelector>, CSSSelector::RelationType after);
    void appendTagHistory(CSSSelector::RelationType, PassOwnPtr<CSSParserSelector>);
    void appendTagHistory(CSSParserSelectorCombinator, PassOwnPtr<CSSParserSelector>);
    void prependTagSelector(const QualifiedName&, bool tagIsForNamespaceRule = false);
    PassOwnPtr<CSSParserSelector> releaseTagHistory();

private:
    OwnPtr<CSSSelector> m_selector;
    OwnPtr<CSSParserSelector> m_tagHistory;
};

inline bool CSSParserSelector::needsImplicitShadowCombinatorForMatching() const
{
    return match() == CSSSelector::PseudoElement
        && (pseudoElementType() == CSSSelector::PseudoElementWebKitCustom
#if ENABLE(VIDEO_TRACK)
            || pseudoElementType() == CSSSelector::PseudoElementCue
#endif
            || pseudoElementType() == CSSSelector::PseudoElementPart
            || pseudoElementType() == CSSSelector::PseudoElementSlotted
            || pseudoElementType() == CSSSelector::PseudoElementWebKitCustomLegacyPrefixed);
}

inline bool CSSParserSelector::isPseudoElementCueFunction() const
{
#if ENABLE(VIDEO_TRACK)
    return m_selector->m_match == CSSSelector::PseudoElement && m_selector->pseudoElementType() == CSSSelector::PseudoElementCue;
#else
    return false;
#endif
}

}
