-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse_test.go
213 lines (164 loc) · 3.97 KB
/
parse_test.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
package xss
import (
"testing"
)
func TestGetTagName(t *testing.T) {
x := "<a href=\"https://example.com\">"
tagName := getTagName(x)
if tagName != "a" {
t.Errorf("get tagname1 err %s", tagName)
}
t.Logf("get tag name1 %s", tagName)
x = "<br/>"
tagName = getTagName(x)
if tagName != "br" {
t.Errorf("get tagname2 err %s", tagName)
}
t.Logf("get tag name2 %s", tagName)
x = "</strong>"
tagName = getTagName(x)
if tagName != "strong" {
t.Errorf("get tagname3 err %s", tagName)
}
t.Logf("get tag name3 %s", tagName)
}
func TestIsClosingTag(t *testing.T) {
x := "<a href=\"https://example.com\">"
isClose := isClosing(x)
if isClose == true {
t.Errorf("TestIsClosingTag err %v", isClose)
}
x = "</a>"
isClose = isClosing(x)
if isClose == false {
t.Errorf("TestIsClosingTag err %v", isClose)
}
}
func TestStripQuoteWrap(t *testing.T) {
x := "'asdfasdfadfafd'"
result := stripQuoteWrap(x)
if result != "asdfasdfadfafd" {
t.Errorf("TestStripQuoteWrap err %v", result)
}
x = "\"asdfasdfadfafd\""
result = stripQuoteWrap(x)
if result != "asdfasdfadfafd" {
t.Errorf("TestStripQuoteWrap err %v", result)
}
}
func escapeHtml(input string) string {
return input
}
func TestParseTag(t *testing.T) {
xh := "hello<A href=\"#\">www</A>ccc<b><br/>"
index := 0
onTag := func(sourcePosition int, position int, tag string, html string, isClosing bool) string {
// fmt.Printf("...onTag tagname:%s\n html:%s",tag,html)
if index == 0 {
if tag == "a" && html == "<A href=\"#\">" {
t.Logf("parse tag success tag:%s html:%s", tag, html)
} else {
t.Errorf("parse tag failed")
}
}
if index == 1 {
if tag == "a" && html == "</A>" {
t.Logf("parse tag success tag:%s html:%s", tag, html)
} else {
t.Errorf("parse tag failed")
}
}
if index == 2 {
if tag == "b" && html == "<b>" {
t.Logf("parse tag success tag:%s html:%s", tag, html)
} else {
t.Errorf("parse tag failed")
}
}
if index == 3 {
if tag == "br" && html == "<br/>" {
t.Logf("parse tag success tag:%s html:%s", tag, html)
} else {
t.Errorf("parse tag failed")
}
}
index++
return html
}
result := parseTag(xh, onTag, escapeHtml)
if result != xh {
t.Errorf("parseTag err %v", result)
return
}
t.Logf("parseTag %v", result)
}
func TestParseAttr(t *testing.T) {
xh := "href=\"#\"attr1=b attr2=c attr3 attr4='value4\"'attr5"
index := 0
onAttr := func(name string, value string) string {
if index == 0 {
if name == "href" && value == "#" {
t.Logf("parse attr success name:%s value:%s", name, value)
} else {
t.Errorf("parse attr failed")
}
}
if index == 1 {
if name == "attr1" && value == "b" {
t.Logf("parse attr success name:%s value:%s", name, value)
} else {
t.Errorf("parse attr failed")
}
}
if index == 2 {
if name == "attr2" && value == "c" {
t.Logf("parse attr success name:%s value:%s", name, value)
} else {
t.Errorf("parse attr failed")
}
}
if index == 3 {
if name == "attr3" && value == "" {
t.Logf("parse attr success name:%s value:%s", name, value)
} else {
t.Errorf("parse attr failed")
}
}
if index == 4 {
if name == "attr4" && value == "value4\"" {
t.Logf("parse attr success name:%s value:%s", name, value)
} else {
t.Errorf("parse attr failed")
}
}
if index == 5 {
if name == "attr5" && value == "" {
t.Logf("parse attr success name:%s value:%s", name, value)
} else {
t.Errorf("parse attr failed")
}
}
index++
return name + "=" + value
}
result := parseAttr(xh, onAttr)
t.Logf("parseAttr result %v", result)
}
func TestFindBeforeEqual(t *testing.T) {
i := findBeforeEqual("<a>", 1)
if i != -1 {
t.Errorf("findNextEqual error %d", i)
}
}
func TestFindNextEqual(t *testing.T) {
i := findNextEqual("<a>", 3)
if i != -1 {
t.Errorf("findNextEqual error %d", i)
}
}
func TestIsClosing(t *testing.T) {
i := isClosing("a")
if i != false {
t.Errorf("TestIsClosing error")
}
}