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='document'): | 61 factory_provider_name='_Elements'): |
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 or SVG tag | 63 self.tag = tag or name # The HTML 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] | |
80 return info | 79 return info |
81 | 80 |
82 _html_element_constructors = { | 81 _html_element_constructors = { |
83 'AnchorElement' : | 82 'AnchorElement' : |
84 ElementConstructorInfo(tag='a', opt_params=[('DOMString', 'href')]), | 83 ElementConstructorInfo(tag='a', opt_params=[('DOMString', 'href')]), |
85 'AreaElement': 'area', | 84 'AreaElement': 'area', |
86 'ButtonElement': 'button', | 85 'ButtonElement': 'button', |
87 'BRElement': 'br', | 86 'BRElement': 'br', |
88 'BaseElement': 'base', | 87 'BaseElement': 'base', |
89 'BodyElement': 'body', | 88 'BodyElement': 'body', |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 'TableElement': 'table', | 141 'TableElement': 'table', |
143 'TableRowElement': 'tr', | 142 'TableRowElement': 'tr', |
144 #'TableSectionElement' <thead> <tbody> <tfoot> | 143 #'TableSectionElement' <thead> <tbody> <tfoot> |
145 'TextAreaElement': 'textarea', | 144 'TextAreaElement': 'textarea', |
146 'TitleElement': 'title', | 145 'TitleElement': 'title', |
147 'TrackElement': 'track', | 146 'TrackElement': 'track', |
148 'UListElement': 'ul', | 147 'UListElement': 'ul', |
149 'VideoElement': 'video' | 148 'VideoElement': 'video' |
150 } | 149 } |
151 | 150 |
152 _svg_element_constructors = { | 151 def HtmlElementConstructorInfos(typename): |
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'): | |
221 """Returns list of ElementConstructorInfos about the convenience constructors | 152 """Returns list of ElementConstructorInfos about the convenience constructors |
222 for an Element or SvgElement.""" | 153 for an Element.""" |
223 # TODO(sra): Handle multiple and named constructors. | 154 # TODO(sra): Handle multiple and named constructors. |
224 if typename not in element_constructors: | 155 if typename not in _html_element_constructors: |
225 return [] | 156 return [] |
226 infos = element_constructors[typename] | 157 infos = _html_element_constructors[typename] |
227 if isinstance(infos, str): | 158 if isinstance(infos, str): |
228 infos = ElementConstructorInfo(tag=infos, | 159 infos = ElementConstructorInfo(tag=infos) |
229 factory_provider_name=factory_provider_name) | |
230 if not isinstance(infos, list): | 160 if not isinstance(infos, list): |
231 infos = [infos] | 161 infos = [infos] |
232 return infos | 162 return infos |
233 | 163 |
| 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 |
234 # ------------------------------------------------------------------------------ | 185 # ------------------------------------------------------------------------------ |
235 | 186 |
236 class HtmlDartInterfaceGenerator(object): | 187 class HtmlDartInterfaceGenerator(object): |
237 """Generates dart interface and implementation for the DOM IDL interface.""" | 188 """Generates dart interface and implementation for the DOM IDL interface.""" |
238 | 189 |
239 def __init__(self, options, library_emitter, event_generator, interface, | 190 def __init__(self, options, library_emitter, event_generator, interface, |
240 backend): | 191 backend): |
241 self._renamer = options.renamer | 192 self._renamer = options.renamer |
242 self._database = options.database | 193 self._database = options.database |
243 self._template_loader = options.templates | 194 self._template_loader = options.templates |
(...skipping 24 matching lines...) Expand all Loading... |
268 NAME=self._interface.id, | 219 NAME=self._interface.id, |
269 PARAMS=info.ParametersDeclaration(self._DartType)) | 220 PARAMS=info.ParametersDeclaration(self._DartType)) |
270 self._backend.GenerateCallback(info) | 221 self._backend.GenerateCallback(info) |
271 | 222 |
272 def GenerateInterface(self): | 223 def GenerateInterface(self): |
273 interface_name = self._interface_type_info.interface_name() | 224 interface_name = self._interface_type_info.interface_name() |
274 | 225 |
275 factory_provider = None | 226 factory_provider = None |
276 if interface_name in interface_factories: | 227 if interface_name in interface_factories: |
277 factory_provider = interface_factories[interface_name] | 228 factory_provider = interface_factories[interface_name] |
278 factory_constructor_name = None | |
279 | 229 |
280 constructors = [] | 230 constructors = [] |
281 if interface_name in _static_classes: | 231 if interface_name in _static_classes: |
282 constructor_info = None | 232 constructor_info = None |
283 else: | 233 else: |
284 constructor_info = AnalyzeConstructor(self._interface) | 234 constructor_info = AnalyzeConstructor(self._interface) |
285 if constructor_info: | 235 if constructor_info: |
286 constructors.append(constructor_info) | 236 constructors.append(constructor_info) |
287 factory_provider = '_' + interface_name + 'FactoryProvider' | 237 factory_provider = '_' + interface_name + 'FactoryProvider' |
288 factory_provider_emitter = self._library_emitter.FileEmitter( | 238 factory_provider_emitter = self._library_emitter.FileEmitter( |
289 '_%sFactoryProvider' % interface_name, self._library_name) | 239 '_%sFactoryProvider' % interface_name, self._library_name) |
290 self._backend.EmitFactoryProvider( | 240 self._backend.EmitFactoryProvider( |
291 constructor_info, factory_provider, factory_provider_emitter) | 241 constructor_info, factory_provider, factory_provider_emitter) |
292 | 242 |
293 # HTML Elements and SVG Elements have convenience constructors. | 243 infos = HtmlElementConstructorInfos(interface_name) |
294 infos = ElementConstructorInfos(interface_name, | 244 if infos: |
295 _element_constructors[self._library_name], factory_provider_name= | 245 template = self._template_loader.Load( |
296 _factory_ctr_strings[self._library_name]['provider_name']) | 246 'factoryprovider_Elements.darttemplate') |
| 247 EmitHtmlElementFactoryConstructors( |
| 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) |
297 | 254 |
298 if infos: | |
299 factory_constructor_name = _factory_ctr_strings[ | |
300 self._library_name]['constructor_name'] | |
301 | |
302 for info in infos: | 255 for info in infos: |
303 constructors.append(info.ConstructorInfo(self._interface.id)) | 256 constructors.append(info.ConstructorInfo(self._interface.id)) |
304 if factory_provider: | 257 if factory_provider: |
305 assert factory_provider == info.factory_provider_name | 258 assert factory_provider == info.factory_provider_name |
306 else: | 259 else: |
307 factory_provider = info.factory_provider_name | 260 factory_provider = info.factory_provider_name |
308 | 261 |
309 implementation_emitter = self._ImplementationEmitter() | 262 implementation_emitter = self._ImplementationEmitter() |
310 | 263 |
311 base_type_info = None | 264 base_type_info = None |
(...skipping 30 matching lines...) Expand all Loading... |
342 LIBRARYNAME=self._library_name, | 295 LIBRARYNAME=self._library_name, |
343 CLASSNAME=self._interface_type_info.implementation_name(), | 296 CLASSNAME=self._interface_type_info.implementation_name(), |
344 EXTENDS=' extends %s' % base_class if base_class else '', | 297 EXTENDS=' extends %s' % base_class if base_class else '', |
345 IMPLEMENTS=implements_str, | 298 IMPLEMENTS=implements_str, |
346 DOMNAME=self._interface.doc_js_name, | 299 DOMNAME=self._interface.doc_js_name, |
347 NATIVESPEC=self._backend.NativeSpec()) | 300 NATIVESPEC=self._backend.NativeSpec()) |
348 self._backend.StartInterface(self._implementation_members_emitter) | 301 self._backend.StartInterface(self._implementation_members_emitter) |
349 | 302 |
350 self._backend.AddConstructors(constructors, factory_provider, | 303 self._backend.AddConstructors(constructors, factory_provider, |
351 self._interface_type_info.implementation_name(), | 304 self._interface_type_info.implementation_name(), |
352 base_class, factory_constructor_name=factory_constructor_name) | 305 base_class) |
353 | 306 |
354 events_class_name = self._event_generator.ProcessInterface( | 307 events_class_name = self._event_generator.ProcessInterface( |
355 self._interface, interface_name, | 308 self._interface, interface_name, |
356 self._backend.CustomJSMembers(), | 309 self._backend.CustomJSMembers(), |
357 implementation_emitter) | 310 implementation_emitter) |
358 if events_class_name: | 311 if events_class_name: |
359 self._backend.EmitEventGetter(events_class_name) | 312 self._backend.EmitEventGetter(events_class_name) |
360 | 313 |
361 merged_interface = self._interface_type_info.merged_interface() | 314 merged_interface = self._interface_type_info.merged_interface() |
362 if merged_interface: | 315 if merged_interface: |
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
973 'svg': DartLibrary('svg', template_loader, library_type, output_dir), | 926 'svg': DartLibrary('svg', template_loader, library_type, output_dir), |
974 'html': DartLibrary('html', template_loader, library_type, output_dir), | 927 'html': DartLibrary('html', template_loader, library_type, output_dir), |
975 } | 928 } |
976 | 929 |
977 def AddFile(self, basename, library_name, path): | 930 def AddFile(self, basename, library_name, path): |
978 self._libraries[library_name].AddFile(path) | 931 self._libraries[library_name].AddFile(path) |
979 | 932 |
980 def Emit(self, emitter, auxiliary_dir): | 933 def Emit(self, emitter, auxiliary_dir): |
981 for lib in self._libraries.values(): | 934 for lib in self._libraries.values(): |
982 lib.Emit(emitter, auxiliary_dir) | 935 lib.Emit(emitter, auxiliary_dir) |
OLD | NEW |