Skip to content

Commit

Permalink
Fix render str (#3920)
Browse files Browse the repository at this point in the history
* Fix text render

* changelog
  • Loading branch information
willmcgugan authored Dec 22, 2023
1 parent ff70df8 commit 6a89446
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- `Widget.move_child` would break if `before`/`after` is set to the index of the widget in `child` https://github.com/Textualize/textual/issues/1743
- Fixed auto width text not processing markup https://github.com/Textualize/textual/issues/3918

### Changed

Expand Down
2 changes: 1 addition & 1 deletion src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -3227,7 +3227,7 @@ def _render(self) -> ConsoleRenderable | RichCast:
"""
renderable = self.render()
if isinstance(renderable, str):
return Text(renderable)
return Text.from_markup(renderable)
return renderable

async def run_action(self, action: str) -> None:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,17 @@ def compose(self) -> ComposeResult:
with pytest.raises(MountError):
async with app.run_test():
pass


async def test_render_returns_text():
"""Test that render processes console markup when returning a string."""

# Regression test for https://github.com/Textualize/textual/issues/3918
class SimpleWidget(Widget):
def render(self) -> str:
return "Hello [b]World[/b]!"

widget = SimpleWidget()
render_result = widget._render()
assert isinstance(render_result, Text)
assert render_result.plain == "Hello World!"

0 comments on commit 6a89446

Please sign in to comment.