-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
72 lines (63 loc) · 1.31 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package gohtml
import (
"bytes"
"golang.org/x/net/html"
"regexp"
"strings"
)
var _importantAttrs = map[string]bool{
"alt": true,
"src": true,
"href": true,
}
var _unusedElements = map[string]bool{
"script": true,
"style": true,
"meta": true,
"link": true,
}
var _mergeTextElements = map[string]bool{
"b": true,
"i": true,
"strong": true,
"font": true,
"span": true,
}
var _voidElements = map[string]bool{
"area": true,
"base": true,
"br": true,
"col": true,
"command": true,
"embed": true,
"hr": true,
"img": true,
"input": true,
"keygen": true,
"link": true,
"meta": true,
"param": true,
"source": true,
"track": true,
"wbr": true,
}
func RemoveUTF8BOM(htmlStr string) string {
return strings.Replace(htmlStr, "\xEF\xBB\xBF", "", -1)
}
func RemoveHTMLTags(htmlStr string, tags []string) string {
for _, tag := range tags {
htmlStr = strings.Replace(htmlStr, "<"+tag+">", "", -1)
htmlStr = strings.Replace(htmlStr, "</"+tag+">", "", -1)
re := regexp.MustCompile("<" + tag + " [^<]*>")
strs := re.FindAllString(htmlStr, -1)
for _, s := range strs {
htmlStr = strings.Replace(htmlStr, s, "", -1)
}
}
return htmlStr
}
func Render(n *html.Node) string {
buf := new(bytes.Buffer)
html.Render(buf, n)
return buf.String()
}