OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 """This module provides shared functionality for the system to generate | 6 """This module provides shared functionality for the system to generate |
7 Dart:html APIs from the IDL database.""" | 7 Dart:html APIs from the IDL database.""" |
8 | 8 |
9 import emitter | 9 import emitter |
10 import os | 10 import os |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 # Information for generating element constructors. | 51 # Information for generating element constructors. |
52 # | 52 # |
53 # TODO(sra): maybe remove all the argument complexity and use cascades. | 53 # TODO(sra): maybe remove all the argument complexity and use cascades. |
54 # | 54 # |
55 # var c = new CanvasElement(width: 100, height: 70); | 55 # var c = new CanvasElement(width: 100, height: 70); |
56 # var c = new CanvasElement()..width = 100..height = 70; | 56 # var c = new CanvasElement()..width = 100..height = 70; |
57 # | 57 # |
58 class ElementConstructorInfo(object): | 58 class ElementConstructorInfo(object): |
59 def __init__(self, name=None, tag=None, | 59 def __init__(self, name=None, tag=None, |
60 params=[], opt_params=[], | 60 params=[], opt_params=[], |
61 factory_provider_name='_Elements'): | 61 factory_provider_name='document'): |
62 self.name = name # The constructor name 'h1' in 'HeadingElement.h1' | 62 self.name = name # The constructor name 'h1' in 'HeadingElement.h1' |
63 self.tag = tag or name # The HTML tag | 63 self.tag = tag or name # The HTML or SVG tag |
64 self.params = params | 64 self.params = params |
65 self.opt_params = opt_params | 65 self.opt_params = opt_params |
66 self.factory_provider_name = factory_provider_name | 66 self.factory_provider_name = factory_provider_name |
67 | 67 |
68 def ConstructorInfo(self, interface_name): | 68 def ConstructorInfo(self, interface_name): |
69 info = OperationInfo() | 69 info = OperationInfo() |
70 info.overloads = None | 70 info.overloads = None |
71 info.declared_name = interface_name | 71 info.declared_name = interface_name |
72 info.name = interface_name | 72 info.name = interface_name |
73 info.constructor_name = self.name | 73 info.constructor_name = self.name |
74 info.js_name = None | 74 info.js_name = None |
75 info.type_name = interface_name | 75 info.type_name = interface_name |
76 info.param_infos = map(lambda tXn: ParamInfo(tXn[1], tXn[0], True), | 76 info.param_infos = map(lambda tXn: ParamInfo(tXn[1], tXn[0], True), |
77 self.opt_params) | 77 self.opt_params) |
78 info.requires_named_arguments = True | 78 info.requires_named_arguments = True |
| 79 info.factory_parameters = ['"%s"' % self.tag] |
79 return info | 80 return info |
80 | 81 |
81 _html_element_constructors = { | 82 _html_element_constructors = { |
82 'AnchorElement' : | 83 'AnchorElement' : |
83 ElementConstructorInfo(tag='a', opt_params=[('DOMString', 'href')]), | 84 ElementConstructorInfo(tag='a', opt_params=[('DOMString', 'href')]), |
84 'AreaElement': 'area', | 85 'AreaElement': 'area', |
85 'ButtonElement': 'button', | 86 'ButtonElement': 'button', |
86 'BRElement': 'br', | 87 'BRElement': 'br', |
87 'BaseElement': 'base', | 88 'BaseElement': 'base', |
88 'BodyElement': 'body', | 89 'BodyElement': 'body', |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 'TableElement': 'table', | 142 'TableElement': 'table', |
142 'TableRowElement': 'tr', | 143 'TableRowElement': 'tr', |
143 #'TableSectionElement' <thead> <tbody> <tfoot> | 144 #'TableSectionElement' <thead> <tbody> <tfoot> |
144 'TextAreaElement': 'textarea', | 145 'TextAreaElement': 'textarea', |
145 'TitleElement': 'title', | 146 'TitleElement': 'title', |
146 'TrackElement': 'track', | 147 'TrackElement': 'track', |
147 'UListElement': 'ul', | 148 'UListElement': 'ul', |
148 'VideoElement': 'video' | 149 'VideoElement': 'video' |
149 } | 150 } |
150 | 151 |
151 def HtmlElementConstructorInfos(typename): | 152 _svg_element_constructors = { |
| 153 'SVGAElement': 'a', |
| 154 'SVGAnimateColorElement': 'animateColor', |
| 155 'SVGAnimateElement': 'animate', |
| 156 'SVGAnimateMotionElement': 'animateMotion', |
| 157 'SVGAnimateTransformElement': 'animateTransform', |
| 158 'SVGAnimationElement': 'animation', |
| 159 'SVGCircleElement': 'circle', |
| 160 'SVGClipPathElement': 'clipPath', |
| 161 'SVGCursorElement': 'cursor', |
| 162 'SVGDefsElement': 'defs', |
| 163 'SVGDescElement': 'desc', |
| 164 'SVGEllipseElement': 'ellipse', |
| 165 'SVGFilterElement': 'filter', |
| 166 'SVGFontElement': 'font', |
| 167 'SVGFontFaceElement': 'font-face', |
| 168 'SVGFontFaceFormatElement': 'font-face-format', |
| 169 'SVGFontFaceNameElement': 'font-face-name', |
| 170 'SVGFontFaceSrcElement': 'font-face-src', |
| 171 'SVGFontFaceUriElement': 'font-face-uri', |
| 172 'SVGForeignObjectElement': 'foreignObject', |
| 173 'SVGGlyphElement': 'glyph', |
| 174 'SVGGElement': 'g', |
| 175 'SVGHKernElement': 'hkern', |
| 176 'SVGImageElement': 'image', |
| 177 'SVGLinearGradientElement': 'linearGradient', |
| 178 'SVGLineElement': 'line', |
| 179 'SVGMarkerElement': 'marker', |
| 180 'SVGMaskElement': 'mask', |
| 181 'SVGMPathElement': 'mpath', |
| 182 'SVGPathElement': 'path', |
| 183 'SVGPatternElement': 'pattern', |
| 184 'SVGPolygonElement': 'polygon', |
| 185 'SVGPolylineElement': 'polyline', |
| 186 'SVGRadialGradientElement': 'radialGradient', |
| 187 'SVGRectElement': 'rect', |
| 188 'SVGScriptElement': 'script', |
| 189 'SVGSetElement': 'set', |
| 190 'SVGStopElement': 'stop', |
| 191 'SVGStyleElement': 'style', |
| 192 'SVGSwitchElement': 'switch', |
| 193 'SVGSymbolElement': 'symbol', |
| 194 'SVGTextElement': 'text', |
| 195 'SVGTitleElement': 'title', |
| 196 'SVGTRefElement': 'tref', |
| 197 'SVGTSpanElement': 'tspan', |
| 198 'SVGUseElement': 'use', |
| 199 'SVGViewElement': 'view', |
| 200 'SVGVKernElement': 'vkern', |
| 201 } |
| 202 |
| 203 _element_constructors = { |
| 204 'html': _html_element_constructors, |
| 205 'svg': _svg_element_constructors |
| 206 } |
| 207 |
| 208 _factory_ctr_strings = { |
| 209 'html': { |
| 210 'provider_name': 'document', |
| 211 'constructor_name': '$dom_createElement' |
| 212 }, |
| 213 'svg': { |
| 214 'provider_name': '_SvgElementFactoryProvider', |
| 215 'constructor_name': 'createSVGElement_tag', |
| 216 }, |
| 217 } |
| 218 |
| 219 def ElementConstructorInfos(typename, element_constructors, |
| 220 factory_provider_name='_Elements'): |
152 """Returns list of ElementConstructorInfos about the convenience constructors | 221 """Returns list of ElementConstructorInfos about the convenience constructors |
153 for an Element.""" | 222 for an Element or SvgElement.""" |
154 # TODO(sra): Handle multiple and named constructors. | 223 # TODO(sra): Handle multiple and named constructors. |
155 if typename not in _html_element_constructors: | 224 if typename not in element_constructors: |
156 return [] | 225 return [] |
157 infos = _html_element_constructors[typename] | 226 infos = element_constructors[typename] |
158 if isinstance(infos, str): | 227 if isinstance(infos, str): |
159 infos = ElementConstructorInfo(tag=infos) | 228 infos = ElementConstructorInfo(tag=infos, |
| 229 factory_provider_name=factory_provider_name) |
160 if not isinstance(infos, list): | 230 if not isinstance(infos, list): |
161 infos = [infos] | 231 infos = [infos] |
162 return infos | 232 return infos |
163 | 233 |
164 def EmitHtmlElementFactoryConstructors(emitter, infos, typename, class_name, | |
165 rename_type): | |
166 for info in infos: | |
167 constructor_info = info.ConstructorInfo(typename) | |
168 | |
169 inits = emitter.Emit( | |
170 '\n' | |
171 ' static $RETURN_TYPE $CONSTRUCTOR($PARAMS) {\n' | |
172 ' $CLASS _e = document.$dom_createElement("$TAG");\n' | |
173 '$!INITS' | |
174 ' return _e;\n' | |
175 ' }\n', | |
176 RETURN_TYPE=rename_type(constructor_info.type_name), | |
177 CONSTRUCTOR=constructor_info.ConstructorFactoryName(rename_type), | |
178 CLASS=class_name, | |
179 TAG=info.tag, | |
180 PARAMS=constructor_info.ParametersDeclaration( | |
181 rename_type, force_optional=True)) | |
182 for param in constructor_info.param_infos: | |
183 inits.Emit(' if ($E != null) _e.$E = $E;\n', E=param.name) | |
184 | |
185 # ------------------------------------------------------------------------------ | 234 # ------------------------------------------------------------------------------ |
186 | 235 |
187 class HtmlDartInterfaceGenerator(object): | 236 class HtmlDartInterfaceGenerator(object): |
188 """Generates dart interface and implementation for the DOM IDL interface.""" | 237 """Generates dart interface and implementation for the DOM IDL interface.""" |
189 | 238 |
190 def __init__(self, options, library_emitter, event_generator, interface, | 239 def __init__(self, options, library_emitter, event_generator, interface, |
191 backend): | 240 backend): |
192 self._renamer = options.renamer | 241 self._renamer = options.renamer |
193 self._database = options.database | 242 self._database = options.database |
194 self._template_loader = options.templates | 243 self._template_loader = options.templates |
(...skipping 24 matching lines...) Expand all Loading... |
219 NAME=self._interface.id, | 268 NAME=self._interface.id, |
220 PARAMS=info.ParametersDeclaration(self._DartType)) | 269 PARAMS=info.ParametersDeclaration(self._DartType)) |
221 self._backend.GenerateCallback(info) | 270 self._backend.GenerateCallback(info) |
222 | 271 |
223 def GenerateInterface(self): | 272 def GenerateInterface(self): |
224 interface_name = self._interface_type_info.interface_name() | 273 interface_name = self._interface_type_info.interface_name() |
225 | 274 |
226 factory_provider = None | 275 factory_provider = None |
227 if interface_name in interface_factories: | 276 if interface_name in interface_factories: |
228 factory_provider = interface_factories[interface_name] | 277 factory_provider = interface_factories[interface_name] |
| 278 factory_constructor_name = None |
229 | 279 |
230 constructors = [] | 280 constructors = [] |
231 if interface_name in _static_classes: | 281 if interface_name in _static_classes: |
232 constructor_info = None | 282 constructor_info = None |
233 else: | 283 else: |
234 constructor_info = AnalyzeConstructor(self._interface) | 284 constructor_info = AnalyzeConstructor(self._interface) |
235 if constructor_info: | 285 if constructor_info: |
236 constructors.append(constructor_info) | 286 constructors.append(constructor_info) |
237 factory_provider = '_' + interface_name + 'FactoryProvider' | 287 factory_provider = '_' + interface_name + 'FactoryProvider' |
238 factory_provider_emitter = self._library_emitter.FileEmitter( | 288 factory_provider_emitter = self._library_emitter.FileEmitter( |
239 '_%sFactoryProvider' % interface_name, self._library_name) | 289 '_%sFactoryProvider' % interface_name, self._library_name) |
240 self._backend.EmitFactoryProvider( | 290 self._backend.EmitFactoryProvider( |
241 constructor_info, factory_provider, factory_provider_emitter) | 291 constructor_info, factory_provider, factory_provider_emitter) |
242 | 292 |
243 infos = HtmlElementConstructorInfos(interface_name) | 293 # HTML Elements and SVG Elements have convenience constructors. |
| 294 infos = ElementConstructorInfos(interface_name, |
| 295 _element_constructors[self._library_name], factory_provider_name= |
| 296 _factory_ctr_strings[self._library_name]['provider_name']) |
| 297 |
244 if infos: | 298 if infos: |
245 template = self._template_loader.Load( | 299 factory_constructor_name = _factory_ctr_strings[ |
246 'factoryprovider_Elements.darttemplate') | 300 self._library_name]['constructor_name'] |
247 EmitHtmlElementFactoryConstructors( | 301 |
248 self._library_emitter.FileEmitter('_Elements', self._library_name, | |
249 template), | |
250 infos, | |
251 self._interface.id, | |
252 self._interface_type_info.implementation_name(), | |
253 self._DartType) | |
254 | |
255 for info in infos: | 302 for info in infos: |
256 constructors.append(info.ConstructorInfo(self._interface.id)) | 303 constructors.append(info.ConstructorInfo(self._interface.id)) |
257 if factory_provider: | 304 if factory_provider: |
258 assert factory_provider == info.factory_provider_name | 305 assert factory_provider == info.factory_provider_name |
259 else: | 306 else: |
260 factory_provider = info.factory_provider_name | 307 factory_provider = info.factory_provider_name |
261 | 308 |
262 implementation_emitter = self._ImplementationEmitter() | 309 implementation_emitter = self._ImplementationEmitter() |
263 | 310 |
264 base_type_info = None | 311 base_type_info = None |
(...skipping 30 matching lines...) Expand all Loading... |
295 LIBRARYNAME=self._library_name, | 342 LIBRARYNAME=self._library_name, |
296 CLASSNAME=self._interface_type_info.implementation_name(), | 343 CLASSNAME=self._interface_type_info.implementation_name(), |
297 EXTENDS=' extends %s' % base_class if base_class else '', | 344 EXTENDS=' extends %s' % base_class if base_class else '', |
298 IMPLEMENTS=implements_str, | 345 IMPLEMENTS=implements_str, |
299 DOMNAME=self._interface.doc_js_name, | 346 DOMNAME=self._interface.doc_js_name, |
300 NATIVESPEC=self._backend.NativeSpec()) | 347 NATIVESPEC=self._backend.NativeSpec()) |
301 self._backend.StartInterface(self._implementation_members_emitter) | 348 self._backend.StartInterface(self._implementation_members_emitter) |
302 | 349 |
303 self._backend.AddConstructors(constructors, factory_provider, | 350 self._backend.AddConstructors(constructors, factory_provider, |
304 self._interface_type_info.implementation_name(), | 351 self._interface_type_info.implementation_name(), |
305 base_class) | 352 base_class, factory_constructor_name=factory_constructor_name) |
306 | 353 |
307 events_class_name = self._event_generator.ProcessInterface( | 354 events_class_name = self._event_generator.ProcessInterface( |
308 self._interface, interface_name, | 355 self._interface, interface_name, |
309 self._backend.CustomJSMembers(), | 356 self._backend.CustomJSMembers(), |
310 implementation_emitter) | 357 implementation_emitter) |
311 if events_class_name: | 358 if events_class_name: |
312 self._backend.EmitEventGetter(events_class_name) | 359 self._backend.EmitEventGetter(events_class_name) |
313 | 360 |
314 merged_interface = self._interface_type_info.merged_interface() | 361 merged_interface = self._interface_type_info.merged_interface() |
315 if merged_interface: | 362 if merged_interface: |
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
926 'svg': DartLibrary('svg', template_loader, library_type, output_dir), | 973 'svg': DartLibrary('svg', template_loader, library_type, output_dir), |
927 'html': DartLibrary('html', template_loader, library_type, output_dir), | 974 'html': DartLibrary('html', template_loader, library_type, output_dir), |
928 } | 975 } |
929 | 976 |
930 def AddFile(self, basename, library_name, path): | 977 def AddFile(self, basename, library_name, path): |
931 self._libraries[library_name].AddFile(path) | 978 self._libraries[library_name].AddFile(path) |
932 | 979 |
933 def Emit(self, emitter, auxiliary_dir): | 980 def Emit(self, emitter, auxiliary_dir): |
934 for lib in self._libraries.values(): | 981 for lib in self._libraries.values(): |
935 lib.Emit(emitter, auxiliary_dir) | 982 lib.Emit(emitter, auxiliary_dir) |
OLD | NEW |