Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSValue.h
1 /*
2  * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifndef CSSValue_h
22 #define CSSValue_h
23
24 #include "bindings/core/v8/ScriptWrappable.h"
25 #include "core/dom/ExceptionCode.h"
26 #include "platform/heap/Handle.h"
27 #include "platform/weborigin/KURL.h"
28 #include "wtf/HashMap.h"
29 #include "wtf/ListHashSet.h"
30 #include "wtf/RefCounted.h"
31 #include "wtf/RefPtr.h"
32
33 namespace blink {
34
35 class ExceptionState;
36 class StyleSheetContents;
37
38 enum CSSTextFormattingFlags { QuoteCSSStringIfNeeded, AlwaysQuoteCSSString };
39
40 // FIXME: The current CSSValue and subclasses should be turned into internal types (StyleValue).
41 // The few subtypes that are actually exposed in CSSOM can be seen in the cloneForCSSOM() function.
42 // They should be handled by separate wrapper classes.
43
44 // Please don't expose more CSSValue types to the web.
45 class CSSValue : public RefCountedWillBeGarbageCollectedFinalized<CSSValue>, public ScriptWrappableBase {
46 public:
47     enum Type {
48         CSS_INHERIT = 0,
49         CSS_PRIMITIVE_VALUE = 1,
50         CSS_VALUE_LIST = 2,
51         CSS_CUSTOM = 3,
52         CSS_INITIAL = 4
53
54     };
55
56     // Override RefCounted's deref() to ensure operator delete is called on
57     // the appropriate subclass type.
58     // When oilpan is enabled the finalize method is called by the garbage
59     // collector and not immediately when deref reached zero.
60 #if !ENABLE(OILPAN)
61     void deref()
62     {
63         if (derefBase())
64             destroy();
65     }
66 #endif // !ENABLE(OILPAN)
67
68     Type cssValueType() const;
69
70     String cssText() const;
71     void setCSSText(const String&, ExceptionState&) { } // FIXME: Not implemented.
72
73     bool isPrimitiveValue() const { return m_classType == PrimitiveClass; }
74     bool isValueList() const { return m_classType >= ValueListClass; }
75
76     bool isBaseValueList() const { return m_classType == ValueListClass; }
77
78     bool isAspectRatioValue() const { return m_classType == AspectRatioClass; }
79     bool isBorderImageSliceValue() const { return m_classType == BorderImageSliceClass; }
80     bool isCanvasValue() const { return m_classType == CanvasClass; }
81     bool isCursorImageValue() const { return m_classType == CursorImageClass; }
82     bool isCrossfadeValue() const { return m_classType == CrossfadeClass; }
83     bool isFontFeatureValue() const { return m_classType == FontFeatureClass; }
84     bool isFontValue() const { return m_classType == FontClass; }
85     bool isFontFaceSrcValue() const { return m_classType == FontFaceSrcClass; }
86     bool isFunctionValue() const { return m_classType == FunctionClass; }
87     bool isImageGeneratorValue() const { return m_classType >= CanvasClass && m_classType <= RadialGradientClass; }
88     bool isGradientValue() const { return m_classType >= LinearGradientClass && m_classType <= RadialGradientClass; }
89     bool isImageSetValue() const { return m_classType == ImageSetClass; }
90     bool isImageValue() const { return m_classType == ImageClass; }
91     bool isImplicitInitialValue() const;
92     bool isInheritedValue() const { return m_classType == InheritedClass; }
93     bool isInitialValue() const { return m_classType == InitialClass; }
94     bool isLinearGradientValue() const { return m_classType == LinearGradientClass; }
95     bool isRadialGradientValue() const { return m_classType == RadialGradientClass; }
96     bool isReflectValue() const { return m_classType == ReflectClass; }
97     bool isShadowValue() const { return m_classType == ShadowClass; }
98     bool isTextCloneCSSValue() const { return m_isTextClone; }
99     bool isCubicBezierTimingFunctionValue() const { return m_classType == CubicBezierTimingFunctionClass; }
100     bool isStepsTimingFunctionValue() const { return m_classType == StepsTimingFunctionClass; }
101     bool isTransformValue() const { return m_classType == CSSTransformClass; }
102     bool isLineBoxContainValue() const { return m_classType == LineBoxContainClass; }
103     bool isCalcValue() const {return m_classType == CalculationClass; }
104     bool isFilterValue() const { return m_classType == CSSFilterClass; }
105     bool isGridTemplateAreasValue() const { return m_classType == GridTemplateAreasClass; }
106     bool isSVGDocumentValue() const { return m_classType == CSSSVGDocumentClass; }
107     bool isUnicodeRangeValue() const { return m_classType == UnicodeRangeClass; }
108     bool isGridLineNamesValue() const { return m_classType == GridLineNamesClass; }
109
110     bool isCSSOMSafe() const { return m_isCSSOMSafe; }
111     bool isSubtypeExposedToCSSOM() const
112     {
113         return isPrimitiveValue() || isValueList();
114     }
115
116     PassRefPtrWillBeRawPtr<CSSValue> cloneForCSSOM() const;
117
118     bool hasFailedOrCanceledSubresources() const;
119
120     bool equals(const CSSValue&) const;
121
122     void finalizeGarbageCollectedObject();
123     void traceAfterDispatch(Visitor*) { }
124     void trace(Visitor*);
125
126 protected:
127
128     static const size_t ClassTypeBits = 6;
129     enum ClassType {
130         PrimitiveClass,
131
132         // Image classes.
133         ImageClass,
134         CursorImageClass,
135
136         // Image generator classes.
137         CanvasClass,
138         CrossfadeClass,
139         LinearGradientClass,
140         RadialGradientClass,
141
142         // Timing function classes.
143         CubicBezierTimingFunctionClass,
144         StepsTimingFunctionClass,
145
146         // Other class types.
147         AspectRatioClass,
148         BorderImageSliceClass,
149         FontFeatureClass,
150         FontClass,
151         FontFaceSrcClass,
152         FunctionClass,
153
154         InheritedClass,
155         InitialClass,
156
157         ReflectClass,
158         ShadowClass,
159         UnicodeRangeClass,
160         LineBoxContainClass,
161         CalculationClass,
162         GridTemplateAreasClass,
163
164         // SVG classes.
165         CSSSVGDocumentClass,
166
167         // List class types must appear after ValueListClass.
168         ValueListClass,
169         ImageSetClass,
170         CSSFilterClass,
171         CSSTransformClass,
172         GridLineNamesClass,
173         // Do not append non-list class types here.
174     };
175
176     static const size_t ValueListSeparatorBits = 2;
177     enum ValueListSeparator {
178         SpaceSeparator,
179         CommaSeparator,
180         SlashSeparator
181     };
182
183     ClassType classType() const { return static_cast<ClassType>(m_classType); }
184
185     explicit CSSValue(ClassType classType, bool isCSSOMSafe = false)
186         : m_isCSSOMSafe(isCSSOMSafe)
187         , m_isTextClone(false)
188         , m_primitiveUnitType(0)
189         , m_hasCachedCSSText(false)
190         , m_isQuirkValue(false)
191         , m_valueListSeparator(SpaceSeparator)
192         , m_classType(classType)
193     {
194     }
195
196     // NOTE: This class is non-virtual for memory and performance reasons.
197     // Don't go making it virtual again unless you know exactly what you're doing!
198
199     ~CSSValue() { }
200
201 private:
202     void destroy();
203
204 protected:
205     unsigned m_isCSSOMSafe : 1;
206     unsigned m_isTextClone : 1;
207     // The bits in this section are only used by specific subclasses but kept here
208     // to maximize struct packing.
209
210     // CSSPrimitiveValue bits:
211     unsigned m_primitiveUnitType : 7; // CSSPrimitiveValue::UnitType
212     mutable unsigned m_hasCachedCSSText : 1;
213     unsigned m_isQuirkValue : 1;
214
215     unsigned m_valueListSeparator : ValueListSeparatorBits;
216
217 private:
218     unsigned m_classType : ClassTypeBits; // ClassType
219 };
220
221 template<typename CSSValueType, size_t inlineCapacity>
222 inline bool compareCSSValueVector(const WillBeHeapVector<RefPtrWillBeMember<CSSValueType>, inlineCapacity>& firstVector, const WillBeHeapVector<RefPtrWillBeMember<CSSValueType>, inlineCapacity>& secondVector)
223 {
224     size_t size = firstVector.size();
225     if (size != secondVector.size())
226         return false;
227
228     for (size_t i = 0; i < size; i++) {
229         const RefPtrWillBeMember<CSSValueType>& firstPtr = firstVector[i];
230         const RefPtrWillBeMember<CSSValueType>& secondPtr = secondVector[i];
231         if (firstPtr == secondPtr || (firstPtr && secondPtr && firstPtr->equals(*secondPtr)))
232             continue;
233         return false;
234     }
235     return true;
236 }
237
238 template<typename CSSValueType>
239 inline bool compareCSSValuePtr(const RefPtr<CSSValueType>& first, const RefPtr<CSSValueType>& second)
240 {
241     return first ? second && first->equals(*second) : !second;
242 }
243
244 template<typename CSSValueType>
245 inline bool compareCSSValuePtr(const RawPtr<CSSValueType>& first, const RawPtr<CSSValueType>& second)
246 {
247     return first ? second && first->equals(*second) : !second;
248 }
249
250 template<typename CSSValueType>
251 inline bool compareCSSValuePtr(const Member<CSSValueType>& first, const Member<CSSValueType>& second)
252 {
253     return first ? second && first->equals(*second) : !second;
254 }
255
256 #define DEFINE_CSS_VALUE_TYPE_CASTS(thisType, predicate) \
257     DEFINE_TYPE_CASTS(thisType, CSSValue, value, value->predicate, value.predicate)
258
259 } // namespace blink
260
261 #endif // CSSValue_h