Add a 'smart' option for ignore-case.

This commit is contained in:
Florian Bruhin 2014-08-12 17:00:18 +02:00
parent 5b3abfa5dd
commit 5b915186d7
4 changed files with 80 additions and 2 deletions

View File

@ -71,7 +71,12 @@ class SearchRunner(QObject):
self.do_search.emit('', QWebPage.HighlightAllOccurrences) self.do_search.emit('', QWebPage.HighlightAllOccurrences)
self._text = text self._text = text
self._flags = 0 self._flags = 0
if not config.get('general', 'ignore-case'): ignore_case = config.get('general', 'ignore-case')
if ignore_case == 'smart':
if not text.islower():
self._flags |= QWebPage.FindCaseSensitively
elif ignore_case:
# True, but not 'smart'
self._flags |= QWebPage.FindCaseSensitively self._flags |= QWebPage.FindCaseSensitively
if config.get('general', 'wrap-search'): if config.get('general', 'wrap-search'):
self._flags |= QWebPage.FindWrapsAroundDocument self._flags |= QWebPage.FindWrapsAroundDocument

View File

@ -168,7 +168,7 @@ SECTION_DESC = {
DATA = OrderedDict([ DATA = OrderedDict([
('general', sect.KeyValue( ('general', sect.KeyValue(
('ignore-case', ('ignore-case',
SettingValue(types.Bool(), 'true'), SettingValue(types.IgnoreCase(), 'smart'),
"Whether to find text on a page case-insensitively."), "Whether to find text on a page case-insensitively."),
('wrap-search', ('wrap-search',

View File

@ -1182,3 +1182,26 @@ class NewTabPosition(BaseType):
('right', "On the right of the current tab."), ('right', "On the right of the current tab."),
('first', "At the left end."), ('first', "At the left end."),
('last', "At the right end.")) ('last', "At the right end."))
class IgnoreCase(Bool):
"""Whether to ignore case when searching."""
def transform(self, value):
if value.lower() == 'smart':
return 'smart'
else:
return super().transform(value)
def validate(self, value):
if value.lower() == 'smart':
return
else:
super().validate(value)
def complete(self):
return [('true', 'Search case-insensitively'),
('false', 'Search case-sensitively'),
('smart', 'Search case-sensitively if there are capital '
'chars')]

View File

@ -1826,5 +1826,55 @@ class AutoSearchTests(unittest.TestCase):
self.assertIsNone(self.t.transform('')) self.assertIsNone(self.t.transform(''))
class IgnoreCase(unittest.TestCase):
"""Test IgnoreCase."""
TESTS = {
'smart': ['smart', 'SMART'],
True: BoolTests.TESTS[True],
False: BoolTests.TESTS[False],
}
INVALID = ['ssmart', 'foo']
def setUp(self):
self.t = conftypes.IgnoreCase()
def test_validate_empty(self):
"""Test validate with empty string and none_ok = False."""
with self.assertRaises(conftypes.ValidationError):
self.t.validate('')
def test_validate_empty_none_ok(self):
"""Test validate with empty string and none_ok = True."""
t = conftypes.IgnoreCase(none_ok=True)
t.validate('')
def test_validate_valid(self):
"""Test validate with valid values."""
for vallist in self.TESTS.values():
for val in vallist:
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(conftypes.ValidationError):
self.t.validate(val)
def test_transform(self):
"""Test transform with all values."""
for out, inputs in self.TESTS.items():
for inp in inputs:
with self.subTest(inp=inp):
self.assertEqual(self.t.transform(inp), out, inp)
def test_transform_empty(self):
"""Test transform with none_ok = False and an empty value."""
self.assertIsNone(self.t.transform(''))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()