diff --git a/doc/help/settings.asciidoc b/doc/help/settings.asciidoc index 518381187..d68cd3949 100644 --- a/doc/help/settings.asciidoc +++ b/doc/help/settings.asciidoc @@ -3449,5 +3449,8 @@ See the setting's valid values for more information on allowed values. See https://sqlite.org/lang_datefunc.html for reference. |UniqueCharString|A string which may not contain duplicate chars. |Url|A URL as a string. +|UrlPattern|A match pattern for a URL. + +See https://developer.chrome.com/apps/match_patterns for the allowed syntax. |VerticalPosition|The position of the download bar. |============== diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 619dfd71d..4d126820a 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -61,7 +61,7 @@ from PyQt5.QtWidgets import QTabWidget, QTabBar from qutebrowser.commands import cmdutils from qutebrowser.config import configexc -from qutebrowser.utils import standarddir, utils, qtutils, urlutils +from qutebrowser.utils import standarddir, utils, qtutils, urlutils, urlmatch from qutebrowser.keyinput import keyutils @@ -1661,3 +1661,22 @@ class Key(BaseType): return keyutils.KeySequence.parse(value) except keyutils.KeyParseError as e: raise configexc.ValidationError(value, str(e)) + + +class UrlPattern(BaseType): + + """A match pattern for a URL. + + See https://developer.chrome.com/apps/match_patterns for the allowed + syntax. + """ + + def to_py(self, value): + self._basic_py_validation(value, str) + if not value: + return None + + try: + return urlmatch.UrlPattern(value) + except urlmatch.ParseError as e: + raise configexc.ValidationError(value, str(e)) diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 533932981..46119be7e 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -35,7 +35,7 @@ from PyQt5.QtGui import QColor, QFont from PyQt5.QtNetwork import QNetworkProxy from qutebrowser.config import configtypes, configexc -from qutebrowser.utils import debug, utils, qtutils +from qutebrowser.utils import debug, utils, qtutils, urlmatch from qutebrowser.browser.network import pac from qutebrowser.keyinput import keyutils from tests.helpers import utils as testutils @@ -2099,6 +2099,21 @@ class TestKey: klass().to_py(val) +class TestUrlPattern: + + @pytest.fixture + def klass(self): + return configtypes.UrlPattern + + def test_to_py_valid(self, klass): + pattern = 'http://*.example.com/' + assert klass().to_py(pattern) == urlmatch.UrlPattern(pattern) + + def test_to_py_invalid(self, klass): + with pytest.raises(configexc.ValidationError): + klass().to_py('http://') + + @pytest.mark.parametrize('first, second, equal', [ (re.compile('foo'), RegexEq('foo'), True), (RegexEq('bar'), re.compile('bar'), True),