Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / build / scripts / make_element_factory.py
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
30 import sys
31 from collections import defaultdict
32
33 import in_generator
34 import template_expander
35 import name_utilities
36
37 from make_qualified_names import MakeQualifiedNamesWriter
38
39
40 class MakeElementFactoryWriter(MakeQualifiedNamesWriter):
41     defaults = dict(MakeQualifiedNamesWriter.default_parameters, **{
42         'JSInterfaceName': None,
43         'Conditional': None,
44         'constructorNeedsCreatedByParser': None,
45         'constructorNeedsFormElement': None,
46         'interfaceName': None,
47         'noConstructor': None,
48         'noTypeHelpers': None,
49         'runtimeEnabled': None,
50     })
51     default_parameters = dict(MakeQualifiedNamesWriter.default_parameters, **{
52         'fallbackInterfaceName': '',
53         'fallbackJSInterfaceName': '',
54     })
55     filters = MakeQualifiedNamesWriter.filters
56
57     def __init__(self, in_file_paths):
58         super(MakeElementFactoryWriter, self).__init__(in_file_paths)
59
60         # FIXME: When we start using these element factories, we'll want to
61         # remove the "new" prefix and also have our base class generate
62         # *Names.h and *Names.cpp.
63         self._outputs.update({
64             (self.namespace + 'ElementFactory.h'): self.generate_factory_header,
65             (self.namespace + 'ElementFactory.cpp'): self.generate_factory_implementation,
66             ('V8' + self.namespace + 'ElementWrapperFactory.h'): self.generate_wrapper_factory_header,
67             ('V8' + self.namespace + 'ElementWrapperFactory.cpp'): self.generate_wrapper_factory_implementation,
68         })
69
70         fallback_interface = self.tags_in_file.parameters['fallbackInterfaceName'].strip('"')
71         fallback_js_interface = self.tags_in_file.parameters['fallbackJSInterfaceName'].strip('"') or fallback_interface
72
73         interface_counts = defaultdict(int)
74         tags = self._template_context['tags']
75         for tag in tags:
76             tag['has_js_interface'] = self._has_js_interface(tag)
77             tag['js_interface'] = self._js_interface(tag)
78             tag['interface'] = self._interface(tag)
79             interface_counts[tag['interface']] += 1
80
81         for tag in tags:
82             tag['multipleTagNames'] = (interface_counts[tag['interface']] > 1 or tag['interface'] == fallback_interface)
83
84         self._template_context.update({
85             'fallback_interface': fallback_interface,
86             'fallback_js_interface': fallback_js_interface,
87         })
88
89     @template_expander.use_jinja('ElementFactory.h.tmpl', filters=filters)
90     def generate_factory_header(self):
91         return self._template_context
92
93     @template_expander.use_jinja('ElementFactory.cpp.tmpl', filters=filters)
94     def generate_factory_implementation(self):
95         return self._template_context
96
97     @template_expander.use_jinja('ElementWrapperFactory.h.tmpl', filters=filters)
98     def generate_wrapper_factory_header(self):
99         return self._template_context
100
101     @template_expander.use_jinja('ElementWrapperFactory.cpp.tmpl', filters=filters)
102     def generate_wrapper_factory_implementation(self):
103         return self._template_context
104
105     def _interface(self, tag):
106         if tag['interfaceName']:
107             return tag['interfaceName']
108         name = name_utilities.upper_first(tag['name'])
109         # FIXME: We shouldn't hard-code HTML here.
110         if name == 'HTML':
111             name = 'Html'
112         dash = name.find('-')
113         while dash != -1:
114             name = name[:dash] + name[dash + 1].upper() + name[dash + 2:]
115             dash = name.find('-')
116         return '%s%sElement' % (self.namespace, name)
117
118     def _js_interface(self, tag):
119         if tag['JSInterfaceName']:
120             return tag['JSInterfaceName']
121         return self._interface(tag)
122
123     def _has_js_interface(self, tag):
124         return not tag['noConstructor'] and self._js_interface(tag) != ('%sElement' % self.namespace)
125
126
127 if __name__ == "__main__":
128     in_generator.Maker(MakeElementFactoryWriter).main(sys.argv)