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