Skip to content

Commit 1171571

Browse files
Merge pull request #115 from COMP1010UNSW/maddy-fix-empty-para
Fix crash when rendering a paragraph with an empty string
2 parents 5c65145 + 467edcd commit 1171571

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

pyhtml/__tag_base.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,20 @@ def _render(
172172
else:
173173
opening += ">"
174174

175-
if not len(self.children):
175+
# Render children
176+
indent_increase = options.indent if options.spacing == "\n" else ""
177+
children = util.render_children(
178+
self.children,
179+
self._escape_children(),
180+
"" if indent_increase is None else indent + indent_increase,
181+
options,
182+
)
183+
184+
# If rendering the children produced no output
185+
if not len(children):
176186
opening += f"</{self._get_tag_name()}>"
177187
return [opening]
178188
else:
179-
indent_increase = options.indent if options.spacing == "\n" else ""
180-
# Children
181-
children = util.render_children(
182-
self.children,
183-
self._escape_children(),
184-
"" if indent_increase is None else indent + indent_increase,
185-
options,
186-
)
187189
closing = f"</{self._get_tag_name()}>"
188190
if options.spacing == "\n":
189191
return [

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "pyhtml-enhanced"
3-
version = "2.2.3"
3+
version = "2.2.4"
44
description = "A library for building HTML documents with a simple and learnable syntax"
55
readme = "README.md"
66
license = "MIT"

tests/basic_rendering_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,12 @@ def test_whitespace_sensitive_with_attrs():
343343
Do whitespace-sensitive tags render properly when they have attributes?
344344
"""
345345
assert str(pre(test="test")("hi")) == '<pre test="test">hi</pre>'
346+
347+
348+
def test_render_ele_with_empty_str():
349+
"""
350+
Rendering an element with an empty string used to cause a crash. Add a
351+
test so we avoid it.
352+
"""
353+
doc = body("")
354+
assert str(doc) == "<body></body>"

tests/render_options_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,12 @@ def test_paragraphs_render_in_body():
161161
"</body>",
162162
]
163163
)
164+
165+
166+
def test_render_paragraph_with_empty_str():
167+
"""
168+
Rendering a paragraph with an empty string used to cause a crash. Add a
169+
test so we avoid it.
170+
"""
171+
doc = p.p("")
172+
assert str(doc) == "<p></p>"

0 commit comments

Comments
 (0)