]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc: Fix URL linking in ToHTML.
authorGary Burd <gary@beagledreams.com>
Fri, 3 Feb 2012 01:02:05 +0000 (17:02 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 3 Feb 2012 01:02:05 +0000 (17:02 -0800)
Fixes issue: 2832

R=rsc, gri
CC=golang-dev
https://golang.org/cl/5616060

src/pkg/go/doc/comment.go
src/pkg/go/doc/comment_test.go

index e50cdd86e69a919d129c27d91fbc0058a1fa87ae..6f0edd4bade3b7fb78dcfe148d018cef9ef89ba5 100644 (file)
@@ -56,7 +56,7 @@ const (
                filePart + `([:.,]` + filePart + `)*`
 )
 
-var matchRx = regexp.MustCompile(`(` + identRx + `)|(` + urlRx + `)`)
+var matchRx = regexp.MustCompile(`(` + urlRx + `)|(` + identRx + `)`)
 
 var (
        html_a      = []byte(`<a href="`)
@@ -87,7 +87,7 @@ func emphasize(w io.Writer, line string, words map[string]string, nice bool) {
                if m == nil {
                        break
                }
-               // m >= 6 (two parenthesized sub-regexps in matchRx, 1st one is identRx)
+               // m >= 6 (two parenthesized sub-regexps in matchRx, 1st one is urlRx)
 
                // write text before match
                commentEscape(w, line[0:m[0]], nice)
@@ -99,8 +99,8 @@ func emphasize(w io.Writer, line string, words map[string]string, nice bool) {
                if words != nil {
                        url, italics = words[string(match)]
                }
-               if m[2] < 0 {
-                       // didn't match against first parenthesized sub-regexp; must be match against urlRx
+               if m[2] >= 0 {
+                       // match against first parenthesized sub-regexp; must be match against urlRx
                        if !italics {
                                // no alternative URL in words list, use match instead
                                url = string(match)
index e8d7f2e4b0f4d95107c478cd392609cf3b4a5ba4..aa21b8d1b30765e8699445f97f8f105f1ee19721 100644 (file)
@@ -5,6 +5,7 @@
 package doc
 
 import (
+       "bytes"
        "reflect"
        "testing"
 )
@@ -81,3 +82,28 @@ func TestBlocks(t *testing.T) {
                }
        }
 }
+
+var emphasizeTests = []struct {
+       in  string
+       out string
+}{
+       {"http://www.google.com/", `<a href="http://www.google.com/">http://www.google.com/</a>`},
+       {"https://www.google.com/", `<a href="https://www.google.com/">https://www.google.com/</a>`},
+       {"http://www.google.com/path.", `<a href="http://www.google.com/path">http://www.google.com/path</a>.`},
+       {"(http://www.google.com/)", `(<a href="http://www.google.com/">http://www.google.com/</a>)`},
+       {"Foo bar http://example.com/ quux!", `Foo bar <a href="http://example.com/">http://example.com/</a> quux!`},
+       {"Hello http://example.com/%2f/ /world.", `Hello <a href="http://example.com/%2f/">http://example.com/%2f/</a> /world.`},
+       {"Lorem http: ipsum //host/path", "Lorem http: ipsum //host/path"},
+       {"javascript://is/not/linked", "javascript://is/not/linked"},
+}
+
+func TestEmphasize(t *testing.T) {
+       for i, tt := range emphasizeTests {
+               var buf bytes.Buffer
+               emphasize(&buf, tt.in, nil, true)
+               out := buf.String()
+               if out != tt.out {
+                       t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, out, tt.out)
+               }
+       }
+}