forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_html.py
417 lines (373 loc) · 11.7 KB
/
_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
from __future__ import annotations
from collections.abc import Sequence
from typing import TYPE_CHECKING, ClassVar
from reactpy.core.vdom import custom_vdom_constructor, make_vdom_constructor
if TYPE_CHECKING:
from reactpy.types import (
EventHandlerDict,
Key,
VdomAttributes,
VdomChild,
VdomChildren,
VdomDict,
VdomDictConstructor,
)
__all__ = ["html"]
NO_CHILDREN_ALLOWED_HTML_BODY = {
"area",
"base",
"br",
"col",
"command",
"embed",
"hr",
"img",
"input",
"iframe",
"keygen",
"link",
"meta",
"param",
"portal",
"source",
"track",
"wbr",
}
NO_CHILDREN_ALLOWED_SVG = {
"animate",
"animateMotion",
"animateTransform",
"circle",
"desc",
"discard",
"ellipse",
"feBlend",
"feColorMatrix",
"feComponentTransfer",
"feComposite",
"feConvolveMatrix",
"feDiffuseLighting",
"feDisplacementMap",
"feDistantLight",
"feDropShadow",
"feFlood",
"feFuncA",
"feFuncB",
"feFuncG",
"feFuncR",
"feGaussianBlur",
"feImage",
"feMerge",
"feMergeNode",
"feMorphology",
"feOffset",
"fePointLight",
"feSpecularLighting",
"feSpotLight",
"feTile",
"feTurbulence",
"filter",
"foreignObject",
"hatch",
"hatchpath",
"image",
"line",
"linearGradient",
"metadata",
"mpath",
"path",
"polygon",
"polyline",
"radialGradient",
"rect",
"script",
"set",
"stop",
"style",
"text",
"textPath",
"title",
"tspan",
"use",
"view",
}
def _fragment(
attributes: VdomAttributes,
children: Sequence[VdomChild],
key: Key | None,
event_handlers: EventHandlerDict,
) -> VdomDict:
"""An HTML fragment - this element will not appear in the DOM"""
if attributes or event_handlers:
msg = "Fragments cannot have attributes besides 'key'"
raise TypeError(msg)
model: VdomDict = {"tagName": ""}
if children:
model["children"] = children
if key is not None:
model["key"] = key
return model
def _script(
attributes: VdomAttributes,
children: Sequence[VdomChild],
key: Key | None,
event_handlers: EventHandlerDict,
) -> VdomDict:
"""Create a new `<script> <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script>`__ element.
.. warning::
Be careful to sanitize data from untrusted sources before using it in a script.
See the "Notes" for more details
This behaves slightly differently than a normal script element in that it may be run
multiple times if its key changes (depending on specific browser behaviors). If no
key is given, the key is inferred to be the content of the script or, lastly its
'src' attribute if that is given.
Notes:
Do not use unsanitized data from untrusted sources anywhere in your script.
Doing so may allow for malicious code injection
(`XSS <https://en.wikipedia.org/wiki/Cross-site_scripting>`__`).
"""
model: VdomDict = {"tagName": "script"}
if event_handlers:
msg = "'script' elements do not support event handlers"
raise ValueError(msg)
if children:
if len(children) > 1:
msg = "'script' nodes may have, at most, one child."
raise ValueError(msg)
if not isinstance(children[0], str):
msg = "The child of a 'script' must be a string."
raise ValueError(msg)
model["children"] = children
if key is None:
key = children[0]
if attributes:
model["attributes"] = attributes
if key is None and not children and "src" in attributes:
key = attributes["src"]
if key is not None:
model["key"] = key
return model
class SvgConstructor:
"""Constructor specifically for SVG children."""
__cache__: ClassVar[dict[str, VdomDictConstructor]] = {}
def __call__(
self, *attributes_and_children: VdomAttributes | VdomChildren
) -> VdomDict:
return self.svg(*attributes_and_children)
def __getattr__(self, value: str) -> VdomDictConstructor:
value = value.rstrip("_").replace("_", "-")
if value in self.__cache__:
return self.__cache__[value]
self.__cache__[value] = make_vdom_constructor(
value, allow_children=value not in NO_CHILDREN_ALLOWED_SVG
)
return self.__cache__[value]
# SVG child elements, written out here for auto-complete purposes
# The actual elements are created dynamically in the __getattr__ method.
# Elements other than these can still be created.
a: VdomDictConstructor
animate: VdomDictConstructor
animateMotion: VdomDictConstructor
animateTransform: VdomDictConstructor
circle: VdomDictConstructor
clipPath: VdomDictConstructor
defs: VdomDictConstructor
desc: VdomDictConstructor
discard: VdomDictConstructor
ellipse: VdomDictConstructor
feBlend: VdomDictConstructor
feColorMatrix: VdomDictConstructor
feComponentTransfer: VdomDictConstructor
feComposite: VdomDictConstructor
feConvolveMatrix: VdomDictConstructor
feDiffuseLighting: VdomDictConstructor
feDisplacementMap: VdomDictConstructor
feDistantLight: VdomDictConstructor
feDropShadow: VdomDictConstructor
feFlood: VdomDictConstructor
feFuncA: VdomDictConstructor
feFuncB: VdomDictConstructor
feFuncG: VdomDictConstructor
feFuncR: VdomDictConstructor
feGaussianBlur: VdomDictConstructor
feImage: VdomDictConstructor
feMerge: VdomDictConstructor
feMergeNode: VdomDictConstructor
feMorphology: VdomDictConstructor
feOffset: VdomDictConstructor
fePointLight: VdomDictConstructor
feSpecularLighting: VdomDictConstructor
feSpotLight: VdomDictConstructor
feTile: VdomDictConstructor
feTurbulence: VdomDictConstructor
filter: VdomDictConstructor
foreignObject: VdomDictConstructor
g: VdomDictConstructor
hatch: VdomDictConstructor
hatchpath: VdomDictConstructor
image: VdomDictConstructor
line: VdomDictConstructor
linearGradient: VdomDictConstructor
marker: VdomDictConstructor
mask: VdomDictConstructor
metadata: VdomDictConstructor
mpath: VdomDictConstructor
path: VdomDictConstructor
pattern: VdomDictConstructor
polygon: VdomDictConstructor
polyline: VdomDictConstructor
radialGradient: VdomDictConstructor
rect: VdomDictConstructor
script: VdomDictConstructor
set: VdomDictConstructor
stop: VdomDictConstructor
style: VdomDictConstructor
switch: VdomDictConstructor
symbol: VdomDictConstructor
text: VdomDictConstructor
textPath: VdomDictConstructor
title: VdomDictConstructor
tspan: VdomDictConstructor
use: VdomDictConstructor
view: VdomDictConstructor
class HtmlConstructor:
"""Create a new HTML element. Commonly used elements are provided via auto-complete.
However, any HTML element can be created by calling the element name as an attribute.
If trying to create an element that is illegal syntax in Python, you can postfix an
underscore character (eg. `html.del_` for `<del>`).
If trying to create an element with dashes in the name, you can replace the dashes
with underscores (eg. `html.data_table` for `<data-table>`)."""
# ruff: noqa: N815
__cache__: ClassVar[dict[str, VdomDictConstructor]] = {
"script": custom_vdom_constructor(_script),
"fragment": custom_vdom_constructor(_fragment),
}
def __getattr__(self, value: str) -> VdomDictConstructor:
value = value.rstrip("_").replace("_", "-")
if value in self.__cache__:
return self.__cache__[value]
self.__cache__[value] = make_vdom_constructor(
value, allow_children=value not in NO_CHILDREN_ALLOWED_HTML_BODY
)
return self.__cache__[value]
# HTML elements, written out here for auto-complete purposes
# The actual elements are created dynamically in the __getattr__ method.
# Elements other than these can still be created.
a: VdomDictConstructor
abbr: VdomDictConstructor
address: VdomDictConstructor
area: VdomDictConstructor
article: VdomDictConstructor
aside: VdomDictConstructor
audio: VdomDictConstructor
b: VdomDictConstructor
body: VdomDictConstructor
base: VdomDictConstructor
bdi: VdomDictConstructor
bdo: VdomDictConstructor
blockquote: VdomDictConstructor
br: VdomDictConstructor
button: VdomDictConstructor
canvas: VdomDictConstructor
caption: VdomDictConstructor
cite: VdomDictConstructor
code: VdomDictConstructor
col: VdomDictConstructor
colgroup: VdomDictConstructor
data: VdomDictConstructor
dd: VdomDictConstructor
del_: VdomDictConstructor
details: VdomDictConstructor
dialog: VdomDictConstructor
div: VdomDictConstructor
dl: VdomDictConstructor
dt: VdomDictConstructor
em: VdomDictConstructor
embed: VdomDictConstructor
fieldset: VdomDictConstructor
figcaption: VdomDictConstructor
figure: VdomDictConstructor
footer: VdomDictConstructor
form: VdomDictConstructor
h1: VdomDictConstructor
h2: VdomDictConstructor
h3: VdomDictConstructor
h4: VdomDictConstructor
h5: VdomDictConstructor
h6: VdomDictConstructor
head: VdomDictConstructor
header: VdomDictConstructor
hr: VdomDictConstructor
html: VdomDictConstructor
i: VdomDictConstructor
iframe: VdomDictConstructor
img: VdomDictConstructor
input: VdomDictConstructor
ins: VdomDictConstructor
kbd: VdomDictConstructor
label: VdomDictConstructor
legend: VdomDictConstructor
li: VdomDictConstructor
link: VdomDictConstructor
main: VdomDictConstructor
map: VdomDictConstructor
mark: VdomDictConstructor
math: VdomDictConstructor
menu: VdomDictConstructor
menuitem: VdomDictConstructor
meta: VdomDictConstructor
meter: VdomDictConstructor
nav: VdomDictConstructor
noscript: VdomDictConstructor
object: VdomDictConstructor
ol: VdomDictConstructor
option: VdomDictConstructor
output: VdomDictConstructor
p: VdomDictConstructor
param: VdomDictConstructor
picture: VdomDictConstructor
portal: VdomDictConstructor
pre: VdomDictConstructor
progress: VdomDictConstructor
q: VdomDictConstructor
rp: VdomDictConstructor
rt: VdomDictConstructor
ruby: VdomDictConstructor
s: VdomDictConstructor
samp: VdomDictConstructor
script: VdomDictConstructor
section: VdomDictConstructor
select: VdomDictConstructor
slot: VdomDictConstructor
small: VdomDictConstructor
source: VdomDictConstructor
span: VdomDictConstructor
strong: VdomDictConstructor
style: VdomDictConstructor
sub: VdomDictConstructor
summary: VdomDictConstructor
sup: VdomDictConstructor
table: VdomDictConstructor
tbody: VdomDictConstructor
td: VdomDictConstructor
template: VdomDictConstructor
textarea: VdomDictConstructor
tfoot: VdomDictConstructor
th: VdomDictConstructor
thead: VdomDictConstructor
time: VdomDictConstructor
title: VdomDictConstructor
tr: VdomDictConstructor
track: VdomDictConstructor
u: VdomDictConstructor
ul: VdomDictConstructor
var: VdomDictConstructor
video: VdomDictConstructor
wbr: VdomDictConstructor
fragment: VdomDictConstructor
py_script: VdomDictConstructor
# Special Case: SVG elements
# Since SVG elements have a different set of allowed children, they are
# separated into a different constructor, and are accessed via `html.svg.example()`
svg: SvgConstructor = SvgConstructor()
html = HtmlConstructor()