Tweaks the multi-line heuristic to handle scheme-like text

Adds some options to implement a way to treat multiline text that starts
with a scheme-like line as text instead as an URL.
This commit is contained in:
Tarcisio Fedrizzi 2016-04-07 18:45:56 +02:00
parent f100eb6e03
commit 73c200fb14
3 changed files with 32 additions and 8 deletions

View File

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

View File

@ -158,7 +158,8 @@ def _is_url_dns(urlstr):
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.
Args:
@ -166,6 +167,8 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True):
cwd: The current working directory, or None.
relative: Whether to resolve relative files.
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.
Return:
A target QUrl to a search page or the original URL.
@ -176,7 +179,7 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True):
if path is not None:
url = QUrl.fromLocalFile(path)
elif (not do_search) or is_url(urlstr):
elif not force_search and (not do_search or is_url(urlstr)):
# probably an address
log.url.debug("URL is a fuzzy address")
url = qurl_from_user_input(urlstr)
@ -196,17 +199,21 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True):
return url
def _has_explicit_scheme(url):
def _has_explicit_scheme(url, allow_only_scheme=True):
"""Check if an url has an explicit scheme given.
Args:
url: The URL as QUrl.
allow_only_scheme: if set to True the URL is allowed to contain only
the scheme with an empty path.
"""
# Note that generic URI syntax actually would allow a second colon
# after the scheme delimiter. Since we don't know of any URIs
# using this and want to support e.g. searching for scoped C++
# symbols, we treat this as not an URI anyways.
return (url.isValid() and url.scheme() and
(allow_only_scheme or len(url.path()) > 0) and
not url.path().startswith(' ') and
not url.path().startswith(':'))
@ -223,11 +230,13 @@ def is_special_url(url):
return url.scheme() in special_schemes
def is_url(urlstr):
def is_url(urlstr, allow_only_scheme=True):
"""Check if url seems to be a valid URL.
Args:
urlstr: The URL as string.
allow_only_scheme: if set to True the URL is allowed to contain only
the scheme with an empty path.
Return:
True if it is a valid URL, False otherwise.
@ -255,7 +264,7 @@ def is_url(urlstr):
# This will also catch URLs containing spaces.
return False
if _has_explicit_scheme(qurl):
if _has_explicit_scheme(qurl, allow_only_scheme):
# URLs with explicit schemes are always URLs
log.url.debug("Contains explicit scheme")
url = True

View File

@ -151,6 +151,19 @@ Feature: Yanking and pasting.
- about:blank
- 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 have a fresh instance
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
Given I open about:blank
When I run :tab-only