2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 #ifndef StyleBuilder_h
26 #define StyleBuilder_h
28 #include "CSSPropertyNames.h"
29 #include <wtf/PassRefPtr.h>
30 #include <wtf/RefCounted.h>
38 class PropertyHandler {
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; }
53 InheritFunction m_inherit;
54 InitialFunction m_initial;
55 ApplyFunction m_apply;
59 WTF_MAKE_NONCOPYABLE(StyleBuilder);
61 static const StyleBuilder& sharedStyleBuilder();
63 const PropertyHandler& propertyHandler(CSSPropertyID property) const
65 ASSERT(valid(property));
66 return m_propertyMap[index(property)];
70 static int index(CSSPropertyID property)
72 return property - firstCSSProperty;
75 static bool valid(CSSPropertyID property)
77 int i = index(property);
78 return i >= 0 && i < numCSSProperties;
81 void setPropertyHandler(CSSPropertyID property, const PropertyHandler& handler)
83 ASSERT(valid(property));
84 ASSERT(!propertyHandler(property).isValid());
85 m_propertyMap[index(property)] = handler;
88 void setPropertyHandler(CSSPropertyID newProperty, CSSPropertyID equivalentProperty)
90 ASSERT(valid(newProperty));
91 ASSERT(valid(equivalentProperty));
92 ASSERT(!propertyHandler(newProperty).isValid());
93 m_propertyMap[index(newProperty)] = m_propertyMap[index(equivalentProperty)];
96 PropertyHandler m_propertyMap[numCSSProperties];
101 #endif // StyleBuilder_h