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 "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 Region = 16, 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 isRegionRule() const { return type() == Region; } 65 bool isSupportsRule() const { return type() == Supports; } 66 bool isViewportRule() const { return type() == Viewport; } 67 bool isImportRule() const { return type() == Import; } 68 bool isFilterRule() const { return type() == Filter; } 69 70 PassRefPtr<StyleRuleBase> copy() const; 71 72 void deref() 73 { 74 if (derefBase()) 75 destroy(); 76 } 77 78 // FIXME: There shouldn't be any need for the null parent version. 79 PassRefPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet = 0) const; 80 PassRefPtr<CSSRule> createCSSOMWrapper(CSSRule* parentRule) const; 81 82 protected: 83 StyleRuleBase(Type type) : m_type(type) { } 84 StyleRuleBase(const StyleRuleBase& o) : WTF::RefCountedBase(), m_type(o.m_type) { } 85 86 ~StyleRuleBase() { } 87 88 private: 89 void destroy(); 90 91 PassRefPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const; 92 93 unsigned m_type : 5; 94 }; 95 96 class StyleRule : public StyleRuleBase { 97 WTF_MAKE_FAST_ALLOCATED; 98 public: 99 static PassRefPtr<StyleRule> create() { return adoptRef(new StyleRule()); } 100 101 ~StyleRule(); 102 103 const CSSSelectorList& selectorList() const { return m_selectorList; } 104 const StylePropertySet* properties() const { return m_properties.get(); } 105 MutableStylePropertySet* mutableProperties(); 106 107 void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); } 108 void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); } 109 void setProperties(PassRefPtr<StylePropertySet>); 110 111 PassRefPtr<StyleRule> copy() const { return adoptRef(new StyleRule(*this)); } 112 113 static unsigned averageSizeInBytes(); 114 115 private: 116 StyleRule(); 117 StyleRule(const StyleRule&); 118 119 RefPtr<StylePropertySet> m_properties; 120 CSSSelectorList m_selectorList; 121 }; 122 123 class StyleRuleFontFace : public StyleRuleBase { 124 public: 125 static PassRefPtr<StyleRuleFontFace> create() { return adoptRef(new StyleRuleFontFace); } 126 127 ~StyleRuleFontFace(); 128 129 const StylePropertySet* properties() const { return m_properties.get(); } 130 MutableStylePropertySet* mutableProperties(); 131 132 void setProperties(PassRefPtr<StylePropertySet>); 133 134 PassRefPtr<StyleRuleFontFace> copy() const { return adoptRef(new StyleRuleFontFace(*this)); } 135 136 private: 137 StyleRuleFontFace(); 138 StyleRuleFontFace(const StyleRuleFontFace&); 139 140 RefPtr<StylePropertySet> m_properties; 141 }; 142 143 class StyleRulePage : public StyleRuleBase { 144 public: 145 static PassRefPtr<StyleRulePage> create() { return adoptRef(new StyleRulePage); } 146 147 ~StyleRulePage(); 148 149 const CSSSelector* selector() const { return m_selectorList.first(); } 150 const StylePropertySet* properties() const { return m_properties.get(); } 151 MutableStylePropertySet* mutableProperties(); 152 153 void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); } 154 void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); } 155 void setProperties(PassRefPtr<StylePropertySet>); 156 157 PassRefPtr<StyleRulePage> copy() const { return adoptRef(new StyleRulePage(*this)); } 158 159 private: 160 StyleRulePage(); 161 StyleRulePage(const StyleRulePage&); 162 163 RefPtr<StylePropertySet> m_properties; 164 CSSSelectorList m_selectorList; 165 }; 166 167 class StyleRuleGroup : public StyleRuleBase { 168 public: 169 const Vector<RefPtr<StyleRuleBase> >& childRules() const { return m_childRules; } 170 171 void wrapperInsertRule(unsigned, PassRefPtr<StyleRuleBase>); 172 void wrapperRemoveRule(unsigned); 173 174 protected: 175 StyleRuleGroup(Type, Vector<RefPtr<StyleRuleBase> >& adoptRule); 176 StyleRuleGroup(const StyleRuleGroup&); 177 178 private: 179 Vector<RefPtr<StyleRuleBase> > m_childRules; 180 }; 181 182 class StyleRuleMedia : public StyleRuleGroup { 183 public: 184 static PassRefPtr<StyleRuleMedia> create(PassRefPtr<MediaQuerySet> media, Vector<RefPtr<StyleRuleBase> >& adoptRules) 185 { 186 return adoptRef(new StyleRuleMedia(media, adoptRules)); 187 } 188 189 MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); } 190 191 PassRefPtr<StyleRuleMedia> copy() const { return adoptRef(new StyleRuleMedia(*this)); } 192 193 private: 194 StyleRuleMedia(PassRefPtr<MediaQuerySet>, Vector<RefPtr<StyleRuleBase> >& adoptRules); 195 StyleRuleMedia(const StyleRuleMedia&); 196 197 RefPtr<MediaQuerySet> m_mediaQueries; 198 }; 199 200 class StyleRuleSupports : public StyleRuleGroup { 201 public: 202 static PassRefPtr<StyleRuleSupports> create(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase> >& adoptRules) 203 { 204 return adoptRef(new StyleRuleSupports(conditionText, conditionIsSupported, adoptRules)); 205 } 206 207 String conditionText() const { return m_conditionText; } 208 bool conditionIsSupported() const { return m_conditionIsSupported; } 209 PassRefPtr<StyleRuleSupports> copy() const { return adoptRef(new StyleRuleSupports(*this)); } 210 211 private: 212 StyleRuleSupports(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase> >& adoptRules); 213 StyleRuleSupports(const StyleRuleSupports&); 214 215 String m_conditionText; 216 bool m_conditionIsSupported; 217 }; 218 219 class StyleRuleRegion : public StyleRuleGroup { 220 public: 221 static PassRefPtr<StyleRuleRegion> create(Vector<OwnPtr<CSSParserSelector> >* selectors, Vector<RefPtr<StyleRuleBase> >& adoptRules) 222 { 223 return adoptRef(new StyleRuleRegion(selectors, adoptRules)); 224 } 225 226 const CSSSelectorList& selectorList() const { return m_selectorList; } 227 228 PassRefPtr<StyleRuleRegion> copy() const { return adoptRef(new StyleRuleRegion(*this)); } 229 230 private: 231 StyleRuleRegion(Vector<OwnPtr<CSSParserSelector> >*, Vector<RefPtr<StyleRuleBase> >& adoptRules); 232 StyleRuleRegion(const StyleRuleRegion&); 233 234 CSSSelectorList m_selectorList; 235 }; 236 237 class StyleRuleViewport : public StyleRuleBase { 238 public: 239 static PassRefPtr<StyleRuleViewport> create() { return adoptRef(new StyleRuleViewport); } 240 241 ~StyleRuleViewport(); 242 243 const StylePropertySet* properties() const { return m_properties.get(); } 244 MutableStylePropertySet* mutableProperties(); 245 246 void setProperties(PassRefPtr<StylePropertySet>); 247 248 PassRefPtr<StyleRuleViewport> copy() const { return adoptRef(new StyleRuleViewport(*this)); } 249 250 private: 251 StyleRuleViewport(); 252 StyleRuleViewport(const StyleRuleViewport&); 253 254 RefPtr<StylePropertySet> m_properties; 255 }; 256 257 class StyleRuleFilter : public StyleRuleBase { 258 public: 259 static PassRefPtr<StyleRuleFilter> create(const String& filterName) { return adoptRef(new StyleRuleFilter(filterName)); } 260 261 ~StyleRuleFilter(); 262 263 const String& filterName() const { return m_filterName; } 264 265 const StylePropertySet* properties() const { return m_properties.get(); } 266 MutableStylePropertySet* mutableProperties(); 267 268 void setProperties(PassRefPtr<StylePropertySet>); 269 270 PassRefPtr<StyleRuleFilter> copy() const { return adoptRef(new StyleRuleFilter(*this)); } 271 272 private: 273 StyleRuleFilter(const String&); 274 StyleRuleFilter(const StyleRuleFilter&); 275 276 String m_filterName; 277 RefPtr<StylePropertySet> m_properties; 278 }; 279 280 #define DEFINE_STYLE_RULE_TYPE_CASTS(Type) \ 281 DEFINE_TYPE_CASTS(StyleRule##Type, StyleRuleBase, rule, rule->is##Type##Rule(), rule.is##Type##Rule()) 282 283 DEFINE_TYPE_CASTS(StyleRule, StyleRuleBase, rule, rule->isStyleRule(), rule.isStyleRule()); 284 DEFINE_STYLE_RULE_TYPE_CASTS(FontFace); 285 DEFINE_STYLE_RULE_TYPE_CASTS(Page); 286 DEFINE_STYLE_RULE_TYPE_CASTS(Media); 287 DEFINE_STYLE_RULE_TYPE_CASTS(Supports); 288 DEFINE_STYLE_RULE_TYPE_CASTS(Region); 289 DEFINE_STYLE_RULE_TYPE_CASTS(Viewport); 290 DEFINE_STYLE_RULE_TYPE_CASTS(Filter); 291 292 } // namespace WebCore 293 294 #endif // StyleRule_h 295