22from __future__ import annotations
33
44import json
5+ import re
56from collections .abc import Mapping , Sequence
67from typing import (
78 Any ,
2324 EventHandlerDict ,
2425 EventHandlerType ,
2526 ImportSourceDict ,
27+ InlineJavaScript ,
28+ InlineJavaScriptDict ,
2629 VdomAttributes ,
2730 VdomChildren ,
2831 VdomDict ,
2932 VdomJson ,
3033)
3134
35+ EVENT_ATTRIBUTE_PATTERN = re .compile (r"^on[A-Z]\w+" )
36+
3237VDOM_JSON_SCHEMA = {
3338 "$schema" : "http://json-schema.org/draft-07/schema" ,
3439 "$ref" : "#/definitions/element" ,
4247 "children" : {"$ref" : "#/definitions/elementChildren" },
4348 "attributes" : {"type" : "object" },
4449 "eventHandlers" : {"$ref" : "#/definitions/elementEventHandlers" },
50+ "inlineJavaScript" : {"$ref" : "#/definitions/elementInlineJavaScripts" },
4551 "importSource" : {"$ref" : "#/definitions/importSource" },
4652 },
4753 # The 'tagName' is required because its presence is a useful indicator of
7177 },
7278 "required" : ["target" ],
7379 },
80+ "elementInlineJavaScripts" : {
81+ "type" : "object" ,
82+ "patternProperties" : {
83+ ".*" : "str" ,
84+ },
85+ },
7486 "importSource" : {
7587 "type" : "object" ,
7688 "properties" : {
@@ -160,7 +172,9 @@ def __call__(
160172 """The entry point for the VDOM API, for example reactpy.html(<WE_ARE_HERE>)."""
161173 attributes , children = separate_attributes_and_children (attributes_and_children )
162174 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+ )
164178 if REACTPY_CHECK_JSON_ATTRS .current :
165179 json .dumps (attributes )
166180
@@ -180,6 +194,9 @@ def __call__(
180194 ** ({"children" : children } if children else {}),
181195 ** ({"attributes" : attributes } if attributes else {}),
182196 ** ({"eventHandlers" : event_handlers } if event_handlers else {}),
197+ ** (
198+ {"inlineJavaScript" : inline_javascript } if inline_javascript else {}
199+ ),
183200 ** ({"importSource" : self .import_source } if self .import_source else {}),
184201 }
185202
@@ -212,26 +229,26 @@ def separate_attributes_and_children(
212229 return _attributes , _children
213230
214231
215- def separate_attributes_and_event_handlers (
232+ def separate_attributes_handlers_and_inline_javascript (
216233 attributes : Mapping [str , Any ],
217- ) -> tuple [VdomAttributes , EventHandlerDict ]:
234+ ) -> tuple [VdomAttributes , EventHandlerDict , InlineJavaScriptDict ]:
218235 _attributes : VdomAttributes = {}
219236 _event_handlers : dict [str , EventHandlerType ] = {}
237+ _inline_javascript : dict [str , InlineJavaScript ] = {}
220238
221239 for k , v in attributes .items ():
222- handler : EventHandlerType
223-
224240 if callable (v ):
225- handler = EventHandler (to_event_handler_function (v ))
241+ _event_handlers [ k ] = EventHandler (to_event_handler_function (v ))
226242 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
228248 else :
229249 _attributes [k ] = v
230- continue
231-
232- _event_handlers [k ] = handler
233250
234- return _attributes , _event_handlers
251+ return _attributes , _event_handlers , _inline_javascript
235252
236253
237254def _flatten_children (children : Sequence [Any ]) -> list [Any ]:
0 commit comments