Implement QWebEngineView.createWindow

This commit is contained in:
Florian Bruhin 2016-09-02 07:41:48 +02:00
parent bb4d09ffd9
commit a4cd0291a6
6 changed files with 72 additions and 15 deletions

View File

@ -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,

View File

@ -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,

View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function open_modal() {
if (window.showModalDialog) {
window.showModalDialog();
} else {
window.open('', 'window', 'modal');
}
}
</script>
</head>
<body>
<button onclick="window.open('', 'window')" id="open-normal">normal</button>
<button onclick="open_modal()" id="open-modal">modal</button>
</body>
</html>

View File

@ -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

View File

@ -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

View File

@ -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