| 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 functionality to generate dart:html event classes.""" | 6 """This module provides functionality to generate dart:html event classes.""" |
| 7 | 7 |
| 8 import logging | 8 import logging |
| 9 from generator import FindCommonAnnotations, FormatAnnotations | 9 from generator import GetAnnotationsAndComments, FormatAnnotationsAndComments |
| 10 | 10 |
| 11 _logger = logging.getLogger('dartgenerator') | 11 _logger = logging.getLogger('dartgenerator') |
| 12 | 12 |
| 13 # Events without onEventName attributes in the IDL we want to support. | 13 # Events without onEventName attributes in the IDL we want to support. |
| 14 # We can automatically extract most event names by checking for | 14 # We can automatically extract most event names by checking for |
| 15 # onEventName methods in the IDL but some events aren't listed so we need | 15 # onEventName methods in the IDL but some events aren't listed so we need |
| 16 # to manually add them here so that they are easy for users to find. | 16 # to manually add them here so that they are easy for users to find. |
| 17 _html_manual_events = { | 17 _html_manual_events = { |
| 18 'Element': ['touchleave', 'touchenter', 'webkitTransitionEnd'], | 18 'Element': ['touchleave', 'touchenter', 'webkitTransitionEnd'], |
| 19 'Window': ['DOMContentLoaded'] | 19 'Window': ['DOMContentLoaded'] |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 | 376 |
| 377 class HtmlEventGenerator(object): | 377 class HtmlEventGenerator(object): |
| 378 | 378 |
| 379 def __init__(self, database, renamer, template_loader): | 379 def __init__(self, database, renamer, template_loader): |
| 380 self._event_classes = set() | 380 self._event_classes = set() |
| 381 self._database = database | 381 self._database = database |
| 382 self._renamer = renamer | 382 self._renamer = renamer |
| 383 self._template_loader = template_loader | 383 self._template_loader = template_loader |
| 384 | 384 |
| 385 def EmitStreamProviders(self, interface, custom_events, | 385 def EmitStreamProviders(self, interface, custom_events, |
| 386 members_emitter): | 386 members_emitter, library_name): |
| 387 events = self._GetEvents(interface, custom_events) | 387 events = self._GetEvents(interface, custom_events) |
| 388 if not events: | 388 if not events: |
| 389 return | 389 return |
| 390 | 390 |
| 391 for event_info in events: | 391 for event_info in events: |
| 392 (dom_name, html_name, event_type) = event_info | 392 (dom_name, html_name, event_type) = event_info |
| 393 | 393 |
| 394 # If we're using a different provider, then don't declare one. | 394 # If we're using a different provider, then don't declare one. |
| 395 if self._GetEventRedirection(interface, html_name, event_type): | 395 if self._GetEventRedirection(interface, html_name, event_type): |
| 396 continue | 396 continue |
| 397 | 397 |
| 398 annotations = FormatAnnotations( | 398 annotations = FormatAnnotationsAndComments( |
| 399 FindCommonAnnotations(interface.id, dom_name), ' ') | 399 GetAnnotationsAndComments(interface.id, dom_name, library_name), ' ') |
| 400 | 400 |
| 401 members_emitter.Emit( | 401 members_emitter.Emit( |
| 402 "\n" | 402 "\n" |
| 403 " $(ANNOTATIONS)static const EventStreamProvider<$TYPE> " | 403 " $(ANNOTATIONS)static const EventStreamProvider<$TYPE> " |
| 404 "$(NAME)Event = const EventStreamProvider<$TYPE>('$DOM_NAME');\n", | 404 "$(NAME)Event = const EventStreamProvider<$TYPE>('$DOM_NAME');\n", |
| 405 ANNOTATIONS=annotations, | 405 ANNOTATIONS=annotations, |
| 406 NAME=html_name, | 406 NAME=html_name, |
| 407 DOM_NAME=dom_name, | 407 DOM_NAME=dom_name, |
| 408 TYPE=event_type) | 408 TYPE=event_type) |
| 409 | 409 |
| 410 def EmitStreamGetters(self, interface, custom_events, | 410 def EmitStreamGetters(self, interface, custom_events, |
| 411 members_emitter): | 411 members_emitter, library_name): |
| 412 events = self._GetEvents(interface, custom_events) | 412 events = self._GetEvents(interface, custom_events) |
| 413 if not events: | 413 if not events: |
| 414 return | 414 return |
| 415 | 415 |
| 416 for event_info in events: | 416 for event_info in events: |
| 417 (dom_name, html_name, event_type) = event_info | 417 (dom_name, html_name, event_type) = event_info |
| 418 getter_name = 'on%s%s' % (html_name[:1].upper(), html_name[1:]) | 418 getter_name = 'on%s%s' % (html_name[:1].upper(), html_name[1:]) |
| 419 | 419 |
| 420 # If the provider is declared elsewhere, point to that. | 420 # If the provider is declared elsewhere, point to that. |
| 421 redirection = self._GetEventRedirection(interface, html_name, event_type) | 421 redirection = self._GetEventRedirection(interface, html_name, event_type) |
| 422 if redirection: | 422 if redirection: |
| 423 provider = '%s.%sEvent' % (redirection, html_name) | 423 provider = '%s.%sEvent' % (redirection, html_name) |
| 424 else: | 424 else: |
| 425 provider = html_name + 'Event' | 425 provider = html_name + 'Event' |
| 426 | 426 |
| 427 annotations = FormatAnnotations( | 427 annotations = FormatAnnotationsAndComments( |
| 428 FindCommonAnnotations(interface.id, dom_name), ' ') | 428 GetAnnotationsAndComments(interface.id, dom_name, library_name), ' ') |
| 429 | 429 |
| 430 members_emitter.Emit( | 430 members_emitter.Emit( |
| 431 "\n" | 431 "\n" |
| 432 " $(ANNOTATIONS)Stream<$TYPE> get $(NAME) => " | 432 " $(ANNOTATIONS)Stream<$TYPE> get $(NAME) => " |
| 433 "$PROVIDER.forTarget(this);\n", | 433 "$PROVIDER.forTarget(this);\n", |
| 434 ANNOTATIONS=annotations, | 434 ANNOTATIONS=annotations, |
| 435 NAME=getter_name, | 435 NAME=getter_name, |
| 436 PROVIDER=provider, | 436 PROVIDER=provider, |
| 437 TYPE=event_type) | 437 TYPE=event_type) |
| 438 | 438 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 parent_events_class_name = 'Events' | 587 parent_events_class_name = 'Events' |
| 588 interfaces_with_events = set() | 588 interfaces_with_events = set() |
| 589 for parent in self._database.Hierarchy(interface): | 589 for parent in self._database.Hierarchy(interface): |
| 590 if parent != interface and parent.id in self._event_classes: | 590 if parent != interface and parent.id in self._event_classes: |
| 591 parent_name = self._renamer.RenameInterface(parent) | 591 parent_name = self._renamer.RenameInterface(parent) |
| 592 parent_events_class_name = parent_name + 'Events' | 592 parent_events_class_name = parent_name + 'Events' |
| 593 interfaces_with_events.add(parent) | 593 interfaces_with_events.add(parent) |
| 594 if len(interfaces_with_events) > 1: | 594 if len(interfaces_with_events) > 1: |
| 595 raise Exception('Only one parent event class allowed ' + interface.id) | 595 raise Exception('Only one parent event class allowed ' + interface.id) |
| 596 return parent_events_class_name | 596 return parent_events_class_name |
| OLD | NEW |