Skip to content

Commit

Permalink
Merge pull request #5471 from Textualize/fix-markup-false
Browse files Browse the repository at this point in the history
fix markup false
  • Loading branch information
willmcgugan authored Jan 10, 2025
2 parents 523ca0d + d3fedb9 commit 2852b12
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/textual/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from rich._wrap import divide_line
from rich.cells import set_cell_size
from rich.console import OverflowMethod
from rich.errors import MissingStyle
from rich.segment import Segment, Segments
from rich.terminal_theme import TerminalTheme
from rich.text import Text
Expand Down Expand Up @@ -744,9 +745,12 @@ def render(

@lru_cache(maxsize=1024)
def get_style(style: str, /) -> Style:
visual_style = Style.from_rich_style(
app.console.get_style(style), app.ansi_theme
)
try:
visual_style = Style.from_rich_style(
app.console.get_style(style), app.ansi_theme
)
except MissingStyle:
visual_style = Style()
return visual_style

else:
Expand Down
6 changes: 4 additions & 2 deletions src/textual/visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ class VisualError(Exception):
VisualType: TypeAlias = "RenderableType | SupportsVisual | Visual"


def visualize(widget: Widget, obj: object) -> Visual:
def visualize(widget: Widget, obj: object, markup: bool = True) -> Visual:
"""Get a visual instance from an object.
If the object does not support the Visual protocol and is a Rich renderable, it
will be wrapped in a [RichVisual][textual.visual.RichVisual].
Args:
widget: The parent widget.
obj: An object.
markup: Enable markup.
Returns:
A Visual instance to render the object, or `None` if there is no associated visual.
Expand All @@ -84,7 +86,7 @@ def visualize(widget: Widget, obj: object) -> Visual:
if is_renderable(obj):
# If it is a string, render it to Text
if isinstance(obj, str):
obj = widget.render_str(obj)
obj = widget.render_str(obj) if markup else Text(obj)

if isinstance(obj, Text) and widget.allow_select:
return Content.from_rich_text(
Expand Down
4 changes: 2 additions & 2 deletions src/textual/widgets/_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(
@property
def visual(self) -> Visual:
if self._visual is None:
self._visual = visualize(self, self._content)
self._visual = visualize(self, self._content, markup=self.markup)
return self._visual

@property
Expand Down Expand Up @@ -109,5 +109,5 @@ def update(self, content: RenderableType | SupportsVisual = "") -> None:
"""

self._content = content
self._visual = visualize(self, content)
self._visual = visualize(self, content, markup=self.markup)
self.refresh(layout=True)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -3311,3 +3311,22 @@ def compose(self) -> ComposeResult:
yield Static("\n".join(f"This is some text {n}" for n in range(100)))

assert snap_compare(ScrollbarOpacityApp())


def test_static_markup(snap_compare):
"""Check that markup may be disabled.
You should see 3 labels.
This first label contains an invalid style, and should have tags removed.
The second label should have the word "markup" boldened.
The third label has markup disabled, and should show tags without styles.
"""

class LabelApp(App):
def compose(self) -> ComposeResult:
yield Label("There should be no [foo]tags or style[/foo]")
yield Label("This allows [bold]markup[/bold]")
yield Label("This does not allow [bold]markup[/bold]", markup=False)

snap_compare(LabelApp())

0 comments on commit 2852b12

Please sign in to comment.