Skip to content

Commit 7b1d6f9

Browse files
committed
Move del_html_head_body_transform to from reactpy to reactpy-django
1 parent 510ad5c commit 7b1d6f9

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/reactpy_django/components.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
from reactpy_django.exceptions import ViewNotRegisteredError
1515
from reactpy_django.forms.components import _django_form
1616
from reactpy_django.pyscript.components import _pyscript_component
17-
from reactpy_django.utils import cached_static_file, generate_obj_name, import_module, render_view
17+
from reactpy_django.utils import (
18+
cached_static_file,
19+
del_html_head_body_transform,
20+
generate_obj_name,
21+
import_module,
22+
render_view,
23+
)
1824

1925
if TYPE_CHECKING:
2026
from collections.abc import Sequence
@@ -224,7 +230,7 @@ async def _render_view():
224230
set_converted_view(
225231
utils.html_to_vdom(
226232
response.content.decode("utf-8").strip(),
227-
utils.del_html_head_body_transform,
233+
del_html_head_body_transform,
228234
*transforms,
229235
strict=strict_parsing,
230236
)

src/reactpy_django/utils.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from collections.abc import Awaitable, Mapping, Sequence
4343

4444
from django.views import View
45-
from reactpy.types import ComponentConstructor
45+
from reactpy.types import ComponentConstructor, VdomDict
4646

4747
from reactpy_django.types import FuncParams, Inferred
4848

@@ -517,3 +517,17 @@ def cached_static_file(static_path: str) -> str:
517517
caches[REACTPY_CACHE].set(cache_key, file_contents, timeout=None, version=int(last_modified_time))
518518

519519
return file_contents
520+
521+
522+
def del_html_head_body_transform(vdom: VdomDict) -> VdomDict:
523+
"""Transform intended for use with `html_to_vdom`.
524+
525+
Removes `<html>`, `<head>`, and `<body>` while preserving their children.
526+
527+
Parameters:
528+
vdom:
529+
The VDOM dictionary to transform.
530+
"""
531+
if vdom["tagName"] in {"html", "body", "head"}:
532+
return {"tagName": "", "children": vdom.setdefault("children", [])}
533+
return vdom

0 commit comments

Comments
 (0)