From 2b684d7afc4b4281f682156abe7c8f045da67dec Mon Sep 17 00:00:00 2001 From: dece Date: Sat, 15 May 2021 18:06:04 +0200 Subject: [PATCH] navigation: fix issue with reloading bebop pages --- BOARD.txt | 1 - bebop/browser/browser.py | 17 ++++++++++++++--- bebop/navigation.py | 11 +++++++++-- bebop/tests/test_navigation.py | 6 ++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/BOARD.txt b/BOARD.txt index e01b3be..b45c355 100644 --- a/BOARD.txt +++ b/BOARD.txt @@ -27,7 +27,6 @@ BACKLOG -------------------------------------------------------------------------------- bug: can't input unicode click on links to open them -bug: can't reload bebop: pages bug: exiting editor breaks curses dumb rendering mode per site well, preferences per site maybe? diff --git a/bebop/browser/browser.py b/bebop/browser/browser.py index e8b39f8..696d04e 100644 --- a/bebop/browser/browser.py +++ b/bebop/browser/browser.py @@ -12,7 +12,9 @@ from pathlib import Path from typing import Dict, Optional, Tuple from bebop.bookmarks import ( - get_bookmarks_path, get_bookmarks_document, save_bookmark + get_bookmarks_path, + get_bookmarks_document, + save_bookmark, ) from bebop.colors import ColorPair, init_colors from bebop.command_line import CommandLine @@ -25,7 +27,12 @@ from bebop.links import Links from bebop.mime import MimeType from bebop.mouse import ButtonState from bebop.navigation import ( - get_parent_url, get_root_url, join_url, parse_url, unparse_url + NO_NETLOC_SCHEMES, + get_parent_url, + get_root_url, + join_url, + parse_url, + unparse_url, ) from bebop.page import Page from bebop.page_pad import PagePad @@ -346,7 +353,11 @@ class Browser: parts = parse_url(url, default_scheme=current_scheme) # If there is a no netloc part, try to join the URL. - if parts["netloc"] is None and parts["scheme"] == current_scheme: + if ( + parts["netloc"] is None + and parts["scheme"] == current_scheme + and parts["scheme"] not in NO_NETLOC_SCHEMES + ): base_url = base_url or self.current_url if base_url: parts = parse_url(join_url(base_url, url)) diff --git a/bebop/navigation.py b/bebop/navigation.py index 1258c4d..6428748 100644 --- a/bebop/navigation.py +++ b/bebop/navigation.py @@ -20,6 +20,8 @@ URI_RE = re.compile( "$" ) +NO_NETLOC_SCHEMES = ("bebop",) + class InvalidUrlException(Exception): """Generic exception for invalid URLs used in this module.""" @@ -45,7 +47,8 @@ def parse_url( - absolute: assume the URL is absolute, e.g. in the case we are trying to parse an URL an user has written, which is most of the time an absolute URL even if not perfectly so. This only has an effect if, after the - initial parsing, there is no netloc available. + initial parsing, there is no netloc available and if there is no scheme + that is known to not have a netloc (i.e. the dummy "bebop" scheme). - default_scheme: specify the scheme to use if the URL either does not specify it and we need it (e.g. there is a location), or `absolute` is true; if absolute is true but `default_scheme` is not specified, a netloc @@ -71,7 +74,11 @@ def parse_url( # Smol hack: if we assume it's an absolute URL and no netloc has been found, # just prefix default scheme (if any) and "//". - if absolute and not parts["netloc"]: + if ( + absolute + and not parts["netloc"] + and parts["scheme"] not in NO_NETLOC_SCHEMES + ): scheme = parts["scheme"] or default_scheme prefix = scheme + "://" if scheme else "//" return parse_url(prefix + url) diff --git a/bebop/tests/test_navigation.py b/bebop/tests/test_navigation.py index de18c2d..3965d58 100644 --- a/bebop/tests/test_navigation.py +++ b/bebop/tests/test_navigation.py @@ -50,6 +50,12 @@ class TestNavigation(unittest.TestCase): self.assertEqual(res["scheme"], "file") self.assertEqual(res["path"], "/home/dece/gemini/index.gmi") + # Bebop scheme. + res = parse_url("bebop:welcome") + self.assertEqual(res["scheme"], "bebop") + self.assertIsNone(res["netloc"]) + self.assertEqual(res["path"], "welcome") + def test_join_url(self): url = join_url("gemini://dece.space/", "some-file.gmi") self.assertEqual(url, "gemini://dece.space/some-file.gmi")