Skip to content

Commit

Permalink
Fix selector specificity calculation for pseudo-classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Oct 21, 2023
1 parent f192f09 commit a31988f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 7 additions & 2 deletions css_parser_tests/test_selector.py
Expand Up @@ -442,7 +442,7 @@ def _set(): selector.specificity = 1
'a *': (0, 0, 0, 1),
'a * b': (0, 0, 0, 2),

'a:hover': (0, 0, 0, 1),
'a:hover': (0, 0, 1, 1),

'a:first-line': (0, 0, 0, 2),
'a:first-letter': (0, 0, 0, 2),
Expand Down Expand Up @@ -478,10 +478,15 @@ def _set(): selector.specificity = 1
'#a#a': (0, 2, 0, 0), # e.g. html:id + xml:id
'#a#b': (0, 2, 0, 0),
'#a #b': (0, 2, 0, 0),

# pseudo classes
'p:only-of-type': (0, 0, 1, 1),
':where(p)': (0, 0, 0, 0),
':where(p) span': (0, 0, 0, 1),
}
for text in tests:
selector.selectorText = text
self.assertEqual(tests[text], selector.specificity)
self.assertEqual(tests[text], selector.specificity, text)

def test_reprANDstr(self):
"Selector.__repr__(), .__str__()"
Expand Down
8 changes: 4 additions & 4 deletions src/css_parser/css/selector.py
Expand Up @@ -368,10 +368,10 @@ def append(seq, val, typ=None, token=None):
if not context or context == 'negation':
if 'id' == typ:
new['specificity'][1] += 1
elif 'class' == typ or '[' == val:
new['specificity'][2] += 1
elif typ in ('type-selector', 'negation-type-selector',
'pseudo-element'):
elif '[' == val or typ in ('class', 'pseudo-class'):
if typ != 'pseudo-class' or val != ':where(':
new['specificity'][2] += 1
elif typ in ('type-selector', 'negation-type-selector', 'pseudo-element'):
new['specificity'][3] += 1
if not context and typ in ('type-selector', 'universal'):
# define element
Expand Down

0 comments on commit a31988f

Please sign in to comment.