implement requested changes for PR #3521.

This commit is contained in:
Marc Jauvin 2018-01-24 18:04:05 -05:00
parent 2e912eeadf
commit d7c51f7fc4
4 changed files with 35 additions and 25 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)
@ -139,7 +137,7 @@ class AbstractAction:
raise WebTabError("{} is not a valid web action!".format(name))
self._widget.triggerPageAction(member)
def show_source(self, dispatcher, url):
def show_source(self, win_id, url):
"""Show the source of the current page in a new tab."""
raise NotImplementedError
@ -727,7 +725,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
@ -758,6 +755,7 @@ class CommandDispatcher:
else:
x = None
y = perc
self._current_widget().scroller.to_perc(x, y)
@cmdutils.register(instance='command-dispatcher', scope='window')
@ -1503,15 +1501,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
tab.action.show_source(self, current_url)
if current_url.scheme() == 'view-source':
raise cmdexc.CommandError("Already viewing source!")
tab.action.show_source(self._win_id, current_url)
@cmdutils.register(instance='command-dispatcher', scope='window',
debug=True)

View File

@ -99,12 +99,18 @@ class WebEngineAction(browsertab.AbstractAction):
"""Save the current page."""
self._widget.triggerPageAction(QWebEnginePage.SavePage)
def show_source(self, dispatcher, url):
# use view-source: scheme to show page source for webengine
url = QUrl('view-source:{}'.format(url.toString()))
new_tab = dispatcher._tabbed_browser.tabopen(
url, background=True, related=True)
new_tab.data.viewing_source = True
def show_source(self, win_id, url):
try:
self._widget.triggerPageAction(QWebEnginePage.ViewSource)
except AttributeError:
# Qt < 5.8
# note: it's not possible to build the QUrl object by setting the
# scheme using setScheme('view-source').
# QUrl does the right thing when URL prefix is "view-source:".
new_url = QUrl('view-source:' + url.toString())
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id)
tabbed_browser.tabopen(new_url, background=False, related=True)
class WebEnginePrinting(browsertab.AbstractPrinting):

View File

@ -54,7 +54,15 @@ class WebKitAction(browsertab.AbstractAction):
"""Save the current page."""
raise browsertab.UnsupportedOperationError
def show_source(self, dispatcher, url):
def show_source(self, win_id, url):
def format_url(url):
"""emulate what WebEnginePage::ViewSource does."""
s = url.toString()
s = s.split('//')[-1] # strip scheme
s = s.split('@')[-1] # strip userinfo
return s
def show_source_cb(source):
"""Show source as soon as it's ready."""
# WORKAROUND for https://github.com/PyCQA/pylint/issues/491
@ -62,16 +70,17 @@ class WebKitAction(browsertab.AbstractAction):
lexer = pygments.lexers.HtmlLexer()
formatter = pygments.formatters.HtmlFormatter(
full=True, linenos='table',
title='view-source:{}'.format(url.toDisplayString()))
title='view-source:' + format_url(url))
# pylint: enable=no-member
highlighted = pygments.highlight(source, lexer, formatter)
new_tab = dispatcher._tabbed_browser.tabopen()
new_tab.set_html(highlighted)
new_tab.data.viewing_source = True
new_tab.url = lambda requested=False: QUrl(
'view-source:' + url.toDisplayString())
dispatcher._current_widget().dump_async(show_source_cb)
base_url = QUrl('view-source:' + url.toString())
new_tab = tabbed_browser.tabopen(background=False, related=True)
new_tab.set_html(highlighted, base_url)
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id)
tabbed_browser.currentWidget().dump_async(show_source_cb)
class WebKitPrinting(browsertab.AbstractPrinting):