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 DeprecatedStyleBuilder_h
26#define DeprecatedStyleBuilder_h
27
28#include "CSSPropertyNames.h"
29#include "StylePropertyShorthand.h"
30#include <wtf/PassRefPtr.h>
31#include <wtf/RefCounted.h>
32
33namespace WebCore {
34
35class CSSValue;
36class DeprecatedStyleBuilder;
37class StyleResolver;
38
39class PropertyHandler {
40public:
41    typedef void (*InheritFunction)(CSSPropertyID, StyleResolver*);
42    typedef void (*InitialFunction)(CSSPropertyID, StyleResolver*);
43    typedef void (*ApplyFunction)(CSSPropertyID, StyleResolver*, CSSValue*);
44    PropertyHandler() : m_inherit(0), m_initial(0), m_apply(0) { }
45    PropertyHandler(InheritFunction inherit, InitialFunction initial, ApplyFunction apply) : m_inherit(inherit), m_initial(initial), m_apply(apply) { }
46    void applyInheritValue(CSSPropertyID propertyID, StyleResolver* styleResolver) const { ASSERT(m_inherit); (*m_inherit)(propertyID, styleResolver); }
47    void applyInitialValue(CSSPropertyID propertyID, StyleResolver* styleResolver) const { ASSERT(m_initial); (*m_initial)(propertyID, styleResolver); }
48    void applyValue(CSSPropertyID propertyID, StyleResolver* styleResolver, CSSValue* value) const { ASSERT(m_apply); (*m_apply)(propertyID, styleResolver, value); }
49    bool isValid() const { return m_inherit && m_initial && m_apply; }
50    InheritFunction inheritFunction() const { return m_inherit; }
51    InitialFunction initialFunction() { return m_initial; }
52    ApplyFunction applyFunction() { return m_apply; }
53private:
54    InheritFunction m_inherit;
55    InitialFunction m_initial;
56    ApplyFunction m_apply;
57};
58
59class DeprecatedStyleBuilder {
60    WTF_MAKE_NONCOPYABLE(DeprecatedStyleBuilder); WTF_MAKE_FAST_ALLOCATED;
61public:
62    static const DeprecatedStyleBuilder& sharedStyleBuilder();
63
64    const PropertyHandler& propertyHandler(CSSPropertyID property) const
65    {
66        ASSERT(valid(property));
67        return m_propertyMap[index(property)];
68    }
69private:
70    DeprecatedStyleBuilder();
71    static int index(CSSPropertyID property)
72    {
73        return property - firstCSSProperty;
74    }
75
76    static bool valid(CSSPropertyID property)
77    {
78        int i = index(property);
79        return i >= 0 && i < numCSSProperties;
80    }
81
82    void setPropertyHandler(CSSPropertyID property, const PropertyHandler& handler)
83    {
84        ASSERT(valid(property));
85        ASSERT(!propertyHandler(property).isValid());
86        ASSERT_WITH_MESSAGE(!isExpandedShorthand(property), "Shorthand property id = %d shouldn't be inserted into StyleBuilder. Shorthands should be expanded at parsing time.", property);
87        m_propertyMap[index(property)] = handler;
88    }
89
90    void setPropertyHandler(CSSPropertyID newProperty, CSSPropertyID equivalentProperty)
91    {
92        ASSERT(valid(newProperty));
93        ASSERT(valid(equivalentProperty));
94        ASSERT(!propertyHandler(newProperty).isValid());
95        ASSERT_WITH_MESSAGE(!isExpandedShorthand(newProperty), "Shorthand property id = %d shouldn't be inserted into StyleBuilder. Shorthands should be expanded at parsing time.", newProperty);
96        ASSERT_WITH_MESSAGE(!isExpandedShorthand(equivalentProperty), "Shorthand property id = %d shouldn't be inserted into StyleBuilder. Shorthands should be expanded at parsing time.", equivalentProperty);
97        m_propertyMap[index(newProperty)] = m_propertyMap[index(equivalentProperty)];
98    }
99
100    PropertyHandler m_propertyMap[numCSSProperties];
101};
102
103}
104
105#endif // DeprecatedStyleBuilder_h
106