From d21bd8240631335e5d630b94c011de47f7614ea9 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 3 Feb 2022 10:27:01 +0000 Subject: [PATCH] Adding a test around the whitespace trimming logic --- src/textual/css/parse.py | 3 ++- tests/css/test_parse.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/textual/css/parse.py b/src/textual/css/parse.py index 0ef63547c..89e74228e 100644 --- a/src/textual/css/parse.py +++ b/src/textual/css/parse.py @@ -242,7 +242,8 @@ def substitute_references(tokens: Iterator[Token]) -> Iterable[Token]: variable_name = token.value[1:-1] # Trim the $ and the :, i.e. "$x:" -> "x" yield token - # Lookahead for the variable value tokens + # Store the tokens for any variable definitions, and substitute + # any variable references we encounter with them. leading_whitespace = True while True: token = next(tokens, None) diff --git a/tests/css/test_parse.py b/tests/css/test_parse.py index a08882f5f..79c0ff7e5 100644 --- a/tests/css/test_parse.py +++ b/tests/css/test_parse.py @@ -30,6 +30,23 @@ class TestVariableReferenceSubstitution: Token(name='declaration_set_end', value='}', path='', code=css, location=(0, 31)) ] + def test_simple_reference_no_whitespace(self): + css = "$x:1; #some-widget{border: $x;}" + variables = substitute_references(tokenize(css, "")) + assert list(variables) == [ + Token(name='variable_name', value='$x:', path='', code=css, location=(0, 0)), + Token(name='number', value='1', path='', code=css, location=(0, 3)), + Token(name='variable_value_end', value=';', path='', code=css, location=(0, 4)), + Token(name='whitespace', value=' ', path='', code=css, location=(0, 5)), + Token(name='selector_start_id', value='#some-widget', path='', code=css, location=(0, 6)), + Token(name='declaration_set_start', value='{', path='', code=css, location=(0, 18)), + Token(name='declaration_name', value='border:', path='', code=css, location=(0, 19)), + Token(name='whitespace', value=' ', path='', code=css, location=(0, 26)), + Token(name='number', value='1', path='', code=css, location=(0, 3)), + Token(name='declaration_end', value=';', path='', code=css, location=(0, 29)), + Token(name='declaration_set_end', value='}', path='', code=css, location=(0, 30)) + ] + def test_undefined_variable(self): css = ".thing { border: $not-defined; }" with pytest.raises(UnresolvedVariableError):