inline_test.go (view raw)
1//
2// Blackfriday Markdown Processor
3// Available at http://github.com/russross/blackfriday
4//
5// Copyright © 2011 Russ Ross <russ@russross.com>.
6// Distributed under the Simplified BSD License.
7// See README.md for details.
8//
9
10//
11// Unit tests for inline parsing
12//
13
14package blackfriday
15
16import (
17 "testing"
18)
19
20func runMarkdownInline(input string, extensions, htmlFlags int) string {
21 extensions |= EXTENSION_AUTOLINK
22 extensions |= EXTENSION_STRIKETHROUGH
23
24 htmlFlags |= HTML_USE_XHTML
25
26 renderer := HtmlRenderer(htmlFlags, "", "")
27
28 return string(Markdown([]byte(input), renderer, extensions))
29}
30
31func doTestsInline(t *testing.T, tests []string) {
32 doTestsInlineParam(t, tests, 0, 0)
33}
34
35func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int) {
36 // catch and report panics
37 var candidate string
38 defer func() {
39 if err := recover(); err != nil {
40 t.Errorf("\npanic while processing [%#v]\n", candidate)
41 }
42 }()
43
44 for i := 0; i+1 < len(tests); i += 2 {
45 input := tests[i]
46 candidate = input
47 expected := tests[i+1]
48 actual := runMarkdownInline(candidate, extensions, htmlFlags)
49 if actual != expected {
50 t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
51 candidate, expected, actual)
52 }
53
54 // now test every substring to stress test bounds checking
55 if !testing.Short() {
56 for start := 0; start < len(input); start++ {
57 for end := start + 1; end <= len(input); end++ {
58 candidate = input[start:end]
59 _ = runMarkdownInline(candidate, extensions, htmlFlags)
60 }
61 }
62 }
63 }
64}
65
66func TestRawHtmlTag(t *testing.T) {
67 tests := []string{
68 "zz <style>p {}</style>\n",
69 "<p>zz p {}</p>\n",
70
71 "zz <STYLE>p {}</STYLE>\n",
72 "<p>zz p {}</p>\n",
73
74 "<SCRIPT>alert()</SCRIPT>\n",
75 "<p>alert()</p>\n",
76
77 "zz <SCRIPT>alert()</SCRIPT>\n",
78 "<p>zz alert()</p>\n",
79
80 "zz <script>alert()</script>\n",
81 "<p>zz alert()</p>\n",
82
83 " <script>alert()</script>\n",
84 "<p>alert()</p>\n",
85 }
86 doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE|HTML_SKIP_SCRIPT)
87}
88
89func TestEmphasis(t *testing.T) {
90 var tests = []string{
91 "nothing inline\n",
92 "<p>nothing inline</p>\n",
93
94 "simple *inline* test\n",
95 "<p>simple <em>inline</em> test</p>\n",
96
97 "*at the* beginning\n",
98 "<p><em>at the</em> beginning</p>\n",
99
100 "at the *end*\n",
101 "<p>at the <em>end</em></p>\n",
102
103 "*try two* in *one line*\n",
104 "<p><em>try two</em> in <em>one line</em></p>\n",
105
106 "over *two\nlines* test\n",
107 "<p>over <em>two\nlines</em> test</p>\n",
108
109 "odd *number of* markers* here\n",
110 "<p>odd <em>number of</em> markers* here</p>\n",
111
112 "odd *number\nof* markers* here\n",
113 "<p>odd <em>number\nof</em> markers* here</p>\n",
114
115 "simple _inline_ test\n",
116 "<p>simple <em>inline</em> test</p>\n",
117
118 "_at the_ beginning\n",
119 "<p><em>at the</em> beginning</p>\n",
120
121 "at the _end_\n",
122 "<p>at the <em>end</em></p>\n",
123
124 "_try two_ in _one line_\n",
125 "<p><em>try two</em> in <em>one line</em></p>\n",
126
127 "over _two\nlines_ test\n",
128 "<p>over <em>two\nlines</em> test</p>\n",
129
130 "odd _number of_ markers_ here\n",
131 "<p>odd <em>number of</em> markers_ here</p>\n",
132
133 "odd _number\nof_ markers_ here\n",
134 "<p>odd <em>number\nof</em> markers_ here</p>\n",
135
136 "mix of *markers_\n",
137 "<p>mix of *markers_</p>\n",
138 }
139 doTestsInline(t, tests)
140}
141
142func TestStrong(t *testing.T) {
143 var tests = []string{
144 "nothing inline\n",
145 "<p>nothing inline</p>\n",
146
147 "simple **inline** test\n",
148 "<p>simple <strong>inline</strong> test</p>\n",
149
150 "**at the** beginning\n",
151 "<p><strong>at the</strong> beginning</p>\n",
152
153 "at the **end**\n",
154 "<p>at the <strong>end</strong></p>\n",
155
156 "**try two** in **one line**\n",
157 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
158
159 "over **two\nlines** test\n",
160 "<p>over <strong>two\nlines</strong> test</p>\n",
161
162 "odd **number of** markers** here\n",
163 "<p>odd <strong>number of</strong> markers** here</p>\n",
164
165 "odd **number\nof** markers** here\n",
166 "<p>odd <strong>number\nof</strong> markers** here</p>\n",
167
168 "simple __inline__ test\n",
169 "<p>simple <strong>inline</strong> test</p>\n",
170
171 "__at the__ beginning\n",
172 "<p><strong>at the</strong> beginning</p>\n",
173
174 "at the __end__\n",
175 "<p>at the <strong>end</strong></p>\n",
176
177 "__try two__ in __one line__\n",
178 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
179
180 "over __two\nlines__ test\n",
181 "<p>over <strong>two\nlines</strong> test</p>\n",
182
183 "odd __number of__ markers__ here\n",
184 "<p>odd <strong>number of</strong> markers__ here</p>\n",
185
186 "odd __number\nof__ markers__ here\n",
187 "<p>odd <strong>number\nof</strong> markers__ here</p>\n",
188
189 "mix of **markers__\n",
190 "<p>mix of **markers__</p>\n",
191 }
192 doTestsInline(t, tests)
193}
194
195func TestEmphasisMix(t *testing.T) {
196 var tests = []string{
197 "***triple emphasis***\n",
198 "<p><strong><em>triple emphasis</em></strong></p>\n",
199
200 "***triple\nemphasis***\n",
201 "<p><strong><em>triple\nemphasis</em></strong></p>\n",
202
203 "___triple emphasis___\n",
204 "<p><strong><em>triple emphasis</em></strong></p>\n",
205
206 "***triple emphasis___\n",
207 "<p>***triple emphasis___</p>\n",
208
209 "*__triple emphasis__*\n",
210 "<p><em><strong>triple emphasis</strong></em></p>\n",
211
212 "__*triple emphasis*__\n",
213 "<p><strong><em>triple emphasis</em></strong></p>\n",
214
215 "**improper *nesting** is* bad\n",
216 "<p><strong>improper *nesting</strong> is* bad</p>\n",
217
218 "*improper **nesting* is** bad\n",
219 "<p><em>improper **nesting</em> is** bad</p>\n",
220 }
221 doTestsInline(t, tests)
222}
223
224func TestStrikeThrough(t *testing.T) {
225 var tests = []string{
226 "nothing inline\n",
227 "<p>nothing inline</p>\n",
228
229 "simple ~~inline~~ test\n",
230 "<p>simple <del>inline</del> test</p>\n",
231
232 "~~at the~~ beginning\n",
233 "<p><del>at the</del> beginning</p>\n",
234
235 "at the ~~end~~\n",
236 "<p>at the <del>end</del></p>\n",
237
238 "~~try two~~ in ~~one line~~\n",
239 "<p><del>try two</del> in <del>one line</del></p>\n",
240
241 "over ~~two\nlines~~ test\n",
242 "<p>over <del>two\nlines</del> test</p>\n",
243
244 "odd ~~number of~~ markers~~ here\n",
245 "<p>odd <del>number of</del> markers~~ here</p>\n",
246
247 "odd ~~number\nof~~ markers~~ here\n",
248 "<p>odd <del>number\nof</del> markers~~ here</p>\n",
249 }
250 doTestsInline(t, tests)
251}
252
253func TestCodeSpan(t *testing.T) {
254 var tests = []string{
255 "`source code`\n",
256 "<p><code>source code</code></p>\n",
257
258 "` source code with spaces `\n",
259 "<p><code>source code with spaces</code></p>\n",
260
261 "` source code with spaces `not here\n",
262 "<p><code>source code with spaces</code>not here</p>\n",
263
264 "a `single marker\n",
265 "<p>a `single marker</p>\n",
266
267 "a single multi-tick marker with ``` no text\n",
268 "<p>a single multi-tick marker with ``` no text</p>\n",
269
270 "markers with ` ` a space\n",
271 "<p>markers with a space</p>\n",
272
273 "`source code` and a `stray\n",
274 "<p><code>source code</code> and a `stray</p>\n",
275
276 "`source *with* _awkward characters_ in it`\n",
277 "<p><code>source *with* _awkward characters_ in it</code></p>\n",
278
279 "`split over\ntwo lines`\n",
280 "<p><code>split over\ntwo lines</code></p>\n",
281
282 "```multiple ticks``` for the marker\n",
283 "<p><code>multiple ticks</code> for the marker</p>\n",
284
285 "```multiple ticks `with` ticks inside```\n",
286 "<p><code>multiple ticks `with` ticks inside</code></p>\n",
287 }
288 doTestsInline(t, tests)
289}
290
291func TestLineBreak(t *testing.T) {
292 var tests = []string{
293 "this line \nhas a break\n",
294 "<p>this line<br />\nhas a break</p>\n",
295
296 "this line \ndoes not\n",
297 "<p>this line\ndoes not</p>\n",
298
299 "this has an \nextra space\n",
300 "<p>this has an<br />\nextra space</p>\n",
301 }
302 doTestsInline(t, tests)
303}
304
305func TestInlineLink(t *testing.T) {
306 var tests = []string{
307 "[foo](/bar/)\n",
308 "<p><a href=\"/bar/\">foo</a></p>\n",
309
310 "[foo with a title](/bar/ \"title\")\n",
311 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
312
313 "[foo with a title](/bar/\t\"title\")\n",
314 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
315
316 "[foo with a title](/bar/ \"title\" )\n",
317 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
318
319 "[foo with a title](/bar/ title with no quotes)\n",
320 "<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n",
321
322 "[foo]()\n",
323 "<p>[foo]()</p>\n",
324
325 "![foo](/bar/)\n",
326 "<p><img src=\"/bar/\" alt=\"foo\" />\n</p>\n",
327
328 "![foo with a title](/bar/ \"title\")\n",
329 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
330
331 "![foo with a title](/bar/\t\"title\")\n",
332 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
333
334 "![foo with a title](/bar/ \"title\" )\n",
335 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
336
337 "![foo with a title](/bar/ title with no quotes)\n",
338 "<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" />\n</p>\n",
339
340 "![foo]()\n",
341 "<p>![foo]()</p>\n",
342
343 "[a link]\t(/with_a_tab/)\n",
344 "<p><a href=\"/with_a_tab/\">a link</a></p>\n",
345
346 "[a link] (/with_spaces/)\n",
347 "<p><a href=\"/with_spaces/\">a link</a></p>\n",
348
349 "[text (with) [[nested] (brackets)]](/url/)\n",
350 "<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n",
351
352 "[text (with) [broken nested] (brackets)]](/url/)\n",
353 "<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n",
354
355 "[text\nwith a newline](/link/)\n",
356 "<p><a href=\"/link/\">text\nwith a newline</a></p>\n",
357
358 "[text in brackets] [followed](/by a link/)\n",
359 "<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n",
360
361 "[link with\\] a closing bracket](/url/)\n",
362 "<p><a href=\"/url/\">link with] a closing bracket</a></p>\n",
363
364 "[link with\\[ an opening bracket](/url/)\n",
365 "<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n",
366
367 "[link with\\) a closing paren](/url/)\n",
368 "<p><a href=\"/url/\">link with) a closing paren</a></p>\n",
369
370 "[link with\\( an opening paren](/url/)\n",
371 "<p><a href=\"/url/\">link with( an opening paren</a></p>\n",
372
373 "[link]( with whitespace)\n",
374 "<p><a href=\"with whitespace\">link</a></p>\n",
375
376 "[link]( with whitespace )\n",
377 "<p><a href=\"with whitespace\">link</a></p>\n",
378
379 "[link](url \"one quote)\n",
380 "<p><a href=\"url "one quote\">link</a></p>\n",
381
382 "[link](url 'one quote)\n",
383 "<p><a href=\"url 'one quote\">link</a></p>\n",
384
385 "[link](<url>)\n",
386 "<p><a href=\"url\">link</a></p>\n",
387
388 "[link & ampersand](/url/)\n",
389 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
390
391 "[link & ampersand](/url/)\n",
392 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
393
394 "[link](/url/&query)\n",
395 "<p><a href=\"/url/&query\">link</a></p>\n",
396 }
397 doTestsInline(t, tests)
398}
399
400func TestReferenceLink(t *testing.T) {
401 var tests = []string{
402 "[link][ref]\n",
403 "<p>[link][ref]</p>\n",
404
405 "[link][ref]\n [ref]: /url/ \"title\"\n",
406 "<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
407
408 "[link][ref]\n [ref]: /url/\n",
409 "<p><a href=\"/url/\">link</a></p>\n",
410
411 " [ref]: /url/\n",
412 "",
413
414 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
415 "",
416
417 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n [4spaces]: /url/\n",
418 "<pre><code>[4spaces]: /url/\n</code></pre>\n",
419
420 "[hmm](ref2)\n [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
421 "<p><a href=\"ref2\">hmm</a></p>\n",
422
423 "[ref]\n",
424 "<p>[ref]</p>\n",
425
426 "[ref]\n [ref]: /url/ \"title\"\n",
427 "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
428 }
429 doTestsInline(t, tests)
430}
431
432func TestTags(t *testing.T) {
433 var tests = []string{
434 "a <span>tag</span>\n",
435 "<p>a <span>tag</span></p>\n",
436
437 "<span>tag</span>\n",
438 "<p><span>tag</span></p>\n",
439
440 "<span>mismatch</spandex>\n",
441 "<p><span>mismatch</spandex></p>\n",
442
443 "a <singleton /> tag\n",
444 "<p>a <singleton /> tag</p>\n",
445 }
446 doTestsInline(t, tests)
447}
448
449func TestAutoLink(t *testing.T) {
450 var tests = []string{
451 "go to <http://foo.com/>\n",
452 "<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
453
454 "a secure <https://link.org>\n",
455 "<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
456
457 "an email <mailto:some@one.com>\n",
458 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
459
460 "an email <mailto://some@one.com>\n",
461 "<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
462
463 "an email <some@one.com>\n",
464 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
465
466 "an ftp <ftp://old.com>\n",
467 "<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
468
469 "an ftp <ftp:old.com>\n",
470 "<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
471
472 "a link with <http://new.com?query=foo&bar>\n",
473 "<p>a link with <a href=\"http://new.com?query=foo&bar\">" +
474 "http://new.com?query=foo&bar</a></p>\n",
475
476 "quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
477 "<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
478
479 "quotes mean a tag <http://new.com?query='foo'&bar>\n",
480 "<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
481
482 "unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
483 "<p>unless escaped <a href=\"http://new.com?query="foo"&bar\">" +
484 "http://new.com?query="foo"&bar</a></p>\n",
485
486 "even a > can be escaped <http://new.com?q=\\>&etc>\n",
487 "<p>even a > can be escaped <a href=\"http://new.com?q=>&etc\">" +
488 "http://new.com?q=>&etc</a></p>\n",
489 }
490 doTestsInline(t, tests)
491}