-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse.go
296 lines (231 loc) · 5.03 KB
/
parse.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package xss
import (
"strings"
// "regexp"
// "fmt"
)
type OnTagFunc func(sourcePosition int, position int, tag string, html string, isClosing bool) string
type escapeFunc func(html string) string
type onAttrFunc func(name, value string) string
//getTagName get tag name
func getTagName(html string) string {
if len(html) <= 2 {
return ""
}
var tagName = ""
i := spaceIndex(html)
if i == -1 {
tagName = html[1 : len(html)-1]
} else {
tagName = html[1 : i+1]
}
tagName = strings.ToLower(strings.TrimSpace(tagName))
if len(tagName) < 2 {
return tagName
}
if tagName[0:1] == "/" && len(tagName) >= 2 {
tagName = tagName[1:]
}
if tagName[len(tagName)-1:] == "/" {
tagName = tagName[:len(tagName)-1]
}
return tagName
}
//parseTag
func parseTag(html string, onTag OnTagFunc, escapeHtml escapeFunc) string {
// rethtml := ""
var retHTML strings.Builder
lastPos := 0
tagStart := -1
quoteStart := ""
currentPos := 0
htmlLen := len(html)
currentTagName := ""
currentHtml := ""
chariterator:
for currentPos = 0; currentPos < htmlLen; currentPos++ {
c := html[currentPos : currentPos+1]
if tagStart == -1 {
if c == "<" {
tagStart = currentPos
continue
}
} else {
if quoteStart == "" {
if c == "<" {
// rethtml += escapeHtml(html[lastPos:currentPos])
retHTML.WriteString(escapeHtml(html[lastPos:currentPos]))
tagStart = currentPos
lastPos = currentPos
continue
}
if c == ">" {
// rethtml += escapeHtml(html[lastPos:tagStart])
retHTML.WriteString(escapeHtml(html[lastPos:tagStart]))
currentHtml = html[tagStart : currentPos+1]
currentTagName = getTagName(currentHtml)
// rethtml += onTag(tagStart, len(rethtml), currentTagName, currentHtml, isClosing(currentHtml))
retHTML.WriteString(onTag(tagStart, retHTML.Len(), currentTagName, currentHtml, isClosing(currentHtml)))
lastPos = currentPos + 1
tagStart = -1
continue
}
if c == "'" || c == "\"" {
i := 1
ic := html[currentPos-i : currentPos]
for {
if !(ic == " " || ic == "=") {
break
}
if ic == "=" {
quoteStart = c
continue chariterator
}
i = i + 1
ic = html[currentPos-i : currentPos-i+1]
}
}
} else {
if c == quoteStart {
quoteStart = ""
continue
}
}
}
}
if lastPos < len(html) {
// rethtml += escapeHtml(html[lastPos:])
retHTML.WriteString(escapeHtml(html[lastPos:]))
}
return retHTML.String()
}
//parseAttr
func parseAttr(html string, onAttr onAttrFunc) string {
lastPos := 0
retAttr := []string{}
tmpName := ""
htmlLen := len(html)
addAttr := func(name string, value string) {
name = strings.TrimSpace(name)
name = strings.ToLower(regAttr.ReplaceAllString(name, ""))
if len(name) < 1 {
return
}
ret := onAttr(name, value)
if len(ret) > 0 {
retAttr = append(retAttr, ret)
}
}
for i := 0; i < htmlLen; i++ {
c := html[i : i+1]
v := ""
j := -1
if tmpName == "" && c == "=" {
tmpName = html[lastPos:i]
lastPos = i + 1
continue
}
if tmpName != "" {
if i == lastPos && (c == "\"" || c == "'") && html[i-1:i] == "=" {
tmpIndex := strings.Index(html[i+1:], c)
j = i + 1 + tmpIndex
if tmpIndex == -1 {
break
} else {
v = strings.TrimSpace(html[lastPos+1 : j])
addAttr(tmpName, v)
tmpName = ""
i = j
lastPos = i + 1
continue
}
}
}
//empty
if regEmtpy.MatchString(c) {
html = regEmtpy.ReplaceAllString(html, " ")
if tmpName == "" {
j = findNextEqual(html, i)
if j == -1 {
v = strings.TrimSpace(html[lastPos:i])
addAttr(v, "")
tmpName = ""
lastPos = i + 1
continue
} else {
i = j - 1
continue
}
} else {
j = findBeforeEqual(html, i-1)
if j == -1 {
v = strings.TrimSpace(html[lastPos:i])
v = stripQuoteWrap(v)
addAttr(tmpName, v)
tmpName = ""
lastPos = i + 1
continue
} else {
continue
}
}
}
}
if lastPos < len(html) {
if tmpName == "" {
addAttr(html[lastPos:], "")
} else {
addAttr(tmpName, stripQuoteWrap(strings.TrimSpace(html[lastPos:])))
}
}
return strings.TrimSpace(strings.Join(retAttr, " "))
}
//isClosing is close tag
func isClosing(html string) bool {
if len(html) < 2 {
return false
}
return html[0:2] == "</"
}
//isQuoteWrapString
func isQuoteWrapString(text string) bool {
f := text[0:1]
e := text[len(text)-1:]
if (f == "\"" && e == "\"") || (f == "'" && e == "'") {
return true
}
return false
}
//stripQuoteWrap
func stripQuoteWrap(text string) string {
if isQuoteWrapString(text) {
return text[1 : len(text)-1]
}
return text
}
func findNextEqual(str string, i int) int {
for ix := i; ix < len(str); ix++ {
c := str[ix : ix+1]
if c == " " {
continue
}
if c == "=" {
return ix
}
return -1
}
return -1
}
func findBeforeEqual(str string, i int) int {
for ix := i; ix > 0; ix-- {
c := str[ix : ix+1]
if c == " " {
continue
}
if c == "=" {
return ix
}
return -1
}
return -1
}