Skip to content

Commit 6233cb7

Browse files
author
Onimock
committed
Fix: QSS selector matching to include pseudo-states, pseudo-elements, and attributes with full typing
1 parent 0c883d9 commit 6233cb7

File tree

2 files changed

+457
-58
lines changed

2 files changed

+457
-58
lines changed

src/qss_parser/qss_parser.py

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,50 +1763,29 @@ def _get_rules_for_selector(
17631763
List[QSSRule]: List of matching rules.
17641764
"""
17651765
matching_rules: Set[QSSRule] = set()
1766-
base_selector = selector.split("::")[0].split(":")[0].strip()
1766+
escaped_selector: str = re.escape(selector)
1767+
pattern: Pattern[str] = re.compile(rf"^{escaped_selector}([: \[\>]|$|::)")
17671768

17681769
for rule in rules:
1769-
rule_selectors = [s.strip() for s in rule.selector.split(",")]
1770+
rule_selectors: List[str] = [s.strip() for s in rule.selector.split(",")]
17701771
for sel in rule_selectors:
1771-
if sel == selector:
1772-
matching_rules.add(rule)
1773-
continue
1774-
1775-
sel_without_attrs = Constants.COMPILED_ATTRIBUTE_PATTERN.sub(
1776-
"", sel
1777-
).strip()
1778-
if not re.search(r"[> ]+", sel_without_attrs):
1779-
part_base = sel_without_attrs.split("::")[0].split(":")[0].strip()
1780-
if part_base == base_selector:
1781-
if (
1782-
base_selector.startswith("#")
1783-
and base_selector[1:] != object_name
1784-
):
1785-
continue
1786-
if (
1787-
not base_selector.startswith("#")
1788-
and base_selector != class_name
1772+
if pattern.search(sel):
1773+
if selector.startswith("#") and f"#{object_name}" not in sel:
1774+
continue
1775+
if not selector.startswith("#") and selector != class_name:
1776+
sel_without_attrs: str = (
1777+
Constants.COMPILED_ATTRIBUTE_PATTERN.sub("", sel).strip()
1778+
)
1779+
parts: List[str] = [
1780+
part.strip()
1781+
for part in re.split(r"[> ]+", sel_without_attrs)
1782+
if part.strip()
1783+
]
1784+
if not any(
1785+
part.split("::")[0].split(":")[0] == selector
1786+
for part in parts
17891787
):
17901788
continue
1791-
matching_rules.add(rule)
1792-
continue
1793-
1794-
sel_parts = [
1795-
part.strip()
1796-
for part in re.split(r"[> ]+", sel_without_attrs)
1797-
if part.strip()
1798-
]
1799-
class_match = False
1800-
object_match = True
1801-
for part in sel_parts:
1802-
part_base = part.split("::")[0].split(":")[0].strip()
1803-
if part_base == class_name:
1804-
class_match = True
1805-
elif part_base.startswith("#") and part_base[1:] != object_name:
1806-
object_match = False
1807-
break
1808-
1809-
if class_match and object_match:
18101789
matching_rules.add(rule)
18111790

18121791
return list(matching_rules)

0 commit comments

Comments
 (0)