mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
426bbb2d60
commit
4989be9377
@ -552,6 +552,12 @@ def select_pseudo(cache, pseudo):
|
|||||||
raise ExpressionError(
|
raise ExpressionError(
|
||||||
"The pseudo-class :%s is not supported" % pseudo.ident)
|
"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):
|
for item in cache.iterparsedselector(pseudo.selector):
|
||||||
if func(cache, item):
|
if func(cache, item):
|
||||||
yield item
|
yield item
|
||||||
@ -561,39 +567,46 @@ def select_first_child(cache, elem):
|
|||||||
return cache.sibling_count(elem) == 0
|
return cache.sibling_count(elem) == 0
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
|
select_first_child.is_pseudo = True
|
||||||
|
|
||||||
def select_last_child(cache, elem):
|
def select_last_child(cache, elem):
|
||||||
try:
|
try:
|
||||||
return cache.sibling_count(elem, before=False) == 0
|
return cache.sibling_count(elem, before=False) == 0
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
|
select_last_child.is_pseudo = True
|
||||||
|
|
||||||
def select_only_child(cache, elem):
|
def select_only_child(cache, elem):
|
||||||
try:
|
try:
|
||||||
return cache.all_sibling_count(elem) == 0
|
return cache.all_sibling_count(elem) == 0
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
|
select_only_child.is_pseudo = True
|
||||||
|
|
||||||
def select_first_of_type(cache, elem):
|
def select_first_of_type(cache, elem):
|
||||||
try:
|
try:
|
||||||
return cache.sibling_count(elem, same_type=True) == 0
|
return cache.sibling_count(elem, same_type=True) == 0
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
|
select_first_of_type.is_pseudo = True
|
||||||
|
|
||||||
def select_last_of_type(cache, elem):
|
def select_last_of_type(cache, elem):
|
||||||
try:
|
try:
|
||||||
return cache.sibling_count(elem, before=False, same_type=True) == 0
|
return cache.sibling_count(elem, before=False, same_type=True) == 0
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
|
select_last_of_type.is_pseudo = True
|
||||||
|
|
||||||
def select_only_of_type(cache, elem):
|
def select_only_of_type(cache, elem):
|
||||||
try:
|
try:
|
||||||
return cache.all_sibling_count(elem, same_type=True) == 0
|
return cache.all_sibling_count(elem, same_type=True) == 0
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
|
select_only_of_type.is_pseudo = True
|
||||||
|
|
||||||
def select_empty(cache, elem):
|
def select_empty(cache, elem):
|
||||||
return cache.is_empty(elem)
|
return cache.is_empty(elem)
|
||||||
|
select_empty.is_pseudo = True
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import unittest, sys, argparse, json
|
|||||||
|
|
||||||
from lxml import etree, html
|
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.parser import tokenize, parse
|
||||||
from css_selectors.select import Select
|
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'di\a0 v', r'div\['), [])
|
||||||
self.ae(pcss(r'[h\a0 ref]', r'[h\]ref]'), [])
|
self.ae(pcss(r'[h\a0 ref]', r'[h\]ref]'), [])
|
||||||
|
|
||||||
|
self.assertRaises(ExpressionError, lambda : tuple(select('body:nth-child')))
|
||||||
|
|
||||||
del app
|
del app
|
||||||
|
|
||||||
def test_select_shakespeare(self):
|
def test_select_shakespeare(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user