Clean up emitting of follow_selected_done

This commit is contained in:
Florian Bruhin 2018-09-15 15:46:56 +02:00
parent 9edbcfb828
commit 59413810bf
2 changed files with 16 additions and 12 deletions

View File

@ -335,7 +335,13 @@ class WebEngineCaret(browsertab.AbstractCaret):
self._tab.run_js_async(javascript.assemble('caret', 'getSelection'),
callback)
def _follow_selected_cb(self, js_elem, tab=False):
def _follow_selected_cb_wrapped(self, js_elem, tab):
try:
self._follow_selected_cb(js_elem, tab)
finally:
self.follow_selected_done.emit()
def _follow_selected_cb(self, js_elem, tab):
"""Callback for javascript which clicks the selected element.
Args:
@ -343,13 +349,11 @@ class WebEngineCaret(browsertab.AbstractCaret):
tab: Open in a new tab.
"""
if js_elem is None:
self.follow_selected_done.emit()
return
if js_elem == "focused":
# we had a focused element, not a selected one. Just send <enter>
self._follow_enter(tab)
self.follow_selected_done.emit()
return
assert isinstance(js_elem, dict), js_elem
@ -368,8 +372,6 @@ class WebEngineCaret(browsertab.AbstractCaret):
except webelem.Error as e:
message.error(str(e))
self.follow_selected_done.emit()
def follow_selected(self, *, tab=False):
if self._tab.search.search_displayed:
# We are currently in search mode.
@ -384,8 +386,9 @@ class WebEngineCaret(browsertab.AbstractCaret):
# click an existing blue selection
js_code = javascript.assemble('webelem',
'find_selected_focused_link')
self._tab.run_js_async(js_code, lambda jsret:
self._follow_selected_cb(jsret, tab))
self._tab.run_js_async(
js_code,
lambda jsret: self._follow_selected_cb_wrapped(jsret, tab))
def _js_call(self, command, *args, callback=None):
code = javascript.assemble('caret', command, *args)

View File

@ -348,7 +348,7 @@ class WebKitCaret(browsertab.AbstractCaret):
def selection(self, callback):
callback(self._widget.selectedText())
def follow_selected(self, *, tab=False):
def _follow_selected(self, *, tab=False):
if QWebSettings.globalSettings().testAttribute(
QWebSettings.JavascriptEnabled):
if tab:
@ -369,13 +369,11 @@ class WebKitCaret(browsertab.AbstractCaret):
# https://github.com/annulen/webkit/commit/0e75f3272d149bc64899c161f150eb341a2417af
# TODO find a way to check if something is focused
self._follow_enter(tab)
self.follow_selected_done.emit()
return
try:
selected_element = xml.etree.ElementTree.fromstring(
'<html>{}</html>'.format(selection)).find('a')
except xml.etree.ElementTree.ParseError:
self.follow_selected_done.emit()
raise browsertab.WebTabError('Could not parse selected '
'element!')
@ -383,7 +381,6 @@ class WebKitCaret(browsertab.AbstractCaret):
try:
url = selected_element.attrib['href']
except KeyError:
self.follow_selected_done.emit()
raise browsertab.WebTabError('Anchor element without '
'href!')
url = self._tab.url().resolved(QUrl(url))
@ -392,7 +389,11 @@ class WebKitCaret(browsertab.AbstractCaret):
else:
self._tab.openurl(url)
self.follow_selected_done.emit()
def follow_selected(self, *, tab=False):
try:
self._follow_selected(tab=tab)
finally:
self.follow_selected_done.emit()
class WebKitZoom(browsertab.AbstractZoom):