godoc: make struct fields linkable in HTML mode
This adds <span id="StructName.FieldName"> elements around
field names, starting at the comment if present, so people
can link to /pkg/somepkg/#SomeStruct.SomeField.
Fixes golang/go#16753
Change-Id: I4a8b30605d18e9e33e3d42f273a95067ac491438
Reviewed-on: https://go-review.googlesource.com/33690
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
diff --git a/godoc/godoc_test.go b/godoc/godoc_test.go
index a10a1ab..ce57d99 100644
--- a/godoc/godoc_test.go
+++ b/godoc/godoc_test.go
@@ -5,6 +5,9 @@
package godoc
import (
+ "go/ast"
+ "go/parser"
+ "go/token"
"testing"
)
@@ -116,3 +119,40 @@
}
}
}
+
+// Test that we add <span id="StructName.FieldName"> elements
+// to the HTML of struct fields.
+func TestStructFieldsIDAttributes(t *testing.T) {
+ p := &Presentation{
+ DeclLinks: true,
+ }
+ src := []byte(`
+package foo
+
+type T struct {
+ NoDoc string
+
+ // Doc has a comment.
+ Doc string
+}
+`)
+ fset := token.NewFileSet()
+ af, err := parser.ParseFile(fset, "foo.go", src, parser.ParseComments)
+ if err != nil {
+ t.Fatal(err)
+ }
+ genDecl := af.Decls[0].(*ast.GenDecl)
+ pi := &PageInfo{
+ FSet: fset,
+ }
+ got := p.node_htmlFunc(pi, genDecl, true)
+ want := `type T struct {
+<span id="T.NoDoc">NoDoc</span> <a href="/pkg/builtin/#string">string</a>
+
+<span class="comment"><span id="T.Doc">// Doc </span>has a comment.</span>
+Doc <a href="/pkg/builtin/#string">string</a>
+}`
+ if got != want {
+ t.Errorf(" got: %q\nwant: %q\n", got, want)
+ }
+}