diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index f6aa9205d..3df4c288c 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -335,6 +335,13 @@ class WebEngineCaret(browsertab.AbstractCaret): """ if js_elem is None: return + if js_elem == "focused": + # we had a focused element, not a selected one. Just send + if tab: + self._tab.key_press(Qt.Key_Enter, modifier=Qt.ControlModifier) + else: + self._tab.key_press(Qt.Key_Enter) + assert isinstance(js_elem, dict), js_elem elem = webengineelem.WebEngineElement(js_elem, tab=self._tab) if tab: @@ -364,7 +371,8 @@ class WebEngineCaret(browsertab.AbstractCaret): else: # click an existing blue selection - js_code = javascript.assemble('webelem', 'find_selected_link') + js_code = javascript.assemble('webelem', + 'find_selected_focused_link') self._tab.run_js_async(js_code, lambda jsret: self._follow_selected_cb(jsret, tab)) diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 9d5305f10..36da17dac 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -377,11 +377,26 @@ class WebKitCaret(browsertab.AbstractCaret): QWebSettings.JavascriptEnabled): if tab: self._tab.data.override_target = usertypes.ClickTarget.tab - self._tab.run_js_async( - 'window.getSelection().anchorNode.parentNode.click()') + self._tab.run_js_async(""" + const aElm = document.activeElement; + if (window.getSelection().anchorNode) { + window.getSelection().anchorNode.parentNode.click(); + } else if (aElm && aElm !== document.body) { + aElm.click(); + } + """) else: selection = self._widget.selectedHtml() if not selection: + # Getting here may mean we crashed, but we can't do anything + # about that until this commit is released: + # https://github.com/annulen/webkit/commit/0e75f3272d149bc64899c161f150eb341a2417af + # TODO find a way to check if something is focused + if tab: + self._tab.key_press(Qt.Key_Enter, + modifier=Qt.ControlModifier) + else: + self._tab.key_press(Qt.Key_Enter) return try: selected_element = xml.etree.ElementTree.fromstring( diff --git a/qutebrowser/javascript/webelem.js b/qutebrowser/javascript/webelem.js index f7ab0f636..ad18899f6 100644 --- a/qutebrowser/javascript/webelem.js +++ b/qutebrowser/javascript/webelem.js @@ -243,6 +243,7 @@ window._qutebrowser.webelem = (function() { }; // Runs a function in a frame until the result is not null, then return + // If no frame succeds, return null function run_frames(func) { for (let i = 0; i < window.frames.length; ++i) { const frame = window.frames[i]; @@ -328,8 +329,10 @@ window._qutebrowser.webelem = (function() { return serialize_elem(elem); }; - // Function for returning a selection to python (so we can click it) - funcs.find_selected_link = () => { + // Function for returning a selection or focus to python (so we can click + // it). If nothing is selected but there is something focused, returns + // "focused" + funcs.find_selected_focused_link = () => { const elem = window.getSelection().baseNode; if (elem) { return serialize_elem(elem.parentNode); @@ -342,7 +345,11 @@ window._qutebrowser.webelem = (function() { } return null; }); - return serialized_frame_elem; + + if (serialized_frame_elem) { + return serialized_frame_elem; + } + return funcs.find_focused() && "focused"; }; funcs.set_value = (id, value) => { diff --git a/tests/end2end/features/caret.feature b/tests/end2end/features/caret.feature index 803016539..268828edd 100644 --- a/tests/end2end/features/caret.feature +++ b/tests/end2end/features/caret.feature @@ -321,6 +321,38 @@ Feature: Caret mode - data/caret.html - data/hello.txt (active) + Scenario: :follow-selected with link tabbing (without JS) + When I set content.javascript.enabled to false + And I run :leave-mode + And I run :jseval document.activeElement.blur(); + And I run :fake-key + And I run :follow-selected + Then data/hello.txt should be loaded + + Scenario: :follow-selected with link tabbing (with JS) + When I set content.javascript.enabled to true + And I run :leave-mode + And I run :jseval document.activeElement.blur(); + And I run :fake-key + And I run :follow-selected + Then data/hello.txt should be loaded + + Scenario: :follow-selected with link tabbing in a tab (without JS) + When I set content.javascript.enabled to false + And I run :leave-mode + And I run :jseval document.activeElement.blur(); + And I run :fake-key + And I run :follow-selected --tab + Then data/hello.txt should be loaded + + Scenario: :follow-selected with link tabbing in a tab (with JS) + When I set content.javascript.enabled to true + And I run :leave-mode + And I run :jseval document.activeElement.blur(); + And I run :fake-key + And I run :follow-selected --tab + Then data/hello.txt should be loaded + # Search + caret mode # https://bugreports.qt.io/browse/QTBUG-60673