mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Fix specificity ordering
This commit is contained in:
@@ -296,13 +296,15 @@ class Stylesheet:
|
|||||||
rule_attributes[key].append((rule_specificity, value))
|
rule_attributes[key].append((rule_specificity, value))
|
||||||
|
|
||||||
# For each rule declared for this node, keep only the most specific one
|
# For each rule declared for this node, keep only the most specific one
|
||||||
node_rules: RulesMap = cast(
|
get_first_item = itemgetter(0)
|
||||||
RulesMap,
|
node_rules: RulesMap = cast(RulesMap, {})
|
||||||
{
|
for name, specificity_rules in rule_attributes.items():
|
||||||
name: specificity_rules[-1][1]
|
highest_specificity = max(specificity_rules, key=get_first_item)[0]
|
||||||
for name, specificity_rules in rule_attributes.items()
|
rules_with_highest_specificity = [
|
||||||
},
|
rule for rule in specificity_rules if rule[0] == highest_specificity
|
||||||
)
|
]
|
||||||
|
node_rules[name] = rules_with_highest_specificity[-1][1]
|
||||||
|
|
||||||
self.replace_rules(node, node_rules, animate=animate)
|
self.replace_rules(node, node_rules, animate=animate)
|
||||||
if isinstance(node, Widget):
|
if isinstance(node, Widget):
|
||||||
node._refresh_scrollbars()
|
node._refresh_scrollbars()
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ from textual.dom import DOMNode
|
|||||||
|
|
||||||
|
|
||||||
def test_stylesheet_apply_takes_final_rule_in_specificity_clash():
|
def test_stylesheet_apply_takes_final_rule_in_specificity_clash():
|
||||||
css = ".a {background: red; color: lime} .b {background:blue}"
|
css = ".a {background: red; color: lime} .b {background: blue}"
|
||||||
stylesheet = Stylesheet()
|
stylesheet = Stylesheet()
|
||||||
stylesheet.source["test.css"] = css
|
stylesheet.source["test.css"] = css
|
||||||
stylesheet.parse()
|
stylesheet.parse()
|
||||||
|
|
||||||
node = DOMNode(classes="a b")
|
node = DOMNode(classes="a b", id="c")
|
||||||
stylesheet.apply(node)
|
stylesheet.apply(node)
|
||||||
|
|
||||||
assert node.styles.color == Color(0, 255, 0) # color: lime
|
assert node.styles.color == Color(0, 255, 0) # color: lime
|
||||||
|
|||||||
Reference in New Issue
Block a user