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 RefCountedWillBeGarbageCollectedFinalized<SameSizeAsStyleRuleBase> {
43 COMPILE_ASSERT(sizeof(StyleRuleBase) <= sizeof(SameSizeAsStyleRuleBase), StyleRuleBase_should_stay_small);
45 PassRefPtrWillBeRawPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet) const
47 return createCSSOMWrapper(parentSheet, 0);
50 PassRefPtrWillBeRawPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSRule* parentRule) const
52 return createCSSOMWrapper(0, parentRule);
55 void StyleRuleBase::trace(Visitor* visitor)
59 toStyleRule(this)->traceAfterDispatch(visitor);
62 toStyleRulePage(this)->traceAfterDispatch(visitor);
65 toStyleRuleFontFace(this)->traceAfterDispatch(visitor);
68 toStyleRuleMedia(this)->traceAfterDispatch(visitor);
71 toStyleRuleSupports(this)->traceAfterDispatch(visitor);
74 toStyleRuleImport(this)->traceAfterDispatch(visitor);
77 toStyleRuleKeyframes(this)->traceAfterDispatch(visitor);
80 toStyleRuleViewport(this)->traceAfterDispatch(visitor);
83 toStyleRuleFilter(this)->traceAfterDispatch(visitor);
94 void StyleRuleBase::finalizeGarbageCollectedObject()
98 toStyleRule(this)->~StyleRule();
101 toStyleRulePage(this)->~StyleRulePage();
104 toStyleRuleFontFace(this)->~StyleRuleFontFace();
107 toStyleRuleMedia(this)->~StyleRuleMedia();
110 toStyleRuleSupports(this)->~StyleRuleSupports();
113 toStyleRuleImport(this)->~StyleRuleImport();
116 toStyleRuleKeyframes(this)->~StyleRuleKeyframes();
119 toStyleRuleViewport(this)->~StyleRuleViewport();
122 toStyleRuleFilter(this)->~StyleRuleFilter();
127 ASSERT_NOT_REACHED();
130 ASSERT_NOT_REACHED();
133 void StyleRuleBase::destroy()
137 delete toStyleRule(this);
140 delete toStyleRulePage(this);
143 delete toStyleRuleFontFace(this);
146 delete toStyleRuleMedia(this);
149 delete toStyleRuleSupports(this);
152 delete toStyleRuleImport(this);
155 delete toStyleRuleKeyframes(this);
158 delete toStyleRuleViewport(this);
161 delete toStyleRuleFilter(this);
166 ASSERT_NOT_REACHED();
169 ASSERT_NOT_REACHED();
172 PassRefPtrWillBeRawPtr<StyleRuleBase> StyleRuleBase::copy() const
176 return toStyleRule(this)->copy();
178 return toStyleRulePage(this)->copy();
180 return toStyleRuleFontFace(this)->copy();
182 return toStyleRuleMedia(this)->copy();
184 return toStyleRuleSupports(this)->copy();
186 // FIXME: Copy import rules.
187 ASSERT_NOT_REACHED();
190 return toStyleRuleKeyframes(this)->copy();
192 return toStyleRuleViewport(this)->copy();
194 return toStyleRuleFilter(this)->copy();
198 ASSERT_NOT_REACHED();
201 ASSERT_NOT_REACHED();
205 PassRefPtrWillBeRawPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const
207 RefPtrWillBeRawPtr<CSSRule> rule = nullptr;
208 StyleRuleBase* self = const_cast<StyleRuleBase*>(this);
211 rule = CSSStyleRule::create(toStyleRule(self), parentSheet);
214 rule = CSSPageRule::create(toStyleRulePage(self), parentSheet);
217 rule = CSSFontFaceRule::create(toStyleRuleFontFace(self), parentSheet);
220 rule = CSSMediaRule::create(toStyleRuleMedia(self), parentSheet);
223 rule = CSSSupportsRule::create(toStyleRuleSupports(self), parentSheet);
226 rule = CSSImportRule::create(toStyleRuleImport(self), parentSheet);
229 rule = CSSKeyframesRule::create(toStyleRuleKeyframes(self), parentSheet);
232 rule = CSSViewportRule::create(toStyleRuleViewport(self), parentSheet);
235 rule = CSSFilterRule::create(toStyleRuleFilter(self), parentSheet);
240 ASSERT_NOT_REACHED();
244 rule->setParentRule(parentRule);
245 return rule.release();
248 unsigned StyleRule::averageSizeInBytes()
250 return sizeof(StyleRule) + sizeof(CSSSelector) + StylePropertySet::averageSizeInBytes();
253 StyleRule::StyleRule()
254 : StyleRuleBase(Style)
258 StyleRule::StyleRule(const StyleRule& o)
260 , m_properties(o.m_properties->mutableCopy())
261 , m_selectorList(o.m_selectorList)
265 StyleRule::~StyleRule()
269 MutableStylePropertySet& StyleRule::mutableProperties()
271 if (!m_properties->isMutable())
272 m_properties = m_properties->mutableCopy();
273 return *toMutableStylePropertySet(m_properties.get());
276 void StyleRule::setProperties(PassRefPtrWillBeRawPtr<StylePropertySet> properties)
278 m_properties = properties;
281 void StyleRule::traceAfterDispatch(Visitor* visitor)
283 visitor->trace(m_properties);
284 StyleRuleBase::traceAfterDispatch(visitor);
287 StyleRulePage::StyleRulePage()
288 : StyleRuleBase(Page)
292 StyleRulePage::StyleRulePage(const StyleRulePage& o)
294 , m_properties(o.m_properties->mutableCopy())
295 , m_selectorList(o.m_selectorList)
299 StyleRulePage::~StyleRulePage()
303 MutableStylePropertySet& StyleRulePage::mutableProperties()
305 if (!m_properties->isMutable())
306 m_properties = m_properties->mutableCopy();
307 return *toMutableStylePropertySet(m_properties.get());
310 void StyleRulePage::setProperties(PassRefPtrWillBeRawPtr<StylePropertySet> properties)
312 m_properties = properties;
315 void StyleRulePage::traceAfterDispatch(Visitor* visitor)
317 visitor->trace(m_properties);
318 StyleRuleBase::traceAfterDispatch(visitor);
321 StyleRuleFontFace::StyleRuleFontFace()
322 : StyleRuleBase(FontFace)
326 StyleRuleFontFace::StyleRuleFontFace(const StyleRuleFontFace& o)
328 , m_properties(o.m_properties->mutableCopy())
332 StyleRuleFontFace::~StyleRuleFontFace()
336 MutableStylePropertySet& StyleRuleFontFace::mutableProperties()
338 if (!m_properties->isMutable())
339 m_properties = m_properties->mutableCopy();
340 return *toMutableStylePropertySet(m_properties);
343 void StyleRuleFontFace::setProperties(PassRefPtrWillBeRawPtr<StylePropertySet> properties)
345 m_properties = properties;
348 void StyleRuleFontFace::traceAfterDispatch(Visitor* visitor)
350 visitor->trace(m_properties);
351 StyleRuleBase::traceAfterDispatch(visitor);
354 StyleRuleGroup::StyleRuleGroup(Type type, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRule)
355 : StyleRuleBase(type)
357 m_childRules.swap(adoptRule);
360 StyleRuleGroup::StyleRuleGroup(const StyleRuleGroup& o)
362 , m_childRules(o.m_childRules.size())
364 for (unsigned i = 0; i < m_childRules.size(); ++i)
365 m_childRules[i] = o.m_childRules[i]->copy();
368 void StyleRuleGroup::wrapperInsertRule(unsigned index, PassRefPtrWillBeRawPtr<StyleRuleBase> rule)
370 m_childRules.insert(index, rule);
373 void StyleRuleGroup::wrapperRemoveRule(unsigned index)
375 m_childRules.remove(index);
378 void StyleRuleGroup::traceAfterDispatch(Visitor* visitor)
380 visitor->trace(m_childRules);
381 StyleRuleBase::traceAfterDispatch(visitor);
384 StyleRuleMedia::StyleRuleMedia(PassRefPtrWillBeRawPtr<MediaQuerySet> media, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules)
385 : StyleRuleGroup(Media, adoptRules)
386 , m_mediaQueries(media)
390 StyleRuleMedia::StyleRuleMedia(const StyleRuleMedia& o)
393 if (o.m_mediaQueries)
394 m_mediaQueries = o.m_mediaQueries->copy();
397 void StyleRuleMedia::traceAfterDispatch(Visitor* visitor)
399 visitor->trace(m_mediaQueries);
400 StyleRuleGroup::traceAfterDispatch(visitor);
403 StyleRuleSupports::StyleRuleSupports(const String& conditionText, bool conditionIsSupported, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules)
404 : StyleRuleGroup(Supports, adoptRules)
405 , m_conditionText(conditionText)
406 , m_conditionIsSupported(conditionIsSupported)
410 StyleRuleSupports::StyleRuleSupports(const StyleRuleSupports& o)
412 , m_conditionText(o.m_conditionText)
413 , m_conditionIsSupported(o.m_conditionIsSupported)
417 StyleRuleViewport::StyleRuleViewport()
418 : StyleRuleBase(Viewport)
422 StyleRuleViewport::StyleRuleViewport(const StyleRuleViewport& o)
424 , m_properties(o.m_properties->mutableCopy())
428 StyleRuleViewport::~StyleRuleViewport()
432 MutableStylePropertySet& StyleRuleViewport::mutableProperties()
434 if (!m_properties->isMutable())
435 m_properties = m_properties->mutableCopy();
436 return *toMutableStylePropertySet(m_properties);
439 void StyleRuleViewport::setProperties(PassRefPtrWillBeRawPtr<StylePropertySet> properties)
441 m_properties = properties;
444 void StyleRuleViewport::traceAfterDispatch(Visitor* visitor)
446 visitor->trace(m_properties);
447 StyleRuleBase::traceAfterDispatch(visitor);
450 StyleRuleFilter::StyleRuleFilter(const String& filterName)
451 : StyleRuleBase(Filter)
452 , m_filterName(filterName)
456 StyleRuleFilter::StyleRuleFilter(const StyleRuleFilter& o)
458 , m_filterName(o.m_filterName)
459 , m_properties(o.m_properties->mutableCopy())
463 StyleRuleFilter::~StyleRuleFilter()
467 MutableStylePropertySet& StyleRuleFilter::mutableProperties()
469 if (!m_properties->isMutable())
470 m_properties = m_properties->mutableCopy();
471 return *toMutableStylePropertySet(m_properties);
474 void StyleRuleFilter::setProperties(PassRefPtrWillBeRawPtr<StylePropertySet> properties)
476 m_properties = properties;
479 void StyleRuleFilter::traceAfterDispatch(Visitor* visitor)
481 visitor->trace(m_properties);
482 StyleRuleBase::traceAfterDispatch(visitor);