From 35e16a8e6ea7ae4aa33a44ccbe4c7ad29f205899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= Date: Fri, 22 Jan 2016 18:18:17 +0100 Subject: [PATCH] paste-primary: fix undo/redo not working It seems that unlike Gecko, WebKit does not support undo/redo operations when the textarea's `value` attribute is changed directly. Fortunately there is a WebKit-specific workaround using textInput event. References: * http://stackoverflow.com/a/7554295 * http://help.dottoro.com/ljuecqgv.php --- qutebrowser/browser/commands.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index aac584621..b254efbcf 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1324,21 +1324,14 @@ class CommandDispatcher: clipboard = QApplication.clipboard() if clipboard.supportsSelection(): sel = clipboard.text(QClipboard.Selection) - log.misc.debug("Pasting selection: '{}'".format(sel)) + log.misc.debug("Pasting primary selection into element {} " + "(selection is '{}')".format(elem.debug_text(), sel)) elem.evaluateJavaScript(""" - var sel = '%s'; - if (this.selectionStart || this.selectionStart == '0') { - var startPos = this.selectionStart; - var endPos = this.selectionEnd; - this.value = this.value.substring(0, startPos) - + sel - + this.value.substring(endPos, this.value.length); - this.selectionStart = startPos + sel.length; - this.selectionEnd = startPos + sel.length; - } else { - this.value += sel; - } - """ % sel) + var sel = '{}'; + var event = document.createEvent('TextEvent'); + event.initTextEvent('textInput', true, true, null, sel); + this.dispatchEvent(event); + """.format(sel)) def _clear_search(self, view, text): """Clear search string/highlights for the given view.