Preview panel: Improve robustness of resource loading from the book

Ensure that resources are always loaded from the current container, even
if the current container was changed since the base URL was specified.
While this is unlikely to happen, it is best to be robust.
This commit is contained in:
Kovid Goyal 2014-05-18 17:30:55 +05:30
parent 65e2fcce8e
commit 90b902ae90
2 changed files with 21 additions and 3 deletions

View File

@ -310,8 +310,8 @@ class Container(object): # {{{
for elem in self.parsed(name).xpath('//*[@src]'):
yield (elem.get('src'), elem.sourceline, 0) if get_line_numbers else elem.get('src')
def abspath_to_name(self, fullpath):
return self.relpath(os.path.abspath(fullpath)).replace(os.sep, '/')
def abspath_to_name(self, fullpath, root=None):
return self.relpath(os.path.abspath(fullpath), base=root).replace(os.sep, '/')
def name_to_abspath(self, name):
return os.path.abspath(join(self.root, *name.split('/')))

View File

@ -238,6 +238,7 @@ class NetworkAccessManager(QNetworkAccessManager):
def __init__(self, *args):
QNetworkAccessManager.__init__(self, *args)
self.current_root = None
self.cache = QNetworkDiskCache(self)
self.setCache(self.cache)
self.cache.setCacheDirectory(PersistentTemporaryDirectory(prefix='disk_cache_'))
@ -251,7 +252,7 @@ class NetworkAccessManager(QNetworkAccessManager):
path = path[1:]
c = current_container()
try:
name = c.abspath_to_name(path)
name = c.abspath_to_name(path, root=self.current_root)
except ValueError: # Happens on windows with absolute paths on different drives
name = None
if c.has_name(name):
@ -306,6 +307,14 @@ class WebPage(QWebPage):
self.mainFrame().javaScriptWindowObjectCleared.connect(self.init_javascript)
self.init_javascript()
@dynamic_property
def current_root(self):
def fget(self):
return self.networkAccessManager().current_root
def fset(self, val):
self.networkAccessManager().current_root = val
return property(fget=fget, fset=fset)
def javaScriptConsoleMessage(self, msg, lineno, source_id):
prints('preview js:%s:%s:'%(unicode(source_id), lineno), unicode(msg))
@ -408,6 +417,11 @@ class WebView(QWebView):
page margins and embedded fonts that use font name aliasing.
'''))
self.page().current_root = None
def setUrl(self, qurl):
self.page().current_root = current_container().root
return QWebView.setUrl(self, qurl)
def inspect(self):
self.inspector.parent().show()
@ -565,6 +579,10 @@ class Preview(QWidget):
self.view.clear()
self.current_name = None
@property
def current_root(self):
return self.view.page().current_root
@property
def is_visible(self):
return actions['preview-dock'].isChecked()