1212from .types import Specificity3
1313
1414if TYPE_CHECKING :
15+ from typing_extensions import Self
16+
1517 from ..dom import DOMNode
1618
1719
1820class SelectorType (Enum ):
21+ """Type of selector."""
22+
1923 UNIVERSAL = 1
24+ """i.e. * operator"""
2025 TYPE = 2
26+ """A CSS type, e.g Label"""
2127 CLASS = 3
28+ """CSS class, e.g. .loaded"""
2229 ID = 4
30+ """CSS ID, e.g. #main"""
2331 NESTED = 5
32+ """Placeholder for nesting operator, i.e &"""
2433
2534
2635class CombinatorType (Enum ):
36+ """Type of combinator."""
37+
2738 SAME = 1
39+ """Selector is combined with previous selector"""
2840 DESCENDENT = 2
41+ """Selector is a descendant of the previous selector"""
2942 CHILD = 3
43+ """Selector is an immediate child of the previous selector"""
3044
3145
3246@dataclass
@@ -116,6 +130,8 @@ def _check_id(self, node: DOMNode) -> bool:
116130
117131@dataclass
118132class Declaration :
133+ """A single CSS declaration (not yet processed)."""
134+
119135 token : Token
120136 name : str
121137 tokens : list [Token ] = field (default_factory = list )
@@ -124,6 +140,8 @@ class Declaration:
124140@rich .repr .auto (angular = True )
125141@dataclass
126142class SelectorSet :
143+ """A set of selectors associated with a rule set."""
144+
127145 selectors : list [Selector ] = field (default_factory = list )
128146 specificity : Specificity3 = (0 , 0 , 0 )
129147
@@ -141,6 +159,21 @@ def __rich_repr__(self) -> rich.repr.Result:
141159 yield selectors
142160 yield None , self .specificity
143161
162+ def _total_specificity (self ) -> Self :
163+ """Calculate total specificity of selectors.
164+
165+ Returns:
166+ Self.
167+ """
168+ id_total = class_total = type_total = 0
169+ for selector in self .selectors :
170+ _id , _class , _type = selector .specificity
171+ id_total += _id
172+ class_total += _class
173+ type_total += _type
174+ self .specificity = (id_total , class_total , type_total )
175+ return self
176+
144177 @classmethod
145178 def from_selectors (cls , selectors : list [list [Selector ]]) -> Iterable [SelectorSet ]:
146179 for selector_list in selectors :
0 commit comments