-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
208 lines (201 loc) · 5.08 KB
/
html.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (C) 2018 Christopher E. Miller
// MIT license, see LICENSE file.
package htmlstrip
import (
"html"
"io"
"strings"
)
// Writer strips any HTML written, calls W.Write() with the plain text.
type Writer struct {
W io.Writer
gotLT, gotLTBang, gotLTBangDash, // Set to false once determined which: tag, doctype and comment.
gotTagAttribSlash, // Detect self-closing tag.
inOpenTag, inTagAttribs, inCloseTag,
inComment, inEntity bool
curEntity []byte // content of the current entity (if inEntity) excluding &;
curTagName []byte // name of the current tag (ONLY if inOpenTag or inCloseTag)
curCommentDashes byte // If inComment, number of dashes found in a row, looking for the end.
inScriptTag,
inStyleTag,
inHeadTag bool // Keep track to skip script, style and head tags.
plainbuf []byte
lastWrote byte
err error
}
var _ io.Writer = &Writer{}
func isspace(b byte) bool {
return b == ' ' || b == '\t' || b == '\r' || b == '\n'
}
const maxTagNameLen = 256
const maxEntityLen = 32
func (p *Writer) Write(data []byte) (int, error) {
if p.err != nil {
return 0, p.err
}
plainbuf := p.plainbuf[:0]
appendPlain := func(elems ...byte) {
if len(elems) > 0 {
if !p.inScriptTag && !p.inStyleTag && !p.inHeadTag {
plainbuf = append(plainbuf, elems...)
p.lastWrote = elems[len(elems)-1]
}
}
}
gotTag := func(tagName []byte, isOpen bool, isClose bool) {
//fmt.Printf("gotTag `%s` isOpen=%v isClose=%v\n", tagName, isOpen, isClose)
switch strings.ToLower(string(p.curTagName)) {
case "style":
p.inStyleTag = isOpen && !isClose
case "script":
p.inScriptTag = isOpen && !isClose
case "head":
p.inHeadTag = isOpen && !isClose
case "br":
if isOpen {
appendPlain('\n')
}
case "div", "blockquote", "dd", "dl", "fieldset", "form",
"h1", "h2", "h3", "h4", "h5", "h6", "hr", "noscript",
"ol", "ul", "li", "p", "pre", "section", "table":
if p.lastWrote != '\n' && p.lastWrote != 0 {
appendPlain('\n')
}
}
}
for _, b := range data {
dostate:
switch {
case p.gotLT:
p.gotLT = false
p.curTagName = p.curTagName[:0]
if b == '!' {
p.gotLTBang = true
} else if b == '/' {
p.inCloseTag = true
} else if isspace(b) {
appendPlain('<')
goto dostate
} else {
p.inOpenTag = true
goto dostate
}
case p.gotLTBang:
p.gotLTBang = false
if b == '-' {
p.gotLTBangDash = true
} else {
p.curTagName = append(p.curTagName, '!')
p.inOpenTag = true
goto dostate
}
case p.gotLTBangDash:
p.gotLTBangDash = false
if b == '-' {
p.inComment = true
p.curCommentDashes = 2
} else {
p.curTagName = append(p.curTagName, '!', '-')
p.inOpenTag = true
goto dostate
}
case p.inComment:
if b == '-' {
p.curCommentDashes++
if p.curCommentDashes > 2 {
p.curCommentDashes = 2
}
} else if b == '>' {
if p.curCommentDashes == 2 {
p.inComment = false
}
} else {
p.curCommentDashes = 0
}
case p.inOpenTag:
switch b {
case '<':
// Bad HTML, let them jump into a new tag.
appendPlain('<')
appendPlain(p.curTagName...)
p.inOpenTag = false
p.gotLT = true
case '>':
p.inOpenTag = false
gotTag(p.curTagName, true, false)
case ' ', '\t', '\r', '\n', '/':
p.inOpenTag = false
p.inTagAttribs = true
goto dostate // Needed for self-closing tag.
default:
if len(p.curTagName) < maxTagNameLen {
p.curTagName = append(p.curTagName, b)
}
}
case p.inTagAttribs:
if b == '/' {
p.gotTagAttribSlash = true
} else if b == '>' {
// If p.gotTagAttribSlash it's a self-closing tag.
gotTag(p.curTagName, true, p.gotTagAttribSlash)
p.inTagAttribs = false
p.gotTagAttribSlash = false
} else {
if !isspace(b) {
p.gotTagAttribSlash = false
}
}
case p.inCloseTag:
if b == '>' {
p.inCloseTag = false
gotTag(p.curTagName, false, true)
} else {
if !isspace(b) {
if len(p.curTagName) < maxTagNameLen {
p.curTagName = append(p.curTagName, b)
}
}
}
case p.inEntity:
if (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9') || b == '#' {
if len(p.curEntity) >= maxEntityLen {
// Entity too long, it won't turn into anything so treat it all as text...
p.inEntity = false
appendPlain('&')
appendPlain(p.curEntity...)
goto dostate
} else {
p.curEntity = append(p.curEntity, b)
}
} else if b == ';' {
p.inEntity = false
appendPlain([]byte(html.UnescapeString("&" + string(p.curEntity) + ";"))...)
} else {
// Got invalid entity stuff, so treat it all as text...
p.inEntity = false
appendPlain('&')
appendPlain(p.curEntity...)
goto dostate
}
default:
switch b {
case '<':
p.gotLT = true
case '&':
p.inEntity = true
p.curEntity = p.curEntity[:0]
default:
if isspace(b) {
if !isspace(p.lastWrote) && p.lastWrote != 0 {
appendPlain(' ')
}
} else {
appendPlain(b)
}
}
}
}
p.plainbuf = plainbuf[:0]
_, p.err = p.W.Write(plainbuf)
return len(data), p.err
}