Skip to content

Invalid tag crashes app even when escaping #6155

@kittenswolf

Description

@kittenswolf

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions