本文整理汇总了Golang中html/template.HTMLEscape函数的典型用法代码示例。如果您正苦于以下问题:Golang HTMLEscape函数的具体用法?Golang HTMLEscape怎么用?Golang HTMLEscape使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HTMLEscape函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: formatCode
func formatCode(src []byte, annotations []doc.TypeAnnotation) htemp.HTML {
// Collect comment positions.
var (
comments []doc.TypeAnnotation
s scanner.Scanner
)
fset := token.NewFileSet()
file := fset.AddFile("", fset.Base(), len(src))
s.Init(file, src, nil, scanner.ScanComments)
commentLoop:
for {
pos, tok, lit := s.Scan()
switch tok {
case token.EOF:
break commentLoop
case token.COMMENT:
p := file.Offset(pos)
comments = append(comments, doc.TypeAnnotation{Pos: p, End: p + len(lit)})
}
}
// Merge type annotations and comments without modifying the caller's slice
// of annoations.
switch {
case len(comments) == 0:
// nothing to do
case len(annotations) == 0:
annotations = comments
default:
annotations = append(comments, annotations...)
sort.Sort(sortByPos(annotations))
}
var buf bytes.Buffer
last := 0
for _, a := range annotations {
htemp.HTMLEscape(&buf, src[last:a.Pos])
if a.Name != "" {
p := a.ImportPath
if p != "" {
p = "/" + p
}
buf.WriteString(`<a href="`)
buf.WriteString(escapePath(p))
buf.WriteByte('#')
buf.WriteString(escapePath(a.Name))
buf.WriteString(`">`)
htemp.HTMLEscape(&buf, src[a.Pos:a.End])
buf.WriteString(`</a>`)
} else {
buf.WriteString(`<span class="com">`)
htemp.HTMLEscape(&buf, src[a.Pos:a.End])
buf.WriteString(`</span>`)
}
last = a.End
}
htemp.HTMLEscape(&buf, src[last:])
return htemp.HTML(buf.String())
}
示例2: renderElement
func renderElement(element interface{}, contextChain []interface{}, buf io.Writer) error {
switch elem := element.(type) {
case *textElement:
buf.Write(elem.text)
case *varElement:
defer func() {
if r := recover(); r != nil {
fmt.Printf("Panic while looking up %q: %s\n", elem.name, r)
}
}()
val, err := lookup(contextChain, elem.name, AllowMissingVariables)
if err != nil {
return err
}
if val.IsValid() {
if elem.raw {
fmt.Fprint(buf, val.Interface())
} else {
s := fmt.Sprint(val.Interface())
template.HTMLEscape(buf, []byte(s))
}
}
case *sectionElement:
if err := renderSection(elem, contextChain, buf); err != nil {
return err
}
case *Template:
if err := elem.renderTemplate(contextChain, buf); err != nil {
return err
}
}
return nil
}
示例3: login
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
crutime := time.Now().Unix()
fmt.Println("crutime = ", crutime)
h := md5.New()
s := strconv.FormatInt(crutime, 10)
fmt.Println("s = ", s)
io.WriteString(h, s)
fmt.Println("h's md5 = ", h.Sum(nil))
token := fmt.Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, token)
} else {
r.ParseForm()
token := r.Form.Get("token")
if token != "" {
fmt.Println("token is ", token)
} else {
fmt.Println("token is not exists ")
}
fmt.Println("username length:", len(r.Form["username"][0]))
fmt.Println("username:", template.HTMLEscapeString(r.Form.Get("username")))
fmt.Println("password:", template.HTMLEscapeString(r.Form.Get("password")))
template.HTMLEscape(w, []byte(r.Form.Get("username")))
}
}
示例4: Example_escape
func Example_escape() {
const s = `"Fran & Freddie's Diner" <[email protected]>`
v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<[email protected]>`}
fmt.Println(template.HTMLEscapeString(s))
template.HTMLEscape(os.Stdout, []byte(s))
fmt.Fprintln(os.Stdout, "")
fmt.Println(template.HTMLEscaper(v...))
fmt.Println(template.JSEscapeString(s))
template.JSEscape(os.Stdout, []byte(s))
fmt.Fprintln(os.Stdout, "")
fmt.Println(template.JSEscaper(v...))
fmt.Println(template.URLQueryEscaper(v...))
// Output:
// "Fran & Freddie's Diner" <[email protected]>
// "Fran & Freddie's Diner" <[email protected]>
// "Fran & Freddie's Diner"32<[email protected]>
// \"Fran & Freddie\'s Diner\" \[email protected]\x3E
// \"Fran & Freddie\'s Diner\" \[email protected]\x3E
// \"Fran & Freddie\'s Diner\"32\[email protected]\x3E
// %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E
}
示例5: login
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method: ", r.Method)
if r.Method == "GET" {
curtime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(curtime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("login.html")
t.Execute(w, token)
} else {
r.ParseForm()
token := r.Form.Get("token")
if token != "" {
fmt.Println("token is ok")
} else {
fmt.Println("token is error")
}
slice := []string{"apple", "pear", "banana"}
log.Println(r.Form.Get("fruit"))
for _, v := range slice {
if v == r.Form.Get("fruit") {
fmt.Println(v)
}
}
log.Println("username: ", r.Form["username"])
log.Println("password: ", r.Form["password"])
template.HTMLEscape(w, []byte(r.Form.Get("username")))
}
}
示例6: renderElement
func renderElement(element interface{}, contextChain []interface{}, buf io.Writer) {
switch elem := element.(type) {
case string:
buf.Write([]byte(element.(string)))
case *textElement:
buf.Write(elem.text)
case *varElement:
defer func() {
if r := recover(); r != nil {
fmt.Printf("Panic while looking up %q: %s\n", elem.name, r)
}
}()
val := lookup(contextChain, elem.name)
if val.IsValid() {
if elem.raw {
fmt.Fprint(buf, val.Interface())
} else {
s := fmt.Sprint(val.Interface())
template.HTMLEscape(buf, []byte(s))
}
}
case *sectionElement:
renderSection(elem, contextChain, buf)
case *Template:
elem.renderTemplate(contextChain, buf)
}
}
示例7: login
func login(w ResponseWriter, r *Request) {
Println("方法:", r.Method)
if r.Method == "GET" {
crutime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(crutime, 10))
token := Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("login.html")
t.Execute(w, token)
} else {
r.ParseForm()
token := r.Form.Get("token")
if token != "" {
Println("标识:", token)
// 验证合法性
} else {
Println("标识:未获取")
// 报错
}
Println(r)
Println("用户名长度:", len(r.Form["username"][0]))
Println("用户名:", template.HTMLEscapeString(r.Form.Get("username")))
Println("密码:", template.HTMLEscapeString(r.Form.Get("password")))
template.HTMLEscape(w, []byte(r.Form.Get("username")))
}
}
示例8: login
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method: ", r.Method)
if r.Method == "GET" {
cruTime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(cruTime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("04-02-03-duplicate-prevention.gtpl")
t.Execute(w, token)
} else {
// log in request
r.ParseForm()
token := r.Form.Get("token")
if token != "" {
// check token validity
fmt.Println("TODO: check if the token is valid: %s\n", token)
} else {
// give error if no token
fmt.Println("TODO: handle error as token is not valid!")
}
fmt.Printf("Username length: %v\n", len(r.Form["username"][0]))
fmt.Printf("Username : %v\n", template.HTMLEscapeString(r.Form.Get("username")))
fmt.Printf("password : %v\n", template.HTMLEscapeString(r.Form.Get("password")))
template.HTMLEscape(w, []byte(r.Form.Get("username")))
}
}
示例9: login
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("Method", r.Method)
if r.Method == "GET" {
crutime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(crutime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, token)
} else {
r.ParseForm()
token := r.Form.Get("token")
if token != "" {
// check token validity
} else {
// give error if no token
}
fmt.Println("username length:", len(r.Form["username"][0]))
fmt.Println("username:", template.HTMLEscapeString(r.Form.Get("username")))
fmt.Println("password:", template.HTMLEscapeString(r.Form.Get("password")))
template.HTMLEscape(w, []byte(r.Form.Get("username")))
}
}
示例10: login
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //获取请求的方法
if r.Method == "GET" {
crutime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(crutime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
fmt.Println("token", token)
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, token)
} else {
//请求的是登陆数据,那么执行登陆的逻辑判断
r.ParseForm()
token := r.Form.Get("token")
if token != "" {
//验证 token 的合法性
} else {
//不存在 token 报错
}
fmt.Println("username length:", len(r.Form["username"][0]))
fmt.Println("username:",
template.HTMLEscapeString(r.Form.Get("username"))) //输出到服务器端
fmt.Println("password:",
template.HTMLEscapeString(r.Form.Get("password")))
template.HTMLEscape(w, []byte(r.Form.Get("username"))) //输出到客户端
}
}
示例11: writeText
// Write text to w; optionally html-escaped.
func writeText(w io.Writer, text []byte, html bool) {
if html {
template.HTMLEscape(w, text)
return
}
w.Write(text)
}
示例12: codeFn
func codeFn(c doc.Code, typ *doc.Type) htemp.HTML {
var buf bytes.Buffer
last := 0
src := []byte(c.Text)
for _, a := range c.Annotations {
htemp.HTMLEscape(&buf, src[last:a.Pos])
switch a.Kind {
case doc.PackageLinkAnnotation:
p := "/" + c.Paths[a.PathIndex]
buf.WriteString(`<a href="`)
buf.WriteString(escapePath(p))
buf.WriteString(`">`)
htemp.HTMLEscape(&buf, src[a.Pos:a.End])
buf.WriteString(`</a>`)
case doc.ExportLinkAnnotation, doc.BuiltinAnnotation:
var p string
if a.Kind == doc.BuiltinAnnotation {
p = "/builtin"
} else if a.PathIndex >= 0 {
p = "/" + c.Paths[a.PathIndex]
}
n := src[a.Pos:a.End]
n = n[bytes.LastIndex(n, period)+1:]
buf.WriteString(`<a href="`)
buf.WriteString(escapePath(p))
buf.WriteByte('#')
buf.WriteString(escapePath(string(n)))
buf.WriteString(`">`)
htemp.HTMLEscape(&buf, src[a.Pos:a.End])
buf.WriteString(`</a>`)
case doc.CommentAnnotation:
buf.WriteString(`<span class="com">`)
htemp.HTMLEscape(&buf, src[a.Pos:a.End])
buf.WriteString(`</span>`)
case doc.AnchorAnnotation:
buf.WriteString(`<span id="`)
if typ != nil {
htemp.HTMLEscape(&buf, []byte(typ.Name))
buf.WriteByte('.')
}
htemp.HTMLEscape(&buf, src[a.Pos:a.End])
buf.WriteString(`">`)
htemp.HTMLEscape(&buf, src[a.Pos:a.End])
buf.WriteString(`</span>`)
default:
htemp.HTMLEscape(&buf, src[a.Pos:a.End])
}
last = int(a.End)
}
htemp.HTMLEscape(&buf, src[last:])
return htemp.HTML(buf.String())
}
示例13: TestHTMLEscape
func TestHTMLEscape(t *testing.T) {
const s = `"Fran & Freddie's Diner" <[email protected]>`
v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<[email protected]>`}
fmt.Println(template.HTMLEscapeString(s))
template.HTMLEscape(os.Stdout, []byte(s))
fmt.Fprint(os.Stdout, "")
fmt.Println(template.JSEscapeString(s))
fmt.Println(template.JSEscaper(v...))
fmt.Println(template.URLQueryEscaper(v...))
}
示例14: login
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method: ", r.Method)
if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
} else {
r.ParseForm()
fmt.Println("username:",
template.HTMLEscapeString(r.Form.Get("username")))
fmt.Println("password:",
template.HTMLEscapeString(r.Form.Get("password")))
template.HTMLEscape(w, []byte(r.Form.Get("username")))
}
}
示例15: villagePreWriteHandler
func villagePreWriteHandler(w http.ResponseWriter, r *http.Request) {
c := gae.NewContext(r)
g := goon.FromContext(c)
u := user.Current(c)
preWriteView := view.PreWriteView{}
buf := new(bytes.Buffer)
template.HTMLEscape(buf, []byte(r.FormValue("comment")))
t := buf.String()
preWriteView.Text = strings.Replace(t, "\n", "<br>", -1)
preWriteView.HiddenText = r.FormValue("comment")
commentType := r.FormValue("commentType")
characterID := r.FormValue("characterID")
preWriteView.CharacterID = characterID
if commentType == "personal" {
preWriteView.IsPersonal = true
} else if commentType == "whisper" {
preWriteView.IsWhisper = true
} else if commentType == "graveyard" {
preWriteView.IsGraveyard = true
} else {
preWriteView.IsPublic = true
}
no, err := strconv.ParseInt(r.FormValue("vno"), 10, 64)
if err != nil || len(preWriteView.Text) <= 5 || user.Current(c) == nil || len(preWriteView.Text) > 1000 {
bad(w)
return
}
preWriteView.VillageNo = no
village := Village{No: no}
if err := g.Get(&village); err != nil {
bad(w)
return
}
vKey := g.Key(village)
person := Person{UserID: u.ID, ParentKey: vKey, CharacterID: characterID}
if err := g.Get(&person); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
preWriteView.Face = person.Face
preWriteView.Author = person.Name
if err = prewriteTmpl.ExecuteTemplate(w, "base", preWriteView); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}