From 524e0119edac00c8fa9dbe00362f6bbfac5cad21 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 20 Oct 2012 08:59:49 +0530 Subject: [PATCH] Fix regression that broke certain CSS selectors. Fixes #1068937 (Regression: CSS not interpreted correctly) --- src/calibre/ebooks/oeb/stylizer.py | 7 ++++++- src/calibre/ebooks/oeb/transforms/split.py | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/oeb/stylizer.py b/src/calibre/ebooks/oeb/stylizer.py index 68978b9637..1579dde481 100644 --- a/src/calibre/ebooks/oeb/stylizer.py +++ b/src/calibre/ebooks/oeb/stylizer.py @@ -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) diff --git a/src/calibre/ebooks/oeb/transforms/split.py b/src/calibre/ebooks/oeb/transforms/split.py index e46ddb5fb5..5b00400dbf 100644 --- a/src/calibre/ebooks/oeb/transforms/split.py +++ b/src/calibre/ebooks/oeb/transforms/split.py @@ -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')