Skip to content

Commit d49477c

Browse files
Merge pull request #1336 from Textualize/fix-1335
Fix default CSS retrieval from widgets with no `DEFAULT_CSS` that inherit from widgets that do have `DEFAULT_CSS`
2 parents 338d74c + 37dec51 commit d49477c

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2727
- Fixed visibility not affecting children https://github.com/Textualize/textual/issues/1313
2828
- Fixed issue with auto width/height and relative children https://github.com/Textualize/textual/issues/1319
2929
- Fixed issue with offset applied to containers https://github.com/Textualize/textual/issues/1256
30+
- Fixed default CSS retrieval for widgets with no `DEFAULT_CSS` that inherited from widgets with `DEFAULT_CSS` https://github.com/Textualize/textual/issues/1335
3031

3132
## [0.5.0] - 2022-11-20
3233

src/textual/dom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def get_path(base: Type[DOMNode]) -> str:
266266
return f"{base.__name__}"
267267

268268
for tie_breaker, base in enumerate(self._node_bases):
269-
css = base.DEFAULT_CSS.strip()
269+
css = base.__dict__.get("DEFAULT_CSS", "").strip()
270270
if css:
271271
css_stack.append((get_path(base), css, -tie_breaker))
272272

tests/test_dom.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,43 @@ def test_validate():
4545
node.toggle_class("1")
4646

4747

48+
def test_get_default_css():
49+
class A(DOMNode):
50+
pass
51+
class B(A):
52+
pass
53+
class C(B):
54+
DEFAULT_CSS = "C"
55+
class D(C):
56+
pass
57+
class E(D):
58+
DEFAULT_CSS = "E"
59+
node = DOMNode()
60+
node_css = node.get_default_css()
61+
a = A()
62+
a_css = a.get_default_css()
63+
b = B()
64+
b_css = b.get_default_css()
65+
c = C()
66+
c_css = c.get_default_css()
67+
d = D()
68+
d_css = d.get_default_css()
69+
e = E()
70+
e_css = e.get_default_css()
71+
72+
# Descendants that don't assign to DEFAULT_CSS don't add new CSS to the stack.
73+
assert len(node_css) == len(a_css) == len(b_css) == 0
74+
assert len(c_css) == len(d_css) == 1
75+
assert len(e_css) == 2
76+
77+
# Descendants do push the priority of the ancestors' rules down.
78+
assert c_css[0][2] == d_css[0][2] + 1 == 0
79+
80+
# The CSS on the stack is the correct one.
81+
assert e_css[0][1:] == ("E", 0)
82+
assert e_css[1][1:] == ("C", -2)
83+
84+
4885
@pytest.fixture
4986
def search():
5087
"""

0 commit comments

Comments
 (0)