1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef PropertySetCSSStyleDeclaration_h
27#define PropertySetCSSStyleDeclaration_h
28
29#include "CSSStyleDeclaration.h"
30#include <wtf/HashMap.h>
31#include <wtf/OwnPtr.h>
32#include <wtf/RefPtr.h>
33
34namespace WebCore {
35
36class CSSRule;
37class CSSProperty;
38class CSSValue;
39class MutableStylePropertySet;
40class StyleSheetContents;
41class StyledElement;
42
43class PropertySetCSSStyleDeclaration : public CSSStyleDeclaration {
44public:
45    PropertySetCSSStyleDeclaration(MutableStylePropertySet* propertySet) : m_propertySet(propertySet) { }
46
47    virtual StyledElement* parentElement() const { return 0; }
48    virtual void clearParentElement() { ASSERT_NOT_REACHED(); }
49    StyleSheetContents* contextStyleSheet() const;
50
51    virtual void ref() OVERRIDE;
52    virtual void deref() OVERRIDE;
53
54private:
55    virtual CSSRule* parentRule() const OVERRIDE { return 0; };
56    virtual unsigned length() const OVERRIDE;
57    virtual String item(unsigned index) const OVERRIDE;
58    virtual PassRefPtr<CSSValue> getPropertyCSSValue(const String& propertyName) OVERRIDE;
59    virtual String getPropertyValue(const String& propertyName) OVERRIDE;
60    virtual String getPropertyPriority(const String& propertyName) OVERRIDE;
61    virtual String getPropertyShorthand(const String& propertyName) OVERRIDE;
62    virtual bool isPropertyImplicit(const String& propertyName) OVERRIDE;
63    virtual void setProperty(const String& propertyName, const String& value, const String& priority, ExceptionCode&) OVERRIDE;
64    virtual String removeProperty(const String& propertyName, ExceptionCode&) OVERRIDE;
65    virtual String cssText() const OVERRIDE;
66    virtual void setCssText(const String&, ExceptionCode&) OVERRIDE;
67    virtual PassRefPtr<CSSValue> getPropertyCSSValueInternal(CSSPropertyID) OVERRIDE;
68    virtual String getPropertyValueInternal(CSSPropertyID) OVERRIDE;
69    virtual void setPropertyInternal(CSSPropertyID, const String& value, bool important, ExceptionCode&) OVERRIDE;
70
71    virtual PassRefPtr<MutableStylePropertySet> copyProperties() const OVERRIDE;
72
73    CSSValue* cloneAndCacheForCSSOM(CSSValue*);
74
75protected:
76    enum MutationType { NoChanges, PropertyChanged };
77    virtual bool willMutate() WARN_UNUSED_RETURN { return true; }
78    virtual void didMutate(MutationType) { }
79
80    MutableStylePropertySet* m_propertySet;
81    OwnPtr<HashMap<CSSValue*, RefPtr<CSSValue> > > m_cssomCSSValueClones;
82};
83
84class StyleRuleCSSStyleDeclaration : public PropertySetCSSStyleDeclaration
85{
86public:
87    static PassRefPtr<StyleRuleCSSStyleDeclaration> create(MutableStylePropertySet* propertySet, CSSRule* parentRule)
88    {
89        return adoptRef(new StyleRuleCSSStyleDeclaration(propertySet, parentRule));
90    }
91    virtual ~StyleRuleCSSStyleDeclaration();
92
93    void clearParentRule() { m_parentRule = 0; }
94
95    virtual void ref() OVERRIDE;
96    virtual void deref() OVERRIDE;
97
98    void reattach(MutableStylePropertySet*);
99
100private:
101    StyleRuleCSSStyleDeclaration(MutableStylePropertySet*, CSSRule*);
102
103    virtual CSSStyleSheet* parentStyleSheet() const OVERRIDE;
104
105    virtual CSSRule* parentRule() const OVERRIDE { return m_parentRule;  }
106
107    virtual bool willMutate() override WARN_UNUSED_RETURN;
108    virtual void didMutate(MutationType) OVERRIDE;
109
110    unsigned m_refCount;
111    CSSRule* m_parentRule;
112};
113
114class InlineCSSStyleDeclaration : public PropertySetCSSStyleDeclaration
115{
116public:
117    InlineCSSStyleDeclaration(MutableStylePropertySet* propertySet, StyledElement* parentElement)
118        : PropertySetCSSStyleDeclaration(propertySet)
119        , m_parentElement(parentElement)
120    {
121    }
122
123private:
124    virtual CSSStyleSheet* parentStyleSheet() const OVERRIDE;
125    virtual StyledElement* parentElement() const OVERRIDE { return m_parentElement; }
126    virtual void clearParentElement() OVERRIDE { m_parentElement = 0; }
127
128    virtual void didMutate(MutationType) OVERRIDE;
129
130    StyledElement* m_parentElement;
131};
132
133} // namespace WebCore
134
135#endif
136