Skip to content

Commit d7eb55e

Browse files
Avoid double indenting for single-line elements
1 parent 103e928 commit d7eb55e

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

pyhtml/__tag_base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ def _render(
202202
# Add the closing tag onto the end
203203
return [
204204
*out[:-1],
205-
indent_post + out[-1] + options.spacing + closing,
205+
# Only include post indentation if it's on a different line
206+
# to the pre indentation
207+
(indent_post if len(out) > 1 else "")
208+
+ out[-1]
209+
+ options.spacing
210+
+ closing,
206211
]
207212

208213
def render(self) -> str:

tests/basic_rendering_test.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_renders_elements_with_children():
4646
)
4747

4848

49-
def test_renders_deeply_nested_children():
49+
def test_renders_nested_children():
5050
doc = body(
5151
div(
5252
span("Hello world"),
@@ -66,6 +66,30 @@ def test_renders_deeply_nested_children():
6666
)
6767

6868

69+
def test_renders_deeply_nested_children():
70+
doc = body(
71+
div(
72+
span(
73+
div("Hello world"),
74+
),
75+
),
76+
)
77+
78+
assert str(doc) == "\n".join(
79+
[
80+
"<body>",
81+
" <div>",
82+
" <span>",
83+
" <div>",
84+
" Hello world",
85+
" </div>",
86+
" </span>",
87+
" </div>",
88+
"</body>",
89+
]
90+
)
91+
92+
6993
def test_renders_attributes():
7094
doc = body(foo="bar")
7195

tests/render_options_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,16 @@ def test_default_render_options_paragraph():
148148
def test_extra_space_is_respected_in_paragraphs():
149149
doc = p.p(" Paragraph ")
150150
assert str(doc) == "<p> Paragraph </p>"
151+
152+
153+
def test_paragraphs_render_in_body():
154+
doc = p.body(
155+
p.p("Paragraph"),
156+
)
157+
assert str(doc) == "\n".join(
158+
[
159+
"<body>",
160+
" <p>Paragraph</p>",
161+
"</body>",
162+
]
163+
)

0 commit comments

Comments
 (0)