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 "core/css/CSSFilterRule.h"
26 #include "core/css/CSSFontFaceRule.h"
27 #include "core/css/CSSImportRule.h"
28 #include "core/css/CSSKeyframesRule.h"
29 #include "core/css/CSSMediaRule.h"
30 #include "core/css/CSSPageRule.h"
31 #include "core/css/CSSStyleRule.h"
32 #include "core/css/CSSSupportsRule.h"
33 #include "core/css/CSSViewportRule.h"
34 #include "core/css/StylePropertySet.h"
35 #include "core/css/StyleRuleImport.h"
39 struct SameSizeAsStyleRuleBase : public WTF::RefCountedBase {
43 COMPILE_ASSERT(sizeof(StyleRuleBase) <= sizeof(SameSizeAsStyleRuleBase), StyleRuleBase_should_stay_small);
45 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet) const
47 return createCSSOMWrapper(parentSheet, 0);
50 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSRule* parentRule) const
52 return createCSSOMWrapper(0, parentRule);
55 void StyleRuleBase::destroy()
59 delete toStyleRule(this);
62 delete toStyleRulePage(this);
65 delete toStyleRuleFontFace(this);
68 delete toStyleRuleMedia(this);
71 delete toStyleRuleSupports(this);
74 delete toStyleRuleImport(this);
77 delete toStyleRuleKeyframes(this);
80 delete toStyleRuleViewport(this);
83 delete toStyleRuleFilter(this);
94 PassRefPtr<StyleRuleBase> StyleRuleBase::copy() const
98 return toStyleRule(this)->copy();
100 return toStyleRulePage(this)->copy();
102 return toStyleRuleFontFace(this)->copy();
104 return toStyleRuleMedia(this)->copy();
106 return toStyleRuleSupports(this)->copy();
108 // FIXME: Copy import rules.
109 ASSERT_NOT_REACHED();
112 return toStyleRuleKeyframes(this)->copy();
114 return toStyleRuleViewport(this)->copy();
116 return toStyleRuleFilter(this)->copy();
120 ASSERT_NOT_REACHED();
123 ASSERT_NOT_REACHED();
127 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const
129 RefPtr<CSSRule> rule;
130 StyleRuleBase* self = const_cast<StyleRuleBase*>(this);
133 rule = CSSStyleRule::create(toStyleRule(self), parentSheet);
136 rule = CSSPageRule::create(toStyleRulePage(self), parentSheet);
139 rule = CSSFontFaceRule::create(toStyleRuleFontFace(self), parentSheet);
142 rule = CSSMediaRule::create(toStyleRuleMedia(self), parentSheet);
145 rule = CSSSupportsRule::create(toStyleRuleSupports(self), parentSheet);
148 rule = CSSImportRule::create(toStyleRuleImport(self), parentSheet);
151 rule = CSSKeyframesRule::create(toStyleRuleKeyframes(self), parentSheet);
154 rule = CSSViewportRule::create(toStyleRuleViewport(self), parentSheet);
157 rule = CSSFilterRule::create(toStyleRuleFilter(self), parentSheet);
162 ASSERT_NOT_REACHED();
166 rule->setParentRule(parentRule);
167 return rule.release();
170 unsigned StyleRule::averageSizeInBytes()
172 return sizeof(StyleRule) + sizeof(CSSSelector) + StylePropertySet::averageSizeInBytes();
175 StyleRule::StyleRule()
176 : StyleRuleBase(Style)
180 StyleRule::StyleRule(const StyleRule& o)
182 , m_properties(o.m_properties->mutableCopy())
183 , m_selectorList(o.m_selectorList)
187 StyleRule::~StyleRule()
191 MutableStylePropertySet* StyleRule::mutableProperties()
193 if (!m_properties->isMutable())
194 m_properties = m_properties->mutableCopy();
195 return toMutableStylePropertySet(m_properties);
198 void StyleRule::setProperties(PassRefPtr<StylePropertySet> properties)
200 m_properties = properties;
203 StyleRulePage::StyleRulePage()
204 : StyleRuleBase(Page)
208 StyleRulePage::StyleRulePage(const StyleRulePage& o)
210 , m_properties(o.m_properties->mutableCopy())
211 , m_selectorList(o.m_selectorList)
215 StyleRulePage::~StyleRulePage()
219 MutableStylePropertySet* StyleRulePage::mutableProperties()
221 if (!m_properties->isMutable())
222 m_properties = m_properties->mutableCopy();
223 return toMutableStylePropertySet(m_properties);
226 void StyleRulePage::setProperties(PassRefPtr<StylePropertySet> properties)
228 m_properties = properties;
231 StyleRuleFontFace::StyleRuleFontFace()
232 : StyleRuleBase(FontFace)
236 StyleRuleFontFace::StyleRuleFontFace(const StyleRuleFontFace& o)
238 , m_properties(o.m_properties->mutableCopy())
242 StyleRuleFontFace::~StyleRuleFontFace()
246 MutableStylePropertySet* StyleRuleFontFace::mutableProperties()
248 if (!m_properties->isMutable())
249 m_properties = m_properties->mutableCopy();
250 return toMutableStylePropertySet(m_properties);
253 void StyleRuleFontFace::setProperties(PassRefPtr<StylePropertySet> properties)
255 m_properties = properties;
258 StyleRuleGroup::StyleRuleGroup(Type type, Vector<RefPtr<StyleRuleBase> >& adoptRule)
259 : StyleRuleBase(type)
261 m_childRules.swap(adoptRule);
264 StyleRuleGroup::StyleRuleGroup(const StyleRuleGroup& o)
266 , m_childRules(o.m_childRules.size())
268 for (unsigned i = 0; i < m_childRules.size(); ++i)
269 m_childRules[i] = o.m_childRules[i]->copy();
272 void StyleRuleGroup::wrapperInsertRule(unsigned index, PassRefPtr<StyleRuleBase> rule)
274 m_childRules.insert(index, rule);
277 void StyleRuleGroup::wrapperRemoveRule(unsigned index)
279 m_childRules.remove(index);
282 StyleRuleMedia::StyleRuleMedia(PassRefPtr<MediaQuerySet> media, Vector<RefPtr<StyleRuleBase> >& adoptRules)
283 : StyleRuleGroup(Media, adoptRules)
284 , m_mediaQueries(media)
288 StyleRuleMedia::StyleRuleMedia(const StyleRuleMedia& o)
291 if (o.m_mediaQueries)
292 m_mediaQueries = o.m_mediaQueries->copy();
295 StyleRuleSupports::StyleRuleSupports(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase> >& adoptRules)
296 : StyleRuleGroup(Supports, adoptRules)
297 , m_conditionText(conditionText)
298 , m_conditionIsSupported(conditionIsSupported)
302 StyleRuleSupports::StyleRuleSupports(const StyleRuleSupports& o)
304 , m_conditionText(o.m_conditionText)
305 , m_conditionIsSupported(o.m_conditionIsSupported)
309 StyleRuleViewport::StyleRuleViewport()
310 : StyleRuleBase(Viewport)
314 StyleRuleViewport::StyleRuleViewport(const StyleRuleViewport& o)
316 , m_properties(o.m_properties->mutableCopy())
320 StyleRuleViewport::~StyleRuleViewport()
324 MutableStylePropertySet* StyleRuleViewport::mutableProperties()
326 if (!m_properties->isMutable())
327 m_properties = m_properties->mutableCopy();
328 return toMutableStylePropertySet(m_properties);
331 void StyleRuleViewport::setProperties(PassRefPtr<StylePropertySet> properties)
333 m_properties = properties;
336 StyleRuleFilter::StyleRuleFilter(const String& filterName)
337 : StyleRuleBase(Filter)
338 , m_filterName(filterName)
342 StyleRuleFilter::StyleRuleFilter(const StyleRuleFilter& o)
344 , m_filterName(o.m_filterName)
345 , m_properties(o.m_properties->mutableCopy())
349 StyleRuleFilter::~StyleRuleFilter()
353 MutableStylePropertySet* StyleRuleFilter::mutableProperties()
355 if (!m_properties->isMutable())
356 m_properties = m_properties->mutableCopy();
357 return toMutableStylePropertySet(m_properties);
360 void StyleRuleFilter::setProperties(PassRefPtr<StylePropertySet> properties)
362 m_properties = properties;
365 } // namespace WebCore