Merge branch 'hcraT-empty_uri'

This commit is contained in:
Florian Bruhin 2016-05-29 10:38:45 +02:00
commit 4b97ef88ac
6 changed files with 46 additions and 11 deletions

View File

@ -53,6 +53,7 @@ Changed
colored logs without it. colored logs without it.
- URLs are now shown decoded when hovering. - URLs are now shown decoded when hovering.
- Keybindings are now shown in the command completion - Keybindings are now shown in the command completion
- Improved behaviour when pasting multiple lines
Fixed Fixed
----- -----

View File

@ -148,12 +148,12 @@ Contributors, sorted by the number of commits in descending order:
* Martin Tournoij * Martin Tournoij
* Raphael Pierzina * Raphael Pierzina
* Joel Torstensson * Joel Torstensson
* Tarcisio Fedrizzi
* Patric Schmitz * Patric Schmitz
* Claude * Claude
* Corentin Julé * Corentin Julé
* meles5 * meles5
* Jakub Klinkovský * Jakub Klinkovský
* Tarcisio Fedrizzi
* Philipp Hansch * Philipp Hansch
* Panagiotis Ktistakis * Panagiotis Ktistakis
* Artur Shaik * Artur Shaik

View File

@ -841,6 +841,7 @@ class CommandDispatcher:
bg: Open in a background tab. bg: Open in a background tab.
window: Open in new window. window: Open in new window.
""" """
force_search = False
if sel and utils.supports_selection(): if sel and utils.supports_selection():
target = "Primary selection" target = "Primary selection"
else: else:
@ -852,15 +853,16 @@ class CommandDispatcher:
log.misc.debug("{} contained: {!r}".format(target, text)) log.misc.debug("{} contained: {!r}".format(target, text))
text_urls = [u for u in text.split('\n') if u.strip()] text_urls = [u for u in text.split('\n') if u.strip()]
if (len(text_urls) > 1 and not urlutils.is_url(text_urls[0]) and if (len(text_urls) > 1 and not urlutils.is_url(text_urls[0]) and
urlutils.get_path_if_valid(text_urls[0], urlutils.get_path_if_valid(
check_exists=True) is None): text_urls[0], check_exists=True) is None):
force_search = True
text_urls = [text] text_urls = [text]
for i, text_url in enumerate(text_urls): for i, text_url in enumerate(text_urls):
if not window and i > 0: if not window and i > 0:
tab = False tab = False
bg = True bg = True
try: try:
url = urlutils.fuzzy_url(text_url) url = urlutils.fuzzy_url(text_url, force_search=force_search)
except urlutils.InvalidUrlError as e: except urlutils.InvalidUrlError as e:
raise cmdexc.CommandError(e) raise cmdexc.CommandError(e)
self._open(url, tab, bg, window) self._open(url, tab, bg, window)

View File

@ -158,7 +158,8 @@ def _is_url_dns(urlstr):
return not info.error() return not info.error()
def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True): def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True,
force_search=False):
"""Get a QUrl based on a user input which is URL or search term. """Get a QUrl based on a user input which is URL or search term.
Args: Args:
@ -166,6 +167,8 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True):
cwd: The current working directory, or None. cwd: The current working directory, or None.
relative: Whether to resolve relative files. relative: Whether to resolve relative files.
do_search: Whether to perform a search on non-URLs. do_search: Whether to perform a search on non-URLs.
force_search: Whether to force a search even if the content can be
interpreted as an URL or a path.
Return: Return:
A target QUrl to a search page or the original URL. A target QUrl to a search page or the original URL.
@ -174,18 +177,18 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True):
path = get_path_if_valid(urlstr, cwd=cwd, relative=relative, path = get_path_if_valid(urlstr, cwd=cwd, relative=relative,
check_exists=True) check_exists=True)
if path is not None: if not force_search and path is not None:
url = QUrl.fromLocalFile(path) url = QUrl.fromLocalFile(path)
elif (not do_search) or is_url(urlstr): elif force_search or (do_search and not is_url(urlstr)):
# probably an address # probably a search term
log.url.debug("URL is a fuzzy address")
url = qurl_from_user_input(urlstr)
else: # probably a search term
log.url.debug("URL is a fuzzy search term") log.url.debug("URL is a fuzzy search term")
try: try:
url = _get_search_url(urlstr) url = _get_search_url(urlstr)
except ValueError: # invalid search engine except ValueError: # invalid search engine
url = qurl_from_user_input(urlstr) url = qurl_from_user_input(urlstr)
else: # probably an address
log.url.debug("URL is a fuzzy address")
url = qurl_from_user_input(urlstr)
log.url.debug("Converting fuzzy term {!r} to URL -> {}".format( log.url.debug("Converting fuzzy term {!r} to URL -> {}".format(
urlstr, url.toDisplayString())) urlstr, url.toDisplayString()))
if do_search and config.get('general', 'auto-search') and urlstr: if do_search and config.get('general', 'auto-search') and urlstr:
@ -207,6 +210,7 @@ def _has_explicit_scheme(url):
# using this and want to support e.g. searching for scoped C++ # using this and want to support e.g. searching for scoped C++
# symbols, we treat this as not an URI anyways. # symbols, we treat this as not an URI anyways.
return (url.isValid() and url.scheme() and return (url.isValid() and url.scheme() and
(url.host() or url.path()) and
not url.path().startswith(' ') and not url.path().startswith(' ') and
not url.path().startswith(':')) not url.path().startswith(':'))

View File

@ -151,6 +151,20 @@ Feature: Yanking and pasting.
- about:blank - about:blank
- data/hello.txt?q=this%20url%3A%0Ahttp%3A//qutebrowser.org%0Ashould%20not%20open (active) - data/hello.txt?q=this%20url%3A%0Ahttp%3A//qutebrowser.org%0Ashould%20not%20open (active)
Scenario: Pasting multiline whose first line looks like an URI
Given I open about:blank
When I run :tab-only
When I set searchengines -> DEFAULT to http://localhost:(port)/data/hello.txt?q={}
And I put the following lines into the clipboard:
text:
should open
as search
And I run :paste -t
And I wait until data/hello.txt?q=text%3A%0Ashould%20open%0Aas%20search is loaded
Then the following tabs should be open:
- about:blank
- data/hello.txt?q=text%3A%0Ashould%20open%0Aas%20search (active)
Scenario: Pasting multiple urls in a background tab Scenario: Pasting multiple urls in a background tab
Given I open about:blank Given I open about:blank
When I run :tab-only When I run :tab-only

View File

@ -238,6 +238,19 @@ class TestFuzzyUrl:
with pytest.raises(urlutils.InvalidUrlError): with pytest.raises(urlutils.InvalidUrlError):
urlutils.fuzzy_url(url, do_search=True) urlutils.fuzzy_url(url, do_search=True)
@pytest.mark.parametrize('urlstring', [
'http://www.qutebrowser.org/',
'/foo',
'test'
])
def test_force_search(self, urlstring, get_search_url_mock):
"""Test the force search option."""
get_search_url_mock.return_value = QUrl('search_url')
url = urlutils.fuzzy_url(urlstring, force_search=True)
assert url == QUrl('search_url')
@pytest.mark.parametrize('path, check_exists', [ @pytest.mark.parametrize('path, check_exists', [
('/foo', False), ('/foo', False),
('/bar', True), ('/bar', True),
@ -312,6 +325,7 @@ def test_get_search_url_invalid(urlutils_config_stub, url):
(True, True, False, 'qute::foo'), (True, True, False, 'qute::foo'),
# Invalid URLs # Invalid URLs
(False, False, False, ''), (False, False, False, ''),
(False, True, False, 'onlyscheme:'),
(False, True, False, 'http:foo:0'), (False, True, False, 'http:foo:0'),
# Not URLs # Not URLs
(False, True, False, 'foo bar'), # no DNS because of space (False, True, False, 'foo bar'), # no DNS because of space