diff --git a/htpy/__init__.py b/htpy/__init__.py index 997a846..1a343e1 100644 --- a/htpy/__init__.py +++ b/htpy/__init__.py @@ -275,9 +275,6 @@ def _iter_context(self, ctx: dict[Context[t.Any], t.Any]) -> Iterator[str]: yield from _iter_node_context(self._children, ctx) yield f"" - def __repr__(self) -> str: - return f"<{self.__class__.__name__} '{self}'>" - # Allow starlette Response.render to directly render this element without # explicitly casting to str: # https://github.com/encode/starlette/blob/5ed55c441126687106109a3f5e051176f88cd3e6/starlette/responses.py#L44-L49 @@ -308,6 +305,9 @@ def __getitem__(self: ElementSelf, children: Node) -> ElementSelf: _validate_children(children) return self.__class__(self._name, self._attrs, children) # pyright: ignore [reportUnknownArgumentType] + def __repr__(self) -> str: + return f"<{self.__class__.__name__} '<{self._name}{self._attrs}>...'>" + class HTMLElement(Element): def _iter_context(self, ctx: dict[Context[t.Any], t.Any]) -> Iterator[str]: @@ -319,6 +319,9 @@ class VoidElement(BaseElement): def _iter_context(self, ctx: dict[Context[t.Any], t.Any]) -> Iterator[str]: yield f"<{self._name}{self._attrs}>" + def __repr__(self) -> str: + return f"<{self.__class__.__name__} '<{self._name}{self._attrs}>'>" + def render_node(node: Node) -> _Markup: return _Markup("".join(iter_node(node))) diff --git a/tests/test_element.py b/tests/test_element.py index eac1c85..2579316 100644 --- a/tests/test_element.py +++ b/tests/test_element.py @@ -19,7 +19,7 @@ def test_invalid_element_name() -> None: def test_element_repr() -> None: - assert repr(htpy.div("#a")) == """'>""" + assert repr(htpy.div("#a")) == """...'>""" def test_void_element_repr() -> None: