From 4c250686140712d7621a15eecadf67511cf5677e Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Sat, 25 Jan 2025 20:37:14 -0800 Subject: [PATCH 1/7] Functional new syntax for HTML --- src/reactpy/__init__.py | 5 +- src/reactpy/_html.py | 482 +++++++++++++++++++++++++++++ src/reactpy/html.py | 500 ------------------------------- src/reactpy/svg.py | 139 --------- src/reactpy/widgets.py | 2 +- {src/reactpy => tests}/sample.py | 0 tests/test_backend/test_utils.py | 2 +- tests/test_sample.py | 2 +- tests/test_testing.py | 2 +- 9 files changed, 488 insertions(+), 646 deletions(-) create mode 100644 src/reactpy/_html.py delete mode 100644 src/reactpy/html.py delete mode 100644 src/reactpy/svg.py rename {src/reactpy => tests}/sample.py (100%) diff --git a/src/reactpy/__init__.py b/src/reactpy/__init__.py index d54e82174..f22aa5832 100644 --- a/src/reactpy/__init__.py +++ b/src/reactpy/__init__.py @@ -1,4 +1,5 @@ -from reactpy import backend, config, html, logging, sample, svg, types, web, widgets +from reactpy import backend, config, logging, types, web, widgets +from reactpy._html import html from reactpy.backend.utils import run from reactpy.core import hooks from reactpy.core.component import component @@ -37,8 +38,6 @@ "html_to_vdom", "logging", "run", - "sample", - "svg", "types", "use_callback", "use_connection", diff --git a/src/reactpy/_html.py b/src/reactpy/_html.py new file mode 100644 index 000000000..69bbda783 --- /dev/null +++ b/src/reactpy/_html.py @@ -0,0 +1,482 @@ +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.core.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 `