diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 41ddc6c80..bc9c5cc18 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -381,7 +381,7 @@ class WebEngineTab(browsertab.AbstractTab): def __init__(self, win_id, mode_manager, parent=None): super().__init__(win_id) - widget = webview.WebEngineView(tabdata=self.data) + widget = webview.WebEngineView(tabdata=self.data, win_id=win_id) self.history = WebEngineHistory(self) self.scroller = WebEngineScroller(self, parent=self) self.caret = WebEngineCaret(win_id=win_id, mode_manager=mode_manager, diff --git a/qutebrowser/browser/webengine/webview.py b/qutebrowser/browser/webengine/webview.py index 451a493a0..5c8d863e4 100644 --- a/qutebrowser/browser/webengine/webview.py +++ b/qutebrowser/browser/webengine/webview.py @@ -26,17 +26,64 @@ from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage # pylint: enable=no-name-in-module,import-error,useless-suppression from qutebrowser.config import config -from qutebrowser.utils import log, debug, usertypes +from qutebrowser.utils import log, debug, usertypes, objreg class WebEngineView(QWebEngineView): """Custom QWebEngineView subclass with qutebrowser-specific features.""" - def __init__(self, tabdata, parent=None): + def __init__(self, tabdata, win_id, parent=None): super().__init__(parent) + self._win_id = win_id self.setPage(WebEnginePage(tabdata, parent=self)) + def createWindow(self, wintype): + """Called by Qt when a page wants to create a new window. + + This function is called from the createWindow() method of the + associated QWebEnginePage, each time the page wants to create a new + window of the given type. This might be the result, for example, of a + JavaScript request to open a document in a new window. + + Args: + wintype: This enum describes the types of window that can be + created by the createWindow() function. + + QWebEnginePage::WebBrowserWindow: + A complete web browser window. + QWebEnginePage::WebBrowserTab: + A web browser tab. + QWebEnginePage::WebDialog: + A window without decoration. + QWebEnginePage::WebBrowserBackgroundTab: + A web browser tab without hiding the current visible + WebEngineView. (Added in Qt 5.7) + + Return: + The new QWebEngineView object. + """ + background = False + if wintype == QWebEnginePage.WebBrowserWindow: + log.webview.warning("WebBrowserWindow requested, but we don't " + "support that!") + elif wintype == QWebEnginePage.WebBrowserTab: + pass + elif wintype == QWebEnginePage.WebDialog: + log.webview.warning("WebDialog requested, but we don't support " + "that!") + elif (hasattr(QWebEnginePage, 'WebBrowserBackgroundTab') and + wintype == QWebEnginePage.WebBrowserBackgroundTab): + background = True + else: + raise ValueError("Invalid wintype {}".format(debug.qenum_key( + QWebEnginePage, wintype))) + + tabbed_browser = objreg.get('tabbed-browser', scope='window', + window=self._win_id) + # pylint: disable=protected-access + return tabbed_browser.tabopen(background=background)._widget + class WebEnginePage(QWebEnginePage): @@ -74,11 +121,6 @@ class WebEnginePage(QWebEnginePage): logger = level_to_logger[level] logger(logstring) - def createWindow(self, _typ): - """Handle new windows via JS.""" - log.stub() - return None - def acceptNavigationRequest(self, url: QUrl, typ: QWebEnginePage.NavigationType, diff --git a/tests/end2end/data/javascript/window_open.html b/tests/end2end/data/javascript/window_open.html new file mode 100644 index 000000000..974f821f0 --- /dev/null +++ b/tests/end2end/data/javascript/window_open.html @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + diff --git a/tests/end2end/features/hints.feature b/tests/end2end/features/hints.feature index 683defd8c..420ec12d1 100644 --- a/tests/end2end/features/hints.feature +++ b/tests/end2end/features/hints.feature @@ -19,7 +19,6 @@ Feature: Using hints ### Opening in current or new tab - @qtwebengine_todo: createWindow is not implemented yet Scenario: Following a hint and force to open in current tab. When I open data/hints/link_blank.html And I hint with args "links current" and follow a @@ -27,7 +26,6 @@ Feature: Using hints Then the following tabs should be open: - data/hello.txt (active) - @qtwebengine_todo: createWindow is not implemented yet Scenario: Following a hint and allow to open in new tab. When I open data/hints/link_blank.html And I hint with args "links normal" and follow a @@ -36,7 +34,6 @@ Feature: Using hints - data/hints/link_blank.html - data/hello.txt (active) - @qtwebengine_todo: createWindow is not implemented yet Scenario: Following a hint to link with sub-element and force to open in current tab. When I open data/hints/link_span.html And I run :tab-close @@ -154,13 +151,11 @@ Feature: Using hints And I hint wht args "links normal" and follow a Then "navigation request: url http://localhost:*/data/hello2.txt, type NavigationTypeLinkClicked, *" should be logged - @qtwebengine_todo: createWindow is not implemented yet Scenario: Opening a link inside a specific iframe When I open data/hints/iframe_target.html And I hint with args "links normal" and follow a Then "navigation request: url http://localhost:*/data/hello.txt, type NavigationTypeLinkClicked, *" should be logged - @qtwebengine_todo: createWindow is not implemented yet Scenario: Opening a link with specific target frame in a new tab When I open data/hints/iframe_target.html And I hint with args "links tab" and follow a diff --git a/tests/end2end/features/javascript.feature b/tests/end2end/features/javascript.feature index b95e4e7d7..bb34db70b 100644 --- a/tests/end2end/features/javascript.feature +++ b/tests/end2end/features/javascript.feature @@ -9,7 +9,6 @@ Feature: Javascript stuff # https://github.com/The-Compiler/qutebrowser/issues/906 - @qtwebengine_todo: createWindow is not implemented yet Scenario: Closing a JS window twice (issue 906) When I open about:blank And I open data/javascript/issue906.html in a new tab diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index 493e6e8af..9ae612f87 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -551,7 +551,6 @@ Feature: Various utility commands. Then the page should not be scrolled And the error "prompt-accept: This command is only allowed in prompt/yesno mode." should be shown - @qtwebengine_todo: createWindow is not implemented yet Scenario: :repeat-command with mode-switching command Given I open data/hints/link_blank.html And I run :tab-only