go.tools/present: links with the prefix "javascript:" should be executed in the same window.

Using url.Parse instead of more ad hoc solution.

R=r
CC=golang-codereviews
https://golang.org/cl/47320043
This commit is contained in:
Francesc Campoy 2014-01-03 14:51:34 -08:00
parent 822669c658
commit 5f5d42a466
1 changed files with 7 additions and 4 deletions

View File

@ -6,6 +6,7 @@ package present
import ( import (
"fmt" "fmt"
"log"
"net/url" "net/url"
"strings" "strings"
) )
@ -40,18 +41,20 @@ func parseLink(ctx *Context, fileName string, lineno int, text string) (Elem, er
return Link{url, label}, nil return Link{url, label}, nil
} }
func renderLink(url, text string) string { func renderLink(href, text string) string {
text = font(text) text = font(text)
if text == "" { if text == "" {
text = url text = href
} }
// Open links in new window only when their url is absolute. // Open links in new window only when their url is absolute.
target := "_blank" target := "_blank"
if url[0] == '/' { if u, err := url.Parse(href); err != nil {
log.Println("rendernLink parsing url: %v", err)
} else if !u.IsAbs() || u.Scheme == "javascript" {
target = "_self" target = "_self"
} }
return fmt.Sprintf(`<a href="%s" target="%s">%s</a>`, url, target, text) return fmt.Sprintf(`<a href="%s" target="%s">%s</a>`, href, target, text)
} }
// parseInlineLink parses an inline link at the start of s, and returns // parseInlineLink parses an inline link at the start of s, and returns