/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 *           (C) 2001 Peter Kelly (pmk@post.com)
 *           (C) 2001 Dirk Mueller (mueller@kde.org)
 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 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 "CSSPrimitiveValue.h"
#include "CSSValueKeywords.h"
#include "Element.h"
#include "StylePropertySet.h"

namespace WebCore {

class Attribute;

class StyledElement : public Element {
public:
    virtual ~StyledElement();

    void invalidateStyleAttribute();

    const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : 0; }
    
    bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
    bool setInlineStyleProperty(CSSPropertyID, CSSPropertyID identifier, bool important = false);
	bool setInlineStyleProperty(CSSPropertyID, double value, CSSUnitType, bool important = false);
    bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
    bool removeInlineStyleProperty(CSSPropertyID);
    void removeAllInlineStyleProperties();

    void synchronizeStyleAttributeInternal() const;

    WEBCORE_EXPORT CSSStyleDeclaration& cssomStyle();

    // https://html.spec.whatwg.org/#presentational-hints
    const StylePropertySet* presentationAttributeStyle() const;
    virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
    virtual const StylePropertySet* additionalPresentationAttributeStyle() const { return nullptr; }
    virtual void collectExtraStyleForPresentationalHints(MutableStyleProperties&) { }

protected:
    StyledElement(const QualifiedName&, Document*, ConstructionType);

    virtual void attributeChanged(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue, AttributeModificationReason = ModifiedDirectly) OVERRIDE;

    virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }

    void addPropertyToPresentationalHintStyle(MutableStyleProperties&, CSSPropertyID, CSSValueID identifier);
    void addPropertyToPresentationalHintStyle(MutableStyleProperties&, CSSPropertyID, double value, CSSUnitType);
    void addPropertyToPresentationalHintStyle(MutableStyleProperties&, CSSPropertyID, const String& value);

    void addPropertyToPresentationAttributeStyle(MutableStylePropertySet* style, CSSPropertyID propertyID, CSSValueID identifier)
    {
    	addPropertyToPresentationalHintStyle(*style, propertyID, identifier);
    }
    void addPropertyToPresentationAttributeStyle(MutableStylePropertySet* style, CSSPropertyID propertyID, double value, CSSUnitType unit)
    {
    	addPropertyToPresentationalHintStyle(*style, propertyID, value, unit);
    }
    void addPropertyToPresentationAttributeStyle(MutableStylePropertySet* style, CSSPropertyID propertyID, const String& value)
    {
       	addPropertyToPresentationalHintStyle(*style, propertyID, value);
    }

    virtual void addSubresourceAttributeURLs(ListHashSet<KURL>&) const OVERRIDE;

private:
    void styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason);

    void inlineStyleChanged();
    PropertySetCSSStyleDeclaration* inlineStyleCSSOMWrapper();
    void setInlineStyleFromString(const AtomicString&);
    MutableStylePropertySet* ensureMutableInlineStyle();

    void rebuildPresentationAttributeStyle();
};

#if !ENABLE(DUMP_StyledElements)
inline StyledElement::StyledElement(const QualifiedName& name, Document* document, ConstructionType type)
: Element(name, document, type)
{
}
#endif

inline bool isStyledElement(const Node* node)
{
    return node && node->isStyledElement();
}

inline bool isStyledElement(const Node& node)
{
    return node.isStyledElement();
}

NODE_TYPE_CASTS(StyledElement)

template <typename ArgType>
struct ElementTypeCastTraits<const StyledElement, ArgType> {
    static bool is(ArgType& node) { return isStyledElement(node); }
};

inline const StylePropertySet* StyledElement::presentationAttributeStyle() const
{
    if (!elementData())
        return nullptr;
    if (elementData()->m_presentationAttributeStyleIsDirty)
    	const_cast<StyledElement&>(*this).rebuildPresentationAttributeStyle();
    return elementData()->presentationAttributeStyle();
}

} //namespace
