Skip to content

Commit c3a084d

Browse files
Copilotjaraco
andauthored
Refactor: eliminate code duplication introduced by selector_pseudo_elements
Agent-Logs-Url: https://github.com/jaraco/cssutils/sessions/7ae1780a-16ff-4d2c-abbc-00d8112298cd Co-authored-by: jaraco <308610+jaraco@users.noreply.github.com>
1 parent 087378a commit c3a084d

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

cssutils/css/selector.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,18 @@ def _pseudo(self, expected, seq, token, tokenizer=None):
240240
# function
241241
# "pseudo-" "class" or "element"
242242
if val.lower() in Constants.selector_pseudos:
243-
# Selectors Level 4: :has(), :is(), :where(), etc.
244-
# accept a full selector list as argument
245-
self.context.append('pseudo-class-has')
246-
return Constants.simple_selector_sequence
247-
if val.lower() in Constants.selector_pseudo_elements:
243+
ctx = 'pseudo-class-has'
244+
elif val.lower() in Constants.selector_pseudo_elements:
248245
# CSS4 pseudo-elements accepting a full selector argument
249246
# (e.g. ::slotted(), ::cue()).
250-
self.context.append('pseudo-element')
251-
return Constants.simple_selector_sequence
252-
self.context.append(typ)
253-
return Constants.expressionstart
247+
ctx = 'pseudo-element'
248+
else:
249+
self.context.append(typ)
250+
return Constants.expressionstart
251+
# Selectors Level 4: both pseudo-class and pseudo-element
252+
# forms that accept a full selector list as argument
253+
self.context.append(ctx)
254+
return Constants.simple_selector_sequence
254255
elif 'negation' == context:
255256
return Constants.negationend
256257
elif 'pseudo-element' == typ:
@@ -431,17 +432,6 @@ def _char(self, expected, seq, token, tokenizer=None): # noqa: C901
431432
else:
432433
return Constants.simple_selector_sequence + Constants.combinator
433434

434-
# context: pseudo-element with selector argument (e.g. ::slotted(.foo))
435-
if ')' == val and context == 'pseudo-element' and 'combinator' in expected:
436-
# ::slotted(selector) end
437-
self.append(seq, val, 'function-end', token=token)
438-
self.context.pop() # pseudo-element is done
439-
context = self.context[-1]
440-
if 'pseudo-element' == context:
441-
return Constants.combinator
442-
else:
443-
return Constants.simple_selector_sequence + Constants.combinator
444-
445435
# context: pseudo (at least one expression)
446436
if val in '+-' and context.startswith('pseudo-'):
447437
# :func(+ -)"
@@ -455,11 +445,16 @@ def _char(self, expected, seq, token, tokenizer=None): # noqa: C901
455445
if (
456446
')' == val
457447
and context.startswith('pseudo-')
458-
and Constants.expression == expected
448+
and (
449+
Constants.expression == expected
450+
or (context == 'pseudo-element' and 'combinator' in expected)
451+
)
459452
):
460-
# :func(expression)"
453+
# :func(expression) or ::slotted(selector) end
461454
self.append(seq, val, 'function-end', token=token)
462455
self.context.pop() # pseudo is done
456+
# context still holds the pre-pop value: check what *type* of pseudo
457+
# we just closed to decide what may follow.
463458
if 'pseudo-element' == context:
464459
return Constants.combinator
465460
else:

0 commit comments

Comments
 (0)