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 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 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#pragma once
27
28#include "CSSParserContext.h"
29#include "CSSStyleDeclaration.h"
30#include "DeprecatedCSSOMValue.h"
31#include <memory>
32#include <wtf/HashMap.h>
33#include <wtf/RefPtr.h>
34#include <wtf/WeakPtr.h>
35
36namespace WebCore {
37
38class CSSRule;
39class CSSProperty;
40class CSSValue;
41class MutableStyleProperties;
42class StyleSheetContents;
43class StyledElement;
44
45class PropertySetCSSStyleDeclaration : public CSSStyleDeclaration {
46 WTF_MAKE_ISO_ALLOCATED(PropertySetCSSStyleDeclaration);
47public:
48 explicit PropertySetCSSStyleDeclaration(MutableStyleProperties& propertySet)
49 : m_propertySet(&propertySet)
50 { }
51
52 virtual void clearParentElement() { ASSERT_NOT_REACHED(); }
53
54 StyleSheetContents* contextStyleSheet() const;
55
56protected:
57 enum MutationType { NoChanges, PropertyChanged };
58
59 virtual CSSParserContext cssParserContext() const;
60
61 MutableStyleProperties* m_propertySet;
62 std::unique_ptr<HashMap<CSSValue*, WeakPtr<DeprecatedCSSOMValue>>> m_cssomValueWrappers;
63
64private:
65 void ref() override;
66 void deref() override;
67
68 CSSRule* parentRule() const override { return nullptr; }
69 unsigned length() const final;
70 String item(unsigned index) const final;
71 RefPtr<DeprecatedCSSOMValue> getPropertyCSSValue(const String& propertyName) final;
72 String getPropertyValue(const String& propertyName) final;
73 String getPropertyPriority(const String& propertyName) final;
74 String getPropertyShorthand(const String& propertyName) final;
75 bool isPropertyImplicit(const String& propertyName) final;
76 ExceptionOr<void> setProperty(const String& propertyName, const String& value, const String& priority) final;
77 ExceptionOr<String> removeProperty(const String& propertyName) final;
78 String cssText() const final;
79 ExceptionOr<void> setCssText(const String&) final;
80 RefPtr<CSSValue> getPropertyCSSValueInternal(CSSPropertyID) final;
81 String getPropertyValueInternal(CSSPropertyID) final;
82 ExceptionOr<bool> setPropertyInternal(CSSPropertyID, const String& value, bool important) final;
83
84 Ref<MutableStyleProperties> copyProperties() const final;
85
86 RefPtr<DeprecatedCSSOMValue> wrapForDeprecatedCSSOM(CSSValue*);
87
88 virtual bool willMutate() WARN_UNUSED_RETURN { return true; }
89 virtual void didMutate(MutationType) { }
90};
91
92class StyleRuleCSSStyleDeclaration final : public PropertySetCSSStyleDeclaration {
93 WTF_MAKE_ISO_ALLOCATED(StyleRuleCSSStyleDeclaration);
94public:
95 static Ref<StyleRuleCSSStyleDeclaration> create(MutableStyleProperties& propertySet, CSSRule& parentRule)
96 {
97 return adoptRef(*new StyleRuleCSSStyleDeclaration(propertySet, parentRule));
98 }
99 virtual ~StyleRuleCSSStyleDeclaration();
100
101 void clearParentRule() { m_parentRule = nullptr; }
102
103 void ref() final;
104 void deref() final;
105
106 void reattach(MutableStyleProperties&);
107
108private:
109 StyleRuleCSSStyleDeclaration(MutableStyleProperties&, CSSRule&);
110
111 CSSStyleSheet* parentStyleSheet() const final;
112
113 CSSRule* parentRule() const final { return m_parentRule; }
114
115 bool willMutate() final WARN_UNUSED_RETURN;
116 void didMutate(MutationType) final;
117 CSSParserContext cssParserContext() const final;
118
119 unsigned m_refCount;
120 CSSRule* m_parentRule;
121};
122
123class InlineCSSStyleDeclaration final : public PropertySetCSSStyleDeclaration {
124 WTF_MAKE_ISO_ALLOCATED(InlineCSSStyleDeclaration);
125public:
126 InlineCSSStyleDeclaration(MutableStyleProperties& propertySet, StyledElement& parentElement)
127 : PropertySetCSSStyleDeclaration(propertySet)
128 , m_parentElement(&parentElement)
129 {
130 }
131
132private:
133 CSSStyleSheet* parentStyleSheet() const final;
134 StyledElement* parentElement() const final { return m_parentElement; }
135 void clearParentElement() final { m_parentElement = nullptr; }
136
137 bool willMutate() final WARN_UNUSED_RETURN;
138 void didMutate(MutationType) final;
139 CSSParserContext cssParserContext() const final;
140
141 StyledElement* m_parentElement;
142};
143
144} // namespace WebCore
145