diff --git a/src/calibre/ebooks/oeb/stylizer.py b/src/calibre/ebooks/oeb/stylizer.py index 7a559ca15c..f9a199042c 100644 --- a/src/calibre/ebooks/oeb/stylizer.py +++ b/src/calibre/ebooks/oeb/stylizer.py @@ -172,11 +172,7 @@ class Stylizer(object): basename = os.path.basename(path) cssname = os.path.splitext(basename)[0] + '.css' stylesheets = [html_css_stylesheet()] - head = xpath(tree, '/h:html/h:head') - if head: - head = head[0] - else: - head = [] + style_tags = xpath(tree, '//*[local-name()="style" or local-name()="link"]') # Add cssutils parsing profiles from output_profile for profile in self.opts.output_profile.extra_css_modules: @@ -187,7 +183,7 @@ class Stylizer(object): parser = CSSParser(fetcher=self._fetch_css_file, log=logging.getLogger('calibre.css')) self.font_face_rules = [] - for elem in head: + for elem in style_tags: if (elem.tag == XHTML('style') and elem.get('type', CSS_MIME) in OEB_STYLES): text = elem.text if elem.text else u'' diff --git a/src/calibre/ebooks/oeb/transforms/flatcss.py b/src/calibre/ebooks/oeb/transforms/flatcss.py index 343e87d507..39caa2b247 100644 --- a/src/calibre/ebooks/oeb/transforms/flatcss.py +++ b/src/calibre/ebooks/oeb/transforms/flatcss.py @@ -503,14 +503,14 @@ class CSSFlattener(object): def flatten_head(self, item, href, global_href): html = item.data head = html.find(XHTML('head')) - for node in head: + for node in html.xpath('//*[local-name()="style" or local-name()="link"]'): if node.tag == XHTML('link') \ and node.get('rel', 'stylesheet') == 'stylesheet' \ and node.get('type', CSS_MIME) in OEB_STYLES: - head.remove(node) + node.getparent().remove(node) elif node.tag == XHTML('style') \ and node.get('type', CSS_MIME) in OEB_STYLES: - head.remove(node) + node.getparent().remove(node) href = item.relhref(href) l = etree.SubElement(head, XHTML('link'), rel='stylesheet', type=CSS_MIME, href=href)