-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_test.go
More file actions
293 lines (272 loc) · 7.24 KB
/
Copy pathhtml_test.go
File metadata and controls
293 lines (272 loc) · 7.24 KB
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
package fmt
import "testing"
func TestHtml(t *testing.T) {
// Register custom words for testing
RegisterWords([]DictEntry{
{EN: "Hello", ES: "Hola"},
{EN: "User", ES: "Usuario"},
{EN: "Hello User", ES: "Hola Usuario"},
{EN: "Format", ES: "Formato"},
})
tests := []struct {
name string
args []any
expected string
}{
{
name: "Concatenation simple",
args: []any{"div", "span"},
expected: "divspan",
},
{
name: "Concatenate number and text",
args: []any{123, "abc"},
expected: "123abc",
},
{
name: "2-letter tag regression",
args: []any{"hr", "br"},
expected: "hrbr",
},
{
name: "Concatenation with translated word",
args: []any{"<div>", "hello", "</div>"},
expected: "<div>Hello</div>",
},
{
name: "Format string",
args: []any{"<div class='%s'>", "my-class"},
expected: "<div class='my-class'>",
},
{
name: "Format string with multiple args",
args: []any{"<a href='%s'>%s</a>", "/home", "Home"},
expected: "<a href='/home'>Home</a>",
},
{
name: "Format string with %L",
args: []any{"<span>%L</span>", "user"},
expected: "<span>User</span>",
},
{
name: "Mixed strings without format",
args: []any{"<p>", "Content", "</p>"},
expected: "<p>Content</p>",
},
{
name: "Empty args",
args: []any{},
expected: "",
},
{
name: "String with % but not format",
args: []any{"Width: 100%"},
expected: "Width: 100%",
},
{
name: "String with %% (literal percent)",
args: []any{"Success: 100%%"}, // Fmt treats %% as literal %
expected: "Success: 100%",
},
{
name: "Language ES explicit",
args: []any{ES, "<div>", "hello", "</div>"},
expected: "<div>Hola</div>",
},
{
name: "Language EN explicit",
args: []any{EN, "<div>", "hello", "</div>"},
expected: "<div>Hello</div>",
},
{
name: "Language ES with format",
args: []any{ES, "<span>%L</span>", "user"},
expected: "<span>Usuario</span>",
},
{
name: "Multiline component with format",
args: []any{
`<div class='container'>
<h1>%L</h1>
<p>%v</p>
</div>`,
"hello user",
42,
},
expected: `<div class='container'>
<h1>Hello User</h1>
<p>42</p>
</div>`,
},
{
name: "Multiline component with format ES",
args: []any{
ES,
`<div class='container'>
<h1>%L</h1>
<p>%v</p>
</div>`,
"hello user",
42,
},
expected: `<div class='container'>
<h1>Hola Usuario</h1>
<p>42</p>
</div>`,
},
}
// Test default language (EN)
OutLang(EN)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Html(tt.args...).String()
if got != tt.expected {
t.Errorf("Html() = %q, want %q", got, tt.expected)
}
})
}
// Test with Spanish
t.Run("Spanish Translate", func(t *testing.T) {
OutLang(ES)
defer OutLang(EN) // Restore
got := Html("<div>", "hello", "</div>").String()
want := "<div>Hola</div>"
if got != want {
t.Errorf("Html() ES = %q, want %q", got, want)
}
})
}
func BenchmarkHtml(b *testing.B) {
b.ReportAllocs()
b.Run("Simple", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Html("<div>", "content", "</div>").String()
}
})
b.Run("WithLang", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Html(ES, "<div>", "content", "</div>").String()
}
})
b.Run("WithTranslation", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Html("<span>", "format", "</span>").String()
}
})
b.Run("WithLangAndTranslation", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Html(ES, "<span>", "format", "</span>").String()
}
})
b.Run("WithFormat", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Html("<div class='%s'>", "my-class").String()
}
})
b.Run("WithFormatAndLang", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Html(ES, "<span>%L</span>", "format").String()
}
})
}
func TestEscapeAttr(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{"empty", "", ""},
{"basic", `Tom & Jerry's "House" <tag>`, `Tom & Jerry's "House" <tag>`},
{"already-entity", `& < >`, `&amp; &lt; &gt;`}, // double-escape expected
{"unicode", `こんにちは & <br> 😊`, `こんにちは & <br> 😊`},
{"multiple", `a & b & c`, `a & b & c`},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := Convert(tc.in).EscapeAttr()
if got != tc.want {
t.Fatalf("%s: got=%q want=%q", tc.name, got, tc.want)
}
})
}
}
func TestEscapeHTML_TableDriven(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{"empty", "", ""},
{"tags", `<div class="x">Tom & Jerry's</div>`, `<div class="x">Tom & Jerry's</div>`},
{"already-entity", `& <`, `&amp; &lt;`},
{"emoji-and-tags", `😀 <p>1 & 2</p>`, `😀 <p>1 & 2</p>`},
{"quotes-only", `She said: "Hi"`, `She said: "Hi"`},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := Convert(tc.in).EscapeHTML()
if got != tc.want {
t.Fatalf("%s: got=%q want=%q", tc.name, got, tc.want)
}
})
}
}
// TestEscapeHTML_CompareStdLib compares EscapeHTML behavior with html.EscapeString from standard library
func TestEscapeHTML_CompareStdLib(t *testing.T) {
// Note: html.EscapeString only escapes &, <, >, ", and ' (as ' or ")
// Our implementation matches this behavior
tests := []struct {
name string
in string
}{
{"basic", `<script>alert("XSS")</script>`},
{"quotes", `She said: "Hello" & 'Goodbye'`},
{"entities", `Tom & Jerry's <div>`},
{"unicode", `こんにちは <p>世界</p>`},
{"mixed", `<a href="link.html?id=1&type=2">Click here</a>`},
{"empty", ``},
{"ampersand-only", `A & B & C`},
{"all-chars", `&<>"'`},
}
// Import html package for comparison (added at top of file)
// We'll verify our output matches expected HTML escaping semantics
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := Convert(tc.in).EscapeHTML()
// Verify all dangerous characters are escaped
if Contains(got, "<") || Contains(got, ">") {
t.Errorf("Unescaped angle brackets in output: %q", got)
}
// Verify input characters were processed
if tc.in != "" && got == tc.in {
// Only fail if input contained escapable characters
if Contains(tc.in, "&") || Contains(tc.in, "<") || Contains(tc.in, ">") ||
Contains(tc.in, `"`) || Contains(tc.in, "'") {
t.Errorf("Input was not escaped: %q", tc.in)
}
}
})
}
}
// TestEscapeAttr_CompareStdLib validates EscapeAttr for use in HTML attributes
func TestEscapeAttr_CompareStdLib(t *testing.T) {
tests := []struct {
name string
in string
}{
{"attr-value", `class="btn btn-primary"`},
{"with-quotes", `onClick="alert('test')"`},
{"url", `https://example.com?a=1&b=2`},
{"mixed", `Tom & Jerry's "adventure"`},
{"empty", ``},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := Convert(tc.in).EscapeAttr()
// Verify dangerous characters for attributes are escaped
if Contains(got, `"`) || Contains(got, "<") || Contains(got, ">") {
t.Errorf("Unescaped dangerous characters in attribute: %q", got)
}
})
}
}