Flutter Engine
The Flutter Engine
prototype_htmleventgenerator.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
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.
5"""Generates the dart:html event providers and getters for the prototype."""
6
7import monitored
8import subprocess
9
10OUTPUT_FILE = 'prototype_events.dart'
11
12_custom_events = set(['mouseWheel', 'transitionEnd', 'visibilityChange'])
13
14_type_specific_events = set(['doubleClick', 'error'])
15
16_deprecated_event_interfaces = set([
17 'AccessibleNode', 'AccessibleNodeList', 'ApplicationCache', 'FileWriter',
18 'VRSession'
19])
20_deprecated_event_types = set(['ForeignFetchEvent', 'MediaStreamEvent'])
21
22_non_html_types = monitored.Dict(
23 'prototype_htmleventgenerator._non_html_events', {
24 'ContextEvent': 'web_gl',
25 'Database': 'indexed_db',
26 'OpenDBRequest': 'indexed_db',
27 'Request': 'indexed_db',
28 'SvgElement': 'svg',
29 'Transaction': 'indexed_db',
30 'VersionChangeEvent': 'indexed_db'
31 })
32
33
35
36 def __init__(self, database, renamer, metadata, htmleventgenerator):
37 self._database = database
38 self._renamer = renamer
39 self._metadata = metadata
40 self._html_event_generator = htmleventgenerator
41 self._all_event_providers = {}
42 self._all_event_getters = {}
43
44 # Gather all EventStreamProviders for the given interface type.
45 def CollectStreamProviders(self, interface, custom_events, library_name):
46
47 events = self._html_event_generator.GetEvents(interface, custom_events)
48 if not events:
49 return
50
51 for event_info in events:
52 (dom_name, html_name, event_type) = event_info
53 # Skip custom events that are added to glue code separately
54 if html_name in _custom_events:
55 continue
56 annotation_name = dom_name + 'Event'
57
58 annotations = self._metadata.FormatMetadata(
59 self._metadata.GetMetadata(library_name, interface,
60 annotation_name, 'on' + dom_name),
61 ' ')
62
63 # Strip comment from annotations
64 if '/**' in annotations:
65 annotations = annotations[annotations.index('*/') + 2:]
66 event_type = self._formatEventType(event_type)
67 html_name = (html_name if html_name not in _type_specific_events
68 else html_name + interface.doc_js_name)
69
71 {html_name: [annotations, dom_name, event_type]})
72
73 # Gather all getters `onFooEvent` for the given interface type.
74 def CollectStreamGetters(self, interface, custom_events, library_name):
75 events = self._html_event_generator.GetEvents(interface, custom_events)
76 if not events:
77 return
78 interface_events = {}
79 for event_info in events:
80 (dom_name, html_name, event_type) = event_info
81 # Skip custom events that are added to glue code separately
82 if html_name in _custom_events:
83 continue
84 getter_name = 'on%s%s' % (html_name[:1].upper(), html_name[1:])
85 annotation_name = 'on' + dom_name
86
87 annotations = self._metadata.GetFormattedMetadata(
88 library_name, interface, annotation_name, ' ')
89
90 # Strip comment from annotations
91 if '///' in annotations:
92 annotations = annotations[annotations.index('\n') + 1:]
93
94 event_type = self._formatEventType(event_type)
95 html_name = (html_name if html_name not in _type_specific_events
96 else html_name + interface.doc_js_name)
97 provider = html_name + 'Event'
98
99 interface_events.update(
100 {getter_name: [annotations, event_type, provider]})
101 self._all_event_getters.update({interface: interface_events})
102
103 # Returns whether the given interface has `Element` as a supertype.
104 def _isElement(self, interface):
105 for parent in self._database.Hierarchy(interface):
106 if parent.id == 'Element':
107 return True
108 return False
109
110 # Formats event_type by stripping other namespace prefixing.
111 def _formatEventType(self, event_type):
112 if '.' in event_type:
113 event_type = event_type[event_type.index('.') + 1:]
114 return event_type
115
116 # Output Providers and Event Getters to the output file.
117 def WriteFile(self, dart_bin):
118 output_file = open(OUTPUT_FILE, 'w')
119 output_file.write("""
120/// Exposing the Streams of events for legacy dart:html.
121library dart.html_events;
122
123import 'dart:html' as html;
124import 'dart:html_common';
125import 'dart:indexed_db' as indexed_db;
126import 'dart:event_stream' show ElementStream, EventStreamProvider;
127import 'dart:svg' as svg;
128import 'dart:web_gl' as web_gl;
129
130""")
131 self._writeProviders(output_file)
132 self._writeEventGetters(output_file)
133 output_file.close()
134 formatCommand = ' '.join([dart_bin, 'format', OUTPUT_FILE])
135 subprocess.call([formatCommand], shell=True)
136
137 # Output all EventstreamProviders.
138 def _writeProviders(self, output_file):
139 output_file.write("""
140/// Statically accessible `EventStreamProvider`s for all event types.
141class EventStreamProviders {
142""")
143 for html_name, info in sorted(self._all_event_providers.items()):
144 (annotations, dom_name, event_type) = info
145 if event_type in _deprecated_event_types:
146 continue
147 event_prefix = ('html' if event_type not in _non_html_types else
148 _non_html_types[event_type])
149 output_file.write("""
150 %sstatic const EventStreamProvider<%s.%s> %sEvent =
151 const EventStreamProvider<%s.%s>('%s');
152""" % (annotations, event_prefix, event_type, html_name, event_prefix,
153 event_type, dom_name))
154 output_file.write("""\n}\n""")
155
156 # Output all onFooEvent getters in extensions.
157 def _writeEventGetters(self, output_file):
158 for interface, events in self._all_event_getters.items():
159 isElement = self._isElement(interface)
160 interface_name = interface.doc_js_name
161 if interface_name in _deprecated_event_interfaces:
162 continue
163 interface_name = self._renamer.RenameInterface(interface)
164 interface_prefix = ('html' if interface_name not in _non_html_types
165 else _non_html_types[interface_name])
166 output_file.write("""
167
168/// Additional Event getters for [%s].
169extension %sEventGetters on %s.%s {
170""" % (interface_name, interface_name, interface_prefix, interface_name))
171 for getter_name, info in sorted(events.items()):
172 (annotations, event_type, provider) = info
173 if event_type in _deprecated_event_types:
174 continue
175 event_prefix = ('html' if event_type not in _non_html_types else
176 _non_html_types[event_type])
177 output_file.write("""
178%s
179%sStream<%s.%s> get %s => EventStreamProviders.%s.%s(this);
180""" % (annotations, 'Element' if isElement else '', event_prefix, event_type,
181 getter_name, provider, 'forElement' if isElement else 'forTarget'))
182 output_file.write("""\n}\n""")
def __init__(self, database, renamer, metadata, htmleventgenerator)
def CollectStreamGetters(self, interface, custom_events, library_name)
def CollectStreamProviders(self, interface, custom_events, library_name)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not set
Definition: switches.h:76
Definition: update.py:1
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741