[CSS Parser] Remove WebkitCSSTransformValue
[WebKit-https.git] / Source / WebCore / css / CSSValue.cpp
1 /*
2  * Copyright (C) 2011 Andreas Kling (kling@webkit.org)
3  * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27
28 #include "config.h"
29 #include "CSSValue.h"
30
31 #include "CSSAnimationTriggerScrollValue.h"
32 #include "CSSAspectRatioValue.h"
33 #include "CSSBorderImageSliceValue.h"
34 #include "CSSCalculationValue.h"
35 #include "CSSCanvasValue.h"
36 #include "CSSContentDistributionValue.h"
37 #include "CSSCrossfadeValue.h"
38 #include "CSSCursorImageValue.h"
39 #include "CSSCustomIdentValue.h"
40 #include "CSSCustomPropertyValue.h"
41 #include "CSSFilterImageValue.h"
42 #include "CSSFontFaceSrcValue.h"
43 #include "CSSFontFeatureValue.h"
44 #include "CSSFontValue.h"
45 #include "CSSFontVariationValue.h"
46 #include "CSSFunctionValue.h"
47 #include "CSSGradientValue.h"
48 #include "CSSImageSetValue.h"
49 #include "CSSImageValue.h"
50 #include "CSSInheritedValue.h"
51 #include "CSSInitialValue.h"
52 #include "CSSLineBoxContainValue.h"
53 #include "CSSNamedImageValue.h"
54 #include "CSSPendingSubstitutionValue.h"
55 #include "CSSPrimitiveValue.h"
56 #include "CSSProperty.h"
57 #include "CSSReflectValue.h"
58 #include "CSSShadowValue.h"
59 #include "CSSTimingFunctionValue.h"
60 #include "CSSUnicodeRangeValue.h"
61 #include "CSSUnsetValue.h"
62 #include "CSSValueList.h"
63 #include "CSSVariableReferenceValue.h"
64
65 #if ENABLE(CSS_GRID_LAYOUT)
66 #include "CSSGridAutoRepeatValue.h"
67 #include "CSSGridLineNamesValue.h"
68 #include "CSSGridTemplateAreasValue.h"
69 #endif
70
71 namespace WebCore {
72
73 struct SameSizeAsCSSValue : public RefCounted<SameSizeAsCSSValue> {
74     uint32_t bitfields;
75 };
76
77 COMPILE_ASSERT(sizeof(CSSValue) == sizeof(SameSizeAsCSSValue), CSS_value_should_stay_small);
78
79 class TextCloneCSSValue final : public CSSValue {
80 public:
81     static Ref<TextCloneCSSValue> create(ClassType classType, const String& text)
82     {
83         return adoptRef(*new TextCloneCSSValue(classType, text));
84     }
85
86     String cssText() const { return m_cssText; }
87
88 private:
89     TextCloneCSSValue(ClassType classType, const String& text) 
90         : CSSValue(classType, /*isCSSOMSafe*/ true)
91         , m_cssText(text)
92     {
93         m_isTextClone = true;
94     }
95
96     String m_cssText;
97 };
98
99 bool CSSValue::isImplicitInitialValue() const
100 {
101     return m_classType == InitialClass && downcast<CSSInitialValue>(*this).isImplicit();
102 }
103
104 CSSValue::Type CSSValue::cssValueType() const
105 {
106     if (isInheritedValue())
107         return CSS_INHERIT;
108     if (isPrimitiveValue())
109         return CSS_PRIMITIVE_VALUE;
110     if (isValueList())
111         return CSS_VALUE_LIST;
112     if (isInitialValue())
113         return CSS_INITIAL;
114     if (isUnsetValue())
115         return CSS_UNSET;
116     if (isRevertValue())
117         return CSS_REVERT;
118     return CSS_CUSTOM;
119 }
120
121 bool CSSValue::traverseSubresources(const std::function<bool (const CachedResource&)>& handler) const
122 {
123     // This should get called for internal instances only.
124     ASSERT(!isCSSOMSafe());
125
126     if (is<CSSValueList>(*this))
127         return downcast<CSSValueList>(*this).traverseSubresources(handler);
128     if (is<CSSFontFaceSrcValue>(*this))
129         return downcast<CSSFontFaceSrcValue>(*this).traverseSubresources(handler);
130     if (is<CSSImageValue>(*this))
131         return downcast<CSSImageValue>(*this).traverseSubresources(handler);
132     if (is<CSSCrossfadeValue>(*this))
133         return downcast<CSSCrossfadeValue>(*this).traverseSubresources(handler);
134     if (is<CSSFilterImageValue>(*this))
135         return downcast<CSSFilterImageValue>(*this).traverseSubresources(handler);
136     if (is<CSSImageSetValue>(*this))
137         return downcast<CSSImageSetValue>(*this).traverseSubresources(handler);
138     return false;
139 }
140
141 template<class ChildClassType>
142 inline static bool compareCSSValues(const CSSValue& first, const CSSValue& second)
143 {
144     return static_cast<const ChildClassType&>(first).equals(static_cast<const ChildClassType&>(second));
145 }
146
147 bool CSSValue::equals(const CSSValue& other) const
148 {
149     if (m_isTextClone) {
150         ASSERT(isCSSOMSafe());
151         return static_cast<const TextCloneCSSValue*>(this)->cssText() == other.cssText();
152     }
153
154     if (m_classType == other.m_classType) {
155         switch (m_classType) {
156         case AspectRatioClass:
157             return compareCSSValues<CSSAspectRatioValue>(*this, other);
158         case BorderImageSliceClass:
159             return compareCSSValues<CSSBorderImageSliceValue>(*this, other);
160         case CanvasClass:
161             return compareCSSValues<CSSCanvasValue>(*this, other);
162         case NamedImageClass:
163             return compareCSSValues<CSSNamedImageValue>(*this, other);
164         case CursorImageClass:
165             return compareCSSValues<CSSCursorImageValue>(*this, other);
166         case FilterImageClass:
167             return compareCSSValues<CSSFilterImageValue>(*this, other);
168         case FontClass:
169             return compareCSSValues<CSSFontValue>(*this, other);
170         case FontFaceSrcClass:
171             return compareCSSValues<CSSFontFaceSrcValue>(*this, other);
172         case FontFeatureClass:
173             return compareCSSValues<CSSFontFeatureValue>(*this, other);
174 #if ENABLE(VARIATION_FONTS)
175         case FontVariationClass:
176             return compareCSSValues<CSSFontVariationValue>(*this, other);
177 #endif
178         case FunctionClass:
179             return compareCSSValues<CSSFunctionValue>(*this, other);
180         case LinearGradientClass:
181             return compareCSSValues<CSSLinearGradientValue>(*this, other);
182         case RadialGradientClass:
183             return compareCSSValues<CSSRadialGradientValue>(*this, other);
184         case CrossfadeClass:
185             return compareCSSValues<CSSCrossfadeValue>(*this, other);
186         case ImageClass:
187             return compareCSSValues<CSSImageValue>(*this, other);
188         case InheritedClass:
189             return compareCSSValues<CSSInheritedValue>(*this, other);
190         case InitialClass:
191             return compareCSSValues<CSSInitialValue>(*this, other);
192         case UnsetClass:
193             return compareCSSValues<CSSUnsetValue>(*this, other);
194         case RevertClass:
195             return compareCSSValues<CSSRevertValue>(*this, other);
196 #if ENABLE(CSS_GRID_LAYOUT)
197         case GridAutoRepeatClass:
198             return compareCSSValues<CSSGridAutoRepeatValue>(*this, other);
199         case GridLineNamesClass:
200             return compareCSSValues<CSSGridLineNamesValue>(*this, other);
201         case GridTemplateAreasClass:
202             return compareCSSValues<CSSGridTemplateAreasValue>(*this, other);
203 #endif
204         case PrimitiveClass:
205             return compareCSSValues<CSSPrimitiveValue>(*this, other);
206         case ReflectClass:
207             return compareCSSValues<CSSReflectValue>(*this, other);
208         case ShadowClass:
209             return compareCSSValues<CSSShadowValue>(*this, other);
210         case CubicBezierTimingFunctionClass:
211             return compareCSSValues<CSSCubicBezierTimingFunctionValue>(*this, other);
212         case StepsTimingFunctionClass:
213             return compareCSSValues<CSSStepsTimingFunctionValue>(*this, other);
214         case SpringTimingFunctionClass:
215             return compareCSSValues<CSSSpringTimingFunctionValue>(*this, other);
216         case UnicodeRangeClass:
217             return compareCSSValues<CSSUnicodeRangeValue>(*this, other);
218         case ValueListClass:
219             return compareCSSValues<CSSValueList>(*this, other);
220         case LineBoxContainClass:
221             return compareCSSValues<CSSLineBoxContainValue>(*this, other);
222         case CalculationClass:
223             return compareCSSValues<CSSCalcValue>(*this, other);
224         case ImageSetClass:
225             return compareCSSValues<CSSImageSetValue>(*this, other);
226 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
227         case AnimationTriggerScrollClass:
228             return compareCSSValues<CSSAnimationTriggerScrollValue>(*this, other);
229 #endif
230         case CSSContentDistributionClass:
231             return compareCSSValues<CSSContentDistributionValue>(*this, other);
232         case CustomPropertyClass:
233             return compareCSSValues<CSSCustomPropertyValue>(*this, other);
234         case VariableReferenceClass:
235             return compareCSSValues<CSSVariableReferenceValue>(*this, other);
236         case PendingSubstitutionValueClass:
237             return compareCSSValues<CSSPendingSubstitutionValue>(*this, other);
238         default:
239             ASSERT_NOT_REACHED();
240             return false;
241         }
242     } else if (is<CSSValueList>(*this) && !is<CSSValueList>(other))
243         return downcast<CSSValueList>(*this).equals(other);
244     else if (!is<CSSValueList>(*this) && is<CSSValueList>(other))
245         return static_cast<const CSSValueList&>(other).equals(*this);
246     return false;
247 }
248
249 String CSSValue::cssText() const
250 {
251     if (m_isTextClone) {
252          ASSERT(isCSSOMSafe());
253         return static_cast<const TextCloneCSSValue*>(this)->cssText();
254     }
255     ASSERT(!isCSSOMSafe() || isSubtypeExposedToCSSOM());
256
257     switch (classType()) {
258     case AspectRatioClass:
259         return downcast<CSSAspectRatioValue>(*this).customCSSText();
260     case BorderImageSliceClass:
261         return downcast<CSSBorderImageSliceValue>(*this).customCSSText();
262     case CanvasClass:
263         return downcast<CSSCanvasValue>(*this).customCSSText();
264     case NamedImageClass:
265         return downcast<CSSNamedImageValue>(*this).customCSSText();
266     case CursorImageClass:
267         return downcast<CSSCursorImageValue>(*this).customCSSText();
268     case FilterImageClass:
269         return downcast<CSSFilterImageValue>(*this).customCSSText();
270     case FontClass:
271         return downcast<CSSFontValue>(*this).customCSSText();
272     case FontFaceSrcClass:
273         return downcast<CSSFontFaceSrcValue>(*this).customCSSText();
274     case FontFeatureClass:
275         return downcast<CSSFontFeatureValue>(*this).customCSSText();
276 #if ENABLE(VARIATION_FONTS)
277     case FontVariationClass:
278         return downcast<CSSFontVariationValue>(*this).customCSSText();
279 #endif
280     case FunctionClass:
281         return downcast<CSSFunctionValue>(*this).customCSSText();
282     case LinearGradientClass:
283         return downcast<CSSLinearGradientValue>(*this).customCSSText();
284     case RadialGradientClass:
285         return downcast<CSSRadialGradientValue>(*this).customCSSText();
286     case CrossfadeClass:
287         return downcast<CSSCrossfadeValue>(*this).customCSSText();
288     case ImageClass:
289         return downcast<CSSImageValue>(*this).customCSSText();
290     case InheritedClass:
291         return downcast<CSSInheritedValue>(*this).customCSSText();
292     case InitialClass:
293         return downcast<CSSInitialValue>(*this).customCSSText();
294     case UnsetClass:
295         return downcast<CSSUnsetValue>(*this).customCSSText();
296     case RevertClass:
297         return downcast<CSSRevertValue>(*this).customCSSText();
298 #if ENABLE(CSS_GRID_LAYOUT)
299     case GridAutoRepeatClass:
300         return downcast<CSSGridAutoRepeatValue>(*this).customCSSText();
301     case GridLineNamesClass:
302         return downcast<CSSGridLineNamesValue>(*this).customCSSText();
303     case GridTemplateAreasClass:
304         return downcast<CSSGridTemplateAreasValue>(*this).customCSSText();
305 #endif
306     case PrimitiveClass:
307         return downcast<CSSPrimitiveValue>(*this).customCSSText();
308     case ReflectClass:
309         return downcast<CSSReflectValue>(*this).customCSSText();
310     case ShadowClass:
311         return downcast<CSSShadowValue>(*this).customCSSText();
312     case CubicBezierTimingFunctionClass:
313         return downcast<CSSCubicBezierTimingFunctionValue>(*this).customCSSText();
314     case StepsTimingFunctionClass:
315         return downcast<CSSStepsTimingFunctionValue>(*this).customCSSText();
316     case SpringTimingFunctionClass:
317         return downcast<CSSSpringTimingFunctionValue>(*this).customCSSText();
318     case UnicodeRangeClass:
319         return downcast<CSSUnicodeRangeValue>(*this).customCSSText();
320     case ValueListClass:
321         return downcast<CSSValueList>(*this).customCSSText();
322     case LineBoxContainClass:
323         return downcast<CSSLineBoxContainValue>(*this).customCSSText();
324     case CalculationClass:
325         return downcast<CSSCalcValue>(*this).customCSSText();
326     case ImageSetClass:
327         return downcast<CSSImageSetValue>(*this).customCSSText();
328 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
329     case AnimationTriggerScrollClass:
330         return downcast<CSSAnimationTriggerScrollValue>(*this).customCSSText();
331 #endif
332     case CSSContentDistributionClass:
333         return downcast<CSSContentDistributionValue>(*this).customCSSText();
334     case CustomPropertyClass:
335         return downcast<CSSCustomPropertyValue>(*this).customCSSText();
336     case CustomIdentClass:
337         return downcast<CSSCustomIdentValue>(*this).customCSSText();
338     case VariableReferenceClass:
339         return downcast<CSSVariableReferenceValue>(*this).customCSSText();
340     case PendingSubstitutionValueClass:
341         return downcast<CSSPendingSubstitutionValue>(*this).customCSSText();
342     }
343
344     ASSERT_NOT_REACHED();
345     return String();
346 }
347
348 void CSSValue::destroy()
349 {
350     if (m_isTextClone) {
351         ASSERT(isCSSOMSafe());
352         delete static_cast<TextCloneCSSValue*>(this);
353         return;
354     }
355     ASSERT(!isCSSOMSafe() || isSubtypeExposedToCSSOM());
356
357     switch (classType()) {
358     case AspectRatioClass:
359         delete downcast<CSSAspectRatioValue>(this);
360         return;
361     case BorderImageSliceClass:
362         delete downcast<CSSBorderImageSliceValue>(this);
363         return;
364     case CanvasClass:
365         delete downcast<CSSCanvasValue>(this);
366         return;
367     case NamedImageClass:
368         delete downcast<CSSNamedImageValue>(this);
369         return;
370     case CursorImageClass:
371         delete downcast<CSSCursorImageValue>(this);
372         return;
373     case FontClass:
374         delete downcast<CSSFontValue>(this);
375         return;
376     case FontFaceSrcClass:
377         delete downcast<CSSFontFaceSrcValue>(this);
378         return;
379     case FontFeatureClass:
380         delete downcast<CSSFontFeatureValue>(this);
381         return;
382 #if ENABLE(VARIATION_FONTS)
383     case FontVariationClass:
384         delete downcast<CSSFontVariationValue>(this);
385         return;
386 #endif
387     case FunctionClass:
388         delete downcast<CSSFunctionValue>(this);
389         return;
390     case LinearGradientClass:
391         delete downcast<CSSLinearGradientValue>(this);
392         return;
393     case RadialGradientClass:
394         delete downcast<CSSRadialGradientValue>(this);
395         return;
396     case CrossfadeClass:
397         delete downcast<CSSCrossfadeValue>(this);
398         return;
399     case ImageClass:
400         delete downcast<CSSImageValue>(this);
401         return;
402     case InheritedClass:
403         delete downcast<CSSInheritedValue>(this);
404         return;
405     case InitialClass:
406         delete downcast<CSSInitialValue>(this);
407         return;
408     case UnsetClass:
409         delete downcast<CSSUnsetValue>(this);
410         return;
411     case RevertClass:
412         delete downcast<CSSRevertValue>(this);
413         return;
414 #if ENABLE(CSS_GRID_LAYOUT)
415     case GridAutoRepeatClass:
416         delete downcast<CSSGridAutoRepeatValue>(this);
417         return;
418     case GridLineNamesClass:
419         delete downcast<CSSGridLineNamesValue>(this);
420         return;
421     case GridTemplateAreasClass:
422         delete downcast<CSSGridTemplateAreasValue>(this);
423         return;
424 #endif
425     case PrimitiveClass:
426         delete downcast<CSSPrimitiveValue>(this);
427         return;
428     case ReflectClass:
429         delete downcast<CSSReflectValue>(this);
430         return;
431     case ShadowClass:
432         delete downcast<CSSShadowValue>(this);
433         return;
434     case CubicBezierTimingFunctionClass:
435         delete downcast<CSSCubicBezierTimingFunctionValue>(this);
436         return;
437     case StepsTimingFunctionClass:
438         delete downcast<CSSStepsTimingFunctionValue>(this);
439         return;
440     case SpringTimingFunctionClass:
441         delete downcast<CSSSpringTimingFunctionValue>(this);
442         return;
443     case UnicodeRangeClass:
444         delete downcast<CSSUnicodeRangeValue>(this);
445         return;
446     case ValueListClass:
447         delete downcast<CSSValueList>(this);
448         return;
449     case LineBoxContainClass:
450         delete downcast<CSSLineBoxContainValue>(this);
451         return;
452     case CalculationClass:
453         delete downcast<CSSCalcValue>(this);
454         return;
455     case ImageSetClass:
456         delete downcast<CSSImageSetValue>(this);
457         return;
458     case FilterImageClass:
459         delete downcast<CSSFilterImageValue>(this);
460         return;
461 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
462     case AnimationTriggerScrollClass:
463         delete downcast<CSSAnimationTriggerScrollValue>(this);
464         return;
465 #endif
466     case CSSContentDistributionClass:
467         delete downcast<CSSContentDistributionValue>(this);
468         return;
469     case CustomPropertyClass:
470         delete downcast<CSSCustomPropertyValue>(this);
471         return;
472     case CustomIdentClass:
473         delete downcast<CSSCustomIdentValue>(this);
474         return;
475     case VariableReferenceClass:
476         delete downcast<CSSVariableReferenceValue>(this);
477         return;
478     case PendingSubstitutionValueClass:
479         delete downcast<CSSPendingSubstitutionValue>(this);
480         return;
481     }
482     ASSERT_NOT_REACHED();
483 }
484
485 RefPtr<CSSValue> CSSValue::cloneForCSSOM() const
486 {
487     switch (classType()) {
488     case PrimitiveClass:
489         return downcast<CSSPrimitiveValue>(*this).cloneForCSSOM();
490     case ValueListClass:
491         return downcast<CSSValueList>(*this).cloneForCSSOM();
492     case ImageClass:
493     case CursorImageClass:
494         return downcast<CSSImageValue>(*this).cloneForCSSOM();
495     case ImageSetClass:
496         return downcast<CSSImageSetValue>(*this).cloneForCSSOM();
497     default:
498         ASSERT(!isSubtypeExposedToCSSOM());
499         return TextCloneCSSValue::create(classType(), cssText());
500     }
501 }
502
503 bool CSSValue::treatAsInheritedValue(CSSPropertyID propertyID) const
504 {
505     return classType() == InheritedClass || (classType() == UnsetClass && CSSProperty::isInheritedProperty(propertyID));
506 }
507
508 bool CSSValue::treatAsInitialValue(CSSPropertyID propertyID) const
509 {
510     return classType() == InitialClass || (classType() == UnsetClass && !CSSProperty::isInheritedProperty(propertyID));
511 }
512
513 }