Get rid of configtypes.AutoSearch and IgnoreCase

This commit is contained in:
Florian Bruhin 2017-06-13 10:28:05 +02:00
parent 921d02e4d3
commit 5ec47da127
7 changed files with 52 additions and 102 deletions

View File

@ -188,13 +188,28 @@ class AbstractSearch(QObject):
self.text = None
self.search_displayed = False
def search(self, text, *, ignore_case=False, reverse=False,
def _is_case_sensitive(self, ignore_case):
"""Check if case-sensitivity should be used.
This assumes self.text is already set properly.
Arguments:
ignore_case: The ignore_case value from the config.
"""
mapping = {
'smart': not self.text.islower(),
'never': False,
'always': True,
}
return mapping[ignore_case]
def search(self, text, *, ignore_case='never', reverse=False,
result_cb=None):
"""Find the given text on the page.
Args:
text: The text to search for.
ignore_case: Search case-insensitively. (True/False/'smart')
ignore_case: Search case-insensitively. ('always'/'never/'smart')
reverse: Reverse search direction.
result_cb: Called with a bool indicating whether a match was found.
"""

View File

@ -152,20 +152,16 @@ class WebEngineSearch(browsertab.AbstractSearch):
callback(found)
self._widget.findText(text, flags, wrapped_callback)
def search(self, text, *, ignore_case=False, reverse=False,
def search(self, text, *, ignore_case='never', reverse=False,
result_cb=None):
flags = QWebEnginePage.FindFlags(0)
if ignore_case == 'smart':
if not text.islower():
flags |= QWebEnginePage.FindCaseSensitively
elif not ignore_case:
flags |= QWebEnginePage.FindCaseSensitively
if reverse:
flags |= QWebEnginePage.FindBackward
self.text = text
self._flags = flags
self._find(text, flags, result_cb, 'search')
self._flags = QWebEnginePage.FindFlags(0)
if self._is_case_sensitive(ignore_case):
self._flags |= QWebEnginePage.FindCaseSensitively
if reverse:
self._flags |= QWebEnginePage.FindBackward
self._find(text, self._flags, result_cb, 'search')
def clear(self):
self.search_displayed = False

View File

@ -140,24 +140,21 @@ class WebKitSearch(browsertab.AbstractSearch):
self._widget.findText('')
self._widget.findText('', QWebPage.HighlightAllOccurrences)
def search(self, text, *, ignore_case=False, reverse=False,
def search(self, text, *, ignore_case='never', reverse=False,
result_cb=None):
self.search_displayed = True
flags = QWebPage.FindWrapsAroundDocument
if ignore_case == 'smart':
if not text.islower():
flags |= QWebPage.FindCaseSensitively
elif not ignore_case:
flags |= QWebPage.FindCaseSensitively
self.text = text
self._flags = QWebPage.FindWrapsAroundDocument
if self._is_case_sensitive(ignore_case):
self._flags |= QWebPage.FindCaseSensitively
if reverse:
flags |= QWebPage.FindBackward
self._flags |= QWebPage.FindBackward
# We actually search *twice* - once to highlight everything, then again
# to get a mark so we can navigate.
found = self._widget.findText(text, flags)
self._widget.findText(text, flags | QWebPage.HighlightAllOccurrences)
self.text = text
self._flags = flags
self._call_cb(result_cb, found, text, flags, 'search')
found = self._widget.findText(text, self._flags)
self._widget.findText(text,
self._flags | QWebPage.HighlightAllOccurrences)
self._call_cb(result_cb, found, text, self._flags, 'search')
def next_result(self, *, result_cb=None):
self.search_displayed = True

View File

@ -1,7 +1,12 @@
# general
ignore_case:
type: IgnoreCase
type:
name: String
valid_values:
- always: Search case-insensitively
- never: Search case-sensitively
- smart: Search case-sensitively if there are capital chars
default: smart
desc: Whether to find text on a page case-insensitively.
@ -45,7 +50,12 @@ default_page:
for a blank page.
auto_search:
type: AutoSearch
type:
name: String
valid_values:
- naive: Use simple/naive check.
- dns: Use DNS requests (might be slow!).
- never: Never search automatically.
default: naive
desc: Whether to start a search when something else than a URL is entered.

View File

@ -1159,44 +1159,6 @@ class Encoding(BaseType):
return value
class AutoSearch(BaseType):
"""Whether to start a search when something else than a URL is entered."""
def __init__(self, none_ok=False):
super().__init__(none_ok)
self.booltype = Bool(none_ok=none_ok)
self.valid_values = ValidValues(
('naive', "Use simple/naive check."),
('dns', "Use DNS requests (might be slow!)."),
('false', "Never search automatically."))
def from_py(self, value):
self._basic_validation(value, pytype=(str, bool))
if not value:
return None
if isinstance(value, bool):
if self.booltype.from_py(value):
# boolean true is an alias for naive matching
return 'naive'
else:
return False
elif value.lower() in ['naive', 'dns']:
return value.lower()
else:
# Should be prevented by valid_values
assert False, value
def transform(self, value):
if not value:
return None
elif value.lower() in ['naive', 'dns']:
return value.lower()
elif self.booltype.transform(value):
pass
class Position(MappingType):
"""The position of the tab bar."""
@ -1337,34 +1299,6 @@ class NewTabPosition(BaseType):
('last', "At the end."))
class IgnoreCase(Bool):
"""Whether to ignore case when searching."""
def __init__(self, none_ok=False):
super().__init__(none_ok)
self.valid_values = ValidValues(
('true', "Search case-insensitively"),
('false', "Search case-sensitively"),
('smart', "Search case-sensitively if there "
"are capital chars"))
def transform(self, value):
if value.lower() == 'smart':
return 'smart'
else:
return super().transform(value)
def validate(self, value):
self._basic_validation(value)
if not value:
return
if value.lower() == 'smart':
return
else:
super().validate(value)
class UserAgent(BaseType):
"""The user agent to use."""

View File

@ -194,7 +194,7 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True,
url = qurl_from_user_input(urlstr)
log.url.debug("Converting fuzzy term {!r} to URL -> {}".format(
urlstr, url.toDisplayString()))
if do_search and config.val.auto_search and urlstr:
if do_search and config.val.auto_search != 'never' and urlstr:
qtutils.ensure_valid(url)
else:
if not url.isValid():
@ -248,7 +248,7 @@ def is_url(urlstr):
qurl = QUrl(urlstr)
qurl_userinput = qurl_from_user_input(urlstr)
if not autosearch:
if autosearch == 'never':
# no autosearch, so everything is a URL unless it has an explicit
# search engine.
try:
@ -270,7 +270,7 @@ def is_url(urlstr):
log.url.debug("Is localhost.")
url = True
elif is_special_url(qurl):
# Special URLs are always URLs, even with autosearch=False
# Special URLs are always URLs, even with autosearch=never
log.url.debug("Is a special URL.")
url = True
elif autosearch == 'dns':

View File

@ -347,7 +347,7 @@ def test_get_search_url_invalid(urlutils_config_stub, url):
# autosearch = False
(False, True, False, 'This is a URL without autosearch'),
])
@pytest.mark.parametrize('auto_search', ['dns', 'naive', False])
@pytest.mark.parametrize('auto_search', ['dns', 'naive', 'never'])
def test_is_url(urlutils_config_stub, fake_dns, is_url, is_url_no_autosearch,
uses_dns, url, auto_search):
"""Test is_url().
@ -384,11 +384,9 @@ def test_is_url(urlutils_config_stub, fake_dns, is_url, is_url_no_autosearch,
assert not fake_dns.used
assert result == is_url
elif auto_search == 'naive':
urlutils_config_stub.data['general']['auto-search'] = 'naive'
assert urlutils.is_url(url) == is_url
assert not fake_dns.used
elif not auto_search:
urlutils_config_stub.data['general']['auto-search'] = False
elif auto_search == 'never':
assert urlutils.is_url(url) == is_url_no_autosearch
assert not fake_dns.used
else: