Fix regression that broke certain CSS selectors. Fixes #1068937 (Regression: CSS not interpreted correctly)

This commit is contained in:
Kovid Goyal 2012-10-20 08:59:49 +05:30
parent 950a288d9a
commit 524e0119ed
2 changed files with 9 additions and 3 deletions

View File

@ -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)

View File

@ -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')