Add error handling for invalid selectors

This commit is contained in:
Florian Bruhin 2018-10-08 19:07:53 +02:00
parent 6d4c8f5b13
commit 121483aa90
7 changed files with 38 additions and 5 deletions

View File

@ -632,6 +632,9 @@ class AbstractElements:
def find_css(self, selector, callback, *, only_visible=False):
"""Find all HTML elements matching a given selector async.
If there's an error, the callback is called with a webelem.Error
instance.
Args:
callback: The callback to be called when the search finished.
selector: The CSS selector to search for.

View File

@ -601,8 +601,12 @@ class HintManager(QObject):
return
if elems is None:
message.error("There was an error while getting hint elements")
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

View File

@ -21,6 +21,7 @@
import posixpath
from qutebrowser.browser import webelem
from qutebrowser.config import config
from qutebrowser.utils import objreg, urlutils, log, message, qtutils
from qutebrowser.mainwindow import mainwindow
@ -116,7 +117,10 @@ def prevnext(*, browsertab, win_id, baseurl, prev=False,
"""
def _prevnext_cb(elems):
if elems is None:
message.error("There was an error while getting hint elements")
message.error("Unknown error while getting hint elements")
return
elif isinstance(elems, webelem.Error):
message.error(str(elems))
return
elem = _find_prevnext(prev, elems)

View File

@ -596,9 +596,12 @@ class WebEngineElements(browsertab.AbstractElements):
if js_elems is None:
callback(None)
return
elif not js_elems['success']:
callback(webelem.Error(js_elems['error']))
return
elems = []
for js_elem in js_elems:
for js_elem in js_elems['result']:
elem = webengineelem.WebEngineElement(js_elem, tab=self._tab)
elems.append(elem)
callback(elems)

View File

@ -214,7 +214,14 @@ window._qutebrowser.webelem = (function() {
}
funcs.find_css = (selector, only_visible) => {
const elems = document.querySelectorAll(selector);
let elems;
try {
elems = document.querySelectorAll(selector);
} catch (ex) {
return {"success": false, "error": ex.toString()};
}
const subelem_frames = window.frames;
const out = [];
@ -239,7 +246,7 @@ window._qutebrowser.webelem = (function() {
}
}
return out;
return {"success": true, "result": out};
};
// Runs a function in a frame until the result is not null, then return

View File

@ -213,6 +213,13 @@ Feature: Using hints
And I hint with args "custom" and follow a
Then the javascript message "beep!" should be logged
@qtwebkit_skip
Scenario: Invalid custom selector
When I open data/hints/custom_group.html
And I set hints.selectors to {"custom":["@"]}
And I run :hint custom
Then the error "SyntaxError: Failed to execute 'querySelectorAll' on 'Document': '@' is not a valid selector." should be shown
# https://github.com/qutebrowser/qutebrowser/issues/1613
Scenario: Hinting inputs with padding
When I open data/hints/input.html

View File

@ -75,6 +75,11 @@ Feature: Using :navigate
And I run :navigate next
Then data/navigate/next.html should be loaded
Scenario: Navigating with invalid selector
When I set hints.selectors to {"links": ["@"]}
And I run :navigate next
Then the error "SyntaxError: Failed to execute 'querySelectorAll' on 'Document': '@' is not a valid selector." should be shown
# increment/decrement
Scenario: Incrementing number in URL