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