summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authormrsdizzie <joe.mccann@gmail.com>2019-02-28 07:31:53 -0500
committerLunny Xiao <xiaolunwen@gmail.com>2019-02-28 20:31:53 +0800
commit4a2e92bcd1060ba0c124732690fb2f51c7f9d79d (patch)
treea0d30850da4b57d32ebfad18b76ff09cc4ade2ce /modules
parent4b7237b63ee4b5b1892ac6fa14afcd29568147fb (diff)
downloadgitea-4a2e92bcd1060ba0c124732690fb2f51c7f9d79d.tar.gz
gitea-4a2e92bcd1060ba0c124732690fb2f51c7f9d79d.zip
Modify linkRegex to require http|https (#6171)
Modify the current linkRegex to require http|https which appears to be the intended behavior based on the comments. Right now, it also matches anything starting with www as well. Also add testing for linkRegex
Diffstat (limited to 'modules')
-rw-r--r--modules/markup/html.go2
-rw-r--r--modules/markup/html_test.go62
2 files changed, 63 insertions, 1 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go
index 9040d43e8c..8ce8740748 100644
--- a/modules/markup/html.go
+++ b/modules/markup/html.go
@@ -66,7 +66,7 @@ var (
// matches http/https links. used for autlinking those. partly modified from
// the original present in autolink.js
- linkRegex = regexp.MustCompile(`(?:(?:http|https):\/\/(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+:=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?`)
+ linkRegex = regexp.MustCompile(`(?:(?:http|https):\/\/(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+(?:\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+:=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?`)
)
// regexp for full links to issues/pulls
diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go
index bf7606e1da..f17d00cd67 100644
--- a/modules/markup/html_test.go
+++ b/modules/markup/html_test.go
@@ -67,6 +67,68 @@ func TestMisc_IsSameDomain(t *testing.T) {
assert.False(t, IsSameDomain("favicon.ico"))
}
+func TestRender_links(t *testing.T) {
+ setting.AppURL = AppURL
+ setting.AppSubURL = AppSubURL
+
+ test := func(input, expected string) {
+ buffer := RenderString("a.md", input, setting.AppSubURL, nil)
+ assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
+ }
+ // Text that should be turned into URL
+
+ test(
+ "https://www.example.com",
+ `<p><a href="https://www.example.com" rel="nofollow">https://www.example.com</a></p>`)
+ test(
+ "http://www.example.com",
+ `<p><a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>`)
+ test(
+ "https://example.com",
+ `<p><a href="https://example.com" rel="nofollow">https://example.com</a></p>`)
+ test(
+ "http://example.com",
+ `<p><a href="http://example.com" rel="nofollow">http://example.com</a></p>`)
+ test(
+ "http://foo.com/blah_blah",
+ `<p><a href="http://foo.com/blah_blah" rel="nofollow">http://foo.com/blah_blah</a></p>`)
+ test(
+ "http://foo.com/blah_blah/",
+ `<p><a href="http://foo.com/blah_blah/" rel="nofollow">http://foo.com/blah_blah/</a></p>`)
+ test(
+ "http://www.example.com/wpstyle/?p=364",
+ `<p><a href="http://www.example.com/wpstyle/?p=364" rel="nofollow">http://www.example.com/wpstyle/?p=364</a></p>`)
+ test(
+ "https://www.example.com/foo/?bar=baz&inga=42&quux",
+ `<p><a href="https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux" rel="nofollow">https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux</a></p>`)
+ test(
+ "http://142.42.1.1/",
+ `<p><a href="http://142.42.1.1/" rel="nofollow">http://142.42.1.1/</a></p>`)
+
+ // Test that should *not* be turned into URL
+ test(
+ "www.example.com",
+ `<p>www.example.com</p>`)
+ test(
+ "example.com",
+ `<p>example.com</p>`)
+ test(
+ "test.example.com",
+ `<p>test.example.com</p>`)
+ test(
+ "http://",
+ `<p>http://</p>`)
+ test(
+ "https://",
+ `<p>https://</p>`)
+ test(
+ "://",
+ `<p>://</p>`)
+ test(
+ "www",
+ `<p>www</p>`)
+}
+
func TestRender_ShortLinks(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL