From 0fa0fca0b5e53303037401d913198f89bd1d6b63 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 13 Jun 2012 10:28:52 +0530 Subject: [PATCH] AZW3 Output: Close self closing script/style/title/head tags explicitly as they cause problems in webkit based renderers like the Kindle Fire and calibre's viewers. --- src/calibre/ebooks/mobi/writer8/main.py | 10 ++++++++++ src/calibre/ebooks/mobi/writer8/skeleton.py | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/mobi/writer8/main.py b/src/calibre/ebooks/mobi/writer8/main.py index 0f45b8e494..5acb04ebb3 100644 --- a/src/calibre/ebooks/mobi/writer8/main.py +++ b/src/calibre/ebooks/mobi/writer8/main.py @@ -53,6 +53,7 @@ class KF8Writer(object): self.log('\tGenerating KF8 markup...') self.dup_data() + self.cleanup_markup() self.replace_resource_links() self.extract_css_into_flows() self.extract_svg_into_flows() @@ -89,6 +90,15 @@ class KF8Writer(object): def data(self, item): return self._data_cache.get(item.href, item.data) + def cleanup_markup(self): + for item in self.oeb.spine: + root = self.data(item) + + # Remove empty script tags as they are pointless + for tag in XPath('//h:script')(root): + if not tag.text and not tag.get('src', False): + tag.getparent().remove(tag) + def replace_resource_links(self): ''' Replace links to resources (raster images/fonts) with pointers to the MOBI record containing the resource. The pointers are of the form: diff --git a/src/calibre/ebooks/mobi/writer8/skeleton.py b/src/calibre/ebooks/mobi/writer8/skeleton.py index 7f79f12726..ae083163d2 100644 --- a/src/calibre/ebooks/mobi/writer8/skeleton.py +++ b/src/calibre/ebooks/mobi/writer8/skeleton.py @@ -33,7 +33,8 @@ aid_able_tags = {'a', 'abbr', 'address', 'article', 'aside', 'audio', 'b', 'video'} _self_closing_pat = re.compile(bytes( - r'<(?P%s)(?=[\s/])(?P[^>]*)/>'%('|'.join(aid_able_tags))), + r'<(?P%s)(?=[\s/])(?P[^>]*)/>'%('|'.join(aid_able_tags|{'script', + 'style', 'title', 'head'}))), re.IGNORECASE) def close_self_closing_tags(raw): @@ -118,6 +119,7 @@ class Skeleton(object): def render(self, root): raw = tostring(root, xml_declaration=True) raw = raw.replace(b'