Home | History | Annotate | Download | only in css
      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, 2005, 2006, 2008, 2012 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 #include "config.h"
     23 #include "core/css/StyleRule.h"
     24 
     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"
     38 
     39 namespace WebCore {
     40 
     41 struct SameSizeAsStyleRuleBase : public WTF::RefCountedBase {
     42     unsigned bitfields;
     43 };
     44 
     45 COMPILE_ASSERT(sizeof(StyleRuleBase) <= sizeof(SameSizeAsStyleRuleBase), StyleRuleBase_should_stay_small);
     46 
     47 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet) const
     48 {
     49     return createCSSOMWrapper(parentSheet, 0);
     50 }
     51 
     52 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSRule* parentRule) const
     53 {
     54     return createCSSOMWrapper(0, parentRule);
     55 }
     56 
     57 void StyleRuleBase::destroy()
     58 {
     59     switch (type()) {
     60     case Style:
     61         delete toStyleRule(this);
     62         return;
     63     case Page:
     64         delete toStyleRulePage(this);
     65         return;
     66     case FontFace:
     67         delete toStyleRuleFontFace(this);
     68         return;
     69     case Media:
     70         delete toStyleRuleMedia(this);
     71         return;
     72     case Supports:
     73         delete toStyleRuleSupports(this);
     74         return;
     75     case Region:
     76         delete toStyleRuleRegion(this);
     77         return;
     78     case Import:
     79         delete toStyleRuleImport(this);
     80         return;
     81     case Keyframes:
     82         delete toStyleRuleKeyframes(this);
     83         return;
     84     case Viewport:
     85         delete toStyleRuleViewport(this);
     86         return;
     87     case Filter:
     88         delete toStyleRuleFilter(this);
     89         return;
     90     case Unknown:
     91     case Charset:
     92     case Keyframe:
     93         ASSERT_NOT_REACHED();
     94         return;
     95     }
     96     ASSERT_NOT_REACHED();
     97 }
     98 
     99 PassRefPtr<StyleRuleBase> StyleRuleBase::copy() const
    100 {
    101     switch (type()) {
    102     case Style:
    103         return toStyleRule(this)->copy();
    104     case Page:
    105         return toStyleRulePage(this)->copy();
    106     case FontFace:
    107         return toStyleRuleFontFace(this)->copy();
    108     case Media:
    109         return toStyleRuleMedia(this)->copy();
    110     case Supports:
    111         return toStyleRuleSupports(this)->copy();
    112     case Region:
    113         return toStyleRuleRegion(this)->copy();
    114     case Import:
    115         // FIXME: Copy import rules.
    116         ASSERT_NOT_REACHED();
    117         return 0;
    118     case Keyframes:
    119         return toStyleRuleKeyframes(this)->copy();
    120     case Viewport:
    121         return toStyleRuleViewport(this)->copy();
    122     case Filter:
    123         return toStyleRuleFilter(this)->copy();
    124     case Unknown:
    125     case Charset:
    126     case Keyframe:
    127         ASSERT_NOT_REACHED();
    128         return 0;
    129     }
    130     ASSERT_NOT_REACHED();
    131     return 0;
    132 }
    133 
    134 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const
    135 {
    136     RefPtr<CSSRule> rule;
    137     StyleRuleBase* self = const_cast<StyleRuleBase*>(this);
    138     switch (type()) {
    139     case Style:
    140         rule = CSSStyleRule::create(toStyleRule(self), parentSheet);
    141         break;
    142     case Page:
    143         rule = CSSPageRule::create(toStyleRulePage(self), parentSheet);
    144         break;
    145     case FontFace:
    146         rule = CSSFontFaceRule::create(toStyleRuleFontFace(self), parentSheet);
    147         break;
    148     case Media:
    149         rule = CSSMediaRule::create(toStyleRuleMedia(self), parentSheet);
    150         break;
    151     case Supports:
    152         rule = CSSSupportsRule::create(toStyleRuleSupports(self), parentSheet);
    153         break;
    154     case Region:
    155         rule = CSSRegionRule::create(toStyleRuleRegion(self), parentSheet);
    156         break;
    157     case Import:
    158         rule = CSSImportRule::create(toStyleRuleImport(self), parentSheet);
    159         break;
    160     case Keyframes:
    161         rule = CSSKeyframesRule::create(toStyleRuleKeyframes(self), parentSheet);
    162         break;
    163     case Viewport:
    164         rule = CSSViewportRule::create(toStyleRuleViewport(self), parentSheet);
    165         break;
    166     case Filter:
    167         rule = CSSFilterRule::create(toStyleRuleFilter(self), parentSheet);
    168         break;
    169     case Unknown:
    170     case Charset:
    171     case Keyframe:
    172         ASSERT_NOT_REACHED();
    173         return 0;
    174     }
    175     if (parentRule)
    176         rule->setParentRule(parentRule);
    177     return rule.release();
    178 }
    179 
    180 unsigned StyleRule::averageSizeInBytes()
    181 {
    182     return sizeof(StyleRule) + sizeof(CSSSelector) + StylePropertySet::averageSizeInBytes();
    183 }
    184 
    185 StyleRule::StyleRule()
    186     : StyleRuleBase(Style)
    187 {
    188 }
    189 
    190 StyleRule::StyleRule(const StyleRule& o)
    191     : StyleRuleBase(o)
    192     , m_properties(o.m_properties->mutableCopy())
    193     , m_selectorList(o.m_selectorList)
    194 {
    195 }
    196 
    197 StyleRule::~StyleRule()
    198 {
    199 }
    200 
    201 MutableStylePropertySet* StyleRule::mutableProperties()
    202 {
    203     if (!m_properties->isMutable())
    204         m_properties = m_properties->mutableCopy();
    205     return toMutableStylePropertySet(m_properties);
    206 }
    207 
    208 void StyleRule::setProperties(PassRefPtr<StylePropertySet> properties)
    209 {
    210     m_properties = properties;
    211 }
    212 
    213 StyleRulePage::StyleRulePage()
    214     : StyleRuleBase(Page)
    215 {
    216 }
    217 
    218 StyleRulePage::StyleRulePage(const StyleRulePage& o)
    219     : StyleRuleBase(o)
    220     , m_properties(o.m_properties->mutableCopy())
    221     , m_selectorList(o.m_selectorList)
    222 {
    223 }
    224 
    225 StyleRulePage::~StyleRulePage()
    226 {
    227 }
    228 
    229 MutableStylePropertySet* StyleRulePage::mutableProperties()
    230 {
    231     if (!m_properties->isMutable())
    232         m_properties = m_properties->mutableCopy();
    233     return toMutableStylePropertySet(m_properties);
    234 }
    235 
    236 void StyleRulePage::setProperties(PassRefPtr<StylePropertySet> properties)
    237 {
    238     m_properties = properties;
    239 }
    240 
    241 StyleRuleFontFace::StyleRuleFontFace()
    242     : StyleRuleBase(FontFace)
    243 {
    244 }
    245 
    246 StyleRuleFontFace::StyleRuleFontFace(const StyleRuleFontFace& o)
    247     : StyleRuleBase(o)
    248     , m_properties(o.m_properties->mutableCopy())
    249 {
    250 }
    251 
    252 StyleRuleFontFace::~StyleRuleFontFace()
    253 {
    254 }
    255 
    256 MutableStylePropertySet* StyleRuleFontFace::mutableProperties()
    257 {
    258     if (!m_properties->isMutable())
    259         m_properties = m_properties->mutableCopy();
    260     return toMutableStylePropertySet(m_properties);
    261 }
    262 
    263 void StyleRuleFontFace::setProperties(PassRefPtr<StylePropertySet> properties)
    264 {
    265     m_properties = properties;
    266 }
    267 
    268 StyleRuleGroup::StyleRuleGroup(Type type, Vector<RefPtr<StyleRuleBase> >& adoptRule)
    269     : StyleRuleBase(type)
    270 {
    271     m_childRules.swap(adoptRule);
    272 }
    273 
    274 StyleRuleGroup::StyleRuleGroup(const StyleRuleGroup& o)
    275     : StyleRuleBase(o)
    276     , m_childRules(o.m_childRules.size())
    277 {
    278     for (unsigned i = 0; i < m_childRules.size(); ++i)
    279         m_childRules[i] = o.m_childRules[i]->copy();
    280 }
    281 
    282 void StyleRuleGroup::wrapperInsertRule(unsigned index, PassRefPtr<StyleRuleBase> rule)
    283 {
    284     m_childRules.insert(index, rule);
    285 }
    286 
    287 void StyleRuleGroup::wrapperRemoveRule(unsigned index)
    288 {
    289     m_childRules.remove(index);
    290 }
    291 
    292 StyleRuleMedia::StyleRuleMedia(PassRefPtr<MediaQuerySet> media, Vector<RefPtr<StyleRuleBase> >& adoptRules)
    293     : StyleRuleGroup(Media, adoptRules)
    294     , m_mediaQueries(media)
    295 {
    296 }
    297 
    298 StyleRuleMedia::StyleRuleMedia(const StyleRuleMedia& o)
    299     : StyleRuleGroup(o)
    300 {
    301     if (o.m_mediaQueries)
    302         m_mediaQueries = o.m_mediaQueries->copy();
    303 }
    304 
    305 StyleRuleSupports::StyleRuleSupports(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase> >& adoptRules)
    306     : StyleRuleGroup(Supports, adoptRules)
    307     , m_conditionText(conditionText)
    308     , m_conditionIsSupported(conditionIsSupported)
    309 {
    310 }
    311 
    312 StyleRuleSupports::StyleRuleSupports(const StyleRuleSupports& o)
    313     : StyleRuleGroup(o)
    314     , m_conditionText(o.m_conditionText)
    315     , m_conditionIsSupported(o.m_conditionIsSupported)
    316 {
    317 }
    318 
    319 StyleRuleRegion::StyleRuleRegion(Vector<OwnPtr<CSSParserSelector> >* selectors, Vector<RefPtr<StyleRuleBase> >& adoptRules)
    320     : StyleRuleGroup(Region, adoptRules)
    321 {
    322     ASSERT(RuntimeEnabledFeatures::cssRegionsEnabled());
    323     m_selectorList.adoptSelectorVector(*selectors);
    324 }
    325 
    326 StyleRuleRegion::StyleRuleRegion(const StyleRuleRegion& o)
    327     : StyleRuleGroup(o)
    328     , m_selectorList(o.m_selectorList)
    329 {
    330     ASSERT(RuntimeEnabledFeatures::cssRegionsEnabled());
    331 }
    332 
    333 StyleRuleViewport::StyleRuleViewport()
    334     : StyleRuleBase(Viewport)
    335 {
    336 }
    337 
    338 StyleRuleViewport::StyleRuleViewport(const StyleRuleViewport& o)
    339     : StyleRuleBase(o)
    340     , m_properties(o.m_properties->mutableCopy())
    341 {
    342 }
    343 
    344 StyleRuleViewport::~StyleRuleViewport()
    345 {
    346 }
    347 
    348 MutableStylePropertySet* StyleRuleViewport::mutableProperties()
    349 {
    350     if (!m_properties->isMutable())
    351         m_properties = m_properties->mutableCopy();
    352     return toMutableStylePropertySet(m_properties);
    353 }
    354 
    355 void StyleRuleViewport::setProperties(PassRefPtr<StylePropertySet> properties)
    356 {
    357     m_properties = properties;
    358 }
    359 
    360 StyleRuleFilter::StyleRuleFilter(const String& filterName)
    361     : StyleRuleBase(Filter)
    362     , m_filterName(filterName)
    363 {
    364 }
    365 
    366 StyleRuleFilter::StyleRuleFilter(const StyleRuleFilter& o)
    367     : StyleRuleBase(o)
    368     , m_filterName(o.m_filterName)
    369     , m_properties(o.m_properties->mutableCopy())
    370 {
    371 }
    372 
    373 StyleRuleFilter::~StyleRuleFilter()
    374 {
    375 }
    376 
    377 MutableStylePropertySet* StyleRuleFilter::mutableProperties()
    378 {
    379     if (!m_properties->isMutable())
    380         m_properties = m_properties->mutableCopy();
    381     return toMutableStylePropertySet(m_properties);
    382 }
    383 
    384 void StyleRuleFilter::setProperties(PassRefPtr<StylePropertySet> properties)
    385 {
    386     m_properties = properties;
    387 }
    388 
    389 } // namespace WebCore
    390