1 /* 2 * (C) 1999-2003 Lars Knoll (knoll (at) kde.org) 3 * (C) 2002-2003 Dirk Mueller (mueller (at) 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 56 Type type() const { return static_cast<Type>(m_type); } 57 58 bool isCharsetRule() const { return type() == Charset; } 59 bool isFontFaceRule() const { return type() == FontFace; } 60 bool isKeyframesRule() const { return type() == Keyframes; } 61 bool isMediaRule() const { return type() == Media; } 62 bool isPageRule() const { return type() == Page; } 63 bool isStyleRule() const { return type() == Style; } 64 bool isSupportsRule() const { return type() == Supports; } 65 bool isViewportRule() const { return type() == Viewport; } 66 bool isImportRule() const { return type() == Import; } 67 bool isFilterRule() const { return type() == Filter; } 68 69 PassRefPtrWillBeRawPtr<StyleRuleBase> copy() const; 70 71 #if !ENABLE(OILPAN) 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*); 84 void traceAfterDispatch(Visitor*) { }; 85 void finalizeGarbageCollectedObject(); 86 87 protected: 88 StyleRuleBase(Type type) : m_type(type) { } 89 StyleRuleBase(const StyleRuleBase& o) : m_type(o.m_type) { } 90 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: 104 static PassRefPtrWillBeRawPtr<StyleRule> create() { return adoptRefWillBeNoop(new StyleRule()); } 105 106 ~StyleRule(); 107 108 const CSSSelectorList& selectorList() const { return m_selectorList; } 109 const StylePropertySet& properties() const { return *m_properties; } 110 MutableStylePropertySet& mutableProperties(); 111 112 void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); } 113 void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); } 114 void setProperties(PassRefPtr<StylePropertySet>); 115 116 PassRefPtrWillBeRawPtr<StyleRule> copy() const { return adoptRefWillBeNoop(new StyleRule(*this)); } 117 118 static unsigned averageSizeInBytes(); 119 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: 132 static PassRefPtrWillBeRawPtr<StyleRuleFontFace> create() { return adoptRefWillBeNoop(new StyleRuleFontFace); } 133 134 ~StyleRuleFontFace(); 135 136 const StylePropertySet& properties() const { return *m_properties; } 137 MutableStylePropertySet& mutableProperties(); 138 139 void setProperties(PassRefPtr<StylePropertySet>); 140 141 PassRefPtrWillBeRawPtr<StyleRuleFontFace> copy() const { return adoptRefWillBeNoop(new StyleRuleFontFace(*this)); } 142 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: 154 static PassRefPtrWillBeRawPtr<StyleRulePage> create() { return adoptRefWillBeNoop(new StyleRulePage); } 155 156 ~StyleRulePage(); 157 158 const CSSSelector* selector() const { return m_selectorList.first(); } 159 const StylePropertySet& properties() const { return *m_properties; } 160 MutableStylePropertySet& mutableProperties(); 161 162 void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); } 163 void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); } 164 void setProperties(PassRefPtr<StylePropertySet>); 165 166 PassRefPtrWillBeRawPtr<StyleRulePage> copy() const { return adoptRefWillBeNoop(new StyleRulePage(*this)); } 167 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: 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: 197 static PassRefPtrWillBeRawPtr<StyleRuleMedia> create(PassRefPtrWillBeRawPtr<MediaQuerySet> media, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules) 198 { 199 return adoptRefWillBeNoop(new StyleRuleMedia(media, adoptRules)); 200 } 201 202 MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); } 203 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: 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 222 String conditionText() const { return m_conditionText; } 223 bool conditionIsSupported() const { return m_conditionIsSupported; } 224 PassRefPtrWillBeRawPtr<StyleRuleSupports> copy() const { return adoptRefWillBeNoop(new StyleRuleSupports(*this)); } 225 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: 238 static PassRefPtrWillBeRawPtr<StyleRuleViewport> create() { return adoptRefWillBeNoop(new StyleRuleViewport); } 239 240 ~StyleRuleViewport(); 241 242 const StylePropertySet& properties() const { return *m_properties; } 243 MutableStylePropertySet& mutableProperties(); 244 245 void setProperties(PassRefPtr<StylePropertySet>); 246 247 PassRefPtrWillBeRawPtr<StyleRuleViewport> copy() const { return adoptRefWillBeNoop(new StyleRuleViewport(*this)); } 248 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: 260 static PassRefPtrWillBeRawPtr<StyleRuleFilter> create(const String& filterName) { return adoptRefWillBeNoop(new StyleRuleFilter(filterName)); } 261 262 ~StyleRuleFilter(); 263 264 const String& filterName() const { return m_filterName; } 265 266 const StylePropertySet& properties() const { return *m_properties; } 267 MutableStylePropertySet& mutableProperties(); 268 269 void setProperties(PassRefPtr<StylePropertySet>); 270 271 PassRefPtrWillBeRawPtr<StyleRuleFilter> copy() const { return adoptRefWillBeNoop(new StyleRuleFilter(*this)); } 272 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