From f1b9a3c8b51ffd56d969029108859517a2890d9c Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 16 Mar 2015 18:32:17 +0100 Subject: [PATCH] Ensure there's no size for font-family settings. See #549. --- qutebrowser/config/configdata.py | 12 ++--- qutebrowser/config/configtypes.py | 19 ++++++++ qutebrowser/test/config/test_configtypes.py | 53 +++++++++++++++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index bab7ef14f..51334d7fb 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -882,27 +882,27 @@ DATA = collections.OrderedDict([ "Font used for the debugging console."), ('web-family-standard', - SettingValue(typ.String(none_ok=True), ''), + SettingValue(typ.FontFamily(none_ok=True), ''), "Font family for standard fonts."), ('web-family-fixed', - SettingValue(typ.String(none_ok=True), ''), + SettingValue(typ.FontFamily(none_ok=True), ''), "Font family for fixed fonts."), ('web-family-serif', - SettingValue(typ.String(none_ok=True), ''), + SettingValue(typ.FontFamily(none_ok=True), ''), "Font family for serif fonts."), ('web-family-sans-serif', - SettingValue(typ.String(none_ok=True), ''), + SettingValue(typ.FontFamily(none_ok=True), ''), "Font family for sans-serif fonts."), ('web-family-cursive', - SettingValue(typ.String(none_ok=True), ''), + SettingValue(typ.FontFamily(none_ok=True), ''), "Font family for cursive fonts."), ('web-family-fantasy', - SettingValue(typ.String(none_ok=True), ''), + SettingValue(typ.FontFamily(none_ok=True), ''), "Font family for fantasy fonts."), ('web-size-minimum', diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index f72622369..bd4ecdf94 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -700,6 +700,25 @@ class Font(BaseType): raise configexc.ValidationError(value, "must be a valid font") +class FontFamily(Font): + + """A Qt font family.""" + + def validate(self, value): + if not value: + if self._none_ok: + return + else: + raise configexc.ValidationError(value, "may not be empty!") + match = self.font_regex.match(value) + if not match: + raise configexc.ValidationError(value, "must be a valid font") + for group in 'style', 'weight', 'namedweight', 'size': + if match.group(group): + raise configexc.ValidationError(value, "may not include a " + "{}!".format(group)) + + class QtFont(Font): """A Font which gets converted to q QFont.""" diff --git a/qutebrowser/test/config/test_configtypes.py b/qutebrowser/test/config/test_configtypes.py index 3ed94d991..633dc0e32 100644 --- a/qutebrowser/test/config/test_configtypes.py +++ b/qutebrowser/test/config/test_configtypes.py @@ -1196,6 +1196,59 @@ class FontTests(unittest.TestCase): self.assertIsNone(self.t2.transform('')) +class FontFamilyTests(unittest.TestCase): + + """Test FontFamily.""" + + TESTS = ['"Foobar Neue"', 'inconsolatazi4', 'Foobar'] + INVALID = [ + '10pt "Foobar Neue"', + '10PT "Foobar Neue"', + '10px "Foobar Neue"', + '10PX "Foobar Neue"', + 'bold "Foobar Neue"', + 'italic "Foobar Neue"', + 'oblique "Foobar Neue"', + 'normal bold "Foobar Neue"', + 'bold italic "Foobar Neue"', + 'bold 10pt "Foobar Neue"', + 'italic 10pt "Foobar Neue"', + 'oblique 10pt "Foobar Neue"', + 'normal bold 10pt "Foobar Neue"', + 'bold italic 10pt "Foobar Neue"', + ] + + def setUp(self): + self.t = configtypes.FontFamily() + + def test_validate_empty(self): + """Test validate with an empty string.""" + with self.assertRaises(configexc.ValidationError): + self.t.validate('') + + def test_validate_empty_none_ok(self): + """Test validate with an empty string and none_ok=True.""" + t = configtypes.FontFamily(none_ok=True) + t.validate('') + + def test_validate_valid(self): + """Test validate with valid values.""" + for val in self.TESTS: + with self.subTest(val=val): + self.t.validate(val) + + def test_validate_invalid(self): + """Test validate with invalid values.""" + for val in self.INVALID: + with self.subTest(val=val): + with self.assertRaises(configexc.ValidationError, msg=val): + self.t.validate(val) + + def test_transform_empty(self): + """Test transform with an empty value.""" + self.assertIsNone(self.t.transform('')) + + class RegexTests(unittest.TestCase): """Test Regex."""