Conversion: Fix error when converting a document that contains an invalid function based CSS selector without parentheses. Fixes #1440278 [Conversion Error - TypeError: select_nth_last_child() takes exactly 3 arguments (2 given)](https://bugs.launchpad.net/calibre/+bug/1440278)

This commit is contained in:
Kovid Goyal 2015-04-04 09:23:45 +05:30
parent 426bbb2d60
commit 4989be9377
2 changed files with 16 additions and 1 deletions

View File

@ -552,6 +552,12 @@ def select_pseudo(cache, pseudo):
raise ExpressionError(
"The pseudo-class :%s is not supported" % pseudo.ident)
try:
func.is_pseudo
except AttributeError:
raise ExpressionError(
"The pseudo-class :%s is invalid" % pseudo.ident)
for item in cache.iterparsedselector(pseudo.selector):
if func(cache, item):
yield item
@ -561,39 +567,46 @@ def select_first_child(cache, elem):
return cache.sibling_count(elem) == 0
except ValueError:
return False
select_first_child.is_pseudo = True
def select_last_child(cache, elem):
try:
return cache.sibling_count(elem, before=False) == 0
except ValueError:
return False
select_last_child.is_pseudo = True
def select_only_child(cache, elem):
try:
return cache.all_sibling_count(elem) == 0
except ValueError:
return False
select_only_child.is_pseudo = True
def select_first_of_type(cache, elem):
try:
return cache.sibling_count(elem, same_type=True) == 0
except ValueError:
return False
select_first_of_type.is_pseudo = True
def select_last_of_type(cache, elem):
try:
return cache.sibling_count(elem, before=False, same_type=True) == 0
except ValueError:
return False
select_last_of_type.is_pseudo = True
def select_only_of_type(cache, elem):
try:
return cache.all_sibling_count(elem, same_type=True) == 0
except ValueError:
return False
select_only_of_type.is_pseudo = True
def select_empty(cache, elem):
return cache.is_empty(elem)
select_empty.is_pseudo = True
# }}}

View File

@ -10,7 +10,7 @@ import unittest, sys, argparse, json
from lxml import etree, html
from css_selectors.errors import SelectorSyntaxError
from css_selectors.errors import SelectorSyntaxError, ExpressionError
from css_selectors.parser import tokenize, parse
from css_selectors.select import Select
@ -757,6 +757,8 @@ by William Shakespeare
self.ae(pcss(r'di\a0 v', r'div\['), [])
self.ae(pcss(r'[h\a0 ref]', r'[h\]ref]'), [])
self.assertRaises(ExpressionError, lambda : tuple(select('body:nth-child')))
del app
def test_select_shakespeare(self):