move backend dependent code to AbstractAction respective classes

This commit is contained in:
Marc Jauvin 2018-01-23 23:30:22 -05:00
parent 83515628a8
commit 2e912eeadf
5 changed files with 36 additions and 28 deletions

View File

@ -139,6 +139,10 @@ class AbstractAction:
raise WebTabError("{} is not a valid web action!".format(name)) raise WebTabError("{} is not a valid web action!".format(name))
self._widget.triggerPageAction(member) 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: class AbstractPrinting:

View File

@ -1511,33 +1511,7 @@ class CommandDispatcher:
except cmdexc.CommandError as e: except cmdexc.CommandError as e:
message.error(str(e)) message.error(str(e))
return return
tab.action.show_source(self, current_url)
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)
@cmdutils.register(instance='command-dispatcher', scope='window', @cmdutils.register(instance='command-dispatcher', scope='window',
debug=True) debug=True)

View File

@ -99,6 +99,13 @@ class WebEngineAction(browsertab.AbstractAction):
"""Save the current page.""" """Save the current page."""
self._widget.triggerPageAction(QWebEnginePage.SavePage) 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): class WebEnginePrinting(browsertab.AbstractPrinting):

View File

@ -23,6 +23,10 @@ import re
import functools import functools
import xml.etree.ElementTree import xml.etree.ElementTree
import pygments
import pygments.lexers
import pygments.formatters
import sip import sip
from PyQt5.QtCore import (pyqtSlot, Qt, QEvent, QUrl, QPoint, QTimer, QSizeF, from PyQt5.QtCore import (pyqtSlot, Qt, QEvent, QUrl, QPoint, QTimer, QSizeF,
QSize) QSize)
@ -50,6 +54,25 @@ class WebKitAction(browsertab.AbstractAction):
"""Save the current page.""" """Save the current page."""
raise browsertab.UnsupportedOperationError 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): class WebKitPrinting(browsertab.AbstractPrinting):

View File

@ -69,7 +69,7 @@ Feature: Page history
Scenario: History with view-source URL Scenario: History with view-source URL
When I open data/title.html When I open data/title.html
And I run :view-source 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: Then the history should contain:
http://localhost:(port)/data/title.html Test title http://localhost:(port)/data/title.html Test title