• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "platform/heap/Handle.h"
28 #include "wtf/RefPtr.h"
29 
30 namespace WebCore {
31 
32 class CSSRule;
33 class CSSStyleRule;
34 class CSSStyleSheet;
35 class MutableStylePropertySet;
36 class StylePropertySet;
37 
38 class StyleRuleBase : public RefCountedWillBeGarbageCollectedFinalized<StyleRuleBase> {
39     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
40 public:
41     enum Type {
42         Unknown, // Not used.
43         Style,
44         Charset, // Not used. These are internally strings owned by the style sheet.
45         Import,
46         Media,
47         FontFace,
48         Page,
49         Keyframes,
50         Keyframe, // Not used. These are internally non-rule StyleKeyframe objects.
51         Supports = 12,
52         Viewport = 15,
53         Filter = 17
54     };
55 
type()56     Type type() const { return static_cast<Type>(m_type); }
57 
isCharsetRule()58     bool isCharsetRule() const { return type() == Charset; }
isFontFaceRule()59     bool isFontFaceRule() const { return type() == FontFace; }
isKeyframesRule()60     bool isKeyframesRule() const { return type() == Keyframes; }
isMediaRule()61     bool isMediaRule() const { return type() == Media; }
isPageRule()62     bool isPageRule() const { return type() == Page; }
isStyleRule()63     bool isStyleRule() const { return type() == Style; }
isSupportsRule()64     bool isSupportsRule() const { return type() == Supports; }
isViewportRule()65     bool isViewportRule() const { return type() == Viewport; }
isImportRule()66     bool isImportRule() const { return type() == Import; }
isFilterRule()67     bool isFilterRule() const { return type() == Filter; }
68 
69     PassRefPtrWillBeRawPtr<StyleRuleBase> copy() const;
70 
71 #if !ENABLE(OILPAN)
deref()72     void deref()
73     {
74         if (derefBase())
75             destroy();
76     }
77 #endif // !ENABLE(OILPAN)
78 
79     // FIXME: There shouldn't be any need for the null parent version.
80     PassRefPtrWillBeRawPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet = 0) const;
81     PassRefPtrWillBeRawPtr<CSSRule> createCSSOMWrapper(CSSRule* parentRule) const;
82 
83     void trace(Visitor*);
traceAfterDispatch(Visitor *)84     void traceAfterDispatch(Visitor*) { };
85     void finalizeGarbageCollectedObject();
86 
87 protected:
StyleRuleBase(Type type)88     StyleRuleBase(Type type) : m_type(type) { }
StyleRuleBase(const StyleRuleBase & o)89     StyleRuleBase(const StyleRuleBase& o) : m_type(o.m_type) { }
90 
~StyleRuleBase()91     ~StyleRuleBase() { }
92 
93 private:
94     void destroy();
95 
96     PassRefPtrWillBeRawPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const;
97 
98     unsigned m_type : 5;
99 };
100 
101 class StyleRule : public StyleRuleBase {
102     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
103 public:
create()104     static PassRefPtrWillBeRawPtr<StyleRule> create() { return adoptRefWillBeNoop(new StyleRule()); }
105 
106     ~StyleRule();
107 
selectorList()108     const CSSSelectorList& selectorList() const { return m_selectorList; }
properties()109     const StylePropertySet& properties() const { return *m_properties; }
110     MutableStylePropertySet& mutableProperties();
111 
parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector>> & selectors)112     void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); }
wrapperAdoptSelectorList(CSSSelectorList & selectors)113     void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); }
114     void setProperties(PassRefPtr<StylePropertySet>);
115 
copy()116     PassRefPtrWillBeRawPtr<StyleRule> copy() const { return adoptRefWillBeNoop(new StyleRule(*this)); }
117 
118     static unsigned averageSizeInBytes();
119 
traceAfterDispatch(Visitor * visitor)120     void traceAfterDispatch(Visitor* visitor) { StyleRuleBase::traceAfterDispatch(visitor); }
121 
122 private:
123     StyleRule();
124     StyleRule(const StyleRule&);
125 
126     RefPtr<StylePropertySet> m_properties; // Cannot be null.
127     CSSSelectorList m_selectorList;
128 };
129 
130 class StyleRuleFontFace : public StyleRuleBase {
131 public:
create()132     static PassRefPtrWillBeRawPtr<StyleRuleFontFace> create() { return adoptRefWillBeNoop(new StyleRuleFontFace); }
133 
134     ~StyleRuleFontFace();
135 
properties()136     const StylePropertySet& properties() const { return *m_properties; }
137     MutableStylePropertySet& mutableProperties();
138 
139     void setProperties(PassRefPtr<StylePropertySet>);
140 
copy()141     PassRefPtrWillBeRawPtr<StyleRuleFontFace> copy() const { return adoptRefWillBeNoop(new StyleRuleFontFace(*this)); }
142 
traceAfterDispatch(Visitor * visitor)143     void traceAfterDispatch(Visitor* visitor) { StyleRuleBase::traceAfterDispatch(visitor); }
144 
145 private:
146     StyleRuleFontFace();
147     StyleRuleFontFace(const StyleRuleFontFace&);
148 
149     RefPtr<StylePropertySet> m_properties; // Cannot be null.
150 };
151 
152 class StyleRulePage : public StyleRuleBase {
153 public:
create()154     static PassRefPtrWillBeRawPtr<StyleRulePage> create() { return adoptRefWillBeNoop(new StyleRulePage); }
155 
156     ~StyleRulePage();
157 
selector()158     const CSSSelector* selector() const { return m_selectorList.first(); }
properties()159     const StylePropertySet& properties() const { return *m_properties; }
160     MutableStylePropertySet& mutableProperties();
161 
parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector>> & selectors)162     void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); }
wrapperAdoptSelectorList(CSSSelectorList & selectors)163     void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); }
164     void setProperties(PassRefPtr<StylePropertySet>);
165 
copy()166     PassRefPtrWillBeRawPtr<StyleRulePage> copy() const { return adoptRefWillBeNoop(new StyleRulePage(*this)); }
167 
traceAfterDispatch(Visitor * visitor)168     void traceAfterDispatch(Visitor* visitor) { StyleRuleBase::traceAfterDispatch(visitor); }
169 
170 private:
171     StyleRulePage();
172     StyleRulePage(const StyleRulePage&);
173 
174     RefPtr<StylePropertySet> m_properties; // Cannot be null.
175     CSSSelectorList m_selectorList;
176 };
177 
178 class StyleRuleGroup : public StyleRuleBase {
179 public:
childRules()180     const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& childRules() const { return m_childRules; }
181 
182     void wrapperInsertRule(unsigned, PassRefPtrWillBeRawPtr<StyleRuleBase>);
183     void wrapperRemoveRule(unsigned);
184 
185     void traceAfterDispatch(Visitor*);
186 
187 protected:
188     StyleRuleGroup(Type, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRule);
189     StyleRuleGroup(const StyleRuleGroup&);
190 
191 private:
192     WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> > m_childRules;
193 };
194 
195 class StyleRuleMedia : public StyleRuleGroup {
196 public:
create(PassRefPtrWillBeRawPtr<MediaQuerySet> media,WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase>> & adoptRules)197     static PassRefPtrWillBeRawPtr<StyleRuleMedia> create(PassRefPtrWillBeRawPtr<MediaQuerySet> media, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules)
198     {
199         return adoptRefWillBeNoop(new StyleRuleMedia(media, adoptRules));
200     }
201 
mediaQueries()202     MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
203 
copy()204     PassRefPtrWillBeRawPtr<StyleRuleMedia> copy() const { return adoptRefWillBeNoop(new StyleRuleMedia(*this)); }
205 
206     void traceAfterDispatch(Visitor*);
207 
208 private:
209     StyleRuleMedia(PassRefPtrWillBeRawPtr<MediaQuerySet>, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules);
210     StyleRuleMedia(const StyleRuleMedia&);
211 
212     RefPtrWillBeMember<MediaQuerySet> m_mediaQueries;
213 };
214 
215 class StyleRuleSupports : public StyleRuleGroup {
216 public:
create(const String & conditionText,bool conditionIsSupported,WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase>> & adoptRules)217     static PassRefPtrWillBeRawPtr<StyleRuleSupports> create(const String& conditionText, bool conditionIsSupported, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules)
218     {
219         return adoptRefWillBeNoop(new StyleRuleSupports(conditionText, conditionIsSupported, adoptRules));
220     }
221 
conditionText()222     String conditionText() const { return m_conditionText; }
conditionIsSupported()223     bool conditionIsSupported() const { return m_conditionIsSupported; }
copy()224     PassRefPtrWillBeRawPtr<StyleRuleSupports> copy() const { return adoptRefWillBeNoop(new StyleRuleSupports(*this)); }
225 
traceAfterDispatch(Visitor * visitor)226     void traceAfterDispatch(Visitor* visitor) { StyleRuleGroup::traceAfterDispatch(visitor); }
227 
228 private:
229     StyleRuleSupports(const String& conditionText, bool conditionIsSupported, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules);
230     StyleRuleSupports(const StyleRuleSupports&);
231 
232     String m_conditionText;
233     bool m_conditionIsSupported;
234 };
235 
236 class StyleRuleViewport : public StyleRuleBase {
237 public:
create()238     static PassRefPtrWillBeRawPtr<StyleRuleViewport> create() { return adoptRefWillBeNoop(new StyleRuleViewport); }
239 
240     ~StyleRuleViewport();
241 
properties()242     const StylePropertySet& properties() const { return *m_properties; }
243     MutableStylePropertySet& mutableProperties();
244 
245     void setProperties(PassRefPtr<StylePropertySet>);
246 
copy()247     PassRefPtrWillBeRawPtr<StyleRuleViewport> copy() const { return adoptRefWillBeNoop(new StyleRuleViewport(*this)); }
248 
traceAfterDispatch(Visitor * visitor)249     void traceAfterDispatch(Visitor* visitor) { StyleRuleBase::traceAfterDispatch(visitor); }
250 
251 private:
252     StyleRuleViewport();
253     StyleRuleViewport(const StyleRuleViewport&);
254 
255     RefPtr<StylePropertySet> m_properties; // Cannot be null
256 };
257 
258 class StyleRuleFilter : public StyleRuleBase {
259 public:
create(const String & filterName)260     static PassRefPtrWillBeRawPtr<StyleRuleFilter> create(const String& filterName) { return adoptRefWillBeNoop(new StyleRuleFilter(filterName)); }
261 
262     ~StyleRuleFilter();
263 
filterName()264     const String& filterName() const { return m_filterName; }
265 
properties()266     const StylePropertySet& properties() const { return *m_properties; }
267     MutableStylePropertySet& mutableProperties();
268 
269     void setProperties(PassRefPtr<StylePropertySet>);
270 
copy()271     PassRefPtrWillBeRawPtr<StyleRuleFilter> copy() const { return adoptRefWillBeNoop(new StyleRuleFilter(*this)); }
272 
traceAfterDispatch(Visitor * visitor)273     void traceAfterDispatch(Visitor* visitor) { StyleRuleBase::traceAfterDispatch(visitor); }
274 
275 private:
276     StyleRuleFilter(const String&);
277     StyleRuleFilter(const StyleRuleFilter&);
278 
279     String m_filterName;
280     RefPtr<StylePropertySet> m_properties;
281 };
282 
283 #define DEFINE_STYLE_RULE_TYPE_CASTS(Type) \
284     DEFINE_TYPE_CASTS(StyleRule##Type, StyleRuleBase, rule, rule->is##Type##Rule(), rule.is##Type##Rule())
285 
286 DEFINE_TYPE_CASTS(StyleRule, StyleRuleBase, rule, rule->isStyleRule(), rule.isStyleRule());
287 DEFINE_STYLE_RULE_TYPE_CASTS(FontFace);
288 DEFINE_STYLE_RULE_TYPE_CASTS(Page);
289 DEFINE_STYLE_RULE_TYPE_CASTS(Media);
290 DEFINE_STYLE_RULE_TYPE_CASTS(Supports);
291 DEFINE_STYLE_RULE_TYPE_CASTS(Viewport);
292 DEFINE_STYLE_RULE_TYPE_CASTS(Filter);
293 
294 } // namespace WebCore
295 
296 #endif // StyleRule_h
297