Commit c71b6767 authored by Robert Griesemer's avatar Robert Griesemer

godoc: position URLs (containing file names) must be quoted and URL escaped

Since the posLink_url also adds a non-URL attribute, the quoting and  URL-escaping
must happen inside posLink_url (otherwise the non-URL attribute becomes part or the
URL portion of the tag.

R=r
CC=golang-dev
https://golang.org/cl/4888041
parent 5c45e568
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
{{range .}} {{range .}}
{{/* Name is a string - no need for FSet */}} {{/* Name is a string - no need for FSet */}}
{{$name := html .Name}} {{$name := html .Name}}
<h2 id="{{$name}}">func <a href="/{{posLink .Decl $.FSet}}">{{$name}}</a></h2> <h2 id="{{$name}}">func <a href="/{{posLink_url .Decl $.FSet}}">{{$name}}</a></h2>
<p><code>{{node_html .Decl $.FSet}}</code></p> <p><code>{{node_html .Decl $.FSet}}</code></p>
{{comment_html .Doc}} {{comment_html .Doc}}
{{end}} {{end}}
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
{{with .Types}} {{with .Types}}
{{range .}} {{range .}}
{{$tname := node_html .Type.Name $.FSet}} {{$tname := node_html .Type.Name $.FSet}}
<h2 id="{{$tname}}">type <a href="/{{posLink .Decl $.FSet}}">{{$tname}}</a></h2> <h2 id="{{$tname}}">type <a href="/{{posLink_url .Decl $.FSet}}">{{$tname}}</a></h2>
{{comment_html .Doc}} {{comment_html .Doc}}
<p><pre>{{node_html .Decl $.FSet}}</pre></p> <p><pre>{{node_html .Decl $.FSet}}</pre></p>
{{range .Consts}} {{range .Consts}}
...@@ -63,13 +63,13 @@ ...@@ -63,13 +63,13 @@
{{end}} {{end}}
{{range .Factories}} {{range .Factories}}
{{$name := html .Name}} {{$name := html .Name}}
<h3 id="{{$tname}}.{{$name}}">func <a href="/{{posLink .Decl $.FSet}}">{{$name}}</a></h3> <h3 id="{{$tname}}.{{$name}}">func <a href="/{{posLink_url .Decl $.FSet}}">{{$name}}</a></h3>
<p><code>{{node_html .Decl $.FSet}}</code></p> <p><code>{{node_html .Decl $.FSet}}</code></p>
{{comment_html .Doc}} {{comment_html .Doc}}
{{end}} {{end}}
{{range .Methods}} {{range .Methods}}
{{$name := html .Name}} {{$name := html .Name}}
<h3 id="{{$tname}}.{{$name}}">func ({{node_html .Recv $.FSet}}) <a href="/{{posLink .Decl $.FSet}}">{{$name}}</a></h3> <h3 id="{{$tname}}.{{$name}}">func ({{node_html .Recv $.FSet}}) <a href="/{{posLink_url .Decl $.FSet}}">{{$name}}</a></h3>
<p><code>{{node_html .Decl $.FSet}}</code></p> <p><code>{{node_html .Decl $.FSet}}</code></p>
{{comment_html .Doc}} {{comment_html .Doc}}
{{end}} {{end}}
......
...@@ -465,7 +465,7 @@ func pkgLinkFunc(path string) string { ...@@ -465,7 +465,7 @@ func pkgLinkFunc(path string) string {
return pkgHandler.pattern[1:] + relpath // remove trailing '/' for relative URL return pkgHandler.pattern[1:] + relpath // remove trailing '/' for relative URL
} }
func posLinkFunc(node ast.Node, fset *token.FileSet) string { func posLink_urlFunc(node ast.Node, fset *token.FileSet) string {
var relpath string var relpath string
var line int var line int
var low, high int // selection var low, high int // selection
...@@ -481,10 +481,10 @@ func posLinkFunc(node ast.Node, fset *token.FileSet) string { ...@@ -481,10 +481,10 @@ func posLinkFunc(node ast.Node, fset *token.FileSet) string {
} }
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString(relpath) buf.WriteString(http.URLEscape(relpath))
// selection ranges are of form "s=low:high" // selection ranges are of form "s=low:high"
if low < high { if low < high {
fmt.Fprintf(&buf, "?s=%d:%d", low, high) fmt.Fprintf(&buf, "?s=%d:%d", low, high) // no need for URL escaping
// if we have a selection, position the page // if we have a selection, position the page
// such that the selection is a bit below the top // such that the selection is a bit below the top
line -= 10 line -= 10
...@@ -495,16 +495,16 @@ func posLinkFunc(node ast.Node, fset *token.FileSet) string { ...@@ -495,16 +495,16 @@ func posLinkFunc(node ast.Node, fset *token.FileSet) string {
// line id's in html-printed source are of the // line id's in html-printed source are of the
// form "L%d" where %d stands for the line number // form "L%d" where %d stands for the line number
if line > 0 { if line > 0 {
fmt.Fprintf(&buf, "#L%d", line) fmt.Fprintf(&buf, "#L%d", line) // no need for URL escaping
} }
return buf.String() return buf.String()
} }
// fmap describes the template functions installed with all godoc templates. // fmap describes the template functions installed with all godoc templates.
// Convention: template function names ending in "_html" produce an HTML- // Convention: template function names ending in "_html" or "_url" produce
// escaped string; all other function results may require HTML // HTML- or URL-escaped strings; all other function results may
// or URL escaping in the template. // require explicit escaping in the template.
var fmap = template.FuncMap{ var fmap = template.FuncMap{
// various helpers // various helpers
"filename": filenameFunc, "filename": filenameFunc,
...@@ -525,9 +525,9 @@ var fmap = template.FuncMap{ ...@@ -525,9 +525,9 @@ var fmap = template.FuncMap{
"comment_html": comment_htmlFunc, "comment_html": comment_htmlFunc,
// support for URL attributes // support for URL attributes
"pkgLink": pkgLinkFunc, "pkgLink": pkgLinkFunc,
"srcLink": relativeURL, "srcLink": relativeURL,
"posLink": posLinkFunc, "posLink_url": posLink_urlFunc,
} }
func readTemplate(name string) *template.Template { func readTemplate(name string) *template.Template {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment