Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow empty css variables #2984

Merged
merged 3 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fixed a crash when a `SelectionList` had a prompt wider than itself https://github.com/Textualize/textual/issues/2900
- Fixed a bug where `Click` events were bubbling up from `Switch` widgets https://github.com/Textualize/textual/issues/2366
- Fixed a crash when using empty CSS variables https://github.com/Textualize/textual/issues/1849

## [0.30.0] - 2023-07-17

Expand Down
6 changes: 3 additions & 3 deletions src/textual/css/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def substitute_references(
break
if token.name == "variable_name":
variable_name = token.value[1:-1] # Trim the $ and the :, i.e. "$x:" -> "x"
variable_tokens = variables.setdefault(variable_name, [])
yield token

while True:
Expand All @@ -284,7 +285,7 @@ def substitute_references(
if not token:
break
elif token.name == "whitespace":
variables.setdefault(variable_name, []).append(token)
variable_tokens.append(token)
yield token
elif token.name == "variable_value_end":
yield token
Expand All @@ -293,7 +294,6 @@ def substitute_references(
elif token.name == "variable_ref":
ref_name = token.value[1:]
if ref_name in variables:
variable_tokens = variables.setdefault(variable_name, [])
reference_tokens = variables[ref_name]
variable_tokens.extend(reference_tokens)
ref_location = token.location
Expand All @@ -307,7 +307,7 @@ def substitute_references(
else:
_unresolved(ref_name, variables.keys(), token)
else:
variables.setdefault(variable_name, []).append(token)
variable_tokens.append(token)
yield token
token = next(iter_tokens, None)
elif token.name == "variable_ref":
Expand Down
16 changes: 16 additions & 0 deletions tests/css/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ def test_undefined_variable(self):
with pytest.raises(UnresolvedVariableError):
list(substitute_references(tokenize(css, "")))

def test_empty_variable(self):
css = "$x:\n* { background:$x; }"
result = list(substitute_references(tokenize(css, "")))
assert [(t.name, t.value) for t in result] == [
("variable_name", "$x:"),
("variable_value_end", "\n"),
("selector_start_universal", "*"),
("whitespace", " "),
("declaration_set_start", "{"),
("whitespace", " "),
("declaration_name", "background:"),
("declaration_end", ";"),
("whitespace", " "),
("declaration_set_end", "}"),
]

def test_transitive_reference(self):
css = "$x: 1\n$y: $x\n.thing { border: $y }"
assert list(substitute_references(tokenize(css, ""))) == [
Expand Down
Loading