From a07f97e62791a3e1771f619b0517615178343cd2 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 16 Jun 2022 14:32:38 +0100 Subject: [PATCH] Take final rule in event of specificity clash --- src/textual/css/stylesheet.py | 3 +-- tests/css/test_stylesheet.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/textual/css/stylesheet.py b/src/textual/css/stylesheet.py index d286a6637..36de39f32 100644 --- a/src/textual/css/stylesheet.py +++ b/src/textual/css/stylesheet.py @@ -296,11 +296,10 @@ class Stylesheet: rule_attributes[key].append((rule_specificity, value)) # For each rule declared for this node, keep only the most specific one - get_first_item = itemgetter(0) node_rules: RulesMap = cast( RulesMap, { - name: max(specificity_rules, key=get_first_item)[1] + name: specificity_rules[-1][1] for name, specificity_rules in rule_attributes.items() }, ) diff --git a/tests/css/test_stylesheet.py b/tests/css/test_stylesheet.py index 7dca722cc..5bb78dc80 100644 --- a/tests/css/test_stylesheet.py +++ b/tests/css/test_stylesheet.py @@ -7,6 +7,20 @@ from textual.color import Color from textual.css._help_renderables import HelpText from textual.css.stylesheet import Stylesheet, StylesheetParseError from textual.css.tokenizer import TokenizeError +from textual.dom import DOMNode + + +def test_stylesheet_apply_takes_final_rule_in_specificity_clash(): + css = ".a {background: red; color: lime} .b {background:blue}" + stylesheet = Stylesheet() + stylesheet.source["test.css"] = css + stylesheet.parse() + + node = DOMNode(classes="a b") + stylesheet.apply(node) + + assert node.styles.color == Color(0, 255, 0) # color: lime + assert node.styles.background == Color(0, 0, 255) # background: blue @pytest.mark.parametrize(