Merge pull request #1551 from Textualize/fix-1420

Raise clearer exception when `none` is in a space-separated list of text styles.
This commit is contained in:
Will McGugan
2023-01-17 10:05:27 -08:00
committed by GitHub
5 changed files with 40 additions and 4 deletions

View File

@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fail-fast and print pretty tracebacks for Widget compose errors https://github.com/Textualize/textual/pull/1505
- Added Widget._refresh_scroll to avoid expensive layout when scrolling https://github.com/Textualize/textual/pull/1524
- `events.Paste` now bubbles https://github.com/Textualize/textual/issues/1434
- Improved error message when style flag `none` is mixed with other flags (e.g., when setting `text-style`) https://github.com/Textualize/textual/issues/1420
- Clock color in the `Header` widget now matches the header color https://github.com/Textualize/textual/issues/1459
- Programmatic calls to scroll now optionally scroll even if overflow styling says otherwise (introduces a new `force` parameter to all the `scroll_*` methods) https://github.com/Textualize/textual/issues/1201
- `COMPONENT_CLASSES` are now inherited from base classes https://github.com/Textualize/textual/issues/1399

View File

@@ -8,13 +8,13 @@ The `<text-style>` CSS type represents styles that can be applied to text.
## Syntax
A [`<text-style>`](/css_types/text_style) can be any _space-separated_ combination of the following values:
A [`<text-style>`](/css_types/text_style) can be the value `none` for plain text with no styling,
or any _space-separated_ combination of the following values:
| Value | Description |
|-------------|-----------------------------------------------------------------|
| `bold` | **Bold text.** |
| `italic` | _Italic text._ |
| `none` | Plain text with no styling. |
| `reverse` | Reverse video text (foreground and background colors reversed). |
| `strike` | <s>Strikethrough text.</s> |
| `underline` | <u>Underline text.</u> |
@@ -42,5 +42,5 @@ A [`<text-style>`](/css_types/text_style) can be any _space-separated_ combinati
widget.styles.text_style = "strike"
# You can also combine multiple values
widget.styles.text_style = "bold underline italic"
widget.styles.text_style = "strike bold italic reverse
```

View File

@@ -729,6 +729,7 @@ def style_flags_property_help_text(
f"Style flag values such as [i]{property_name}[/] expect space-separated values"
),
Bullet(f"Permitted values are {friendly_list(VALID_STYLE_FLAGS)}"),
Bullet("The value 'none' cannot be mixed with others"),
*ContextSpecificBullets(
inline=[
Bullet(

View File

@@ -12,6 +12,7 @@ from __future__ import annotations
from operator import attrgetter
from typing import TYPE_CHECKING, Generic, Iterable, NamedTuple, TypeVar, cast
import rich.errors
import rich.repr
from rich.style import Style
@@ -909,7 +910,17 @@ class StyleFlagsProperty:
self.name, word, context="inline"
),
)
style = Style.parse(style_flags)
try:
style = Style.parse(style_flags)
except rich.errors.StyleSyntaxError as error:
if "none" in words and len(words) > 1:
raise StyleValueError(
"cannot mix 'none' with other style flags",
help_text=style_flags_property_help_text(
self.name, " ".join(words), context="inline"
),
) from None
raise error from None
if obj.set_rule(self.name, style):
obj.refresh()

View File

@@ -1,4 +1,8 @@
import pytest
from rich.style import Style
from textual.color import Color
from textual.css.errors import StyleValueError
from textual.css.styles import Styles
@@ -7,3 +11,22 @@ def test_box_normalization():
styles = Styles()
styles.border_left = ("none", "red")
assert styles.border_left == ("", Color.parse("red"))
@pytest.mark.parametrize("style_attr", ["text_style", "link_style"])
def test_text_style_none_with_others(style_attr):
"""Style "none" mixed with others should give custom Textual exception."""
styles = Styles()
with pytest.raises(StyleValueError):
setattr(styles, style_attr, "bold none underline italic")
@pytest.mark.parametrize("style_attr", ["text_style", "link_style"])
def test_text_style_set_to_none(style_attr):
"""Setting text style to "none" should clear the styles."""
styles = Styles()
setattr(styles, style_attr, "bold underline italic")
assert getattr(styles, style_attr) != Style.null()
setattr(styles, style_attr, "none")
assert getattr(styles, style_attr) == Style.null()