| 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 import monitored | 9 import monitored |
| 10 from generator import GetAnnotationsAndComments, FormatAnnotationsAndComments | |
| 11 | 10 |
| 12 _logger = logging.getLogger('dartgenerator') | 11 _logger = logging.getLogger('dartgenerator') |
| 13 | 12 |
| 14 # Events without onEventName attributes in the IDL we want to support. | 13 # Events without onEventName attributes in the IDL we want to support. |
| 15 # We can automatically extract most event names by checking for | 14 # We can automatically extract most event names by checking for |
| 16 # 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 |
| 17 # 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. |
| 18 _html_manual_events = monitored.Dict('htmleventgenerator._html_manual_events', { | 17 _html_manual_events = monitored.Dict('htmleventgenerator._html_manual_events', { |
| 19 'Element': ['touchleave', 'touchenter', 'webkitTransitionEnd'], | 18 'Element': ['touchleave', 'touchenter', 'webkitTransitionEnd'], |
| 20 'Window': ['DOMContentLoaded'] | 19 'Window': ['DOMContentLoaded'] |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 'XMLHttpRequestUpload.progress': ('progress', 'ProgressEvent'), | 206 'XMLHttpRequestUpload.progress': ('progress', 'ProgressEvent'), |
| 208 }) | 207 }) |
| 209 | 208 |
| 210 # These classes require an explicit declaration for the "on" method even though | 209 # These classes require an explicit declaration for the "on" method even though |
| 211 # they don't declare any unique events, because the concrete class hierarchy | 210 # they don't declare any unique events, because the concrete class hierarchy |
| 212 # doesn't match the interface hierarchy. | 211 # doesn't match the interface hierarchy. |
| 213 _html_explicit_event_classes = set(['DocumentFragment']) | 212 _html_explicit_event_classes = set(['DocumentFragment']) |
| 214 | 213 |
| 215 class HtmlEventGenerator(object): | 214 class HtmlEventGenerator(object): |
| 216 | 215 |
| 217 def __init__(self, database, renamer, template_loader): | 216 def __init__(self, database, renamer, metadata, template_loader): |
| 218 self._event_classes = set() | 217 self._event_classes = set() |
| 219 self._database = database | 218 self._database = database |
| 220 self._renamer = renamer | 219 self._renamer = renamer |
| 220 self._metadata = metadata |
| 221 self._template_loader = template_loader | 221 self._template_loader = template_loader |
| 222 | 222 |
| 223 def EmitStreamProviders(self, interface, custom_events, | 223 def EmitStreamProviders(self, interface, custom_events, |
| 224 members_emitter, library_name): | 224 members_emitter, library_name): |
| 225 events = self._GetEvents(interface, custom_events) | 225 events = self._GetEvents(interface, custom_events) |
| 226 if not events: | 226 if not events: |
| 227 return | 227 return |
| 228 | 228 |
| 229 for event_info in events: | 229 for event_info in events: |
| 230 (dom_name, html_name, event_type) = event_info | 230 (dom_name, html_name, event_type) = event_info |
| 231 annotation_name = dom_name + 'Event' | 231 annotation_name = dom_name + 'Event' |
| 232 | 232 |
| 233 # If we're using a different provider, then don't declare one. | 233 # If we're using a different provider, then don't declare one. |
| 234 if self._GetEventRedirection(interface, html_name, event_type): | 234 if self._GetEventRedirection(interface, html_name, event_type): |
| 235 continue | 235 continue |
| 236 | 236 |
| 237 annotations = FormatAnnotationsAndComments( | 237 annotations = self._metadata.FormatMetadata( |
| 238 GetAnnotationsAndComments(library_name, interface.id, | 238 self._metadata.GetMetadata(library_name, interface.id, |
| 239 annotation_name), ' ') | 239 annotation_name, 'on' + dom_name), ' ') |
| 240 | 240 |
| 241 members_emitter.Emit( | 241 members_emitter.Emit( |
| 242 "\n" | 242 "\n" |
| 243 " $(ANNOTATIONS)static const EventStreamProvider<$TYPE> " | 243 " $(ANNOTATIONS)static const EventStreamProvider<$TYPE> " |
| 244 "$(NAME)Event = const EventStreamProvider<$TYPE>('$DOM_NAME');\n", | 244 "$(NAME)Event = const EventStreamProvider<$TYPE>('$DOM_NAME');\n", |
| 245 ANNOTATIONS=annotations, | 245 ANNOTATIONS=annotations, |
| 246 NAME=html_name, | 246 NAME=html_name, |
| 247 DOM_NAME=dom_name, | 247 DOM_NAME=dom_name, |
| 248 TYPE=event_type) | 248 TYPE=event_type) |
| 249 | 249 |
| 250 def EmitStreamGetters(self, interface, custom_events, | 250 def EmitStreamGetters(self, interface, custom_events, |
| 251 members_emitter, library_name): | 251 members_emitter, library_name): |
| 252 events = self._GetEvents(interface, custom_events) | 252 events = self._GetEvents(interface, custom_events) |
| 253 if not events: | 253 if not events: |
| 254 return | 254 return |
| 255 | 255 |
| 256 for event_info in events: | 256 for event_info in events: |
| 257 (dom_name, html_name, event_type) = event_info | 257 (dom_name, html_name, event_type) = event_info |
| 258 getter_name = 'on%s%s' % (html_name[:1].upper(), html_name[1:]) | 258 getter_name = 'on%s%s' % (html_name[:1].upper(), html_name[1:]) |
| 259 annotation_name = 'on' + dom_name | 259 annotation_name = 'on' + dom_name |
| 260 | 260 |
| 261 # If the provider is declared elsewhere, point to that. | 261 # If the provider is declared elsewhere, point to that. |
| 262 redirection = self._GetEventRedirection(interface, html_name, event_type) | 262 redirection = self._GetEventRedirection(interface, html_name, event_type) |
| 263 if redirection: | 263 if redirection: |
| 264 provider = '%s.%sEvent' % (redirection, html_name) | 264 provider = '%s.%sEvent' % (redirection, html_name) |
| 265 else: | 265 else: |
| 266 provider = html_name + 'Event' | 266 provider = html_name + 'Event' |
| 267 | 267 |
| 268 annotations = FormatAnnotationsAndComments( | 268 annotations = self._metadata.GetFormattedMetadata( |
| 269 GetAnnotationsAndComments(library_name, interface.id, | 269 library_name, interface.id, annotation_name, ' ') |
| 270 annotation_name), ' ') | |
| 271 | 270 |
| 272 members_emitter.Emit( | 271 members_emitter.Emit( |
| 273 "\n" | 272 "\n" |
| 274 " $(ANNOTATIONS)Stream<$TYPE> get $(NAME) => " | 273 " $(ANNOTATIONS)Stream<$TYPE> get $(NAME) => " |
| 275 "$PROVIDER.forTarget(this);\n", | 274 "$PROVIDER.forTarget(this);\n", |
| 276 ANNOTATIONS=annotations, | 275 ANNOTATIONS=annotations, |
| 277 NAME=getter_name, | 276 NAME=getter_name, |
| 278 PROVIDER=provider, | 277 PROVIDER=provider, |
| 279 TYPE=event_type) | 278 TYPE=event_type) |
| 280 | 279 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 """ | 347 """ |
| 349 key = '%s.%s' % (html_interface_name, dom_event_name) | 348 key = '%s.%s' % (html_interface_name, dom_event_name) |
| 350 if key in _html_event_types: | 349 if key in _html_event_types: |
| 351 return _html_event_types[key] | 350 return _html_event_types[key] |
| 352 key = '*.%s' % dom_event_name | 351 key = '*.%s' % dom_event_name |
| 353 if key in _html_event_types: | 352 if key in _html_event_types: |
| 354 return _html_event_types[key] | 353 return _html_event_types[key] |
| 355 _logger.warn('Cannot resolve event type for %s.%s' % | 354 _logger.warn('Cannot resolve event type for %s.%s' % |
| 356 (html_interface_name, dom_event_name)) | 355 (html_interface_name, dom_event_name)) |
| 357 return None | 356 return None |
| OLD | NEW |