193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#!/usr/bin/env python
293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# Copyright (C) 2013 Google Inc. All rights reserved.
393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#
493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# Redistribution and use in source and binary forms, with or without
593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# modification, are permitted provided that the following conditions are
693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# met:
793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#
893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#     * Redistributions of source code must retain the above copyright
993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# notice, this list of conditions and the following disclaimer.
1093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#     * Redistributions in binary form must reproduce the above
1193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
1293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# in the documentation and/or other materials provided with the
1393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# distribution.
1493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#     * Neither the name of Google Inc. nor the names of its
1593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# contributors may be used to endorse or promote products derived from
1693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# this software without specific prior written permission.
1793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#
1893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)import re
3193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)import sys
3293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)import in_generator
345267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)import template_expander
3593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)class StyleBuilderWriter(in_generator.Writer):
3893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    class_name = 'StyleBuilder'
3993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
4093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    valid_values = {
41f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)        'svg': [True, False],
425267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        'custom_all': [True, False],
435267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        'custom_initial': [True, False],
445267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        'custom_inherit': [True, False],
455267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        'custom_value': [True, False],
4693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
4793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    defaults = {
4893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        'condition': None,
4993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        'name_for_methods': None,
505267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        'use_handlers_for': None,
51f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)        'svg': False,
52e6d4491e48613634a83c1957c72759da80987961Ben Murdoch        'converter': None,
5393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# These depend on property name by default
5493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        'type_name': None,
5593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        'getter': None,
5693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        'setter': None,
5793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        'initial': None,
585267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)# Setting these stops default handlers being generated
595267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)# Setting custom_all is the same as setting the other three
605267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        'custom_all': False,
615267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        'custom_initial': False,
625267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        'custom_inherit': False,
635267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        'custom_value': False,
6493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
6593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
6693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    def __init__(self, in_files, enabled_conditions):
6793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        super(StyleBuilderWriter, self).__init__(in_files, enabled_conditions)
685267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        self._outputs = {("StyleBuilderFunctions.h"): self.generate_style_builder_functions_h,
695267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                         ("StyleBuilderFunctions.cpp"): self.generate_style_builder_functions_cpp,
705267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                         ("StyleBuilder.cpp"): self.generate_style_builder,
715267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                        }
725267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
7393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        self._properties = self.in_file.name_dictionaries
7493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
7593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        def set_if_none(property, key, value):
7693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            if property[key] is None:
7793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)                property[key] = value
7893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
7993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        for property in self._properties:
8093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            cc = self._camelcase_property_name(property["name"])
8193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            property["property_id"] = "CSSProperty" + cc
8293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            cc = property["name_for_methods"] or cc.replace("Webkit", "")
835267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)            property["camel_case_name"] = cc
8493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            set_if_none(property, "type_name", "E" + cc)
8593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            set_if_none(property, "getter", self._lower_first(cc))
8693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            set_if_none(property, "setter", "set" + cc)
8793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            set_if_none(property, "initial", "initial" + cc)
885267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)            if property["custom_all"]:
895267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                property["custom_initial"] = True
905267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                property["custom_inherit"] = True
915267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                property["custom_value"] = True
925267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
935267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        self._properties = dict((property["property_id"], property) for property in self._properties)
9493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
9593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)# FIXME: some of these might be better in a utils file
9693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    @staticmethod
9793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    def _camelcase_property_name(property_name):
9893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return re.sub(r'(^[^-])|-(.)', lambda match: (match.group(1) or match.group(2)).upper(), property_name)
9993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
10093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    @staticmethod
10193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    def _lower_first(s):
10293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return s[0].lower() + s[1:]
10393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
10493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    @staticmethod
10593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    def _upper_first(s):
10693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return s[0].upper() + s[1:]
10793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
1085267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    @template_expander.use_jinja("StyleBuilderFunctions.h.tmpl")
1095267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    def generate_style_builder_functions_h(self):
1105267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return {
1115267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)            "properties": self._properties,
1125267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        }
1135267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1145267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    @template_expander.use_jinja("StyleBuilderFunctions.cpp.tmpl")
1155267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    def generate_style_builder_functions_cpp(self):
1165267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return {
1175267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)            "properties": self._properties,
1185267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        }
1195267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1205267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    @template_expander.use_jinja("StyleBuilder.cpp.tmpl")
1215267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    def generate_style_builder(self):
12293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return {
12393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            "properties": self._properties,
12493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        }
12593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
12693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
12793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)if __name__ == "__main__":
12893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    in_generator.Maker(StyleBuilderWriter).main(sys.argv)
129