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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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='_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 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 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 'TableElement': 'table', | 141 'TableElement': 'table', |
142 'TableRowElement': 'tr', | 142 'TableRowElement': 'tr', |
143 #'TableSectionElement' <thead> <tbody> <tfoot> | 143 #'TableSectionElement' <thead> <tbody> <tfoot> |
144 'TextAreaElement': 'textarea', | 144 'TextAreaElement': 'textarea', |
145 'TitleElement': 'title', | 145 'TitleElement': 'title', |
146 'TrackElement': 'track', | 146 'TrackElement': 'track', |
147 'UListElement': 'ul', | 147 'UListElement': 'ul', |
148 'VideoElement': 'video' | 148 'VideoElement': 'video' |
149 } | 149 } |
150 | 150 |
151 def HtmlElementConstructorInfos(typename): | 151 _svg_element_constructors = { |
152 'SVGAElement': 'a', | |
153 'SVGAnimateColorElement': 'animateColor', | |
154 'SVGAnimateElement': 'animate', | |
155 'SVGAnimateMotionElement': 'animateMotion', | |
156 'SVGAnimateTransformElement': 'animateTransform', | |
157 'SVGAnimationElement': 'animation', | |
158 'SVGCircleElement': 'circle', | |
159 'SVGClipPathElement': 'clipPath', | |
160 'SVGCursorElement': 'cursor', | |
161 'SVGDefsElement': 'defs', | |
162 'SVGDescElement': 'desc', | |
163 'SVGEllipseElement': 'ellipse', | |
164 'SVGFilterElement': 'filter', | |
165 'SVGFontElement': 'font', | |
166 'SVGFontFaceElement': 'font-face', | |
167 'SVGFontFaceFormatElement': 'font-face-format', | |
168 'SVGFontFaceNameElement': 'font-face-name', | |
169 'SVGFontFaceSrcElement': 'font-face-src', | |
170 'SVGFontFaceUriElement': 'font-face-uri', | |
171 'SVGForeignObjectElement': 'foreignObject', | |
172 'SVGGlyphElement': 'glyph', | |
173 'SVGGElement': 'g', | |
174 'SVGHKernElement': 'hkern', | |
175 'SVGImageElement': 'image', | |
176 'SVGLinearGradientElement': 'linearGradient', | |
177 'SVGLineElement': 'line', | |
178 'SVGMarkerElement': 'marker', | |
179 'SVGMaskElement': 'mask', | |
180 'SVGMPathElement': 'mpath', | |
181 'SVGPathElement': 'path', | |
182 'SVGPatternElement': 'pattern', | |
183 'SVGPolygonElement': 'polygon', | |
184 'SVGPolylineElement': 'polyline', | |
185 'SVGRadialGradientElement': 'radialGradient', | |
186 'SVGRectElement': 'rect', | |
187 'SVGScriptElement': 'script', | |
188 'SVGSetElement': 'set', | |
189 'SVGStopElement': 'stop', | |
190 'SVGStyleElement': 'style', | |
191 'SVGSVGElement': 'svg', | |
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 | |
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 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, | 234 def EmitElementFactoryConstructors(emitter, infos, typename, class_name, |
165 rename_type): | 235 rename_type, isHtmlElement): |
236 """Emits convenience factory contructors for HtmlElements and SvgElements.""" | |
166 for info in infos: | 237 for info in infos: |
167 constructor_info = info.ConstructorInfo(typename) | 238 constructor_info = info.ConstructorInfo(typename) |
239 emitted_string = '\n static $RETURN_TYPE $CONSTRUCTOR($PARAMS) {\n' | |
240 if isHtmlElement: | |
241 emitted_string += ' $CLASS _e = document.$dom_createElement("$TAG");\n' | |
blois
2012/11/15 01:35:49
Should just be able to use _SVGElementFactoryProvi
Emily Fortuna
2012/11/15 01:36:51
See below for the SVG cases. This is for the HtmlE
| |
242 else: | |
243 emitted_string += ( | |
244 ' final Element _e =\n' | |
245 ' document.$dom_createElementNS(' | |
246 '"http://www.w3.org/2000/svg", "$TAG");\n') | |
247 emitted_string += ( | |
248 '$!INITS' | |
249 ' return _e;\n' | |
250 ' }\n') | |
168 | 251 |
169 inits = emitter.Emit( | 252 inits = emitter.Emit( |
170 '\n' | 253 emitted_string, |
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), | 254 RETURN_TYPE=rename_type(constructor_info.type_name), |
177 CONSTRUCTOR=constructor_info.ConstructorFactoryName(rename_type), | 255 CONSTRUCTOR=constructor_info.ConstructorFactoryName(rename_type), |
178 CLASS=class_name, | 256 CLASS=class_name, |
179 TAG=info.tag, | 257 TAG=info.tag, |
180 PARAMS=constructor_info.ParametersDeclaration( | 258 PARAMS=constructor_info.ParametersDeclaration( |
181 rename_type, force_optional=True)) | 259 rename_type, force_optional=True)) |
182 for param in constructor_info.param_infos: | 260 for param in constructor_info.param_infos: |
183 inits.Emit(' if ($E != null) _e.$E = $E;\n', E=param.name) | 261 inits.Emit(' if ($E != null) _e.$E = $E;\n', E=param.name) |
184 | 262 |
185 # ------------------------------------------------------------------------------ | 263 # ------------------------------------------------------------------------------ |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 else: | 311 else: |
234 constructor_info = AnalyzeConstructor(self._interface) | 312 constructor_info = AnalyzeConstructor(self._interface) |
235 if constructor_info: | 313 if constructor_info: |
236 constructors.append(constructor_info) | 314 constructors.append(constructor_info) |
237 factory_provider = '_' + interface_name + 'FactoryProvider' | 315 factory_provider = '_' + interface_name + 'FactoryProvider' |
238 factory_provider_emitter = self._library_emitter.FileEmitter( | 316 factory_provider_emitter = self._library_emitter.FileEmitter( |
239 '_%sFactoryProvider' % interface_name, self._library_name) | 317 '_%sFactoryProvider' % interface_name, self._library_name) |
240 self._backend.EmitFactoryProvider( | 318 self._backend.EmitFactoryProvider( |
241 constructor_info, factory_provider, factory_provider_emitter) | 319 constructor_info, factory_provider, factory_provider_emitter) |
242 | 320 |
243 infos = HtmlElementConstructorInfos(interface_name) | 321 isHtmlElement = True |
322 infos = ElementConstructorInfos(interface_name, _html_element_constructors) | |
323 if not infos: | |
324 isHtmlElement = False | |
325 infos = ElementConstructorInfos(interface_name, _svg_element_constructors, | |
326 factory_provider_name='_SvgElements') | |
244 if infos: | 327 if infos: |
328 if isHtmlElement: | |
329 element_str = '_Elements' | |
330 else: | |
331 element_str = '_SvgElements' | |
blois
2012/11/15 01:35:49
We should be able to just get rid of these factory
| |
245 template = self._template_loader.Load( | 332 template = self._template_loader.Load( |
246 'factoryprovider_Elements.darttemplate') | 333 'factoryprovider%s.darttemplate' % element_str) |
247 EmitHtmlElementFactoryConstructors( | 334 EmitElementFactoryConstructors( |
248 self._library_emitter.FileEmitter('_Elements', self._library_name, | 335 self._library_emitter.FileEmitter(element_str, self._library_name, |
249 template), | 336 template), |
250 infos, | 337 infos, |
251 self._interface.id, | 338 self._interface.id, |
252 self._interface_type_info.implementation_name(), | 339 self._interface_type_info.implementation_name(), |
253 self._DartType) | 340 self._DartType, isHtmlElement) |
254 | 341 |
255 for info in infos: | 342 for info in infos: |
256 constructors.append(info.ConstructorInfo(self._interface.id)) | 343 constructors.append(info.ConstructorInfo(self._interface.id)) |
257 if factory_provider: | 344 if factory_provider: |
258 assert factory_provider == info.factory_provider_name | 345 assert factory_provider == info.factory_provider_name |
259 else: | 346 else: |
260 factory_provider = info.factory_provider_name | 347 factory_provider = info.factory_provider_name |
261 | 348 |
262 implementation_emitter = self._ImplementationEmitter() | 349 implementation_emitter = self._ImplementationEmitter() |
263 | 350 |
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
926 'svg': DartLibrary('svg', template_loader, library_type, output_dir), | 1013 'svg': DartLibrary('svg', template_loader, library_type, output_dir), |
927 'html': DartLibrary('html', template_loader, library_type, output_dir), | 1014 'html': DartLibrary('html', template_loader, library_type, output_dir), |
928 } | 1015 } |
929 | 1016 |
930 def AddFile(self, basename, library_name, path): | 1017 def AddFile(self, basename, library_name, path): |
931 self._libraries[library_name].AddFile(path) | 1018 self._libraries[library_name].AddFile(path) |
932 | 1019 |
933 def Emit(self, emitter, auxiliary_dir): | 1020 def Emit(self, emitter, auxiliary_dir): |
934 for lib in self._libraries.values(): | 1021 for lib in self._libraries.values(): |
935 lib.Emit(emitter, auxiliary_dir) | 1022 lib.Emit(emitter, auxiliary_dir) |
OLD | NEW |