@@ -121,7 +121,12 @@ def _escape_children(self) -> bool:
121121 """
122122 return True
123123
124- def _render (self , indent : str , options : FullRenderOptions ) -> list [str ]:
124+ def _render (
125+ self ,
126+ indent : str ,
127+ options : FullRenderOptions ,
128+ skip_indent : bool = False ,
129+ ) -> list [str ]:
125130 """
126131 Renders tag and its children to a list of strings where each string is
127132 a single line of output.
@@ -132,6 +137,8 @@ def _render(self, indent: str, options: FullRenderOptions) -> list[str]:
132137 string to use for indentation
133138 options : FullOptions
134139 rendering options
140+ skip_indent : bool
141+ whether to skip indentation for this element
135142
136143 Returns
137144 -------
@@ -148,8 +155,13 @@ def _render(self, indent: str, options: FullRenderOptions) -> list[str]:
148155 )
149156 )
150157
158+ # Indentation to use before opening tag
159+ indent_pre = "" if skip_indent else indent
160+ # Indentation to use before closing tag
161+ indent_post = "" if skip_indent or options .indent is None else indent
162+
151163 # Tag and attributes
152- opening = f"{ indent } <{ self ._get_tag_name ()} "
164+ opening = f"{ indent_pre } <{ self ._get_tag_name ()} "
153165
154166 # Add pre-content
155167 if (pre := self ._get_tag_pre_content ()) is not None :
@@ -177,7 +189,7 @@ def _render(self, indent: str, options: FullRenderOptions) -> list[str]:
177189 return [
178190 opening ,
179191 * children ,
180- f"{ indent } { closing } " ,
192+ f"{ indent_post } { closing } " ,
181193 ]
182194 else :
183195 # Children must have at least one line, since we would have
@@ -190,7 +202,12 @@ def _render(self, indent: str, options: FullRenderOptions) -> list[str]:
190202 # Add the closing tag onto the end
191203 return [
192204 * out [:- 1 ],
193- 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 ,
194211 ]
195212
196213 def render (self ) -> str :
@@ -217,11 +234,17 @@ def __init__(
217234 # Self-closing tags don't allow children
218235 super ().__init__ (* options , ** attributes )
219236
220- def _render (self , indent : str , options : FullRenderOptions ) -> list [str ]:
237+ def _render (
238+ self ,
239+ indent : str ,
240+ options : FullRenderOptions ,
241+ skip_indent : bool = False ,
242+ ) -> list [str ]:
221243 """
222244 Renders tag and its children to a list of strings where each string is
223245 a single line of output
224246 """
247+ indent_str = "" if skip_indent else indent
225248 attributes = util .filter_attributes (
226249 util .dict_union (
227250 self ._get_default_attributes (self .attributes ),
@@ -230,18 +253,18 @@ def _render(self, indent: str, options: FullRenderOptions) -> list[str]:
230253 )
231254 if len (attributes ):
232255 return [
233- f"{ indent } <{ self ._get_tag_name ()} "
256+ f"{ indent_str } <{ self ._get_tag_name ()} "
234257 f"{ util .render_tag_attributes (attributes )} />"
235258 ]
236259 else :
237- return [f"{ indent } <{ self ._get_tag_name ()} />" ]
260+ return [f"{ indent_str } <{ self ._get_tag_name ()} />" ]
238261
239262
240263@deprecated (
241264 "Overload `_get_default_render_options` to return "
242265 "`RenderOptions(indent=None, spacing='')` instead"
243266)
244- class WhitespaceSensitiveTag (Tag ):
267+ class WhitespaceSensitiveTag (Tag ): # pragma: no cover
245268 """
246269 Whitespace-sensitive tags are tags where whitespace needs to be respected.
247270 """
0 commit comments