Permalink
Browse files

Change tab.find_all_elements() to be async

  • Loading branch information...
The-Compiler committed Aug 5, 2016
1 parent 0169f3a commit d8521f43eeb888c10de0e99df9e3b7a7c991e464
@@ -603,10 +603,11 @@ def icon(self):
def set_html(self, html, base_url):
raise NotImplementedError
- def find_all_elements(self, selector, *, only_visible=False):
- """Find all HTML elements matching a given selector.
+ def find_all_elements(self, selector, callback, *, only_visible=False):
+ """Find all HTML elements matching a given selector async.
Args:
+ callback: The callback to be called when the search finished.
selector: The CSS selector to search for.
only_visible: Only show elements which are visible on screen.
"""
@@ -603,26 +603,51 @@ def _resolve_url(self, elem, baseurl):
qtutils.ensure_valid(url)
return url
- def _find_prevnext(self, tab, prev=False):
- """Find a prev/next element in frame."""
+ def _check_args(self, target, *args):
+ """Check the arguments passed to start() and raise if they're wrong.
+
+ Args:
+ target: A Target enum member.
+ args: Arguments for userscript/download
+ """
+ if not isinstance(target, Target):
+ raise TypeError("Target {} is no Target member!".format(target))
+ if target in [Target.userscript, Target.spawn, Target.run,
+ Target.fill]:
+ if not args:
+ raise cmdexc.CommandError(
+ "'args' is required with target userscript/spawn/run/"
+ "fill.")
+ else:
+ if args:
+ raise cmdexc.CommandError(
+ "'args' is only allowed with target userscript/spawn.")
+
+ def _filter_matches(self, filterstr, elemstr):
+ """Return True if `filterstr` matches `elemstr`."""
+ # Empty string and None always match
+ if not filterstr:
+ return True
+ filterstr = filterstr.casefold()
+ elemstr = elemstr.casefold()
+ # Do multi-word matching
+ return all(word in elemstr for word in filterstr.split())
+
+ def _find_prevnext(self, prev, elems):
+ """Find a prev/next element in the given list of elements."""
# First check for <link rel="prev(ious)|next">
- elems = tab.find_all_elements(webelem.SELECTORS[webelem.Group.links])
rel_values = ('prev', 'previous') if prev else ('next')
for e in elems:
- try:
- rel_attr = e['rel']
- except KeyError:
+ if e.tag_name() != 'link' or 'rel' not in e:
continue
- if rel_attr in rel_values:
+ if e['rel'] in rel_values:
log.hints.debug("Found '{}' with rel={}".format(
- e.debug_text(), rel_attr))
+ e.debug_text(), e['rel']))
return e
+
# Then check for regular links/buttons.
- elems = tab.find_all_elements(
- webelem.SELECTORS[webelem.Group.prevnext])
filterfunc = webelem.FILTERS[webelem.Group.prevnext]
- elems = [e for e in elems if filterfunc(e)]
-
+ elems = [e for e in elems if e.tag_name() != 'link' and filterfunc(e)]
option = 'prev-regexes' if prev else 'next-regexes'
if not elems:
return None
@@ -640,31 +665,53 @@ def _find_prevnext(self, tab, prev=False):
log.hints.vdebug("No match on '{}'!".format(text))
return None
- def _check_args(self, target, *args):
- """Check the arguments passed to start() and raise if they're wrong.
+ def follow_prevnext(self, browsertab, baseurl, prev=False, tab=False,
+ background=False, window=False):
+ """Click a "previous"/"next" element on the page.
Args:
- target: A Target enum member.
- args: Arguments for userscript/download
+ browsertab: The WebKitTab/WebEngineTab of the page.
+ baseurl: The base URL of the current tab.
+ prev: True to open a "previous" link, False to open a "next" link.
+ tab: True to open in a new tab, False for the current tab.
+ background: True to open in a background tab.
+ window: True to open in a new window, False for the current one.
"""
- if not isinstance(target, Target):
- raise TypeError("Target {} is no Target member!".format(target))
- if target in [Target.userscript, Target.spawn, Target.run,
- Target.fill]:
- if not args:
- raise cmdexc.CommandError(
- "'args' is required with target userscript/spawn/run/"
- "fill.")
- else:
- if args:
- raise cmdexc.CommandError(
- "'args' is only allowed with target userscript/spawn.")
+ def _follow_prevnext_cb(elems):
+ elem = self._find_prevnext(prev, elems)
+ word = 'prev' if prev else 'forward'
- def _init_elements(self):
+ if elem is None:
+ message.error(self._win_id, "No {} links found!".format(word))
+ return
+ url = self._resolve_url(elem, baseurl)
+ if url is None:
+ message.error(self._win_id, "No {} links found!".format(word))
+ return
+ qtutils.ensure_valid(url)
+
+ if window:
+ from qutebrowser.mainwindow import mainwindow
+ new_window = mainwindow.MainWindow()
+ new_window.show()
+ tabbed_browser = objreg.get('tabbed-browser', scope='window',
+ window=new_window.win_id)
+ tabbed_browser.tabopen(url, background=False)
+ elif tab:
+ tabbed_browser = objreg.get('tabbed-browser', scope='window',
+ window=self._win_id)
+ tabbed_browser.tabopen(url, background=background)
+ else:
+ browsertab = objreg.get('tab', scope='tab',
+ window=self._win_id, tab=self._tab_id)
+ browsertab.openurl(url)
+
+ selector = ', '.join([webelem.SELECTORS[webelem.Group.links],
+ webelem.SELECTORS[webelem.Group.prevnext]])
+ browsertab.find_all_elements(selector, _follow_prevnext_cb)
+
+ def _start_cb(self, elems):
"""Initialize the elements and labels based on the context set."""
- selector = webelem.SELECTORS[self._context.group]
- elems = self._context.tab.find_all_elements(selector,
- only_visible=True)
filterfunc = webelem.FILTERS.get(self._context.group, lambda e: True)
elems = [e for e in elems if filterfunc(e)]
if not elems:
@@ -681,52 +728,13 @@ def _init_elements(self):
keyparser = keyparsers[usertypes.KeyMode.hint]
keyparser.update_bindings(strings)
- def _filter_matches(self, filterstr, elemstr):
- """Return True if `filterstr` matches `elemstr`."""
- # Empty string and None always match
- if not filterstr:
- return True
- filterstr = filterstr.casefold()
- elemstr = elemstr.casefold()
- # Do multi-word matching
- return all(word in elemstr for word in filterstr.split())
-
- def follow_prevnext(self, browsertab, baseurl, prev=False, tab=False,
- background=False, window=False):
- """Click a "previous"/"next" element on the page.
-
- Args:
- browsertab: The WebKitTab/WebEngineTab of the page.
- baseurl: The base URL of the current tab.
- prev: True to open a "previous" link, False to open a "next" link.
- tab: True to open in a new tab, False for the current tab.
- background: True to open in a background tab.
- window: True to open in a new window, False for the current one.
- """
- from qutebrowser.mainwindow import mainwindow
- elem = self._find_prevnext(browsertab, prev)
- if elem is None:
- raise cmdexc.CommandError("No {} links found!".format(
- "prev" if prev else "forward"))
- url = self._resolve_url(elem, baseurl)
- if url is None:
- raise cmdexc.CommandError("No {} links found!".format(
- "prev" if prev else "forward"))
- qtutils.ensure_valid(url)
- if window:
- new_window = mainwindow.MainWindow()
- new_window.show()
- tabbed_browser = objreg.get('tabbed-browser', scope='window',
- window=new_window.win_id)
- tabbed_browser.tabopen(url, background=False)
- elif tab:
- tabbed_browser = objreg.get('tabbed-browser', scope='window',
- window=self._win_id)
- tabbed_browser.tabopen(url, background=background)
- else:
- tab = objreg.get('tab', scope='tab', window=self._win_id,
- tab=self._tab_id)
- tab.openurl(url)
+ self._context.tab.contents_size_changed.connect(
+ self.on_contents_size_changed)
+ message_bridge = objreg.get('message-bridge', scope='window',
+ window=self._win_id)
+ message_bridge.set_text(self._get_text())
+ modeman.enter(self._win_id, usertypes.KeyMode.hint,
+ 'HintManager.start')
@cmdutils.register(instance='hintmanager', scope='tab', name='hint',
star_args_optional=True, maxsplit=2,
@@ -817,13 +825,9 @@ def start(self, rapid=False, group=webelem.Group.all, target=Target.normal,
self._context.tab = tab
self._context.args = args
self._context.group = group
- self._init_elements()
- tab.contents_size_changed.connect(self.on_contents_size_changed)
- message_bridge = objreg.get('message-bridge', scope='window',
- window=self._win_id)
- message_bridge.set_text(self._get_text())
- modeman.enter(self._win_id, usertypes.KeyMode.hint,
- 'HintManager.start')
+ selector = webelem.SELECTORS[self._context.group]
+ self._context.tab.find_all_elements(selector, self._start_cb,
+ only_visible=True)
def handle_partial_key(self, keystr):
"""Handle a new partial keypress."""
@@ -411,9 +411,9 @@ def set_html(self, html, base_url):
def clear_ssl_errors(self):
log.stub()
- def find_all_elements(self, selector, *, only_visible=False):
+ def find_all_elements(self, selector, callback, *, only_visible=False):
log.stub()
- return []
+ callback([])
def _connect_signals(self):
view = self._widget
@@ -558,7 +558,7 @@ def clear_ssl_errors(self):
def set_html(self, html, base_url):
self._widget.setHtml(html, base_url)
- def find_all_elements(self, selector, *, only_visible=False):
+ def find_all_elements(self, selector, callback, *, only_visible=False):
mainframe = self._widget.page().mainFrame()
if mainframe is None:
raise browsertab.WebTabError("No frame focused!")
@@ -572,7 +572,7 @@ def find_all_elements(self, selector, *, only_visible=False):
if only_visible:
elems = [e for e in elems if e.is_visible(mainframe)]
- return elems
+ callback(elems)
@pyqtSlot()
def _on_frame_load_finished(self):

0 comments on commit d8521f4

Please sign in to comment.