Take final rule in event of specificity clash

This commit is contained in:
Darren Burns
2022-06-16 14:32:38 +01:00
parent fe151a7f25
commit a07f97e627
2 changed files with 15 additions and 2 deletions

View File

@@ -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()
},
)

View File

@@ -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(