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 # TODO(efortuna): The following still need to be added to this list. Currently | |
blois
2012/11/16 17:39:05
I'd remove the TODO and these items. They are unim
Emily Fortuna
2012/11/16 17:50:43
nooo my work! :-)
Okay.
| |
202 # they can only be added via SVGElement.tag('tag name'): | |
203 # SVGAltGlyphDefElement, SVGAltGlyphElement, SVGAltGlyphItemElement, | |
204 # SVGComponentTransferFunctionElement, SVGFEBlendElement, | |
205 # SVGFEColorMatrixElement, SVGFEComponentTransferElement, | |
206 # SVGFECompositeElement, SVGFEConvolveMatrixElement, | |
207 # SVGFEDiffuseLightingElement, SVGFEDisplacementMapElement, | |
208 # SVGFEDistantLightElement, SVGFEDropShadowElement, SVGFEFloodElement, | |
209 # SVGFEFuncAElement, SVGFEFuncBElement, SVGFEFuncGElement, SVGFEFuncRElement, | |
210 # SVGFEGaussianBlurElement, SVGFEImageElement, SVGFEMergeElement, | |
211 # SVGFEMergeNodeElement, SVGFEMorphologyElement, SVGFEOffsetElement, | |
212 # SVGFEPointLightElement, SVGFESpecularLightingElement, SVGFESpotLightElement, | |
213 # SVGFETileElement, SVGFETurbulenceElement, SVGGlyphElement, | |
214 # SVGGlyphRefElement, SVGGradientElement, SVGMetadataElement, | |
215 # SVGMissingGlyphElement, SVGTextContentElement, SVGTextPathElement, | |
216 # SVGTextPositioningElement | |
217 } | |
218 | |
219 _element_constructors = { | |
220 'html': _html_element_constructors, | |
221 'svg': _svg_element_constructors | |
222 } | |
223 | |
224 _factory_ctr_strings = { | |
225 'html': { | |
226 'provider_name': 'document', | |
227 'constructor_name': '$dom_createElement' | |
228 }, | |
229 'svg': { | |
230 'provider_name': '_SvgElementFactoryProvider', | |
231 'constructor_name': 'createSVGElement_tag', | |
232 }, | |
233 } | |
234 | |
235 def ElementConstructorInfos(typename, element_constructors, | |
236 factory_provider_name='_Elements'): | |
152 """Returns list of ElementConstructorInfos about the convenience constructors | 237 """Returns list of ElementConstructorInfos about the convenience constructors |
153 for an Element.""" | 238 for an Element or SvgElement.""" |
154 # TODO(sra): Handle multiple and named constructors. | 239 # TODO(sra): Handle multiple and named constructors. |
155 if typename not in _html_element_constructors: | 240 if typename not in element_constructors: |
156 return [] | 241 return [] |
157 infos = _html_element_constructors[typename] | 242 infos = element_constructors[typename] |
158 if isinstance(infos, str): | 243 if isinstance(infos, str): |
159 infos = ElementConstructorInfo(tag=infos) | 244 infos = ElementConstructorInfo(tag=infos, |
245 factory_provider_name=factory_provider_name) | |
160 if not isinstance(infos, list): | 246 if not isinstance(infos, list): |
161 infos = [infos] | 247 infos = [infos] |
162 return infos | 248 return infos |
163 | 249 |
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 # ------------------------------------------------------------------------------ | 250 # ------------------------------------------------------------------------------ |
186 | 251 |
187 class HtmlDartInterfaceGenerator(object): | 252 class HtmlDartInterfaceGenerator(object): |
188 """Generates dart interface and implementation for the DOM IDL interface.""" | 253 """Generates dart interface and implementation for the DOM IDL interface.""" |
189 | 254 |
190 def __init__(self, options, library_emitter, event_generator, interface, | 255 def __init__(self, options, library_emitter, event_generator, interface, |
191 backend): | 256 backend): |
192 self._renamer = options.renamer | 257 self._renamer = options.renamer |
193 self._database = options.database | 258 self._database = options.database |
194 self._template_loader = options.templates | 259 self._template_loader = options.templates |
(...skipping 24 matching lines...) Expand all Loading... | |
219 NAME=self._interface.id, | 284 NAME=self._interface.id, |
220 PARAMS=info.ParametersDeclaration(self._DartType)) | 285 PARAMS=info.ParametersDeclaration(self._DartType)) |
221 self._backend.GenerateCallback(info) | 286 self._backend.GenerateCallback(info) |
222 | 287 |
223 def GenerateInterface(self): | 288 def GenerateInterface(self): |
224 interface_name = self._interface_type_info.interface_name() | 289 interface_name = self._interface_type_info.interface_name() |
225 | 290 |
226 factory_provider = None | 291 factory_provider = None |
227 if interface_name in interface_factories: | 292 if interface_name in interface_factories: |
228 factory_provider = interface_factories[interface_name] | 293 factory_provider = interface_factories[interface_name] |
294 factory_constructor_name = None | |
229 | 295 |
230 constructors = [] | 296 constructors = [] |
231 if interface_name in _static_classes: | 297 if interface_name in _static_classes: |
232 constructor_info = None | 298 constructor_info = None |
233 else: | 299 else: |
234 constructor_info = AnalyzeConstructor(self._interface) | 300 constructor_info = AnalyzeConstructor(self._interface) |
235 if constructor_info: | 301 if constructor_info: |
236 constructors.append(constructor_info) | 302 constructors.append(constructor_info) |
237 factory_provider = '_' + interface_name + 'FactoryProvider' | 303 factory_provider = '_' + interface_name + 'FactoryProvider' |
238 factory_provider_emitter = self._library_emitter.FileEmitter( | 304 factory_provider_emitter = self._library_emitter.FileEmitter( |
239 '_%sFactoryProvider' % interface_name, self._library_name) | 305 '_%sFactoryProvider' % interface_name, self._library_name) |
240 self._backend.EmitFactoryProvider( | 306 self._backend.EmitFactoryProvider( |
241 constructor_info, factory_provider, factory_provider_emitter) | 307 constructor_info, factory_provider, factory_provider_emitter) |
242 | 308 |
243 infos = HtmlElementConstructorInfos(interface_name) | 309 # HTML Elements and SVG Elements have convenience constructors. |
310 infos = ElementConstructorInfos(interface_name, | |
311 _element_constructors[self._library_name], factory_provider_name= | |
312 _factory_ctr_strings[self._library_name]['provider_name']) | |
313 | |
244 if infos: | 314 if infos: |
245 template = self._template_loader.Load( | 315 factory_constructor_name = _factory_ctr_strings[ |
246 'factoryprovider_Elements.darttemplate') | 316 self._library_name]['constructor_name'] |
247 EmitHtmlElementFactoryConstructors( | 317 |
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: | 318 for info in infos: |
256 constructors.append(info.ConstructorInfo(self._interface.id)) | 319 constructors.append(info.ConstructorInfo(self._interface.id)) |
257 if factory_provider: | 320 if factory_provider: |
258 assert factory_provider == info.factory_provider_name | 321 assert factory_provider == info.factory_provider_name |
259 else: | 322 else: |
260 factory_provider = info.factory_provider_name | 323 factory_provider = info.factory_provider_name |
261 | 324 |
262 implementation_emitter = self._ImplementationEmitter() | 325 implementation_emitter = self._ImplementationEmitter() |
263 | 326 |
264 base_type_info = None | 327 base_type_info = None |
(...skipping 30 matching lines...) Expand all Loading... | |
295 LIBRARYNAME=self._library_name, | 358 LIBRARYNAME=self._library_name, |
296 CLASSNAME=self._interface_type_info.implementation_name(), | 359 CLASSNAME=self._interface_type_info.implementation_name(), |
297 EXTENDS=' extends %s' % base_class if base_class else '', | 360 EXTENDS=' extends %s' % base_class if base_class else '', |
298 IMPLEMENTS=implements_str, | 361 IMPLEMENTS=implements_str, |
299 DOMNAME=self._interface.doc_js_name, | 362 DOMNAME=self._interface.doc_js_name, |
300 NATIVESPEC=self._backend.NativeSpec()) | 363 NATIVESPEC=self._backend.NativeSpec()) |
301 self._backend.StartInterface(self._implementation_members_emitter) | 364 self._backend.StartInterface(self._implementation_members_emitter) |
302 | 365 |
303 self._backend.AddConstructors(constructors, factory_provider, | 366 self._backend.AddConstructors(constructors, factory_provider, |
304 self._interface_type_info.implementation_name(), | 367 self._interface_type_info.implementation_name(), |
305 base_class) | 368 base_class, factory_constructor_name=factory_constructor_name) |
306 | 369 |
307 events_class_name = self._event_generator.ProcessInterface( | 370 events_class_name = self._event_generator.ProcessInterface( |
308 self._interface, interface_name, | 371 self._interface, interface_name, |
309 self._backend.CustomJSMembers(), | 372 self._backend.CustomJSMembers(), |
310 implementation_emitter) | 373 implementation_emitter) |
311 if events_class_name: | 374 if events_class_name: |
312 self._backend.EmitEventGetter(events_class_name) | 375 self._backend.EmitEventGetter(events_class_name) |
313 | 376 |
314 merged_interface = self._interface_type_info.merged_interface() | 377 merged_interface = self._interface_type_info.merged_interface() |
315 if merged_interface: | 378 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), | 989 'svg': DartLibrary('svg', template_loader, library_type, output_dir), |
927 'html': DartLibrary('html', template_loader, library_type, output_dir), | 990 'html': DartLibrary('html', template_loader, library_type, output_dir), |
928 } | 991 } |
929 | 992 |
930 def AddFile(self, basename, library_name, path): | 993 def AddFile(self, basename, library_name, path): |
931 self._libraries[library_name].AddFile(path) | 994 self._libraries[library_name].AddFile(path) |
932 | 995 |
933 def Emit(self, emitter, auxiliary_dir): | 996 def Emit(self, emitter, auxiliary_dir): |
934 for lib in self._libraries.values(): | 997 for lib in self._libraries.values(): |
935 lib.Emit(emitter, auxiliary_dir) | 998 lib.Emit(emitter, auxiliary_dir) |
OLD | NEW |