Skip to content

Commit ae35fd5

Browse files
Merge pull request #4040 from Textualize/improve-nested-tcss
Allow lists of nested selectors and allow styles after nested CSS blocks
2 parents 3abc8ee + 159a54e commit ae35fd5

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919
- Breaking change: Renamed `TextArea.tab_behaviour` to `TextArea.tab_behavior` https://github.com/Textualize/textual/pull/4124
2020
- `TextArea.theme` now defaults to `"css"` instead of None, and is no longer optional https://github.com/Textualize/textual/pull/4157
2121

22+
### Fixed
23+
24+
- Improve support for selector lists in nested TCSS https://github.com/Textualize/textual/issues/3969
25+
- Improve support for rule declarations after nested TCSS rule sets https://github.com/Textualize/textual/issues/3999
26+
2227
## [0.50.1] - 2024-02-09
2328

2429
### Fixed

src/textual/css/tokenize.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
whitespace=r"\s+",
6565
comment_start=COMMENT_START,
6666
comment_line=COMMENT_LINE,
67+
declaration_name=r"[a-zA-Z_\-]+\:",
6768
selector_start_id=r"\#" + IDENTIFIER,
6869
selector_start_class=r"\." + IDENTIFIER,
6970
selector_start_universal=r"\*",
@@ -105,6 +106,7 @@
105106
new_selector=r",",
106107
declaration_set_start=r"\{",
107108
declaration_set_end=r"\}",
109+
nested=r"\&",
108110
).expect_eof(True)
109111

110112
# A rule declaration e.g. "text: red;"

tests/css/test_nested_css.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,53 @@ async def test_nest_app():
4444
assert app.query_one("#foo .paul").styles.background == Color.parse("blue")
4545

4646

47+
class ListOfNestedSelectorsApp(App[None]):
48+
CSS = """
49+
Label {
50+
&.foo, &.bar {
51+
background: red;
52+
}
53+
}
54+
"""
55+
56+
def compose(self) -> ComposeResult:
57+
yield Label("one", classes="foo")
58+
yield Label("two", classes="bar")
59+
yield Label("three", classes="heh")
60+
61+
62+
async def test_lists_of_selectors_in_nested_css() -> None:
63+
"""Regression test for https://github.com/Textualize/textual/issues/3969."""
64+
app = ListOfNestedSelectorsApp()
65+
red = Color.parse("red")
66+
async with app.run_test():
67+
assert app.query_one(".foo").styles.background == red
68+
assert app.query_one(".bar").styles.background == red
69+
assert app.query_one(".heh").styles.background != red
70+
71+
72+
class DeclarationAfterNestedApp(App[None]):
73+
CSS = """
74+
Screen {
75+
Label {
76+
background: red;
77+
}
78+
background: green;
79+
}
80+
"""
81+
82+
def compose(self) -> ComposeResult:
83+
yield Label("one")
84+
85+
86+
async def test_rule_declaration_after_nested() -> None:
87+
"""Regression test for https://github.com/Textualize/textual/issues/3999."""
88+
app = DeclarationAfterNestedApp()
89+
async with app.run_test():
90+
assert app.screen.styles.background == Color.parse("green")
91+
assert app.query_one(Label).styles.background == Color.parse("red")
92+
93+
4794
@pytest.mark.parametrize(
4895
("css", "exception"),
4996
[

0 commit comments

Comments
 (0)