2
2
from __future__ import annotations
3
3
4
4
import json
5
+ import re
5
6
from collections .abc import Mapping , Sequence
6
7
from typing import (
7
8
Any ,
23
24
EventHandlerDict ,
24
25
EventHandlerType ,
25
26
ImportSourceDict ,
27
+ InlineJavaScript ,
28
+ InlineJavaScriptDict ,
26
29
VdomAttributes ,
27
30
VdomChildren ,
28
31
VdomDict ,
29
32
VdomJson ,
30
33
)
31
34
35
+ EVENT_ATTRIBUTE_PATTERN = re .compile (r"^on[A-Z]\w+" )
36
+
32
37
VDOM_JSON_SCHEMA = {
33
38
"$schema" : "http://json-schema.org/draft-07/schema" ,
34
39
"$ref" : "#/definitions/element" ,
42
47
"children" : {"$ref" : "#/definitions/elementChildren" },
43
48
"attributes" : {"type" : "object" },
44
49
"eventHandlers" : {"$ref" : "#/definitions/elementEventHandlers" },
50
+ "inlineJavaScript" : {"$ref" : "#/definitions/elementInlineJavaScripts" },
45
51
"importSource" : {"$ref" : "#/definitions/importSource" },
46
52
},
47
53
# The 'tagName' is required because its presence is a useful indicator of
71
77
},
72
78
"required" : ["target" ],
73
79
},
80
+ "elementInlineJavaScripts" : {
81
+ "type" : "object" ,
82
+ "patternProperties" : {
83
+ ".*" : "str" ,
84
+ },
85
+ },
74
86
"importSource" : {
75
87
"type" : "object" ,
76
88
"properties" : {
@@ -160,7 +172,9 @@ def __call__(
160
172
"""The entry point for the VDOM API, for example reactpy.html(<WE_ARE_HERE>)."""
161
173
attributes , children = separate_attributes_and_children (attributes_and_children )
162
174
key = attributes .get ("key" , None )
163
- attributes , event_handlers = separate_attributes_and_event_handlers (attributes )
175
+ attributes , event_handlers , inline_javascript = (
176
+ separate_attributes_handlers_and_inline_javascript (attributes )
177
+ )
164
178
if REACTPY_CHECK_JSON_ATTRS .current :
165
179
json .dumps (attributes )
166
180
@@ -180,6 +194,9 @@ def __call__(
180
194
** ({"children" : children } if children else {}),
181
195
** ({"attributes" : attributes } if attributes else {}),
182
196
** ({"eventHandlers" : event_handlers } if event_handlers else {}),
197
+ ** (
198
+ {"inlineJavaScript" : inline_javascript } if inline_javascript else {}
199
+ ),
183
200
** ({"importSource" : self .import_source } if self .import_source else {}),
184
201
}
185
202
@@ -212,26 +229,26 @@ def separate_attributes_and_children(
212
229
return _attributes , _children
213
230
214
231
215
- def separate_attributes_and_event_handlers (
232
+ def separate_attributes_handlers_and_inline_javascript (
216
233
attributes : Mapping [str , Any ],
217
- ) -> tuple [VdomAttributes , EventHandlerDict ]:
234
+ ) -> tuple [VdomAttributes , EventHandlerDict , InlineJavaScriptDict ]:
218
235
_attributes : VdomAttributes = {}
219
236
_event_handlers : dict [str , EventHandlerType ] = {}
237
+ _inline_javascript : dict [str , InlineJavaScript ] = {}
220
238
221
239
for k , v in attributes .items ():
222
- handler : EventHandlerType
223
-
224
240
if callable (v ):
225
- handler = EventHandler (to_event_handler_function (v ))
241
+ _event_handlers [ k ] = EventHandler (to_event_handler_function (v ))
226
242
elif isinstance (v , EventHandler ):
227
- handler = v
243
+ _event_handlers [k ] = v
244
+ elif EVENT_ATTRIBUTE_PATTERN .match (k ) and isinstance (v , str ):
245
+ _inline_javascript [k ] = InlineJavaScript (v )
246
+ elif isinstance (v , InlineJavaScript ):
247
+ _inline_javascript [k ] = v
228
248
else :
229
249
_attributes [k ] = v
230
- continue
231
-
232
- _event_handlers [k ] = handler
233
250
234
- return _attributes , _event_handlers
251
+ return _attributes , _event_handlers , _inline_javascript
235
252
236
253
237
254
def _flatten_children (children : Sequence [Any ]) -> list [Any ]:
0 commit comments