From 5eca6e11a58822a0204b662b6c62cb01f39df957 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 27 Jul 2016 16:58:44 +0200 Subject: [PATCH] Fix editor/paste-primary --- qutebrowser/browser/commands.py | 7 ++----- qutebrowser/browser/webkit/webelem.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index ab464d304..3e5581ff3 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1433,10 +1433,7 @@ class CommandDispatcher: raise cmdexc.CommandError("No element focused!") if not elem.is_editable(strict=True): raise cmdexc.CommandError("Focused element is not editable!") - if elem.is_content_editable(): - text = str(elem) - else: - text = elem.evaluateJavaScript('this.value') + text = elem.text(use_js=True) ed = editor.ExternalEditor(self._win_id, self._tabbed_browser) ed.editing_finished.connect(functools.partial( self.on_editing_finished, elem)) @@ -1478,7 +1475,7 @@ class CommandDispatcher: log.misc.debug("Pasting primary selection into element {}".format( elem.debug_text())) - elem.evaluateJavaScript(""" + elem.run_js_async(""" var sel = '{}'; var event = document.createEvent('TextEvent'); event.initTextEvent('textInput', true, true, null, sel); diff --git a/qutebrowser/browser/webkit/webelem.py b/qutebrowser/browser/webkit/webelem.py index 41e321821..a94652669 100644 --- a/qutebrowser/browser/webkit/webelem.py +++ b/qutebrowser/browser/webkit/webelem.py @@ -168,6 +168,18 @@ class WebElementWrapper(collections.abc.MutableMapping): self._check_vanished() return self._elem.styleProperty(name, strategy) + def text(self, *, use_js=False): + """Get the plain text content for this element. + + Args: + use_js: Whether to use javascript if the element isn't content-editable. + """ + self._check_vanished() + if self._elem.is_content_editable() or not use_js: + return self._elem.toPlainText() + else: + return self._elem.evaluateJavaScript('this.value') + def set_text(self, text, *, use_js=False): """Set the given plain text. @@ -332,6 +344,13 @@ class WebElementWrapper(collections.abc.MutableMapping): self._check_vanished() return self._elem.tagName() + def run_js_async(self, code, callback=None): + """Run the given JS snippet async on the element.""" + self._check_vanished() + result = self._elem.evaluateJavaScript(code) + if callback is not None: + callback(result) + def rect_on_view(self, *, elem_geometry=None, adjust_zoom=True, no_js=False): """Get the geometry of the element relative to the webview.