Move URL to yankable string conversion to urlutils

See issue #7693
This commit is contained in:
Michael M 2023-09-01 20:15:45 -05:00
parent a1842e0226
commit 3dc80fa192
No known key found for this signature in database
GPG Key ID: 7744D0A113FFE5E1
4 changed files with 40 additions and 29 deletions

View File

@ -698,28 +698,6 @@ class CommandDispatcher:
"Numeric argument is too large for internal int "
"representation.")
def _yank_url(self, what):
"""Helper method for yank() to get the URL to copy."""
assert what in ['url', 'pretty-url'], what
if what == 'pretty-url':
flags = urlutils.FormatOption.DECODE_RESERVED
else:
flags = urlutils.FormatOption.ENCODED
flags |= urlutils.FormatOption.REMOVE_PASSWORD
url = QUrl(self._current_url())
url_query = QUrlQuery()
url_query_str = url.query()
if '&' not in url_query_str and ';' in url_query_str:
url_query.setQueryDelimiters('=', ';')
url_query.setQuery(url_query_str)
for key in dict(url_query.queryItems()):
if key in config.val.url.yank_ignored_parameters:
url_query.removeQueryItem(key)
url.setQuery(url_query)
return url.toString(flags)
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('what', choices=['selection', 'url', 'pretty-url',
'title', 'domain', 'inline'])
@ -754,7 +732,8 @@ class CommandDispatcher:
self._current_url().host(),
':' + str(port) if port > -1 else '')
elif what in ['url', 'pretty-url']:
s = self._yank_url(what)
url = self._current_url()
s = urlutils.get_url_yank_text(url, what == 'pretty-url')
what = 'URL' # For printing
elif what == 'selection':
def _selection_callback(s):

View File

@ -237,11 +237,7 @@ class HintActions:
sel = (context.target == Target.yank_primary and
utils.supports_selection())
flags = urlutils.FormatOption.ENCODED | urlutils.FormatOption.REMOVE_PASSWORD
if url.scheme() == 'mailto':
flags |= urlutils.FormatOption.REMOVE_SCHEME
urlstr = url.toString(flags)
urlstr = urlutils.get_url_yank_text(url, False)
new_content = urlstr
# only second and consecutive yanks are to append to the clipboard

View File

@ -14,7 +14,7 @@ import mimetypes
from typing import Optional, Tuple, Union, Iterable, cast
from qutebrowser.qt import machinery
from qutebrowser.qt.core import QUrl
from qutebrowser.qt.core import QUrl, QUrlQuery
from qutebrowser.qt.network import QHostInfo, QHostAddress, QNetworkProxy
from qutebrowser.api import cmdutils
@ -682,3 +682,25 @@ def widened_hostnames(hostname: str) -> Iterable[str]:
while hostname:
yield hostname
hostname = hostname.partition(".")[-1]
def get_url_yank_text(url: QUrl, is_pretty: bool) -> str:
"""Get the text that should be yanked for the given URL."""
flags = FormatOption.REMOVE_PASSWORD
if url.scheme() == 'mailto':
flags |= FormatOption.REMOVE_SCHEME
if is_pretty:
flags |= FormatOption.DECODE_RESERVED
else:
flags |= FormatOption.ENCODED
url_query = QUrlQuery()
url_query_str = url.query()
if '&' not in url_query_str and ';' in url_query_str:
url_query.setQueryDelimiters('=', ';')
url_query.setQuery(url_query_str)
for key in dict(url_query.queryItems()):
if key in config.val.url.yank_ignored_parameters:
url_query.removeQueryItem(key)
url.setQuery(url_query)
return url.toString(flags)

View File

@ -787,6 +787,20 @@ class TestParseJavascriptUrl:
assert parsed == source
@pytest.mark.parametrize('url, is_pretty, expected', [
(QUrl('https://example.com'), False, 'https://example.com'),
(QUrl('https://example.com/page'), False, 'https://example.com/page'),
(QUrl('ftp://example.com'), False, 'ftp://example.com'),
(QUrl('ftp://user:password@example.com'), False, 'ftp://user@example.com'),
(QUrl('https://example.com?ref=test'), False, 'https://example.com'),
(QUrl('mailto:email@example.com'), False, 'email@example.com'),
(QUrl('https://example.com/?pipe=%7C'), False, 'https://example.com/?pipe=%7C'),
(QUrl('https://example.com/?pipe=%7C'), True, 'https://example.com/?pipe=|'),
])
def test_get_url_yank_text(url, is_pretty, expected):
assert urlutils.get_url_yank_text(url, is_pretty) == expected
class TestWiden:
@pytest.mark.parametrize('hostname, expected', [