Skip to content

Commit 497b608

Browse files
kennygrantbradfitz
authored andcommitted
go/doc: allow : in godoc links
The emphasize function used a complex regexp to find URLs, which truncated some types of URL and did not match others. This has been simplified and adjusted to allow valid punctuation like :: or ! in the path part and :[] in the host part. Comments were added to clarify what this regexp allows. The path part matches query and fragment also so document this. Removed news, telnet, wais, and prospero protocols. Tests were added for: IPV6 URLs URLs surrounded by brackets URLs containing :: URLs containing :;!- in the path In order to allow punctuation and yet preserve current behaviour, URLs are not permitted to end in .,:;?! to allow the use of normal punctuation surrounding URLs in comments. Fixes golang#18139 Change-Id: I38b2d7a85fe0d171e4bf4aac420f8c2d3ced8a2f Reviewed-on: https://go-review.googlesource.com/37192 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent a4a3d63 commit 497b608

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

src/go/doc/comment.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,19 @@ const (
4848
identRx = `[\pL_][\pL_0-9]*`
4949

5050
// Regexp for URLs
51-
protocol = `https?|ftp|file|gopher|mailto|news|nntp|telnet|wais|prospero`
52-
hostPart = `[a-zA-Z0-9_@\-]+`
53-
filePart = `[a-zA-Z0-9_?%#~&/\-+=()]+` // parentheses may not be matching; see pairedParensPrefixLen
54-
urlRx = `(` + protocol + `)://` + // http://
55-
hostPart + `([.:]` + hostPart + `)*/?` + // //www.google.com:8080/
56-
filePart + `([:.,;]` + filePart + `)*`
51+
// Match parens, and check in pairedParensPrefixLen for balance - see #5043
52+
// Match .,:;?! within path, but not at end - see #18139, #16565
53+
// This excludes some rare yet valid urls ending in common punctuation
54+
// in order to allow sentences ending in URLs.
55+
56+
// protocol (required) e.g. http
57+
protoPart = `(https?|ftp|file|gopher|mailto|nntp)`
58+
// host (required) e.g. www.example.com or [::1]:8080
59+
hostPart = `([a-zA-Z0-9_@\-.\[\]:]+)`
60+
// path+query+fragment (optional) e.g. /path/index.html?q=foo#bar
61+
pathPart = `([.,:;?!]*[a-zA-Z0-9$'()*+&#=@~_/\-\[\]%])*`
62+
63+
urlRx = protoPart + `://` + hostPart + pathPart
5764
)
5865

5966
var matchRx = regexp.MustCompile(`(` + urlRx + `)|(` + identRx + `)`)

src/go/doc/comment_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ func TestToText(t *testing.T) {
150150
var emphasizeTests = []struct {
151151
in, out string
152152
}{
153+
{"http://[::1]:8080/foo.txt", `<a href="http://[::1]:8080/foo.txt">http://[::1]:8080/foo.txt</a>`},
154+
{"before (https://www.google.com) after", `before (<a href="https://www.google.com">https://www.google.com</a>) after`},
155+
{"before https://www.google.com:30/x/y/z:b::c. After", `before <a href="https://www.google.com:30/x/y/z:b::c">https://www.google.com:30/x/y/z:b::c</a>. After`},
156+
{"http://www.google.com/path/:;!-/?query=%34b#093124", `<a href="http://www.google.com/path/:;!-/?query=%34b#093124">http://www.google.com/path/:;!-/?query=%34b#093124</a>`},
157+
{"http://www.google.com/path/:;!-/?query=%34bar#093124", `<a href="http://www.google.com/path/:;!-/?query=%34bar#093124">http://www.google.com/path/:;!-/?query=%34bar#093124</a>`},
158+
{"http://www.google.com/index.html! After", `<a href="http://www.google.com/index.html">http://www.google.com/index.html</a>! After`},
153159
{"http://www.google.com/", `<a href="http://www.google.com/">http://www.google.com/</a>`},
154160
{"https://www.google.com/", `<a href="https://www.google.com/">https://www.google.com/</a>`},
155161
{"http://www.google.com/path.", `<a href="http://www.google.com/path">http://www.google.com/path</a>.`},

0 commit comments

Comments
 (0)