mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Take final rule in event of specificity clash
This commit is contained in:
@@ -296,11 +296,10 @@ 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
|
||||||
get_first_item = itemgetter(0)
|
|
||||||
node_rules: RulesMap = cast(
|
node_rules: RulesMap = cast(
|
||||||
RulesMap,
|
RulesMap,
|
||||||
{
|
{
|
||||||
name: max(specificity_rules, key=get_first_item)[1]
|
name: specificity_rules[-1][1]
|
||||||
for name, specificity_rules in rule_attributes.items()
|
for name, specificity_rules in rule_attributes.items()
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,6 +7,20 @@ from textual.color import Color
|
|||||||
from textual.css._help_renderables import HelpText
|
from textual.css._help_renderables import HelpText
|
||||||
from textual.css.stylesheet import Stylesheet, StylesheetParseError
|
from textual.css.stylesheet import Stylesheet, StylesheetParseError
|
||||||
from textual.css.tokenizer import TokenizeError
|
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(
|
@pytest.mark.parametrize(
|
||||||
|
|||||||
Reference in New Issue
Block a user