Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b3f6c8

Browse files
committed
搜索结果 html 过滤
1 parent eaedd25 commit 0b3f6c8

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

src/http/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ var funcMap = template.FuncMap{
147147
json.Unmarshal([]byte(str), &result)
148148
return result
149149
},
150+
"safeHtml": util.SafeHtml,
150151
}
151152

152153
func tplInclude(file string, dot map[string]interface{}) template.HTML {

src/util/string.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package util
77

88
import (
99
"errors"
10+
"strings"
1011
"unicode"
1112
"unicode/utf8"
1213

@@ -233,3 +234,8 @@ func UnderscoreName(name string) string {
233234

234235
return buffer.String()
235236
}
237+
238+
func SafeHtml(s string) string {
239+
r := strings.NewReplacer("<input", "&lt;input", "<a ", "&lt; a")
240+
return r.Replace(s)
241+
}

template/search.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<h2>
4848
<a href="/{{if eq .Objtype 0}}topics{{else if eq .Objtype 1}}articles{{else if eq .Objtype 2}}resources{{else if eq .Objtype 4}}p{{else}}wiki{{end}}/{{.Objid}}" target="_blank" title="{{.Title}}">{{noescape .HlTitle}}</a></h2>
4949
{{if .Content}}
50-
<p class="text">{{noescape .HlContent}}<a href="/articles/{{.Objid}}" target="_blank" title="阅读全文">阅读全文</a></p>
50+
<p class="text">{{safeHtml (noescape .HlContent)}}<a href="/articles/{{.Objid}}" target="_blank" title="阅读全文">阅读全文</a></p>
5151
{{end}}
5252
</div>
5353
</div>

0 commit comments

Comments
 (0)