From 06175cf827443a0576027e8fa99b9878450bd56d Mon Sep 17 00:00:00 2001 From: Armen Michaeli Date: Mon, 30 Sep 2024 21:40:09 +0200 Subject: [PATCH 1/2] Fix a critical issue with the `selector_list` attribute of `QualifedRule` The "prelude" component of a qualified rule is a list of component values, not just tokens. Parsing it as a selector list, per the `parse_selector_list` API in the `selectors` module, demands a list of _tokens_ (currently), so a `tokens` call is necessary to "flatten" the list of component values. This adds the call, before normalization. --- src/csspring/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/csspring/__init__.py b/src/csspring/__init__.py index de39ab0..d311bb9 100644 --- a/src/csspring/__init__.py +++ b/src/csspring/__init__.py @@ -5,10 +5,10 @@ def _enable_selector_parsing(): This adds on-demand parsing of the prelude part of every qualified rule (see the `prelude` attribute on `QualifiedRule`), when the `selector_list` property is accessed on the latter. Because parsing is only done when the property added with this procedure, is accessed, `csspring.syntax` remains compliant with its respective specification. """ - from .syntax.parsing import normalize_input, QualifiedRule + from .syntax.parsing import normalize_input, QualifiedRule, tokens from .selectors import parse_selector_list def qualified_rule_selector_list(self): - return parse_selector_list(normalize_input(self.prelude)) + return parse_selector_list(normalize_input(tokens(self.prelude))) setattr(QualifiedRule, "selector_list", property(qualified_rule_selector_list)) _enable_selector_parsing() From 889e7ba5bbdcf0b8b55ef1fd965be5d50a0df8ae Mon Sep 17 00:00:00 2001 From: Armen Michaeli Date: Mon, 30 Sep 2024 21:50:17 +0200 Subject: [PATCH 2/2] Add type hints to the init. module of the package The module in question was missing type hinting entirely, which only contributed to the issue the parent commit fixes. This adds type hinting, which also helps validate the initialization behaviour. --- src/csspring/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/csspring/__init__.py b/src/csspring/__init__.py index d311bb9..4911939 100644 --- a/src/csspring/__init__.py +++ b/src/csspring/__init__.py @@ -1,13 +1,13 @@ """Parsing of CSS text aligned with [CSS] specification(s).""" -def _enable_selector_parsing(): +def _enable_selector_parsing() -> None: """Augment parsing procedures to enable parsing of CSS selectors. This adds on-demand parsing of the prelude part of every qualified rule (see the `prelude` attribute on `QualifiedRule`), when the `selector_list` property is accessed on the latter. Because parsing is only done when the property added with this procedure, is accessed, `csspring.syntax` remains compliant with its respective specification. """ - from .syntax.parsing import normalize_input, QualifiedRule, tokens + from .syntax.parsing import normalize_input, Product, QualifiedRule, tokens from .selectors import parse_selector_list - def qualified_rule_selector_list(self): + def qualified_rule_selector_list(self: QualifiedRule) -> Product | None: return parse_selector_list(normalize_input(tokens(self.prelude))) setattr(QualifiedRule, "selector_list", property(qualified_rule_selector_list))