Skip to content

Commit

Permalink
Slightly improve compound and negation compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Jan 16, 2024
1 parent 705b310 commit 86b9312
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cssselect2/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,29 @@ def left(el):

def _compile_compound(selector):
sub_expressions = [
expr for expr in map(_compile_node, selector.simple_selectors)
expr for expr in [
_compile_node(selector)
for selector in selector.simple_selectors]
if expr is not TRUE]
if len(sub_expressions) == 1:
return sub_expressions[0]
if FALSE in sub_expressions:
return FALSE
if sub_expressions:
return lambda el: all(expr(el) for expr in sub_expressions)
return TRUE # all([]) == True
if not sub_expressions:
return TRUE # all([]) == True
return lambda el: all(expr(el) for expr in sub_expressions)


def _compile_negation(selector):
sub_expressions = [
expr for expr in [
_compile_node(selector.parsed_tree)
for selector in selector.selector_list]
if expr is not TRUE]
if not sub_expressions:
if expr is not FALSE]
if TRUE in sub_expressions:
return FALSE
if not sub_expressions:
return TRUE # not any([]) == True
return lambda el: not any(expr(el) for expr in sub_expressions)


Expand Down

0 comments on commit 86b9312

Please sign in to comment.