diff --git a/CHANGELOG.md b/CHANGELOG.md index db01a56..8a74670 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Two digit (and higher) link texts are now in line with one digit ones (#60) - Race condition when reloading pages, could have caused the cache to still be used - Prevent panic (crash) when the server sends an error with an empty meta string (#73) +- URLs with with colon-only schemes (like `mailto:`) are properly recognized ## [1.4.0] - 2020-07-28 diff --git a/display/display.go b/display/display.go index 353f68c..76c05ec 100644 --- a/display/display.go +++ b/display/display.go @@ -549,6 +549,10 @@ func URL(u string) { return } + if !strings.HasPrefix(u, "//") && !strings.HasPrefix(u, "gemini://") && !strings.Contains(u, "://") { + // Assume it's a Gemini URL + u = "gemini://" + u + } go goURL(tabs[curTab], u) } diff --git a/display/util.go b/display/util.go index 4601626..3a70451 100644 --- a/display/util.go +++ b/display/util.go @@ -83,13 +83,6 @@ func normalizeURL(u string) string { return u } - if !strings.Contains(u, "://") && !strings.HasPrefix(u, "//") { - // No scheme at all in the URL - parsed, err = url.Parse("gemini://" + u) - if err != nil { - return u - } - } if parsed.Scheme == "" { // Always add scheme parsed.Scheme = "gemini" diff --git a/display/util_test.go b/display/util_test.go new file mode 100644 index 0000000..4cc3506 --- /dev/null +++ b/display/util_test.go @@ -0,0 +1,33 @@ +package display + +import ( + "testing" +) + +var normalizeURLTests = []struct { + u string + expected string +}{ + {"gemini://example.com:1965/", "gemini://example.com/"}, + {"gemini://example.com", "gemini://example.com/"}, + {"//example.com", "gemini://example.com/"}, + {"//example.com:1965", "gemini://example.com/"}, + {"//example.com:123/", "gemini://example.com:123/"}, + {"gemini://example.com/", "gemini://example.com/"}, + {"gemini://example.com/#fragment", "gemini://example.com/"}, + {"gemini://example.com#fragment", "gemini://example.com/"}, + {"gemini://user@example.com/", "gemini://example.com/"}, + // Other schemes, URL isn't modified + {"mailto:example@example.com", "mailto:example@example.com"}, + {"magnet:?xt=urn:btih:test", "magnet:?xt=urn:btih:test"}, + {"https://example.com", "https://example.com"}, +} + +func TestNormalizeURL(t *testing.T) { + for _, tt := range normalizeURLTests { + actual := normalizeURL(tt.u) + if actual != tt.expected { + t.Errorf("normalizeURL(%s): expected %s, actual %s", tt.u, tt.expected, actual) + } + } +} diff --git a/go.sum b/go.sum index b9cb5e1..c0f0870 100644 --- a/go.sum +++ b/go.sum @@ -203,6 +203,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.7.0 h1:xVKxvI7ouOI5I+U9s2eeiUfMaWBVoXA3AWskkrqK0VM= github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=