Merge remote-tracking branch 'origin/pr/3521'

This commit is contained in:
Florian Bruhin 2018-01-26 09:25:50 +01:00
commit 125b3c1de9
7 changed files with 56 additions and 32 deletions

View File

@ -94,7 +94,6 @@ class TabData:
keep_icon: Whether the (e.g. cloned) icon should not be cleared on page
load.
inspector: The QWebInspector used for this webview.
viewing_source: Set if we're currently showing a source view.
override_target: Override for open_target for fake clicks (like hints).
Only used for QtWebKit.
pinned: Flag to pin the tab.
@ -102,7 +101,6 @@ class TabData:
"""
keep_icon = attr.ib(False)
viewing_source = attr.ib(False)
inspector = attr.ib(None)
override_target = attr.ib(None)
pinned = attr.ib(False)
@ -121,8 +119,10 @@ class AbstractAction:
action_class = None
action_base = None
def __init__(self):
def __init__(self, tab, win_id):
self._widget = None
self._tab = tab
self._win_id = win_id
def exit_fullscreen(self):
"""Exit the fullscreen mode."""
@ -139,6 +139,10 @@ class AbstractAction:
raise WebTabError("{} is not a valid web action!".format(name))
self._widget.triggerPageAction(member)
def show_source(self):
"""Show the source of the current page in a new tab."""
raise NotImplementedError
class AbstractPrinting:
@ -723,7 +727,6 @@ class AbstractTab(QWidget):
def _on_load_started(self):
self._progress = 0
self._has_ssl_errors = False
self.data.viewing_source = False
self._set_load_status(usertypes.LoadStatus.loading)
self.load_started.emit()

View File

@ -29,9 +29,6 @@ from PyQt5.QtWidgets import QApplication, QTabBar, QDialog
from PyQt5.QtCore import Qt, QUrl, QEvent, QUrlQuery
from PyQt5.QtGui import QKeyEvent
from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog
import pygments
import pygments.lexers
import pygments.formatters
from qutebrowser.commands import userscripts, cmdexc, cmdutils, runners
from qutebrowser.config import config, configdata
@ -1510,31 +1507,15 @@ class CommandDispatcher:
def view_source(self):
"""Show the source of the current page in a new tab."""
tab = self._current_widget()
if tab.data.viewing_source:
raise cmdexc.CommandError("Already viewing source!")
try:
current_url = self._current_url()
except cmdexc.CommandError as e:
message.error(str(e))
return
if current_url.scheme() == 'view-source':
raise cmdexc.CommandError("Already viewing source!")
def show_source_cb(source):
"""Show source as soon as it's ready."""
# WORKAROUND for https://github.com/PyCQA/pylint/issues/491
# pylint: disable=no-member
lexer = pygments.lexers.HtmlLexer()
formatter = pygments.formatters.HtmlFormatter(
full=True, linenos='table',
title='Source for {}'.format(current_url.toDisplayString()))
# pylint: enable=no-member
highlighted = pygments.highlight(source, lexer, formatter)
new_tab = self._tabbed_browser.tabopen()
new_tab.set_html(highlighted)
new_tab.data.viewing_source = True
tab.dump_async(show_source_cb)
tab.action.show_source()
@cmdutils.register(instance='command-dispatcher', scope='window',
debug=True)

View File

@ -172,7 +172,7 @@ class WebHistory(sql.SqlTable):
@pyqtSlot(QUrl, QUrl, str)
def add_from_tab(self, url, requested_url, title):
"""Add a new history entry as slot, called from a BrowserTab."""
if any(url.scheme() == 'data' or
if any(url.scheme() in ('data', 'view-source') or
(url.scheme(), url.host()) == ('qute', 'back')
for url in (url, requested_url)):
return

View File

@ -99,6 +99,19 @@ class WebEngineAction(browsertab.AbstractAction):
"""Save the current page."""
self._widget.triggerPageAction(QWebEnginePage.SavePage)
def show_source(self):
try:
self._widget.triggerPageAction(QWebEnginePage.ViewSource)
except AttributeError:
# Qt < 5.8
tb = objreg.get('tabbed-browser', scope='window',
window=self._win_id)
urlstr = self._tab.url().toString(QUrl.RemoveUserInfo)
# The original URL becomes the path of a view-source: URL
# (without a host), but query/fragment should stay.
url = QUrl('view-source:' + urlstr)
tb.tabopen(url, background=False, related=True)
class WebEnginePrinting(browsertab.AbstractPrinting):
@ -596,7 +609,7 @@ class WebEngineTab(browsertab.AbstractTab):
self.search = WebEngineSearch(parent=self)
self.printing = WebEnginePrinting()
self.elements = WebEngineElements(self)
self.action = WebEngineAction()
self.action = WebEngineAction(self, win_id)
self._set_widget(widget)
self._connect_signals()
self.backend = usertypes.Backend.QtWebEngine
@ -613,7 +626,7 @@ class WebEngineTab(browsertab.AbstractTab):
utils.read_file('javascript/caret.js'),
])
script = QWebEngineScript()
script.setInjectionPoint(QWebEngineScript.DocumentCreation)
script.setInjectionPoint(QWebEngineScript.DocumentReady)
script.setSourceCode(js_code)
page = self._widget.page()

View File

@ -23,6 +23,10 @@ import re
import functools
import xml.etree.ElementTree
import pygments
import pygments.lexers
import pygments.formatters
import sip
from PyQt5.QtCore import (pyqtSlot, Qt, QEvent, QUrl, QPoint, QTimer, QSizeF,
QSize)
@ -50,6 +54,29 @@ class WebKitAction(browsertab.AbstractAction):
"""Save the current page."""
raise browsertab.UnsupportedOperationError
def show_source(self):
def show_source_cb(source):
"""Show source as soon as it's ready."""
# WORKAROUND for https://github.com/PyCQA/pylint/issues/491
# pylint: disable=no-member
lexer = pygments.lexers.HtmlLexer()
formatter = pygments.formatters.HtmlFormatter(
full=True, linenos='table')
# pylint: enable=no-member
highlighted = pygments.highlight(source, lexer, formatter)
tb = objreg.get('tabbed-browser', scope='window',
window=self._win_id)
new_tab = tb.tabopen(background=False, related=True)
# The original URL becomes the path of a view-source: URL
# (without a host), but query/fragment should stay.
url = QUrl('view-source:' + urlstr)
new_tab.set_html(highlighted, url)
urlstr = self._tab.url().toString(QUrl.RemoveUserInfo)
self._tab.dump_async(show_source_cb)
class WebKitPrinting(browsertab.AbstractPrinting):
@ -625,7 +652,7 @@ class WebKitTab(browsertab.AbstractTab):
self.search = WebKitSearch(parent=self)
self.printing = WebKitPrinting()
self.elements = WebKitElements(self)
self.action = WebKitAction()
self.action = WebKitAction(self, win_id)
self._set_widget(widget)
self._connect_signals()
self.backend = usertypes.Backend.QtWebKit

View File

@ -69,7 +69,7 @@ Feature: Page history
Scenario: History with view-source URL
When I open data/title.html
And I run :view-source
And I wait for "Changing title for idx * to 'Source for http://localhost:*/data/title.html'" in the log
And I wait for regex "Changing title for idx \d+ to 'view-source:(http://)?localhost:\d+/data/title.html'" in the log
Then the history should contain:
http://localhost:(port)/data/title.html Test title

View File

@ -87,7 +87,7 @@ class Tab(browsertab.AbstractTab):
self.search = browsertab.AbstractSearch(parent=self)
self.printing = browsertab.AbstractPrinting()
self.elements = browsertab.AbstractElements(self)
self.action = browsertab.AbstractAction()
self.action = browsertab.AbstractAction(self, self.win_id)
def _install_event_filter(self):
pass