From 2e912eeadf0db5a8a98947a84ae01a5bca8361c0 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 23 Jan 2018 23:30:22 -0500 Subject: [PATCH] move backend dependent code to AbstractAction respective classes --- qutebrowser/browser/browsertab.py | 4 +++ qutebrowser/browser/commands.py | 28 +------------------ qutebrowser/browser/webengine/webenginetab.py | 7 +++++ qutebrowser/browser/webkit/webkittab.py | 23 +++++++++++++++ tests/end2end/features/history.feature | 2 +- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 324895100..e90c71f5d 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -139,6 +139,10 @@ class AbstractAction: raise WebTabError("{} is not a valid web action!".format(name)) self._widget.triggerPageAction(member) + def show_source(self, dispatcher, url): + """Show the source of the current page in a new tab.""" + raise NotImplementedError + class AbstractPrinting: diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 46c37ced1..3f616d1f0 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1511,33 +1511,7 @@ class CommandDispatcher: except cmdexc.CommandError as e: message.error(str(e)) return - - if tab.backend == usertypes.Backend.QtWebEngine: - # use view-source: scheme to show page source for webengine - url = QUrl('view-source:{}'.format(current_url.toString())) - new_tab = self._tabbed_browser.tabopen( - url, background=True, related=True) - new_tab.data.viewing_source = True - return - - 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 - new_tab.url = lambda requested=False: QUrl( - 'Source: {}'.format(current_url.toDisplayString())) - - tab.dump_async(show_source_cb) + tab.action.show_source(self, current_url) @cmdutils.register(instance='command-dispatcher', scope='window', debug=True) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index aa19f4c6b..496df50e5 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -99,6 +99,13 @@ 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 + class WebEnginePrinting(browsertab.AbstractPrinting): diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 4609f08db..6edda24ed 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -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,25 @@ class WebKitAction(browsertab.AbstractAction): """Save the current page.""" raise browsertab.UnsupportedOperationError + def show_source(self, dispatcher, url): + 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='view-source:{}'.format(url.toDisplayString())) + # 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) + class WebKitPrinting(browsertab.AbstractPrinting): diff --git a/tests/end2end/features/history.feature b/tests/end2end/features/history.feature index 00bd20403..df6610daf 100644 --- a/tests/end2end/features/history.feature +++ b/tests/end2end/features/history.feature @@ -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 "Changing title for idx * to 'view-source:http://localhost:*/data/title.html'" in the log Then the history should contain: http://localhost:(port)/data/title.html Test title