Skip to content

Commit 20bc2dc

Browse files
Copilotjaraco
andauthored
Fix pseudo-class specificity calculation per CSS spec (#76)
Agent-Logs-Url: https://github.com/jaraco/cssutils/sessions/316dd66f-a2ac-406a-8d2e-ee81784b6723 Agent-Logs-Url: https://github.com/jaraco/cssutils/sessions/d2e86c69-261d-4083-b160-b41e82abbf72 Agent-Logs-Url: https://github.com/jaraco/cssutils/sessions/47c0d706-f821-404c-a736-bbd9d6535139 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jaraco <308610+jaraco@users.noreply.github.com>
1 parent e6004c2 commit 20bc2dc

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

cssutils/css/selector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def append(self, seq, val, typ=None, token=None): # noqa: C901
132132
if not context or context == 'negation':
133133
if 'id' == typ:
134134
self.specificity[1] += 1
135-
elif 'class' == typ or '[' == val:
135+
elif 'class' == typ or '[' == val or 'pseudo-class' == typ:
136136
self.specificity[2] += 1
137137
elif typ in (
138138
'type-selector',

newsfragments/76.minor.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed pseudo-class specificity calculation. Pseudo-classes (e.g. ``:hover``, ``:last-child``, ``:nth-child()``) now correctly contribute to the class-weight column per the CSS Selectors spec.

tests/test_selector.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,16 @@ def test_specificity(self):
410410
'* a': (0, 0, 0, 1),
411411
'a *': (0, 0, 0, 1),
412412
'a * b': (0, 0, 0, 2),
413-
'a:hover': (0, 0, 0, 1),
413+
'a:hover': (0, 0, 1, 1),
414414
'a:first-line': (0, 0, 0, 2),
415415
'a:first-letter': (0, 0, 0, 2),
416416
'a:before': (0, 0, 0, 2),
417417
'a:after': (0, 0, 0, 2),
418+
# pseudo-classes count as class selectors (column c)
419+
':last-child': (0, 0, 1, 0),
420+
'tr:last-child': (0, 0, 1, 1),
421+
':nth-child(2)': (0, 0, 1, 0),
422+
'.table > :last-child > tr:last-child > *': (0, 0, 3, 1),
418423
# classes and attributes
419424
'.a': (0, 0, 1, 0),
420425
'*.a': (0, 0, 1, 0),

0 commit comments

Comments
 (0)