-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml2bbcode_test.go
More file actions
393 lines (383 loc) · 11 KB
/
html2bbcode_test.go
File metadata and controls
393 lines (383 loc) · 11 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package html2bbcode
import (
"testing"
)
type test struct {
name string
html string
bbcode string
err error
}
var tests = []test{
test{
"Plain text",
"Lorem ipsum",
"Lorem ipsum",
nil,
},
test{
"Line break",
"Lorem ipsum<br />",
"Lorem ipsum\n",
nil,
},
test{
"Paragraph",
"<p>Lorem ipsum</p>",
"Lorem ipsum\n",
nil,
},
test{
"Bulleted list",
"<ul><li>Lorem</li><li>ipsum</li></ul>",
"[*]Lorem[*]ipsum",
nil},
test{
"Numbered list",
"<ol><li>Lorem</li><li>ipsum</li></ol>",
"[#]Lorem[#]ipsum",
nil,
},
test{
"Nested list",
"<ol><ul><li>Lorem</li></ul><li>ipsum</li><ol><li>dolor</li></ol></ol>",
"[*]Lorem[#]ipsum[#]dolor",
nil,
},
test{
"Naked url",
`<a href="Lorem ipsum">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"img",
`<img src="Lorem ipsum" />`,
"[img=Lorem ipsum]",
nil,
},
test{
"desc image",
`<img class="scale_image" onclick="lightbox.init(this, $(this).width());" alt="https://lut.im/9wZAp52yrB/0RELtSt1QzgHZIoz.jpg" src="https://example.com/image.php?c=1&i=https%3A%2F%2Flut.im%2F9wZAp52yrB%2F0RELtSt1QzgHZIoz.jpg" />`,
"[img=https://lut.im/9wZAp52yrB/0RELtSt1QzgHZIoz.jpg]",
nil},
test{
"img with alt",
`<img alt="https://ptpimg.me/72r077.png" src="https://ptpimg.me/72r077.png" />`,
`[img=https://ptpimg.me/72r077.png]`,
nil,
},
test{
"bold",
"<strong>Lorem ipsum</strong>",
"[b]Lorem ipsum[/b]",
nil,
},
test{
"italic",
`<span style="font-style: italic;">Lorem ipsum</span>`,
"[i]Lorem ipsum[/i]",
nil,
},
test{
"underline",
`<span style="text-decoration: underline;">Lorem ipsum</span>`,
"[u]Lorem ipsum[/u]",
nil,
},
test{
"align center",
`<div style="text-align: center;">Lorem ipsum</div>`,
"[align=center]Lorem ipsum[/align]",
nil,
},
test{
"align left",
`<div style="text-align: left;">Lorem ipsum</div>`,
"[align=left]Lorem ipsum[/align]",
nil,
},
test{
"align right",
`<div style="text-align: right;">Lorem ipsum</div>`,
"[align=right]Lorem ipsum[/align]",
nil,
},
test{
"smiley heart",
`<img border="0" src="static/common/smileys/heart.gif" alt="" />`,
"<3",
nil,
},
test{
"smiley wave",
`<img border="0" src="static/common/smileys/wave.gif" alt="" />`,
":wave:",
nil,
},
test{
"smiley sad",
`<img border="0" src="static/common/smileys/sad.gif" alt="" />`,
":(",
nil,
},
test{
"complete entry with 'More info'",
`<strong><span class="size4">Tracklist</span></strong><br />
<strong>01.</strong> Claude Vonstroke & Eddy M - Getting Hot <span style="font-style: italic;">(3:50)</span><br />
<br />
It's 'Getting Hot', and Claude VonStroke and Eddy M team up to give you what you want! Eddy M hails from Barcelona, where he is a resident of the infamous El Row parties, and him and Claude are a perfect match for this funky, rolling monster.<br />
<br />
<strong>More info:</strong> <span style="display:inline-block; padding: 0px 3px;"><a rel="noreferrer" target="_blank" href="https://listen.tidal.com/album/106594181"><img width="18" class="scale_image" onclick="lightbox.init(this, $(this).width());"
alt="https://ptpimg.me/dhyvs6.png" src="https://ptpimg.me/dhyvs6.png" /> Tidal</a></span> <span style="display:inline-block; padding: 0px 3px;"><a rel="noreferrer" target="_blank" href="https://pro.beatport.com/release/getting-hot/2550383"><img width="18" class="scale_image" onclick="lightbox.init(this, $(this).width());" alt="https://ptpimg.me/26k503.png" src="https://ptpimg.me/26k503.png" /> Beatport</a></span>`,
`[b][size=4]Tracklist[/size][/b]
[b]01.[/b] Claude Vonstroke & Eddy M - Getting Hot [i](3:50)[/i]
It's 'Getting Hot', and Claude VonStroke and Eddy M team up to give you what you want! Eddy M hails from Barcelona, where he is a resident of the infamous El Row parties, and him and Claude are a perfect match for this funky, rolling monster.
[b]More info:[/b] [pad=0|3][url=https://listen.tidal.com/album/106594181][img=18]https://ptpimg.me/dhyvs6.png[/img] Tidal[/url][/pad] [pad=0|3][url=https://pro.beatport.com/release/getting-hot/2550383][img=18]https://ptpimg.me/26k503.png[/img] Beatport[/url][/pad]`,
nil,
},
test{
"spoiler without tag",
`<strong>Hidden text</strong>: <a href="javascript:void(0);" onclick="BBCode.spoiler(this);">Show</a><blockquote class="hidden spoiler">Lorem ipsum</blockquote>`,
"[hide]Lorem ipsum[/hide]",
nil,
},
test{
"spoiler with tag",
`<strong>dolor</strong>: <a href="javascript:void(0);" onclick="BBCode.spoiler(this);">Show</a><blockquote class="hidden spoiler">Lorem ipsum</blockquote>`,
"[hide=dolor]Lorem ipsum[/hide]",
nil,
},
test{
"not spoiler",
`<strong>dolor</strong>: <a href="javascript:void(0);" onclick="BBCode.spoiler(this);">Show</a><blockquote class="dolor">Lorem ipsum</blockquote>`,
"[b]dolor[/b]: [url=javascript:void(0);]Show[/url][quote]Lorem ipsum[/quote]",
nil,
},
test{
"artist",
`<a href="artist.php?artistname=Lorem ipsum">Lorem ipsum</a>`,
"[artist]Lorem ipsum[/artist]",
nil,
},
test{
"artist doesn't match",
`<a href="artist.php?artistname=Lorem ipsum">dolor</a>`,
"dolor",
nil,
},
test{
"link",
`<a rel="noreferrer" target="_blank" href="Lorem ipsum">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"link text",
`<a rel="noreferrer" target="_blank" href="Lorem ipsum">dolor</a>`,
"[url=Lorem ipsum]dolor[/url]",
nil,
},
test{
"taglist",
`<a href="https://example.com/torrents.php?taglist=Lorem ipsum">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"taglist doesn't match",
`<a href="https://example.com/torrents.php?taglist=Lorem ipsum">dolor</a>`,
"dolor",
nil,
},
test{
"recordlabel",
`<a href="https://example.com/torrents.php?recordlabel=Lorem ipsum">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"recordlabel not matching",
`<a href="https://example.com/torrents.php?recordlabel=Lorem ipsum">dolor</a>`,
"dolor",
nil,
},
test{
"color",
`<span style="color: Lorem ipsum;">dolor</span>`,
"[color=Lorem ipsum]dolor[/color]",
nil,
},
test{
"pre",
`<pre>Lorem ipsum</pre>`,
"[pre]Lorem ipsum[/pre]",
nil,
},
test{
"quote",
`<blockquote>Lorem ipsum</blockquote>`,
"[quote]Lorem ipsum[/quote]",
nil,
},
test{
"attributed quote",
`<strong class="quoteheader">dolor</strong> wrote: <blockquote>Lorem ipsum</blockquote>`,
"[quote=dolor]Lorem ipsum[/quote]",
nil,
},
test{
"linked quote",
`<a href="#" onclick="QuoteJump(event, 'ipsum'); return false;"><strong class="quoteheader">Lorem</strong> wrote: </a><blockquote>dolor</blockquote>`,
"[quote=Lorem]dolor[/quote]",
nil,
},
test{
"real example with padding",
`<strong><span class="size4">Tracklist</span></strong><br />
<strong>01.</strong> Claude Vonstroke & Eddy M - Getting Hot <span style="font-style: italic;">(3:50)</span><br />
<br />
It's 'Getting Hot', and Claude VonStroke and Eddy M team up to give you what you want! Eddy M hails from Barcelona, where he is a resident of the infamous El Row parties, and him and Claude are a perfect match for this funky, rolling monster.<br />
<br />
<strong>More info:</strong> <span style="display:inline-block; padding: 0px 3px;"><a rel="noreferrer" target="_blank" href="https://listen.tidal.com/album/106594181"><img width="18" class="scale_image" onclick="lightbox.init(this, $(this).width());" alt="https://ptpimg.me/dhyvs6.png" src="https://ptpimg.me/dhyvs6.png" /> Tidal</a></span> <span style="display:inline-block; padding: 0px 3px;"><a rel="noreferrer" target="_blank" href="https://pro.beatport.com/release/getting-hot/2550383"><img width="18" class="scale_image" onclick="lightbox.init(this, $(this).width());" alt="https://ptpimg.me/26k503.png" src="https://ptpimg.me/26k503.png" /> Beatport</a></span></div>`,
`[b][size=4]Tracklist[/size][/b]
[b]01.[/b] Claude Vonstroke & Eddy M - Getting Hot [i](3:50)[/i]
It's 'Getting Hot', and Claude VonStroke and Eddy M team up to give you what you want! Eddy M hails from Barcelona, where he is a resident of the infamous El Row parties, and him and Claude are a perfect match for this funky, rolling monster.
[b]More info:[/b] [pad=0|3][url=https://listen.tidal.com/album/106594181][img=18]https://ptpimg.me/dhyvs6.png[/img] Tidal[/url][/pad] [pad=0|3][url=https://pro.beatport.com/release/getting-hot/2550383][img=18]https://ptpimg.me/26k503.png[/img] Beatport[/url][/pad]`,
nil,
},
test{
"padding 0 3",
`<span style="display:inline-block; padding: 0px 3px;">Lorem ipsum</span>`,
"[pad=0|3]Lorem ipsum[/pad]",
nil,
},
test{
"padding 5 0 0 0",
`<span style="display:inline-block; padding: 5px 0px 0px 0px;">Lorem ipsum</span>`,
"[pad=5|0|0|0]Lorem ipsum[/pad]",
nil,
},
test{
"forum link",
`<a href="/forums.php?action=viewthread&threadid=dolor">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"collage id",
`<a href="/collages.php?id=dolor">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"torrent id",
`<a href="/torrents.php?id=dolor">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"artist id",
`<a href="/artist.php?id=dolor">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"request id",
`<a href="/requests.php?action=view&id=dolor">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"important",
`<strong class="important_text">Lorem ipsum</strong>`,
"[important]Lorem ipsum[/important]",
nil,
},
test{
"hr",
`<hr><br />`,
"\n",
nil,
},
{
"htmlencoded artist",
`<a href="artist.php?artistname=Lorem+ipsum">Lorem ipsum</a>`,
"[artist]Lorem ipsum[/artist]",
nil,
},
test{
"htmlencoded taglist",
`<a href="https://example.com/torrents.php?taglist=Lorem%20ipsum">Lorem ipsum</a>`,
"Lorem ipsum",
nil,
},
test{
"htmlencoded recordlabel",
`<a href="https://example.com/torrents.php?recordlabel=Mustafa+Av%C5%9Faro%C4%9Flu">Mustafa Avşaroğlu</a>`,
"Mustafa Avşaroğlu",
nil,
},
test{
"user",
`<a href="/user.php?action=search&search=Lorem ipsum">Lorem ipsum</a>`,
"[user]Lorem ipsum[/user]",
nil,
},
test{
"taglist with periods",
`<a href="https://example.com/torrents.php?taglist=lorem.ipsum">lorem ipsum</a>`,
"lorem ipsum",
nil,
},
}
func EqualErrors(a, b error) bool {
if a == b {
return true
}
if a != nil && b != nil && a.Error() == b.Error() {
return true
}
return false
}
func Context(want, got string) (string, string) {
if len(want)+len(got) < 60 {
return want, got
}
var i int
// find where they differ
for i = 0; i < len(want) && i < len(got) && want[i] == got[i]; i++ {
}
start := i - 10
s := "..."
if start < 0 {
start = 0
s = ""
}
end := i + 10
if end > len(want) {
want = s + want[start:]
} else {
want = s + want[start:end] + "..."
}
if end > len(got) {
got = s + got[start:]
} else {
got = s + got[start:end] + "..."
}
return want, got
}
func TestConvert(t *testing.T) {
for _, d := range tests {
bbcode, err := Convert(d.html)
if !EqualErrors(err, d.err) {
t.Errorf(`%s: want err = %v got "%s"`,
d.name, d.err, err)
}
if bbcode != d.bbcode {
want, got := Context(d.bbcode, bbcode)
t.Errorf(`%s: want bbcode = "%s" got "%s"`,
d.name, want, got)
}
}
}