Separate success/error callbacks

This commit is contained in:
Florian Bruhin 2018-11-29 20:29:43 +01:00
parent 0104490978
commit 27ee3280b2
5 changed files with 22 additions and 26 deletions

View File

@ -667,13 +667,15 @@ class AbstractElements:
[typing.Sequence['webelem.AbstractWebElement']], None]
_SingleCallback = typing.Callable[
[typing.Optional['webelem.AbstractWebElement']], None]
_ErrorCallback = typing.Callable[[Exception], None]
def __init__(self, tab: 'AbstractTab') -> None:
self._widget = None
self._tab = tab
def find_css(self, selector: str,
callback: _MultiCallback, *,
callback: _MultiCallback,
error_callback: _ErrorCallback, *,
only_visible: bool = False) -> None:
"""Find all HTML elements matching a given selector async.

View File

@ -597,13 +597,6 @@ class HintManager(QObject):
log.hints.debug("In _start_cb without context!")
return
if elems is None:
message.error("Unknown error while getting hint elements.")
return
elif isinstance(elems, webelem.Error):
message.error(str(elems))
return
if not elems:
message.error("No elements found.")
return
@ -747,8 +740,11 @@ class HintManager(QObject):
except webelem.Error as e:
raise cmdutils.CommandError(str(e))
self._context.tab.elements.find_css(selector, self._start_cb,
only_visible=True)
self._context.tab.elements.find_css(
selector,
callback=self._start_cb,
error_cb=lambda err: message.error(str(err)),
only_visible=True)
def _get_hint_mode(self, mode):
"""Get the hinting mode to use based on a mode argument."""

View File

@ -116,13 +116,6 @@ def prevnext(*, browsertab, win_id, baseurl, prev=False,
window: True to open in a new window, False for the current one.
"""
def _prevnext_cb(elems):
if elems is None:
message.error("Unknown error while getting hint elements")
return
elif isinstance(elems, webelem.Error):
message.error(str(elems))
return
elem = _find_prevnext(prev, elems)
word = 'prev' if prev else 'forward'
@ -155,4 +148,5 @@ def prevnext(*, browsertab, win_id, baseurl, prev=False,
except webelem.Error as e:
raise Error(str(e))
browsertab.elements.find_css(link_selector, _prevnext_cb)
browsertab.elements.find_css(link_selector, callback=_prevnext_cb,
error_cb=lambda err: message.error(str(err)))

View File

@ -591,19 +591,20 @@ class WebEngineElements(browsertab.AbstractElements):
"""QtWebEngine implemementations related to elements on the page."""
def _js_cb_multiple(self, callback, js_elems):
def _js_cb_multiple(self, callback, error_cb, js_elems):
"""Handle found elements coming from JS and call the real callback.
Args:
callback: The callback to call with the found elements.
Called with None if there was an error.
error_cb: The callback to call in case of an error.
js_elems: The elements serialized from javascript.
"""
if js_elems is None:
callback(None)
error_cb(webelem.Error("Unknown error while getting "
"elements"))
return
elif not js_elems['success']:
callback(webelem.Error(js_elems['error']))
error_cb(webelem.Error(js_elems['error']))
return
elems = []
@ -630,10 +631,11 @@ class WebEngineElements(browsertab.AbstractElements):
elem = webengineelem.WebEngineElement(js_elem, tab=self._tab)
callback(elem)
def find_css(self, selector, callback, *, only_visible=False):
def find_css(self, selector, callback, error_cb, *,
only_visible=False):
js_code = javascript.assemble('webelem', 'find_css', selector,
only_visible)
js_cb = functools.partial(self._js_cb_multiple, callback)
js_cb = functools.partial(self._js_cb_multiple, callback, error_cb)
self._tab.run_js_async(js_code, js_cb)
def find_id(self, elem_id, callback):

View File

@ -563,7 +563,8 @@ class WebKitElements(browsertab.AbstractElements):
"""QtWebKit implemementations related to elements on the page."""
def find_css(self, selector, callback, *, only_visible=False):
def find_css(self, selector, callback, error_cb, *, only_visible=False):
utils.unused(error_cb)
mainframe = self._widget.page().mainFrame()
if mainframe is None:
raise browsertab.WebTabError("No frame focused!")
@ -592,7 +593,8 @@ class WebKitElements(browsertab.AbstractElements):
# Escape non-alphanumeric characters in the selector
# https://www.w3.org/TR/CSS2/syndata.html#value-def-identifier
elem_id = re.sub(r'[^a-zA-Z0-9_-]', r'\\\g<0>', elem_id)
self.find_css('#' + elem_id, find_id_cb)
self.find_css('#' + elem_id, find_id_cb,
error_callback=lambda exc: None)
def find_focused(self, callback):
frame = self._widget.page().currentFrame()