Use javascript.assemble for all functions

This commit is contained in:
Florian Bruhin 2016-08-10 17:27:34 +02:00
parent dcec71de19
commit 29b778b6d6
3 changed files with 15 additions and 8 deletions

View File

@ -235,11 +235,11 @@ class WebEngineScroller(browsertab.AbstractScroller):
self._tab.run_js_async(js_code)
def to_point(self, point):
self._tab.run_js_async("window.scroll({x}, {y});".format(
x=point.x(), y=point.y()))
js_code = javascript.assemble('window', 'scroll', point.x(), point.y())
self._tab.run_js_async(js_code)
def delta(self, x=0, y=0):
self._tab.run_js_async("window.scrollBy({x}, {y});".format(x=x, y=y))
self._tab.run_js_async(javascript.assemble('window', 'scrollBy', x, y))
def delta_page(self, x=0, y=0):
js_code = javascript.assemble('scroll', 'delta_page', x, y)

View File

@ -62,6 +62,9 @@ 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)
code = '"use strict";\nwindow._qutebrowser.{}.{}({});'.format(
module, function, js_args)
if module == 'window':
parts = ['window', function]
else:
parts = ['window', '_qutebrowser', module, function]
code = '"use strict";\n{}({});'.format('.'.join(parts), js_args)
return code

View File

@ -138,6 +138,10 @@ def test_convert_js_arg(arg, expected):
assert javascript._convert_js_arg(arg) == expected
def test_assemble():
expected = '"use strict";\nwindow._qutebrowser.foo.func(23);'
assert javascript.assemble('foo', 'func', 23) == expected
@pytest.mark.parametrize('base, expected_base', [
('window', 'window'),
('foo', 'window._qutebrowser.foo'),
])
def test_assemble(base, expected_base):
expected = '"use strict";\n{}.func(23);'.format(expected_base)
assert javascript.assemble(base, 'func', 23) == expected