mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix regression that broke certain CSS selectors. Fixes #1068937 (Regression: CSS not interpreted correctly)
This commit is contained in:
parent
950a288d9a
commit
524e0119ed
@ -125,6 +125,11 @@ class CaseInsensitiveAttributesTranslator(HTMLTranslator):
|
||||
(id_selector.id.lower()))
|
||||
|
||||
ci_css_to_xpath = CaseInsensitiveAttributesTranslator().css_to_xpath
|
||||
NULL_NAMESPACE_REGEX = re.compile(ur'''name\(\) = ['"]h:''')
|
||||
def fix_namespace(raw):
|
||||
ans = NULL_NAMESPACE_REGEX.sub(lambda
|
||||
m:m.group().replace(u'h:', u''), raw)
|
||||
return ans
|
||||
|
||||
class CSSSelector(object):
|
||||
|
||||
@ -136,7 +141,7 @@ class CSSSelector(object):
|
||||
|
||||
def build_selector(self, css, log, func=css_to_xpath):
|
||||
try:
|
||||
return etree.XPath(func(css), namespaces=self.namespaces)
|
||||
return etree.XPath(fix_namespace(func(css)), namespaces=self.namespaces)
|
||||
except:
|
||||
if log is not None:
|
||||
log.exception('Failed to parse CSS selector: %r'%css)
|
||||
|
@ -73,6 +73,7 @@ class Split(object):
|
||||
|
||||
def find_page_breaks(self, item):
|
||||
if self.page_break_selectors is None:
|
||||
from calibre.ebooks.oeb.stylizer import fix_namespace
|
||||
css_to_xpath = HTMLTranslator().css_to_xpath
|
||||
self.page_break_selectors = set([])
|
||||
stylesheets = [x.data for x in self.oeb.manifest if x.media_type in
|
||||
@ -84,7 +85,7 @@ class Split(object):
|
||||
'page-break-after'), 'cssText', '').strip().lower()
|
||||
try:
|
||||
if before and before not in {'avoid', 'auto', 'inherit'}:
|
||||
self.page_break_selectors.add((XPath(css_to_xpath(rule.selectorText)),
|
||||
self.page_break_selectors.add((XPath(fix_namespace(css_to_xpath(rule.selectorText))),
|
||||
True))
|
||||
if self.remove_css_pagebreaks:
|
||||
rule.style.removeProperty('page-break-before')
|
||||
@ -92,7 +93,7 @@ class Split(object):
|
||||
pass
|
||||
try:
|
||||
if after and after not in {'avoid', 'auto', 'inherit'}:
|
||||
self.page_break_selectors.add((XPath(css_to_xpath(rule.selectorText)),
|
||||
self.page_break_selectors.add((XPath(fix_namespace(css_to_xpath(rule.selectorText))),
|
||||
False))
|
||||
if self.remove_css_pagebreaks:
|
||||
rule.style.removeProperty('page-break-after')
|
||||
|
Loading…
x
Reference in New Issue
Block a user