Refactor JS escaping
This renames javascript.convert_js_arg() to javascript.to_js() and uses that instead of string_escape() where possible.
This commit is contained in:
parent
38a4734b9a
commit
718376f154
@ -99,10 +99,10 @@ def _generate_pdfjs_script(filename):
|
||||
}
|
||||
|
||||
const viewer = window.PDFView || window.PDFViewerApplication;
|
||||
viewer.open("{{ url }}");
|
||||
viewer.open({{ url }});
|
||||
});
|
||||
""").render(
|
||||
url=javascript.string_escape(url.toString(QUrl.FullyEncoded)),
|
||||
url=javascript.to_js(url.toString(QUrl.FullyEncoded)),
|
||||
# WORKAROUND for https://bugreports.qt.io/browse/QTBUG-70420
|
||||
disable_create_object_url=(
|
||||
not qtutils.version_check('5.12') and
|
||||
|
@ -125,8 +125,8 @@ class WebKitElement(webelem.AbstractWebElement):
|
||||
self._elem.setPlainText(value)
|
||||
else:
|
||||
log.webelem.debug("Filling {!r} via javascript.".format(self))
|
||||
value = javascript.string_escape(value)
|
||||
self._elem.evaluateJavaScript("this.value='{}'".format(value))
|
||||
value = javascript.to_js(value)
|
||||
self._elem.evaluateJavaScript("this.value={}".format(value))
|
||||
|
||||
def dispatch_event(self, event, bubbles=False,
|
||||
cancelable=False, composed=False):
|
||||
@ -135,10 +135,10 @@ class WebKitElement(webelem.AbstractWebElement):
|
||||
self._elem.evaluateJavaScript(
|
||||
"this.dispatchEvent(new Event({}, "
|
||||
"{{'bubbles': {}, 'cancelable': {}, 'composed': {}}}))"
|
||||
.format(javascript.convert_js_arg(event),
|
||||
javascript.convert_js_arg(bubbles),
|
||||
javascript.convert_js_arg(cancelable),
|
||||
javascript.convert_js_arg(composed)))
|
||||
.format(javascript.to_js(event),
|
||||
javascript.to_js(bubbles),
|
||||
javascript.to_js(cancelable),
|
||||
javascript.to_js(composed)))
|
||||
|
||||
def caret_position(self):
|
||||
"""Get the text caret position for the current element."""
|
||||
@ -154,11 +154,11 @@ class WebKitElement(webelem.AbstractWebElement):
|
||||
raise webelem.Error("Element is not editable!")
|
||||
log.webelem.debug("Inserting text into element {!r}".format(self))
|
||||
self._elem.evaluateJavaScript("""
|
||||
var text = "{}";
|
||||
var text = {};
|
||||
var event = document.createEvent("TextEvent");
|
||||
event.initTextEvent("textInput", true, true, null, text);
|
||||
this.dispatchEvent(event);
|
||||
""".format(javascript.string_escape(text)))
|
||||
""".format(javascript.to_js(text)))
|
||||
|
||||
def _parent(self):
|
||||
"""Get the parent element of this element."""
|
||||
|
@ -49,7 +49,7 @@ def string_escape(text):
|
||||
return text
|
||||
|
||||
|
||||
def convert_js_arg(arg):
|
||||
def to_js(arg):
|
||||
"""Convert the given argument so it's the equivalent in JS."""
|
||||
if arg is None:
|
||||
return 'undefined'
|
||||
@ -60,7 +60,7 @@ def convert_js_arg(arg):
|
||||
elif isinstance(arg, (int, float)):
|
||||
return str(arg)
|
||||
elif isinstance(arg, list):
|
||||
return '[{}]'.format(', '.join(_convert_js_arg(e) for e in arg))
|
||||
return '[{}]'.format(', '.join(to_js(e) for e in arg))
|
||||
else:
|
||||
raise TypeError("Don't know how to handle {!r} of type {}!".format(
|
||||
arg, type(arg).__name__))
|
||||
@ -68,7 +68,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(to_js(arg) for arg in args)
|
||||
if module == 'window':
|
||||
parts = ['window', function]
|
||||
else:
|
||||
|
@ -45,8 +45,8 @@ def test_generate_pdfjs_page(available, snippet, monkeypatch):
|
||||
|
||||
|
||||
# Note that we got double protection, once because we use QUrl.FullyEncoded and
|
||||
# because we use qutebrowser.utils.javascript.string_escape. Characters
|
||||
# like " are already replaced by QUrl.
|
||||
# because we use qutebrowser.utils.javascript.to_js. Characters like " are
|
||||
# already replaced by QUrl.
|
||||
@pytest.mark.parametrize('filename, expected', [
|
||||
('foo.bar', "foo.bar"),
|
||||
('foo"bar', "foo%22bar"),
|
||||
|
@ -62,9 +62,9 @@ class StylesheetTester:
|
||||
"""Check whether the css in ELEMENT is set to VALUE."""
|
||||
self.js.run("console.log({document});"
|
||||
"window.getComputedStyle({document}, null)"
|
||||
".getPropertyValue('{prop}');".format(
|
||||
".getPropertyValue({prop});".format(
|
||||
document=document_element,
|
||||
prop=javascript.string_escape(css_style)),
|
||||
prop=javascript.to_js(css_style)),
|
||||
value)
|
||||
|
||||
def check_eq(self, one, two, true=True):
|
||||
|
@ -86,12 +86,12 @@ class TestStringEscape:
|
||||
(True, 'true'),
|
||||
([23, True, 'x'], '[23, true, "x"]'),
|
||||
])
|
||||
def test_convert_js_arg(arg, expected):
|
||||
def test_to_js(arg, expected):
|
||||
if expected is TypeError:
|
||||
with pytest.raises(TypeError):
|
||||
javascript.convert_js_arg(arg)
|
||||
javascript.to_js(arg)
|
||||
else:
|
||||
assert javascript.convert_js_arg(arg) == expected
|
||||
assert javascript.to_js(arg) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('base, expected_base', [
|
||||
|
Loading…
Reference in New Issue
Block a user