Skip to content

Commit 4a2e92b

Browse files
mrsdizzielunny
authored andcommitted
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
1 parent 4b7237b commit 4a2e92b

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

modules/markup/html.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var (
6666

6767
// matches http/https links. used for autlinking those. partly modified from
6868
// the original present in autolink.js
69-
linkRegex = regexp.MustCompile(`(?:(?:http|https):\/\/(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+:=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?`)
69+
linkRegex = regexp.MustCompile(`(?:(?:http|https):\/\/(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+(?:\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+:=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?`)
7070
)
7171

7272
// regexp for full links to issues/pulls

modules/markup/html_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,68 @@ func TestMisc_IsSameDomain(t *testing.T) {
6767
assert.False(t, IsSameDomain("favicon.ico"))
6868
}
6969

70+
func TestRender_links(t *testing.T) {
71+
setting.AppURL = AppURL
72+
setting.AppSubURL = AppSubURL
73+
74+
test := func(input, expected string) {
75+
buffer := RenderString("a.md", input, setting.AppSubURL, nil)
76+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
77+
}
78+
// Text that should be turned into URL
79+
80+
test(
81+
"https://www.example.com",
82+
`<p><a href="https://www.example.com" rel="nofollow">https://www.example.com</a></p>`)
83+
test(
84+
"http://www.example.com",
85+
`<p><a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>`)
86+
test(
87+
"https://example.com",
88+
`<p><a href="https://example.com" rel="nofollow">https://example.com</a></p>`)
89+
test(
90+
"http://example.com",
91+
`<p><a href="http://example.com" rel="nofollow">http://example.com</a></p>`)
92+
test(
93+
"http://foo.com/blah_blah",
94+
`<p><a href="http://foo.com/blah_blah" rel="nofollow">http://foo.com/blah_blah</a></p>`)
95+
test(
96+
"http://foo.com/blah_blah/",
97+
`<p><a href="http://foo.com/blah_blah/" rel="nofollow">http://foo.com/blah_blah/</a></p>`)
98+
test(
99+
"http://www.example.com/wpstyle/?p=364",
100+
`<p><a href="http://www.example.com/wpstyle/?p=364" rel="nofollow">http://www.example.com/wpstyle/?p=364</a></p>`)
101+
test(
102+
"https://www.example.com/foo/?bar=baz&inga=42&quux",
103+
`<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>`)
104+
test(
105+
"http://142.42.1.1/",
106+
`<p><a href="http://142.42.1.1/" rel="nofollow">http://142.42.1.1/</a></p>`)
107+
108+
// Test that should *not* be turned into URL
109+
test(
110+
"www.example.com",
111+
`<p>www.example.com</p>`)
112+
test(
113+
"example.com",
114+
`<p>example.com</p>`)
115+
test(
116+
"test.example.com",
117+
`<p>test.example.com</p>`)
118+
test(
119+
"http://",
120+
`<p>http://</p>`)
121+
test(
122+
"https://",
123+
`<p>https://</p>`)
124+
test(
125+
"://",
126+
`<p>://</p>`)
127+
test(
128+
"www",
129+
`<p>www</p>`)
130+
}
131+
70132
func TestRender_ShortLinks(t *testing.T) {
71133
setting.AppURL = AppURL
72134
setting.AppSubURL = AppSubURL

0 commit comments

Comments
 (0)