Skip to content

Commit efa4dfa

Browse files
Add node shim.
1 parent b994ecd commit efa4dfa

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

tdom/transformer.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
Text,
3232
Comment,
3333
DocumentType,
34+
Node,
3435
)
3536
from .escaping import (
3637
escape_html_content_in_tag as default_escape_html_content_in_tag,
@@ -819,13 +820,21 @@ def nodes_from_tcomponent(node_api, parent_node, template, maker_info):
819820

820821
result_template, context_values = invoke_component(component_callable, kwargs)
821822

822-
if result_template and result_template.strings != ("",):
823-
assert not context_values
824-
result_tnode = node_api.parse_template(result_template)
825-
return to_render_node_queue_item(
826-
parent_node,
827-
iter(node_api.walk_subtree(parent_node, result_tnode, result_template)),
828-
)
823+
if isinstance(result_template, Template):
824+
if result_template.strings != ("",):
825+
if context_values:
826+
raise NotImplementedError("We have no implemented these yet.")
827+
result_tnode = node_api.parse_template(result_template)
828+
return to_render_node_queue_item(
829+
parent_node,
830+
iter(node_api.walk_subtree(parent_node, result_tnode, result_template)),
831+
)
832+
# SHIM for old-style tail-call-style wrapping.
833+
elif isinstance(result_template, Node):
834+
if isinstance(result_template, Fragment):
835+
parent_node.children.extend(result_template.children)
836+
else:
837+
parent_node.children.append(result_template)
829838

830839

831840
@dataclass(frozen=True)

tdom/transformer_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
from .nodes import Element, Text, DocumentType, Comment, Fragment
1616
from .transformer import NodeService
17+
from .processor import html
1718

1819

1920
THEME_CTX = ContextVar("theme", default="default")
@@ -549,3 +550,12 @@ def test_node_api_comment(node_api):
549550
def test_node_api_document_type(node_api):
550551
el = node_api.make_node_from_template(t"<!doctype html>")
551552
assert el == Fragment(children=[DocumentType("html")])
553+
554+
555+
def test_node_api_component_node_shim(node_api):
556+
def Comp() -> Element:
557+
name = "Node Mode"
558+
return t.cast(Element, html(t"<div>{name}</div>"))
559+
560+
el = node_api.make_node_from_template(t"<div><{Comp} /></div>")
561+
assert el == Element("div", children=[Element("div", children=[Text("Node Mode")])])

0 commit comments

Comments
 (0)