Skip to content

Commit 2852b12

Browse files
authored
Merge pull request #5471 from Textualize/fix-markup-false
fix markup false
2 parents 523ca0d + d3fedb9 commit 2852b12

File tree

5 files changed

+183
-7
lines changed

5 files changed

+183
-7
lines changed

src/textual/content.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from rich._wrap import divide_line
2020
from rich.cells import set_cell_size
2121
from rich.console import OverflowMethod
22+
from rich.errors import MissingStyle
2223
from rich.segment import Segment, Segments
2324
from rich.terminal_theme import TerminalTheme
2425
from rich.text import Text
@@ -744,9 +745,12 @@ def render(
744745

745746
@lru_cache(maxsize=1024)
746747
def get_style(style: str, /) -> Style:
747-
visual_style = Style.from_rich_style(
748-
app.console.get_style(style), app.ansi_theme
749-
)
748+
try:
749+
visual_style = Style.from_rich_style(
750+
app.console.get_style(style), app.ansi_theme
751+
)
752+
except MissingStyle:
753+
visual_style = Style()
750754
return visual_style
751755

752756
else:

src/textual/visual.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ class VisualError(Exception):
6060
VisualType: TypeAlias = "RenderableType | SupportsVisual | Visual"
6161

6262

63-
def visualize(widget: Widget, obj: object) -> Visual:
63+
def visualize(widget: Widget, obj: object, markup: bool = True) -> Visual:
6464
"""Get a visual instance from an object.
6565
6666
If the object does not support the Visual protocol and is a Rich renderable, it
6767
will be wrapped in a [RichVisual][textual.visual.RichVisual].
6868
6969
Args:
70+
widget: The parent widget.
7071
obj: An object.
72+
markup: Enable markup.
7173
7274
Returns:
7375
A Visual instance to render the object, or `None` if there is no associated visual.
@@ -84,7 +86,7 @@ def visualize(widget: Widget, obj: object) -> Visual:
8486
if is_renderable(obj):
8587
# If it is a string, render it to Text
8688
if isinstance(obj, str):
87-
obj = widget.render_str(obj)
89+
obj = widget.render_str(obj) if markup else Text(obj)
8890

8991
if isinstance(obj, Text) and widget.allow_select:
9092
return Content.from_rich_text(

src/textual/widgets/_static.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def __init__(
7474
@property
7575
def visual(self) -> Visual:
7676
if self._visual is None:
77-
self._visual = visualize(self, self._content)
77+
self._visual = visualize(self, self._content, markup=self.markup)
7878
return self._visual
7979

8080
@property
@@ -109,5 +109,5 @@ def update(self, content: RenderableType | SupportsVisual = "") -> None:
109109
"""
110110

111111
self._content = content
112-
self._visual = visualize(self, content)
112+
self._visual = visualize(self, content, markup=self.markup)
113113
self.refresh(layout=True)
Lines changed: 151 additions & 0 deletions
Loading

tests/snapshot_tests/test_snapshots.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3311,3 +3311,22 @@ def compose(self) -> ComposeResult:
33113311
yield Static("\n".join(f"This is some text {n}" for n in range(100)))
33123312

33133313
assert snap_compare(ScrollbarOpacityApp())
3314+
3315+
3316+
def test_static_markup(snap_compare):
3317+
"""Check that markup may be disabled.
3318+
3319+
You should see 3 labels.
3320+
3321+
This first label contains an invalid style, and should have tags removed.
3322+
The second label should have the word "markup" boldened.
3323+
The third label has markup disabled, and should show tags without styles.
3324+
"""
3325+
3326+
class LabelApp(App):
3327+
def compose(self) -> ComposeResult:
3328+
yield Label("There should be no [foo]tags or style[/foo]")
3329+
yield Label("This allows [bold]markup[/bold]")
3330+
yield Label("This does not allow [bold]markup[/bold]", markup=False)
3331+
3332+
snap_compare(LabelApp())

0 commit comments

Comments
 (0)