Make convert_js_arg public and use it for dispatch_element in webkit

This commit is contained in:
Jay Kamat 2018-09-20 18:24:22 -07:00
parent 1da2bdb1e5
commit f0568ece57
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
5 changed files with 9 additions and 10 deletions

View File

@ -135,7 +135,6 @@ class WebEngineElement(webelem.AbstractWebElement):
def set_value(self, value):
self._js_call('set_value', value)
def dispatch_event(self, event, bubbles=False,
cancelable=False, composed=False):
self._js_call('dispatch_event', event, bubbles, cancelable, composed)

View File

@ -132,12 +132,13 @@ class WebKitElement(webelem.AbstractWebElement):
cancelable=False, composed=False):
self._check_vanished()
log.webelem.debug("Firing event on {!r} via javascript.".format(self))
event = javascript.string_escape(event)
self._elem.evaluateJavaScript(
"this.dispatchEvent(new Event('{}', "
"this.dispatchEvent(new Event({}, "
"{{'bubbles': {}, 'cancelable': {}, 'composed': {}}}))"
.format(event, str(bubbles).lower(), str(cancelable).lower(),
str(composed).lower()))
.format(javascript.convert_js_arg(event),
javascript.convert_js_arg(bubbles),
javascript.convert_js_arg(cancelable),
javascript.convert_js_arg(composed)))
def caret_position(self):
"""Get the text caret position for the current element."""

View File

@ -362,7 +362,6 @@ window._qutebrowser.webelem = (function() {
document.execCommand("insertText", false, text);
};
funcs.dispatch_event = (id, event, bubbles = false,
cancelable = false, composed = false) => {
const elem = elements[id];

View File

@ -49,7 +49,7 @@ def string_escape(text):
return text
def _convert_js_arg(arg):
def convert_js_arg(arg):
"""Convert the given argument so it's the equivalent in JS."""
if arg is None:
return 'undefined'
@ -66,7 +66,7 @@ def _convert_js_arg(arg):
def assemble(module, function, *args):
"""Assemble a javascript file and a function call."""
js_args = ', '.join(_convert_js_arg(arg) for arg in args)
js_args = ', '.join(convert_js_arg(arg) for arg in args)
if module == 'window':
parts = ['window', function]
else:

View File

@ -88,9 +88,9 @@ class TestStringEscape:
def test_convert_js_arg(arg, expected):
if expected is TypeError:
with pytest.raises(TypeError):
javascript._convert_js_arg(arg)
javascript.convert_js_arg(arg)
else:
assert javascript._convert_js_arg(arg) == expected
assert javascript.convert_js_arg(arg) == expected
@pytest.mark.parametrize('base, expected_base', [