Micro-optimization

This commit is contained in:
Kovid Goyal 2019-11-07 10:42:06 +05:30
parent e25d26a628
commit f6087f7929
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -571,6 +571,16 @@ def select_nth_last_of_type(cache, function, elem):
# Pseudo elements {{{
def pseudo_func(f):
f.is_pseudo = True
return f
@pseudo_func
def allow_all(cache, item):
return True
def select_pseudo(cache, pseudo):
try:
func = cache.dispatch_map[pseudo.ident.replace('-', '_')]
@ -580,9 +590,7 @@ def select_pseudo(cache, pseudo):
return
if pseudo.ident in cache.ignore_inappropriate_pseudo_classes:
def func(cache, item):
return True
func.is_pseudo = True
func = allow_all
else:
raise ExpressionError(
"The pseudo-class :%s is not supported" % pseudo.ident)
@ -598,6 +606,7 @@ def select_pseudo(cache, pseudo):
yield item
@pseudo_func
def select_first_child(cache, elem):
try:
return cache.sibling_count(elem) == 0
@ -605,9 +614,7 @@ def select_first_child(cache, elem):
return False
select_first_child.is_pseudo = True
@pseudo_func
def select_last_child(cache, elem):
try:
return cache.sibling_count(elem, before=False) == 0
@ -615,9 +622,7 @@ def select_last_child(cache, elem):
return False
select_last_child.is_pseudo = True
@pseudo_func
def select_only_child(cache, elem):
try:
return cache.all_sibling_count(elem) == 0
@ -625,9 +630,7 @@ def select_only_child(cache, elem):
return False
select_only_child.is_pseudo = True
@pseudo_func
def select_first_of_type(cache, elem):
try:
return cache.sibling_count(elem, same_type=True) == 0
@ -635,9 +638,7 @@ def select_first_of_type(cache, elem):
return False
select_first_of_type.is_pseudo = True
@pseudo_func
def select_last_of_type(cache, elem):
try:
return cache.sibling_count(elem, before=False, same_type=True) == 0
@ -645,9 +646,7 @@ def select_last_of_type(cache, elem):
return False
select_last_of_type.is_pseudo = True
@pseudo_func
def select_only_of_type(cache, elem):
try:
return cache.all_sibling_count(elem, same_type=True) == 0
@ -655,15 +654,11 @@ def select_only_of_type(cache, elem):
return False
select_only_of_type.is_pseudo = True
@pseudo_func
def select_empty(cache, elem):
return cache.is_empty(elem)
select_empty.is_pseudo = True
# }}}
default_dispatch_map = {name.partition('_')[2]:obj for name, obj in globals().items() if name.startswith('select_') and callable(obj)}