From c2f289e472e756a36736aeaffd5a5d860f9f0b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 12 Jan 2023 10:58:14 +0000 Subject: [PATCH 1/9] Add failing test. --- tests/css/test_text_style.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/css/test_text_style.py diff --git a/tests/css/test_text_style.py b/tests/css/test_text_style.py new file mode 100644 index 000000000..ea4c7f1fa --- /dev/null +++ b/tests/css/test_text_style.py @@ -0,0 +1,20 @@ +from rich.style import Style + +from textual.css.styles import Styles + + +def test_text_style_none(): + styles = Styles() + styles.text_style = "none" + assert styles.text_style == Style() + + +def test_text_style_none_with_others(): + """Style "none" mixed with others should result in empty style.""" + styles = Styles() + styles.text_style = "bold none underline italic" + + none_styles = Styles() + styles.text_style = "none" + + assert styles.text_style == none_styles.text_style From 4a893b5169092336a626d3bc21a3446a64bcce64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 12 Jan 2023 10:59:55 +0000 Subject: [PATCH 2/9] Short-circuit text style parsing when unnecessary. --- CHANGELOG.md | 1 + src/textual/css/_style_properties.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af9c30012..9d61abc6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - The styles `scrollbar-background-active` and `scrollbar-color-hover` are no longer ignored https://github.com/Textualize/textual/pull/1480 - The widget `Placeholder` can now have its width set to `auto` https://github.com/Textualize/textual/pull/1508 +- The style `text-style` option `none` can also be mixed with other options https://github.com/Textualize/textual/issues/1420 ## [0.9.1] - 2022-12-30 diff --git a/src/textual/css/_style_properties.py b/src/textual/css/_style_properties.py index 266b1a5a1..edb3d68b3 100644 --- a/src/textual/css/_style_properties.py +++ b/src/textual/css/_style_properties.py @@ -891,6 +891,7 @@ class StyleFlagsProperty: Raises: StyleValueError: If the value is an invalid style flag """ + print(repr(style_flags)) _rich_traceback_omit = True if style_flags is None: if obj.clear_rule(self.name): @@ -909,7 +910,8 @@ class StyleFlagsProperty: self.name, word, context="inline" ), ) - style = Style.parse(style_flags) + # rich doesn't like "none" mixed with other styles, so short-circuit here. + style = Style() if "none" in words else Style.parse(style_flags) if obj.set_rule(self.name, style): obj.refresh() From 9d9726f85488c6851347e54cdb05b2b5d5830132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 12 Jan 2023 11:28:28 +0000 Subject: [PATCH 3/9] Remove stray print statement. --- src/textual/css/_style_properties.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/textual/css/_style_properties.py b/src/textual/css/_style_properties.py index edb3d68b3..a7724ec94 100644 --- a/src/textual/css/_style_properties.py +++ b/src/textual/css/_style_properties.py @@ -891,7 +891,6 @@ class StyleFlagsProperty: Raises: StyleValueError: If the value is an invalid style flag """ - print(repr(style_flags)) _rich_traceback_omit = True if style_flags is None: if obj.clear_rule(self.name): From dc318b8c93965c23496e7a38beab758fa38dd144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 12 Jan 2023 14:39:05 +0000 Subject: [PATCH 4/9] Update tests. --- tests/css/test_text_style.py | 20 -------------------- tests/test_style_properties.py | 13 +++++++++++++ 2 files changed, 13 insertions(+), 20 deletions(-) delete mode 100644 tests/css/test_text_style.py diff --git a/tests/css/test_text_style.py b/tests/css/test_text_style.py deleted file mode 100644 index ea4c7f1fa..000000000 --- a/tests/css/test_text_style.py +++ /dev/null @@ -1,20 +0,0 @@ -from rich.style import Style - -from textual.css.styles import Styles - - -def test_text_style_none(): - styles = Styles() - styles.text_style = "none" - assert styles.text_style == Style() - - -def test_text_style_none_with_others(): - """Style "none" mixed with others should result in empty style.""" - styles = Styles() - styles.text_style = "bold none underline italic" - - none_styles = Styles() - styles.text_style = "none" - - assert styles.text_style == none_styles.text_style diff --git a/tests/test_style_properties.py b/tests/test_style_properties.py index f8900c1b7..81abe8039 100644 --- a/tests/test_style_properties.py +++ b/tests/test_style_properties.py @@ -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,12 @@ 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) as exc_info: + setattr(styles, style_attr, "bold none underline italic") From dd2a85d70b29380dd77fbdb21419a3fa1c2d8070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 12 Jan 2023 14:40:25 +0000 Subject: [PATCH 5/9] Raise custom error when 'none' is mixed with other flags. --- src/textual/css/_style_properties.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/textual/css/_style_properties.py b/src/textual/css/_style_properties.py index a7724ec94..73d30f96d 100644 --- a/src/textual/css/_style_properties.py +++ b/src/textual/css/_style_properties.py @@ -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,8 +910,17 @@ class StyleFlagsProperty: self.name, word, context="inline" ), ) - # rich doesn't like "none" mixed with other styles, so short-circuit here. - style = Style() if "none" in words else Style.parse(style_flags) + try: + style = Style.parse(style_flags) + except rich.errors.StyleSyntaxError as exc: + 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 exc from None if obj.set_rule(self.name, style): obj.refresh() From 003d98e01648bad988467c82d5ee9abac171467a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 12 Jan 2023 14:40:46 +0000 Subject: [PATCH 6/9] Update help text. --- src/textual/css/_help_text.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/textual/css/_help_text.py b/src/textual/css/_help_text.py index 7ac541645..8155dcd3c 100644 --- a/src/textual/css/_help_text.py +++ b/src/textual/css/_help_text.py @@ -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( From 036c3e8651a4d49dc1ec7bd16eb05dfedc53c6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 12 Jan 2023 14:42:04 +0000 Subject: [PATCH 7/9] Update documentation. --- docs/css_types/text_style.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/css_types/text_style.md b/docs/css_types/text_style.md index f41646e3c..40afea528 100644 --- a/docs/css_types/text_style.md +++ b/docs/css_types/text_style.md @@ -8,13 +8,13 @@ The `` CSS type represents styles that can be applied to text. ## Syntax -A [``](/css_types/text_style) can be any _space-separated_ combination of the following values: +A [``](/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` | Strikethrough text. | | `underline` | Underline text. | @@ -42,5 +42,5 @@ A [``](/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 ``` From 8f877d826f6c491cb8cbce0ac53c5349581e74fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 12 Jan 2023 14:43:23 +0000 Subject: [PATCH 8/9] Update changelog. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d61abc6a..439fd56f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,12 +22,12 @@ 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 ### Fixed - The styles `scrollbar-background-active` and `scrollbar-color-hover` are no longer ignored https://github.com/Textualize/textual/pull/1480 - The widget `Placeholder` can now have its width set to `auto` https://github.com/Textualize/textual/pull/1508 -- The style `text-style` option `none` can also be mixed with other options https://github.com/Textualize/textual/issues/1420 ## [0.9.1] - 2022-12-30 From 2b9cd81ca5cf9b1755d17a2183519d347f59c3ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Tue, 17 Jan 2023 10:29:19 +0000 Subject: [PATCH 9/9] Cleanup and new test. --- src/textual/css/_style_properties.py | 4 ++-- tests/test_style_properties.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/textual/css/_style_properties.py b/src/textual/css/_style_properties.py index 73d30f96d..36a40f19b 100644 --- a/src/textual/css/_style_properties.py +++ b/src/textual/css/_style_properties.py @@ -912,7 +912,7 @@ class StyleFlagsProperty: ) try: style = Style.parse(style_flags) - except rich.errors.StyleSyntaxError as exc: + except rich.errors.StyleSyntaxError as error: if "none" in words and len(words) > 1: raise StyleValueError( "cannot mix 'none' with other style flags", @@ -920,7 +920,7 @@ class StyleFlagsProperty: self.name, " ".join(words), context="inline" ), ) from None - raise exc from None + raise error from None if obj.set_rule(self.name, style): obj.refresh() diff --git a/tests/test_style_properties.py b/tests/test_style_properties.py index 81abe8039..107aa2a22 100644 --- a/tests/test_style_properties.py +++ b/tests/test_style_properties.py @@ -18,5 +18,15 @@ 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) as exc_info: + 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()