Merge "[CherryPick] [WEBGL] Rename WEBKIT_WEBGL_compressed_texture_s3tc to WEBGL_comp...
[framework/web/webkit-efl.git] / Source / WebCore / css / StyleBuilder.h
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #ifndef StyleBuilder_h
26 #define StyleBuilder_h
27
28 #include "CSSPropertyNames.h"
29 #include <wtf/PassRefPtr.h>
30 #include <wtf/RefCounted.h>
31
32 namespace WebCore {
33
34 class CSSValue;
35 class StyleBuilder;
36 class StyleResolver;
37
38 class PropertyHandler {
39 public:
40     typedef void (*InheritFunction)(StyleResolver*);
41     typedef void (*InitialFunction)(StyleResolver*);
42     typedef void (*ApplyFunction)(StyleResolver*, CSSValue*);
43     PropertyHandler() : m_inherit(0), m_initial(0), m_apply(0) { }
44     PropertyHandler(InheritFunction inherit, InitialFunction initial, ApplyFunction apply) : m_inherit(inherit), m_initial(initial), m_apply(apply) { }
45     void applyInheritValue(StyleResolver* styleResolver) const { ASSERT(m_inherit); (*m_inherit)(styleResolver); }
46     void applyInitialValue(StyleResolver* styleResolver) const { ASSERT(m_initial); (*m_initial)(styleResolver); }
47     void applyValue(StyleResolver* styleResolver, CSSValue* value) const { ASSERT(m_apply); (*m_apply)(styleResolver, value); }
48     bool isValid() const { return m_inherit && m_initial && m_apply; }
49     InheritFunction inheritFunction() const { return m_inherit; }
50     InitialFunction initialFunction() { return m_initial; }
51     ApplyFunction applyFunction() { return m_apply; }
52 private:
53     InheritFunction m_inherit;
54     InitialFunction m_initial;
55     ApplyFunction m_apply;
56 };
57
58 class StyleBuilder {
59     WTF_MAKE_NONCOPYABLE(StyleBuilder);
60 public:
61     static const StyleBuilder& sharedStyleBuilder();
62
63     const PropertyHandler& propertyHandler(CSSPropertyID property) const
64     {
65         ASSERT(valid(property));
66         return m_propertyMap[index(property)];
67     }
68 private:
69     StyleBuilder();
70     static int index(CSSPropertyID property)
71     {
72         return property - firstCSSProperty;
73     }
74
75     static bool valid(CSSPropertyID property)
76     {
77         int i = index(property);
78         return i >= 0 && i < numCSSProperties;
79     }
80
81     void setPropertyHandler(CSSPropertyID property, const PropertyHandler& handler)
82     {
83         ASSERT(valid(property));
84         ASSERT(!propertyHandler(property).isValid());
85         m_propertyMap[index(property)] = handler;
86     }
87
88     void setPropertyHandler(CSSPropertyID newProperty, CSSPropertyID equivalentProperty)
89     {
90         ASSERT(valid(newProperty));
91         ASSERT(valid(equivalentProperty));
92         ASSERT(!propertyHandler(newProperty).isValid());
93         m_propertyMap[index(newProperty)] = m_propertyMap[index(equivalentProperty)];
94     }
95
96     PropertyHandler m_propertyMap[numCSSProperties];
97 };
98
99 }
100
101 #endif // StyleBuilder_h