From d7159d905ad4be2dd6b5b1f4d712d8d65f2f7cdb Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Tue, 7 Jun 2022 15:36:45 +0100 Subject: [PATCH] Fix regex, fix some broken tests around HSL support --- src/textual/color.py | 8 ++++++-- tests/css/test_stylesheet.py | 3 --- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/textual/color.py b/src/textual/color.py index 0d877efcf..0257f7830 100644 --- a/src/textual/color.py +++ b/src/textual/color.py @@ -63,7 +63,7 @@ RE_COLOR = re.compile( rgb{OPEN_BRACE}({DECIMAL}{COMMA}{DECIMAL}{COMMA}{DECIMAL}){CLOSE_BRACE}$| rgba{OPEN_BRACE}({DECIMAL}{COMMA}{DECIMAL}{COMMA}{DECIMAL}{COMMA}{DECIMAL}){CLOSE_BRACE}$| hsl{OPEN_BRACE}({DECIMAL}{COMMA}{PERCENT}{COMMA}{PERCENT}){CLOSE_BRACE}$| -hsla{OPEN_BRACE}({DECIMAL}{COMMA}{PERCENT}{COMMA}{PERCENT}{COMMA}{DECIMAL}){CLOSE_BRACE}$| +hsla{OPEN_BRACE}({DECIMAL}{COMMA}{PERCENT}{COMMA}{PERCENT}{COMMA}{DECIMAL}){CLOSE_BRACE}$ """, re.VERBOSE, ) @@ -290,7 +290,11 @@ class Color(NamedTuple): if color_match is None: error_message = f"failed to parse {color_text!r} as a color" suggested_color = None - if not color_text.startswith("#") and not color_text.startswith("rgb"): + if ( + not color_text.startswith("#") + and not color_text.startswith("rgb") + and not color_text.startswith("hsl") + ): # Seems like we tried to use a color name: let's try to find one that is close enough: suggested_color = get_suggestion(color_text, COLOR_NAME_TO_RGB.keys()) if suggested_color: diff --git a/tests/css/test_stylesheet.py b/tests/css/test_stylesheet.py index 9ef433a29..7dca722cc 100644 --- a/tests/css/test_stylesheet.py +++ b/tests/css/test_stylesheet.py @@ -32,9 +32,6 @@ from textual.css.tokenizer import TokenizeError ["red 4", pytest.raises(StylesheetParseError), None], # space in it ["1", pytest.raises(StylesheetParseError), None], # invalid value ["()", pytest.raises(TokenizeError), None], # invalid tokens - # TODO: allow spaces in rgb/rgba expressions? - ["rgb(200, 90, 30)", pytest.raises(TokenizeError), None], - ["rgba(200,90,30, 0.4)", pytest.raises(TokenizeError), None], ], ) def test_color_property_parsing(css_value, expectation, expected_color):