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