Prevent un-lloadable resources in link tags (non-CSS stylesheets) from causing the resource loading check to timeout

This commit is contained in:
Kovid Goyal 2016-06-12 17:28:40 +05:30
parent 45fbdbea30
commit b0cf1e8c05
2 changed files with 12 additions and 1 deletions

View File

@ -215,6 +215,7 @@ class Container(ContainerBase):
resource_template = link_uid + '|{}|'
xlink_xpath = XPath('//*[@xl:href]')
link_xpath = XPath('//h:a[@href]')
res_link_xpath = XPath('//h:link[@href]')
def link_replacer(base, url):
if url.startswith('#'):
@ -249,6 +250,14 @@ class Container(ContainerBase):
elif mt in OEB_DOCS:
self.virtualized_names.add(name)
root = self.parsed(name)
for link in res_link_xpath(root):
ltype = (link.get('type') or 'text/css').lower()
rel = (link.get('rel') or 'stylesheet').lower()
if ltype != 'text/css' or rel != 'stylesheet':
# This link will not be loaded by the browser anyway
# and will causes the resource load check to hang
link.attrib.clear()
changed.add(name)
rewrite_links(root, partial(link_replacer, name))
for a in link_xpath(root):
href = a.get('href')

View File

@ -229,11 +229,13 @@ def unserialize_html(serialized_data, proceed):
clear(document.head, document.body)
load_required = set()
proceeded = False
hang_timeout = 5
def hangcheck():
nonlocal proceeded
if not proceeded:
proceeded = True
print('WARNING: All resources did not load in {} seconds, proceeding anyway ({} resources left)'.format(hang_timeout, load_required.length))
proceed()
def onload():
@ -258,7 +260,7 @@ def unserialize_html(serialized_data, proceed):
ev.initEvent('DOMContentLoaded', True, True)
document.dispatchEvent(ev)
if load_required.length:
setTimeout(hangcheck, 5000)
setTimeout(hangcheck, hang_timeout * 1000)
else:
proceeded = True
proceed()