Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / StyleRule.h
1 /*
2  * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3  * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4  * Copyright (C) 2002, 2006, 2008, 2012, 2013 Apple Inc. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifndef StyleRule_h
23 #define StyleRule_h
24
25 #include "core/css/CSSSelectorList.h"
26 #include "core/css/MediaList.h"
27 #include "wtf/RefPtr.h"
28
29 namespace WebCore {
30
31 class CSSRule;
32 class CSSStyleRule;
33 class CSSStyleSheet;
34 class MutableStylePropertySet;
35 class StylePropertySet;
36
37 class StyleRuleBase : public WTF::RefCountedBase {
38     WTF_MAKE_FAST_ALLOCATED;
39 public:
40     enum Type {
41         Unknown, // Not used.
42         Style,
43         Charset, // Not used. These are internally strings owned by the style sheet.
44         Import,
45         Media,
46         FontFace,
47         Page,
48         Keyframes,
49         Keyframe, // Not used. These are internally non-rule StyleKeyframe objects.
50         Supports = 12,
51         Viewport = 15,
52         Filter = 17
53     };
54
55     Type type() const { return static_cast<Type>(m_type); }
56
57     bool isCharsetRule() const { return type() == Charset; }
58     bool isFontFaceRule() const { return type() == FontFace; }
59     bool isKeyframesRule() const { return type() == Keyframes; }
60     bool isMediaRule() const { return type() == Media; }
61     bool isPageRule() const { return type() == Page; }
62     bool isStyleRule() const { return type() == Style; }
63     bool isSupportsRule() const { return type() == Supports; }
64     bool isViewportRule() const { return type() == Viewport; }
65     bool isImportRule() const { return type() == Import; }
66     bool isFilterRule() const { return type() == Filter; }
67
68     PassRefPtr<StyleRuleBase> copy() const;
69
70     void deref()
71     {
72         if (derefBase())
73             destroy();
74     }
75
76     // FIXME: There shouldn't be any need for the null parent version.
77     PassRefPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet = 0) const;
78     PassRefPtr<CSSRule> createCSSOMWrapper(CSSRule* parentRule) const;
79
80 protected:
81     StyleRuleBase(Type type) : m_type(type) { }
82     StyleRuleBase(const StyleRuleBase& o) : WTF::RefCountedBase(), m_type(o.m_type) { }
83
84     ~StyleRuleBase() { }
85
86 private:
87     void destroy();
88
89     PassRefPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const;
90
91     unsigned m_type : 5;
92 };
93
94 class StyleRule : public StyleRuleBase {
95     WTF_MAKE_FAST_ALLOCATED;
96 public:
97     static PassRefPtr<StyleRule> create() { return adoptRef(new StyleRule()); }
98
99     ~StyleRule();
100
101     const CSSSelectorList& selectorList() const { return m_selectorList; }
102     const StylePropertySet* properties() const { return m_properties.get(); }
103     MutableStylePropertySet* mutableProperties();
104
105     void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); }
106     void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); }
107     void setProperties(PassRefPtr<StylePropertySet>);
108
109     PassRefPtr<StyleRule> copy() const { return adoptRef(new StyleRule(*this)); }
110
111     static unsigned averageSizeInBytes();
112
113 private:
114     StyleRule();
115     StyleRule(const StyleRule&);
116
117     RefPtr<StylePropertySet> m_properties;
118     CSSSelectorList m_selectorList;
119 };
120
121 class StyleRuleFontFace : public StyleRuleBase {
122 public:
123     static PassRefPtr<StyleRuleFontFace> create() { return adoptRef(new StyleRuleFontFace); }
124
125     ~StyleRuleFontFace();
126
127     const StylePropertySet* properties() const { return m_properties.get(); }
128     MutableStylePropertySet* mutableProperties();
129
130     void setProperties(PassRefPtr<StylePropertySet>);
131
132     PassRefPtr<StyleRuleFontFace> copy() const { return adoptRef(new StyleRuleFontFace(*this)); }
133
134 private:
135     StyleRuleFontFace();
136     StyleRuleFontFace(const StyleRuleFontFace&);
137
138     RefPtr<StylePropertySet> m_properties;
139 };
140
141 class StyleRulePage : public StyleRuleBase {
142 public:
143     static PassRefPtr<StyleRulePage> create() { return adoptRef(new StyleRulePage); }
144
145     ~StyleRulePage();
146
147     const CSSSelector* selector() const { return m_selectorList.first(); }
148     const StylePropertySet* properties() const { return m_properties.get(); }
149     MutableStylePropertySet* mutableProperties();
150
151     void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); }
152     void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); }
153     void setProperties(PassRefPtr<StylePropertySet>);
154
155     PassRefPtr<StyleRulePage> copy() const { return adoptRef(new StyleRulePage(*this)); }
156
157 private:
158     StyleRulePage();
159     StyleRulePage(const StyleRulePage&);
160
161     RefPtr<StylePropertySet> m_properties;
162     CSSSelectorList m_selectorList;
163 };
164
165 class StyleRuleGroup : public StyleRuleBase {
166 public:
167     const Vector<RefPtr<StyleRuleBase> >& childRules() const { return m_childRules; }
168
169     void wrapperInsertRule(unsigned, PassRefPtr<StyleRuleBase>);
170     void wrapperRemoveRule(unsigned);
171
172 protected:
173     StyleRuleGroup(Type, Vector<RefPtr<StyleRuleBase> >& adoptRule);
174     StyleRuleGroup(const StyleRuleGroup&);
175
176 private:
177     Vector<RefPtr<StyleRuleBase> > m_childRules;
178 };
179
180 class StyleRuleMedia : public StyleRuleGroup {
181 public:
182     static PassRefPtr<StyleRuleMedia> create(PassRefPtr<MediaQuerySet> media, Vector<RefPtr<StyleRuleBase> >& adoptRules)
183     {
184         return adoptRef(new StyleRuleMedia(media, adoptRules));
185     }
186
187     MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
188
189     PassRefPtr<StyleRuleMedia> copy() const { return adoptRef(new StyleRuleMedia(*this)); }
190
191 private:
192     StyleRuleMedia(PassRefPtr<MediaQuerySet>, Vector<RefPtr<StyleRuleBase> >& adoptRules);
193     StyleRuleMedia(const StyleRuleMedia&);
194
195     RefPtr<MediaQuerySet> m_mediaQueries;
196 };
197
198 class StyleRuleSupports : public StyleRuleGroup {
199 public:
200     static PassRefPtr<StyleRuleSupports> create(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase> >& adoptRules)
201     {
202         return adoptRef(new StyleRuleSupports(conditionText, conditionIsSupported, adoptRules));
203     }
204
205     String conditionText() const { return m_conditionText; }
206     bool conditionIsSupported() const { return m_conditionIsSupported; }
207     PassRefPtr<StyleRuleSupports> copy() const { return adoptRef(new StyleRuleSupports(*this)); }
208
209 private:
210     StyleRuleSupports(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase> >& adoptRules);
211     StyleRuleSupports(const StyleRuleSupports&);
212
213     String m_conditionText;
214     bool m_conditionIsSupported;
215 };
216
217 class StyleRuleViewport : public StyleRuleBase {
218 public:
219     static PassRefPtr<StyleRuleViewport> create() { return adoptRef(new StyleRuleViewport); }
220
221     ~StyleRuleViewport();
222
223     const StylePropertySet* properties() const { return m_properties.get(); }
224     MutableStylePropertySet* mutableProperties();
225
226     void setProperties(PassRefPtr<StylePropertySet>);
227
228     PassRefPtr<StyleRuleViewport> copy() const { return adoptRef(new StyleRuleViewport(*this)); }
229
230 private:
231     StyleRuleViewport();
232     StyleRuleViewport(const StyleRuleViewport&);
233
234     RefPtr<StylePropertySet> m_properties;
235 };
236
237 class StyleRuleFilter : public StyleRuleBase {
238 public:
239     static PassRefPtr<StyleRuleFilter> create(const String& filterName) { return adoptRef(new StyleRuleFilter(filterName)); }
240
241     ~StyleRuleFilter();
242
243     const String& filterName() const { return m_filterName; }
244
245     const StylePropertySet* properties() const { return m_properties.get(); }
246     MutableStylePropertySet* mutableProperties();
247
248     void setProperties(PassRefPtr<StylePropertySet>);
249
250     PassRefPtr<StyleRuleFilter> copy() const { return adoptRef(new StyleRuleFilter(*this)); }
251
252 private:
253     StyleRuleFilter(const String&);
254     StyleRuleFilter(const StyleRuleFilter&);
255
256     String m_filterName;
257     RefPtr<StylePropertySet> m_properties;
258 };
259
260 #define DEFINE_STYLE_RULE_TYPE_CASTS(Type) \
261     DEFINE_TYPE_CASTS(StyleRule##Type, StyleRuleBase, rule, rule->is##Type##Rule(), rule.is##Type##Rule())
262
263 DEFINE_TYPE_CASTS(StyleRule, StyleRuleBase, rule, rule->isStyleRule(), rule.isStyleRule());
264 DEFINE_STYLE_RULE_TYPE_CASTS(FontFace);
265 DEFINE_STYLE_RULE_TYPE_CASTS(Page);
266 DEFINE_STYLE_RULE_TYPE_CASTS(Media);
267 DEFINE_STYLE_RULE_TYPE_CASTS(Supports);
268 DEFINE_STYLE_RULE_TYPE_CASTS(Viewport);
269 DEFINE_STYLE_RULE_TYPE_CASTS(Filter);
270
271 } // namespace WebCore
272
273 #endif // StyleRule_h