1#!/usr/bin/env python
2# Copyright (C) 2013 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 are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import re
31import sys
32
33import in_generator
34import name_utilities
35from name_utilities import camelcase_property_name, lower_first
36import template_expander
37
38
39class StyleBuilderWriter(in_generator.Writer):
40    class_name = 'StyleBuilder'
41    filters = {
42        'lower_first': lower_first,
43    }
44
45    valid_values = {
46        'svg': [True, False],
47        'font': [True, False],
48        'custom_all': [True, False],
49        'custom_initial': [True, False],
50        'custom_inherit': [True, False],
51        'custom_value': [True, False],
52        'direction_aware': [True, False],
53        'skip': [True, False],
54    }
55    defaults = {
56        'name_for_methods': None,
57        'use_handlers_for': None,
58        'svg': False,
59        'font': False,
60        'converter': None,
61        'direction_aware': False,
62        'skip': False,
63# These depend on property name by default
64        'type_name': None,
65        'getter': None,
66        'setter': None,
67        'initial': None,
68# Setting these stops default handlers being generated
69# Setting custom_all is the same as setting the other three
70        'custom_all': False,
71        'custom_initial': False,
72        'custom_inherit': False,
73        'custom_value': False,
74    }
75
76    def __init__(self, in_files):
77        super(StyleBuilderWriter, self).__init__(in_files)
78        self._outputs = {('StyleBuilderFunctions.h'): self.generate_style_builder_functions_h,
79                         ('StyleBuilderFunctions.cpp'): self.generate_style_builder_functions_cpp,
80                         ('StyleBuilder.cpp'): self.generate_style_builder,
81                        }
82
83        self._properties = self.in_file.name_dictionaries
84
85        def set_if_none(property, key, value):
86            if property[key] is None:
87                property[key] = value
88
89        for property in self._properties:
90            cc = camelcase_property_name(property['name'])
91            property['property_id'] = 'CSSProperty' + cc
92            cc = property['name_for_methods'] or cc.replace('Webkit', '')
93            property['camel_case_name'] = cc
94            set_if_none(property, 'type_name', 'E' + cc)
95            set_if_none(property, 'getter', lower_first(cc))
96            set_if_none(property, 'setter', 'set' + cc)
97            set_if_none(property, 'initial', 'initial' + cc)
98            if property['custom_all']:
99                property['custom_initial'] = True
100                property['custom_inherit'] = True
101                property['custom_value'] = True
102            property['should_declare_functions'] = not property['use_handlers_for'] and not property['direction_aware'] and not property['skip']
103
104        self._properties = dict((property['property_id'], property) for property in self._properties)
105
106    @template_expander.use_jinja('StyleBuilderFunctions.h.tmpl',
107                                 filters=filters)
108    def generate_style_builder_functions_h(self):
109        return {
110            'properties': self._properties,
111        }
112
113    @template_expander.use_jinja('StyleBuilderFunctions.cpp.tmpl',
114                                 filters=filters)
115    def generate_style_builder_functions_cpp(self):
116        return {
117            'properties': self._properties,
118        }
119
120    @template_expander.use_jinja('StyleBuilder.cpp.tmpl', filters=filters)
121    def generate_style_builder(self):
122        return {
123            'properties': self._properties,
124        }
125
126
127if __name__ == '__main__':
128    in_generator.Maker(StyleBuilderWriter).main(sys.argv)
129