Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: lib/dom/scripts/systemhtml.py

Issue 10384024: Element constructors (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rebase Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | lib/dom/templates/html/interface/factoryprovider__Elements.darttemplate » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 from systemfrog import * 9 from systemfrog import *
10 from systeminterface import * 10 from systeminterface import *
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 'webkitSpeechChange': 'speechChange', 432 'webkitSpeechChange': 'speechChange',
433 'webkitsourceclose': 'sourceClose', 433 'webkitsourceclose': 'sourceClose',
434 'webkitsourceended': 'sourceEnded', 434 'webkitsourceended': 'sourceEnded',
435 'webkitsourceopen': 'sourceOpen', 435 'webkitsourceopen': 'sourceOpen',
436 'webkitTransitionEnd': 'transitionEnd', 436 'webkitTransitionEnd': 'transitionEnd',
437 'write': 'write', 437 'write': 'write',
438 'writeend': 'writeEnd', 438 'writeend': 'writeEnd',
439 'writestart': 'writeStart' 439 'writestart': 'writeStart'
440 } 440 }
441 441
442
443
444 # Information for generating element constructors.
445 #
446 # TODO(sra): maybe remove all the argument complexity and use cascades.
447 #
448 # var c = new CanvasElement(width: 100, height: 70);
449 # var c = new CanvasElement()..width = 100..height = 70;
450 #
451 class ElementCtorInfo(object):
452 def __init__(self, tag=None, params=[], opt_params=[],
453 factory_provider_name='_Elements'):
454 self.tag = tag
455 self.params = params
456 self.opt_params = opt_params
457 self.factory_provider_name = factory_provider_name
458
459 def ConstructorInfo(self, interface):
460 info = OperationInfo()
461 info.overloads = None
462 info.declared_name = interface.id
463 info.name = interface.id
464 info.js_name = None
465 info.type_name = interface.id
466 info.param_infos = map(lambda tXn: ParamInfo(tXn[1], None, tXn[0], 'null'),
467 self.opt_params)
468 return info
469
470 _html_element_constructors = {
471 'AnchorElement' : ElementCtorInfo(tag='a', opt_params=[('String', 'href')]),
472 'AreaElement': 'area',
473 'ButtonElement': 'button',
474 'BRElement': 'br',
475 'BaseElement': 'base',
476 'BodyElement': 'body',
477 'ButtonElement': 'button',
478 'CanvasElement':
479 ElementCtorInfo(tag='canvas',
480 opt_params=[('int', 'height'), ('int', 'width')]),
481 'DListElement': 'dl',
482 'DetailsElement': 'details',
483 'DivElement': 'div',
484 'EmbedElement': 'embed',
485 'FieldSetElement': 'fieldset',
486 'Form': 'form',
487 'HRElement': 'hr',
488 'HeadElement': 'head',
489 'HtmlElement': 'html',
490 'IFrameElement': 'iframe',
491 'ImageElement':
492 ElementCtorInfo(tag='img',
493 opt_params=[('String', 'src'),
494 ('int', 'height'), ('int', 'width')]),
495 'InputElement':
496 ElementCtorInfo(tag='input', opt_params=[('String', 'type')]),
497 'KeygenElement': 'keygen',
498 'LIElement': 'li',
499 'LabelElement': 'label',
500 'LegendElement': 'legend',
501 'LinkElement': 'link',
502 'MapElement': 'map',
503 'MenuElement': 'menu',
504 'MeterElement': 'meter',
505 'OListElement': 'ol',
506 'ObjectElement': 'object',
507 'OptGroupElement': 'optgroup',
508 'OutputElement': 'output',
509 'ParagraphElement': 'p',
510 'ParamElement': 'param',
511 'PreElement': 'pre',
512 'ProgressElement': 'progress',
513 'ScriptElement': 'script',
514 'SourceElement': 'source',
515 'SpanElement': 'span',
516 'StyleElement': 'style',
517 'TableCaptionElement': 'caption',
518 'TableCellElement': 'td',
519 'TableColElement': 'col',
520 'TableElement': 'table',
521 'TableRowElement': 'tr',
522 #'TableSectionElement' <thead> <tbody> <tfoot>
523 'TextAreaElement': 'textarea',
524 'TitleElement': 'title',
525 'TrackElement': 'track',
526 'UListElement': 'ul',
527 'VideoElement': 'video'
528 }
529
442 # These classes require an explicit declaration for the "on" method even though 530 # These classes require an explicit declaration for the "on" method even though
443 # they don't declare any unique events, because the concrete class hierarchy 531 # they don't declare any unique events, because the concrete class hierarchy
444 # doesn't match the interface hierarchy. 532 # doesn't match the interface hierarchy.
445 _html_explicit_event_classes = set(['DocumentFragment']) 533 _html_explicit_event_classes = set(['DocumentFragment'])
446 534
447 def _OnAttributeToEventName(on_method): 535 def _OnAttributeToEventName(on_method):
448 event_name = on_method.id[2:] 536 event_name = on_method.id[2:]
449 if event_name in _on_attribute_to_event_name_mapping: 537 if event_name in _on_attribute_to_event_name_mapping:
450 return _on_attribute_to_event_name_mapping[event_name] 538 return _on_attribute_to_event_name_mapping[event_name]
451 else: 539 else:
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 super(HtmlSystem, self).__init__( 672 super(HtmlSystem, self).__init__(
585 templates, database, emitters, output_dir) 673 templates, database, emitters, output_dir)
586 self._shared = HtmlSystemShared(database) 674 self._shared = HtmlSystemShared(database)
587 675
588 class HtmlInterfacesSystem(HtmlSystem): 676 class HtmlInterfacesSystem(HtmlSystem):
589 677
590 def __init__(self, templates, database, emitters, output_dir): 678 def __init__(self, templates, database, emitters, output_dir):
591 super(HtmlInterfacesSystem, self).__init__( 679 super(HtmlInterfacesSystem, self).__init__(
592 templates, database, emitters, output_dir) 680 templates, database, emitters, output_dir)
593 self._dart_interface_file_paths = [] 681 self._dart_interface_file_paths = []
682 self._factory_provider_emitters = {}
594 683
595 def InterfaceGenerator(self, 684 def InterfaceGenerator(self,
596 interface, 685 interface,
597 common_prefix, 686 common_prefix,
598 super_interface_name, 687 super_interface_name,
599 source_filter): 688 source_filter):
600 """.""" 689 """."""
601 interface_name = interface.id 690 interface_name = interface.id
602 dart_interface_file_path = self._FilePathForDartInterface(interface_name) 691 dart_interface_file_path = self._FilePathForDartInterface(interface_name)
603 692
(...skipping 21 matching lines...) Expand all
625 def GenerateLibraries(self): 714 def GenerateLibraries(self):
626 pass 715 pass
627 716
628 717
629 def _FilePathForDartInterface(self, interface_name): 718 def _FilePathForDartInterface(self, interface_name):
630 """Returns the file path of the Dart interface definition.""" 719 """Returns the file path of the Dart interface definition."""
631 # TODO(jmesserly): is this the right path 720 # TODO(jmesserly): is this the right path
632 return os.path.join(self._output_dir, 'html', 'interface', 721 return os.path.join(self._output_dir, 'html', 'interface',
633 '%s.dart' % interface_name) 722 '%s.dart' % interface_name)
634 723
724 def _EmitterForFactoryProviderBody(self, name):
725 if name not in self._factory_provider_emitters:
726 path = self._FilePathForDartInterface(name)
727 self._dart_interface_file_paths.append(path)
728 template = self._templates.Load('factoryprovider_%s.darttemplate' % name)
729 file_emitter = self._emitters.FileEmitter(path)
730 self._factory_provider_emitters[name] = file_emitter.Emit(template)
731 return self._factory_provider_emitters[name]
732
635 # ------------------------------------------------------------------------------ 733 # ------------------------------------------------------------------------------
636 734
637 # TODO(jmesserly): inheritance is probably not the right way to factor this long 735 # TODO(jmesserly): inheritance is probably not the right way to factor this long
638 # term, but it makes merging better for now. 736 # term, but it makes merging better for now.
639 class HtmlDartInterfaceGenerator(DartInterfaceGenerator): 737 class HtmlDartInterfaceGenerator(DartInterfaceGenerator):
640 """Generates Dart Interface definition for one DOM IDL interface.""" 738 """Generates Dart Interface definition for one DOM IDL interface."""
641 739
642 def __init__(self, system, interface, emitter, template, 740 def __init__(self, system, interface, emitter, template,
643 common_prefix, super_interface, source_filter, shared): 741 common_prefix, super_interface, source_filter, shared):
644 super(HtmlDartInterfaceGenerator, self).__init__(system, interface, 742 super(HtmlDartInterfaceGenerator, self).__init__(system, interface,
(...skipping 25 matching lines...) Expand all
670 extends_str += ' extends ' + ', '.join(extends) 768 extends_str += ' extends ' + ', '.join(extends)
671 comment = ',' 769 comment = ','
672 if suppressed_extends: 770 if suppressed_extends:
673 extends_str += ' /*%s %s */' % (comment, ', '.join(suppressed_extends)) 771 extends_str += ' /*%s %s */' % (comment, ', '.join(suppressed_extends))
674 772
675 factory_provider = None 773 factory_provider = None
676 constructor_info = AnalyzeConstructor(self._interface) 774 constructor_info = AnalyzeConstructor(self._interface)
677 if constructor_info: 775 if constructor_info:
678 factory_provider = '_' + typename + 'FactoryProvider'; 776 factory_provider = '_' + typename + 'FactoryProvider';
679 777
778 if not constructor_info:
779 (constructor_info, factory_provider) = self._EmitElementFactory(typename)
780
680 if typename in interface_factories: 781 if typename in interface_factories:
681 factory_provider = interface_factories[typename] 782 factory_provider = interface_factories[typename]
682 783
683 if factory_provider: 784 if factory_provider:
684 extends_str += ' default ' + factory_provider 785 extends_str += ' default ' + factory_provider
685 786
686 # TODO(vsm): Add appropriate package / namespace syntax. 787 # TODO(vsm): Add appropriate package / namespace syntax.
687 (self._type_comment_emitter, 788 (self._type_comment_emitter,
688 self._members_emitter, 789 self._members_emitter,
689 self._top_level_emitter) = self._emitter.Emit( 790 self._top_level_emitter) = self._emitter.Emit(
(...skipping 26 matching lines...) Expand all
716 TYPE=DartType(element_type)) 817 TYPE=DartType(element_type))
717 818
718 emit_events, events = self._shared.GetEventAttributes(self._interface) 819 emit_events, events = self._shared.GetEventAttributes(self._interface)
719 if not emit_events: 820 if not emit_events:
720 return 821 return
721 elif events: 822 elif events:
722 self.AddEventAttributes(events) 823 self.AddEventAttributes(events)
723 else: 824 else:
724 self._EmitEventGetter(self._shared.GetParentEventsClass(self._interface)) 825 self._EmitEventGetter(self._shared.GetParentEventsClass(self._interface))
725 826
827 def _EmitElementFactory(self, typename):
828 """Returns pair (constructor_info, factory_provider_name)."""
829 if typename not in _html_element_constructors:
830 return (None, None)
831 info = _html_element_constructors[typename]
832 if isinstance(info, str): info = ElementCtorInfo(tag=info)
833 constructor_info = info.ConstructorInfo(self._interface)
834 em = self._system._EmitterForFactoryProviderBody(info.factory_provider_name)
835 inits = em.Emit(
836 '\n'
837 ' factory $CONSTRUCTOR($PARAMS) {\n'
838 ' $CLASSNAME _e = _document.$dom_createElement("$TAG");\n'
839 '$!INITS'
840 ' return _e;\n'
841 ' }\n',
842 CONSTRUCTOR=typename,
843 CLASSNAME='_' + typename + 'Impl', # TODO: fix
844 TAG=info.tag,
845 PARAMS=constructor_info.ParametersInterfaceDeclaration())
846 for param in constructor_info.param_infos:
847 inits.Emit(' if ($E != null) _e.$E = $E;\n', E=param.name)
848
849 return (constructor_info, info.factory_provider_name)
850
851
726 def AddAttribute(self, getter, setter): 852 def AddAttribute(self, getter, setter):
727 dom_name = DartDomNameOfAttribute(getter) 853 dom_name = DartDomNameOfAttribute(getter)
728 html_getter_name = self._shared.RenameInHtmlLibrary( 854 html_getter_name = self._shared.RenameInHtmlLibrary(
729 self._interface, dom_name, 'get:') 855 self._interface, dom_name, 'get:')
730 html_setter_name = self._shared.RenameInHtmlLibrary( 856 html_setter_name = self._shared.RenameInHtmlLibrary(
731 self._interface, dom_name, 'set:') 857 self._interface, dom_name, 'set:')
732 858
733 if not html_getter_name or self._shared.IsPrivate(html_getter_name): 859 if not html_getter_name or self._shared.IsPrivate(html_getter_name):
734 getter = None 860 getter = None
735 if not html_setter_name or self._shared.IsPrivate(html_setter_name): 861 if not html_setter_name or self._shared.IsPrivate(html_setter_name):
(...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after
1567 Collect(database.GetInterface(parent.type.id), 1693 Collect(database.GetInterface(parent.type.id),
1568 seen, collected) 1694 seen, collected)
1569 1695
1570 inheritance_closure = {} 1696 inheritance_closure = {}
1571 for interface in database.GetInterfaces(): 1697 for interface in database.GetInterfaces():
1572 seen = set() 1698 seen = set()
1573 collected = [] 1699 collected = []
1574 Collect(interface, seen, collected) 1700 Collect(interface, seen, collected)
1575 inheritance_closure[interface.id] = collected 1701 inheritance_closure[interface.id] = collected
1576 return inheritance_closure 1702 return inheritance_closure
OLDNEW
« no previous file with comments | « no previous file | lib/dom/templates/html/interface/factoryprovider__Elements.darttemplate » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698