E-book viewer: When viewing EPUB files, do not parse html as xhtml even if it has svg tags embedded.

This commit is contained in:
Kovid Goyal 2013-02-18 12:54:55 +05:30
parent 5276bc749e
commit 73bda9c879
2 changed files with 12 additions and 8 deletions

View File

@ -31,7 +31,8 @@ def self_closing_sub(match):
return '<%s%s></%s>'%(match.group(1), match.group(2), match.group(1)) return '<%s%s></%s>'%(match.group(1), match.group(2), match.group(1))
def load_html(path, view, codec='utf-8', mime_type=None, def load_html(path, view, codec='utf-8', mime_type=None,
pre_load_callback=lambda x:None, path_is_html=False): pre_load_callback=lambda x:None, path_is_html=False,
force_as_html=False):
from PyQt4.Qt import QUrl, QByteArray from PyQt4.Qt import QUrl, QByteArray
if mime_type is None: if mime_type is None:
mime_type = guess_type(path)[0] mime_type = guess_type(path)[0]
@ -44,18 +45,20 @@ def load_html(path, view, codec='utf-8', mime_type=None,
html = f.read().decode(codec, 'replace') html = f.read().decode(codec, 'replace')
html = EntityDeclarationProcessor(html).processed_html html = EntityDeclarationProcessor(html).processed_html
has_svg = re.search(r'<[:a-zA-Z]*svg', html) is not None
self_closing_pat = re.compile(r'<\s*([A-Za-z1-6]+)([^>]*)/\s*>') self_closing_pat = re.compile(r'<\s*([A-Za-z1-6]+)([^>]*)/\s*>')
html = self_closing_pat.sub(self_closing_sub, html) html = self_closing_pat.sub(self_closing_sub, html)
loading_url = QUrl.fromLocalFile(path) loading_url = QUrl.fromLocalFile(path)
pre_load_callback(loading_url) pre_load_callback(loading_url)
if has_svg: if force_as_html or re.search(r'<[:a-zA-Z]*svg', html) is None:
view.setHtml(html, loading_url)
else:
view.setContent(QByteArray(html.encode(codec)), mime_type, view.setContent(QByteArray(html.encode(codec)), mime_type,
loading_url) loading_url)
else: mf = view.page().mainFrame()
view.setHtml(html, loading_url) elem = mf.findFirstElement('parsererror')
if not elem.isNull():
return False
return True

View File

@ -790,7 +790,8 @@ class DocumentView(QWebView): # {{{
self.manager.load_started() self.manager.load_started()
load_html(path, self, codec=getattr(path, 'encoding', 'utf-8'), mime_type=getattr(path, load_html(path, self, codec=getattr(path, 'encoding', 'utf-8'), mime_type=getattr(path,
'mime_type', 'text/html'), pre_load_callback=callback) 'mime_type', 'text/html'), pre_load_callback=callback,
force_as_html=True)
entries = set() entries = set()
for ie in getattr(path, 'index_entries', []): for ie in getattr(path, 'index_entries', []):
if ie.start_anchor: if ie.start_anchor: