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