Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
keithasaurus committed Dec 23, 2023
1 parent 5b148c3 commit 7bc5fc9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
21 changes: 13 additions & 8 deletions simple_html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,23 @@ def escape_attribute_key(k: str) -> str:


class Tag:
__slots__ = ("tag_start", "rendered", "closing_tag", "no_children_close")
__slots__ = ("tag_start", "closing_tag", "tag_start_no_attrs", "rendered", "no_children_close")

def __init__(self, name: str, self_closing: bool = False) -> None:
self.tag_start = f"<{name}"
self.tag_start_no_attrs = f"{self.tag_start}>"
self.closing_tag = f"</{name}>"
if self_closing:
self.closing_tag = ""
self.no_children_close = "/>"
else:
self.closing_tag = f"</{name}>"
self.no_children_close = f">{self.closing_tag}"
self.rendered = f"{self.tag_start}{self.no_children_close}"

def __call__(
self,
attributes: Dict[Union[SafeString, str], Union[str, SafeString, None]],
*children: Node,
) -> TagTuple:
) -> Union[TagTuple, SafeString]:
if attributes:
# in this case this is faster than attrs = "".join([...])
attrs = ""
Expand All @@ -126,18 +126,23 @@ def __call__(
if isinstance(key, SafeString)
else escape_attribute_key(key)
)

if isinstance(val, str):
attrs += f' {key}="{escape(val, True)}"'
elif isinstance(val, SafeString):
attrs += f' {key}="{val.safe_str}"'
elif val is None:
attrs += f" {key}"

print("attributes", attrs, attributes)
if children:
return f"{self.tag_start}{attrs}>", children, self.closing_tag
else:
return f"{self.tag_start}{attrs}{self.no_children_close}", children, ""
return f"{self.tag_start}>", children, self.closing_tag
return SafeString(f"{self.tag_start}{attrs}{self.no_children_close}")
elif children:
return self.tag_start_no_attrs, children, self.closing_tag
else:
return SafeString(self.rendered)


DOCTYPE_HTML5 = SafeString("<!doctype html>")
Expand Down Expand Up @@ -265,10 +270,10 @@ def _render(nodes: Iterable[Node], strs: List[str]) -> None:
strs.append(node[0])
_render(node[1], strs)
strs.append(node[2])
elif isinstance(node, str):
strs.append(escape(node))
elif isinstance(node, SafeString):
strs.append(node.safe_str)
elif isinstance(node, str):
strs.append(escape(node))
elif isinstance(node, Tag):
strs.append(node.rendered)
elif isinstance(node, list):
Expand Down
8 changes: 7 additions & 1 deletion tests/test_simple_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Node,
DOCTYPE_HTML5,
render,
escape_attribute_key, render_styles,
escape_attribute_key, render_styles, img,
)


Expand Down Expand Up @@ -129,6 +129,12 @@ def test_attribute_without_value_rendered_as_expected() -> None:
assert render(a({"something": None})) == "<a something></a>"


def test_self_closing_still_nests_children() -> None:
assert render(img({}, div)) == "<img><div></div></img>"
assert render(img({"id": "4"}, div)) == '<img id="4"><div></div></img>'



def test_render_with_doctype() -> None:
assert render(DOCTYPE_HTML5, html) == "<!doctype html><html></html>"

Expand Down

0 comments on commit 7bc5fc9

Please sign in to comment.