Invalid tag crashes app even when escaping
The string [/ is escaped using textual.markup.escape, but the app still crashes:
from textual.app import App, ComposeResult
from textual.widgets import Header, Static
from textual.markup import escape
class BugSample(App):
BINDINGS = [("q", "quit", "Quit")]
def __init__(self):
super().__init__()
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield Static(id="main-text")
def on_mount(self) -> None:
to_show = "[/"
self.query_one("#main-text").update(escape(to_show))
BugSample().run()
Exception:
╭────────────────────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────────────────────────────────────────────────────────╮
│ test.py:18 in on_mount │
│ │
│ 15 │ ╭───────────────────────────────────────────── locals ─────────────────────────────────────────────╮ │
│ 16 │ def on_mount(self) -> None: │ self = BugSample(title='BugSample', classes={'-dark-mode'}, pseudo_classes={'focus', 'dark'}) │ │
│ 17 │ │ to_show = "[/" │ to_show = '[/' │ │
│ ❱ 18 │ │ self.query_one("#main-text").update(escape(to_show)) ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│ 19 │
│ 20 │
│ 21 BugSample().run() │
│ │
│ /home/kwl/.local/lib/python3.10/site-packages/textual/widgets/_static.py:94 in update │
│ │
│ 91 │ │ """ ╭───────────── locals ─────────────╮ │
│ 92 │ │ │ content = '[/' │ │
│ 93 │ │ self.__content = content │ layout = True │ │
│ ❱ 94 │ │ self.__visual = visualize(self, content, markup=self._render_markup) │ self = Static(id='main-text') │ │
│ 95 │ │ self.refresh(layout=layout) ╰──────────────────────────────────╯ │
│ 96 │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
MarkupError: auto closing tag ('[/]') has nothing to close
In addition, the string '[/b' throws MarkupError: closing tag '[/b]' does not match any open tag.
Textual Diagnostics
Versions
| Name | Value |
|---|---|
| Textual | 6.2.1 |
| Rich | 13.7.0 |
Python
| Name | Value |
|---|---|
| Version | 3.10.12 |
| Implementation | CPython |
| Compiler | GCC 11.4.0 |
| Executable | /usr/bin/python3 |
Operating System
| Name | Value |
|---|---|
| System | Linux |
| Release | 6.6.87.2-microsoft-standard-WSL2 |
| Version | #1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025 |
Terminal
| Name | Value |
|---|---|
| Terminal Application | Windows Terminal |
| TERM | xterm-256color |
| COLORTERM | Not set |
| FORCE_COLOR | Not set |
| NO_COLOR | Not set |
Rich Console options
| Name | Value |
|---|---|
| size | width=209, height=52 |
| legacy_windows | False |
| min_width | 1 |
| max_width | 209 |
| is_terminal | True |
| encoding | utf-8 |
| max_height | 52 |
| justify | None |
| overflow | None |
| no_wrap | False |
| highlight | None |
| markup | None |
| height | None |
Thank you for your issue. Give us a little time to review it.
PS. You might want to check the FAQ if you haven't done so already.
This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).
This is an automated reply, generated by FAQtory
This seems to be the problem:
>>> from textual.content import Content
>>> Content.from_markup("[r")
Content('[r')
>>> Content.from_markup("[/")
Traceback (most recent call last):
Cell In[9], line 1
Content.from_markup("[/")
File ~/work/software/playground-textual/.venv/lib/python3.13/site-packages/textual/content.py:253 in from_markup
content = to_content(markup, template_variables=variables or None)
File ~/work/software/playground-textual/.venv/lib/python3.13/site-packages/textual/markup.py:320 in to_content
raise MarkupError(str(error)) from None
MarkupError: auto closing tag ('[/]') has nothing to close
So [r is recognised as text, not markup. But [/ is interpreted exactly the same as [/], which is an autoclosing tag. But there's nothing to close, so it crashes. It shouldn't recognise [/ as a tag.