2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2002, 2005, 2006, 2008, 2012 Apple Inc. All rights reserved.
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.
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.
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.
23 #include "core/css/StyleRule.h"
25 #include "RuntimeEnabledFeatures.h"
26 #include "core/css/CSSFilterRule.h"
27 #include "core/css/CSSFontFaceRule.h"
28 #include "core/css/CSSImportRule.h"
29 #include "core/css/CSSKeyframesRule.h"
30 #include "core/css/CSSMediaRule.h"
31 #include "core/css/CSSPageRule.h"
32 #include "core/css/CSSRegionRule.h"
33 #include "core/css/CSSStyleRule.h"
34 #include "core/css/CSSSupportsRule.h"
35 #include "core/css/CSSViewportRule.h"
36 #include "core/css/StylePropertySet.h"
37 #include "core/css/StyleRuleImport.h"
41 struct SameSizeAsStyleRuleBase : public WTF::RefCountedBase {
45 COMPILE_ASSERT(sizeof(StyleRuleBase) <= sizeof(SameSizeAsStyleRuleBase), StyleRuleBase_should_stay_small);
47 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet) const
49 return createCSSOMWrapper(parentSheet, 0);
52 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSRule* parentRule) const
54 return createCSSOMWrapper(0, parentRule);
57 void StyleRuleBase::destroy()
61 delete toStyleRule(this);
64 delete toStyleRulePage(this);
67 delete toStyleRuleFontFace(this);
70 delete toStyleRuleMedia(this);
73 delete toStyleRuleSupports(this);
76 delete toStyleRuleRegion(this);
79 delete toStyleRuleImport(this);
82 delete toStyleRuleKeyframes(this);
85 delete toStyleRuleViewport(this);
88 delete toStyleRuleFilter(this);
99 PassRefPtr<StyleRuleBase> StyleRuleBase::copy() const
103 return toStyleRule(this)->copy();
105 return toStyleRulePage(this)->copy();
107 return toStyleRuleFontFace(this)->copy();
109 return toStyleRuleMedia(this)->copy();
111 return toStyleRuleSupports(this)->copy();
113 return toStyleRuleRegion(this)->copy();
115 // FIXME: Copy import rules.
116 ASSERT_NOT_REACHED();
119 return toStyleRuleKeyframes(this)->copy();
121 return toStyleRuleViewport(this)->copy();
123 return toStyleRuleFilter(this)->copy();
127 ASSERT_NOT_REACHED();
130 ASSERT_NOT_REACHED();
134 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const
136 RefPtr<CSSRule> rule;
137 StyleRuleBase* self = const_cast<StyleRuleBase*>(this);
140 rule = CSSStyleRule::create(toStyleRule(self), parentSheet);
143 rule = CSSPageRule::create(toStyleRulePage(self), parentSheet);
146 rule = CSSFontFaceRule::create(toStyleRuleFontFace(self), parentSheet);
149 rule = CSSMediaRule::create(toStyleRuleMedia(self), parentSheet);
152 rule = CSSSupportsRule::create(toStyleRuleSupports(self), parentSheet);
155 rule = CSSRegionRule::create(toStyleRuleRegion(self), parentSheet);
158 rule = CSSImportRule::create(toStyleRuleImport(self), parentSheet);
161 rule = CSSKeyframesRule::create(toStyleRuleKeyframes(self), parentSheet);
164 rule = CSSViewportRule::create(toStyleRuleViewport(self), parentSheet);
167 rule = CSSFilterRule::create(toStyleRuleFilter(self), parentSheet);
172 ASSERT_NOT_REACHED();
176 rule->setParentRule(parentRule);
177 return rule.release();
180 unsigned StyleRule::averageSizeInBytes()
182 return sizeof(StyleRule) + sizeof(CSSSelector) + StylePropertySet::averageSizeInBytes();
185 StyleRule::StyleRule()
186 : StyleRuleBase(Style)
190 StyleRule::StyleRule(const StyleRule& o)
192 , m_properties(o.m_properties->mutableCopy())
193 , m_selectorList(o.m_selectorList)
197 StyleRule::~StyleRule()
201 MutableStylePropertySet* StyleRule::mutableProperties()
203 if (!m_properties->isMutable())
204 m_properties = m_properties->mutableCopy();
205 return toMutableStylePropertySet(m_properties);
208 void StyleRule::setProperties(PassRefPtr<StylePropertySet> properties)
210 m_properties = properties;
213 StyleRulePage::StyleRulePage()
214 : StyleRuleBase(Page)
218 StyleRulePage::StyleRulePage(const StyleRulePage& o)
220 , m_properties(o.m_properties->mutableCopy())
221 , m_selectorList(o.m_selectorList)
225 StyleRulePage::~StyleRulePage()
229 MutableStylePropertySet* StyleRulePage::mutableProperties()
231 if (!m_properties->isMutable())
232 m_properties = m_properties->mutableCopy();
233 return toMutableStylePropertySet(m_properties);
236 void StyleRulePage::setProperties(PassRefPtr<StylePropertySet> properties)
238 m_properties = properties;
241 StyleRuleFontFace::StyleRuleFontFace()
242 : StyleRuleBase(FontFace)
246 StyleRuleFontFace::StyleRuleFontFace(const StyleRuleFontFace& o)
248 , m_properties(o.m_properties->mutableCopy())
252 StyleRuleFontFace::~StyleRuleFontFace()
256 MutableStylePropertySet* StyleRuleFontFace::mutableProperties()
258 if (!m_properties->isMutable())
259 m_properties = m_properties->mutableCopy();
260 return toMutableStylePropertySet(m_properties);
263 void StyleRuleFontFace::setProperties(PassRefPtr<StylePropertySet> properties)
265 m_properties = properties;
268 StyleRuleGroup::StyleRuleGroup(Type type, Vector<RefPtr<StyleRuleBase> >& adoptRule)
269 : StyleRuleBase(type)
271 m_childRules.swap(adoptRule);
274 StyleRuleGroup::StyleRuleGroup(const StyleRuleGroup& o)
276 , m_childRules(o.m_childRules.size())
278 for (unsigned i = 0; i < m_childRules.size(); ++i)
279 m_childRules[i] = o.m_childRules[i]->copy();
282 void StyleRuleGroup::wrapperInsertRule(unsigned index, PassRefPtr<StyleRuleBase> rule)
284 m_childRules.insert(index, rule);
287 void StyleRuleGroup::wrapperRemoveRule(unsigned index)
289 m_childRules.remove(index);
292 StyleRuleMedia::StyleRuleMedia(PassRefPtr<MediaQuerySet> media, Vector<RefPtr<StyleRuleBase> >& adoptRules)
293 : StyleRuleGroup(Media, adoptRules)
294 , m_mediaQueries(media)
298 StyleRuleMedia::StyleRuleMedia(const StyleRuleMedia& o)
301 if (o.m_mediaQueries)
302 m_mediaQueries = o.m_mediaQueries->copy();
305 StyleRuleSupports::StyleRuleSupports(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase> >& adoptRules)
306 : StyleRuleGroup(Supports, adoptRules)
307 , m_conditionText(conditionText)
308 , m_conditionIsSupported(conditionIsSupported)
312 StyleRuleSupports::StyleRuleSupports(const StyleRuleSupports& o)
314 , m_conditionText(o.m_conditionText)
315 , m_conditionIsSupported(o.m_conditionIsSupported)
319 StyleRuleRegion::StyleRuleRegion(Vector<OwnPtr<CSSParserSelector> >* selectors, Vector<RefPtr<StyleRuleBase> >& adoptRules)
320 : StyleRuleGroup(Region, adoptRules)
322 ASSERT(RuntimeEnabledFeatures::cssRegionsEnabled());
323 m_selectorList.adoptSelectorVector(*selectors);
326 StyleRuleRegion::StyleRuleRegion(const StyleRuleRegion& o)
328 , m_selectorList(o.m_selectorList)
330 ASSERT(RuntimeEnabledFeatures::cssRegionsEnabled());
333 StyleRuleViewport::StyleRuleViewport()
334 : StyleRuleBase(Viewport)
338 StyleRuleViewport::StyleRuleViewport(const StyleRuleViewport& o)
340 , m_properties(o.m_properties->mutableCopy())
344 StyleRuleViewport::~StyleRuleViewport()
348 MutableStylePropertySet* StyleRuleViewport::mutableProperties()
350 if (!m_properties->isMutable())
351 m_properties = m_properties->mutableCopy();
352 return toMutableStylePropertySet(m_properties);
355 void StyleRuleViewport::setProperties(PassRefPtr<StylePropertySet> properties)
357 m_properties = properties;
360 StyleRuleFilter::StyleRuleFilter(const String& filterName)
361 : StyleRuleBase(Filter)
362 , m_filterName(filterName)
366 StyleRuleFilter::StyleRuleFilter(const StyleRuleFilter& o)
368 , m_filterName(o.m_filterName)
369 , m_properties(o.m_properties->mutableCopy())
373 StyleRuleFilter::~StyleRuleFilter()
377 MutableStylePropertySet* StyleRuleFilter::mutableProperties()
379 if (!m_properties->isMutable())
380 m_properties = m_properties->mutableCopy();
381 return toMutableStylePropertySet(m_properties);
384 void StyleRuleFilter::setProperties(PassRefPtr<StylePropertySet> properties)
386 m_properties = properties;
389 } // namespace WebCore