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 /*
39 defer func() {
40 if err := recover(); err != nil {
41 t.Errorf("\npanic while processing [%#v] (%v)\n", candidate, err)
42 }
43 }()
44 */
45
46 for i := 0; i+1 < len(tests); i += 2 {
47 input := tests[i]
48 candidate = input
49 expected := tests[i+1]
50 actual := runMarkdownInline(candidate, extensions, htmlFlags)
51 if actual != expected {
52 t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
53 candidate, expected, actual)
54 }
55
56 // now test every substring to stress test bounds checking
57 if !testing.Short() {
58 for start := 0; start < len(input); start++ {
59 for end := start + 1; end <= len(input); end++ {
60 candidate = input[start:end]
61 _ = runMarkdownInline(candidate, extensions, htmlFlags)
62 }
63 }
64 }
65 }
66}
67
68func TestRawHtmlTag(t *testing.T) {
69 tests := []string{
70 "zz <style>p {}</style>\n",
71 "<p>zz p {}</p>\n",
72
73 "zz <STYLE>p {}</STYLE>\n",
74 "<p>zz p {}</p>\n",
75
76 "<SCRIPT>alert()</SCRIPT>\n",
77 "<p>alert()</p>\n",
78
79 "zz <SCRIPT>alert()</SCRIPT>\n",
80 "<p>zz alert()</p>\n",
81
82 "zz <script>alert()</script>\n",
83 "<p>zz alert()</p>\n",
84
85 " <script>alert()</script>\n",
86 "<p>alert()</p>\n",
87
88 "<script>alert()</script>\n",
89 "<p>alert()</p>\n",
90
91 "<script src='foo'></script>\n",
92 "<p></p>\n",
93
94 "zz <script src='foo'></script>\n",
95 "<p>zz </p>\n",
96
97 "zz <script src=foo></script>\n",
98 "<p>zz </p>\n",
99 }
100 doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE|HTML_SKIP_SCRIPT)
101}
102
103func TestEmphasis(t *testing.T) {
104 var tests = []string{
105 "nothing inline\n",
106 "<p>nothing inline</p>\n",
107
108 "simple *inline* test\n",
109 "<p>simple <em>inline</em> test</p>\n",
110
111 "*at the* beginning\n",
112 "<p><em>at the</em> beginning</p>\n",
113
114 "at the *end*\n",
115 "<p>at the <em>end</em></p>\n",
116
117 "*try two* in *one line*\n",
118 "<p><em>try two</em> in <em>one line</em></p>\n",
119
120 "over *two\nlines* test\n",
121 "<p>over <em>two\nlines</em> test</p>\n",
122
123 "odd *number of* markers* here\n",
124 "<p>odd <em>number of</em> markers* here</p>\n",
125
126 "odd *number\nof* markers* here\n",
127 "<p>odd <em>number\nof</em> markers* here</p>\n",
128
129 "simple _inline_ test\n",
130 "<p>simple <em>inline</em> test</p>\n",
131
132 "_at the_ beginning\n",
133 "<p><em>at the</em> beginning</p>\n",
134
135 "at the _end_\n",
136 "<p>at the <em>end</em></p>\n",
137
138 "_try two_ in _one line_\n",
139 "<p><em>try two</em> in <em>one line</em></p>\n",
140
141 "over _two\nlines_ test\n",
142 "<p>over <em>two\nlines</em> test</p>\n",
143
144 "odd _number of_ markers_ here\n",
145 "<p>odd <em>number of</em> markers_ here</p>\n",
146
147 "odd _number\nof_ markers_ here\n",
148 "<p>odd <em>number\nof</em> markers_ here</p>\n",
149
150 "mix of *markers_\n",
151 "<p>mix of *markers_</p>\n",
152 }
153 doTestsInline(t, tests)
154}
155
156func TestStrong(t *testing.T) {
157 var tests = []string{
158 "nothing inline\n",
159 "<p>nothing inline</p>\n",
160
161 "simple **inline** test\n",
162 "<p>simple <strong>inline</strong> test</p>\n",
163
164 "**at the** beginning\n",
165 "<p><strong>at the</strong> beginning</p>\n",
166
167 "at the **end**\n",
168 "<p>at the <strong>end</strong></p>\n",
169
170 "**try two** in **one line**\n",
171 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
172
173 "over **two\nlines** test\n",
174 "<p>over <strong>two\nlines</strong> test</p>\n",
175
176 "odd **number of** markers** here\n",
177 "<p>odd <strong>number of</strong> markers** here</p>\n",
178
179 "odd **number\nof** markers** here\n",
180 "<p>odd <strong>number\nof</strong> markers** here</p>\n",
181
182 "simple __inline__ test\n",
183 "<p>simple <strong>inline</strong> test</p>\n",
184
185 "__at the__ beginning\n",
186 "<p><strong>at the</strong> beginning</p>\n",
187
188 "at the __end__\n",
189 "<p>at the <strong>end</strong></p>\n",
190
191 "__try two__ in __one line__\n",
192 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
193
194 "over __two\nlines__ test\n",
195 "<p>over <strong>two\nlines</strong> test</p>\n",
196
197 "odd __number of__ markers__ here\n",
198 "<p>odd <strong>number of</strong> markers__ here</p>\n",
199
200 "odd __number\nof__ markers__ here\n",
201 "<p>odd <strong>number\nof</strong> markers__ here</p>\n",
202
203 "mix of **markers__\n",
204 "<p>mix of **markers__</p>\n",
205 }
206 doTestsInline(t, tests)
207}
208
209func TestEmphasisMix(t *testing.T) {
210 var tests = []string{
211 "***triple emphasis***\n",
212 "<p><strong><em>triple emphasis</em></strong></p>\n",
213
214 "***triple\nemphasis***\n",
215 "<p><strong><em>triple\nemphasis</em></strong></p>\n",
216
217 "___triple emphasis___\n",
218 "<p><strong><em>triple emphasis</em></strong></p>\n",
219
220 "***triple emphasis___\n",
221 "<p>***triple emphasis___</p>\n",
222
223 "*__triple emphasis__*\n",
224 "<p><em><strong>triple emphasis</strong></em></p>\n",
225
226 "__*triple emphasis*__\n",
227 "<p><strong><em>triple emphasis</em></strong></p>\n",
228
229 "**improper *nesting** is* bad\n",
230 "<p><strong>improper *nesting</strong> is* bad</p>\n",
231
232 "*improper **nesting* is** bad\n",
233 "<p><em>improper **nesting</em> is** bad</p>\n",
234 }
235 doTestsInline(t, tests)
236}
237
238func TestStrikeThrough(t *testing.T) {
239 var tests = []string{
240 "nothing inline\n",
241 "<p>nothing inline</p>\n",
242
243 "simple ~~inline~~ test\n",
244 "<p>simple <del>inline</del> test</p>\n",
245
246 "~~at the~~ beginning\n",
247 "<p><del>at the</del> beginning</p>\n",
248
249 "at the ~~end~~\n",
250 "<p>at the <del>end</del></p>\n",
251
252 "~~try two~~ in ~~one line~~\n",
253 "<p><del>try two</del> in <del>one line</del></p>\n",
254
255 "over ~~two\nlines~~ test\n",
256 "<p>over <del>two\nlines</del> test</p>\n",
257
258 "odd ~~number of~~ markers~~ here\n",
259 "<p>odd <del>number of</del> markers~~ here</p>\n",
260
261 "odd ~~number\nof~~ markers~~ here\n",
262 "<p>odd <del>number\nof</del> markers~~ here</p>\n",
263 }
264 doTestsInline(t, tests)
265}
266
267func TestCodeSpan(t *testing.T) {
268 var tests = []string{
269 "`source code`\n",
270 "<p><code>source code</code></p>\n",
271
272 "` source code with spaces `\n",
273 "<p><code>source code with spaces</code></p>\n",
274
275 "` source code with spaces `not here\n",
276 "<p><code>source code with spaces</code>not here</p>\n",
277
278 "a `single marker\n",
279 "<p>a `single marker</p>\n",
280
281 "a single multi-tick marker with ``` no text\n",
282 "<p>a single multi-tick marker with ``` no text</p>\n",
283
284 "markers with ` ` a space\n",
285 "<p>markers with a space</p>\n",
286
287 "`source code` and a `stray\n",
288 "<p><code>source code</code> and a `stray</p>\n",
289
290 "`source *with* _awkward characters_ in it`\n",
291 "<p><code>source *with* _awkward characters_ in it</code></p>\n",
292
293 "`split over\ntwo lines`\n",
294 "<p><code>split over\ntwo lines</code></p>\n",
295
296 "```multiple ticks``` for the marker\n",
297 "<p><code>multiple ticks</code> for the marker</p>\n",
298
299 "```multiple ticks `with` ticks inside```\n",
300 "<p><code>multiple ticks `with` ticks inside</code></p>\n",
301 }
302 doTestsInline(t, tests)
303}
304
305func TestLineBreak(t *testing.T) {
306 var tests = []string{
307 "this line \nhas a break\n",
308 "<p>this line<br />\nhas a break</p>\n",
309
310 "this line \ndoes not\n",
311 "<p>this line\ndoes not</p>\n",
312
313 "this has an \nextra space\n",
314 "<p>this has an<br />\nextra space</p>\n",
315 }
316 doTestsInline(t, tests)
317}
318
319func TestInlineLink(t *testing.T) {
320 var tests = []string{
321 "[foo](/bar/)\n",
322 "<p><a href=\"/bar/\">foo</a></p>\n",
323
324 "[foo with a title](/bar/ \"title\")\n",
325 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
326
327 "[foo with a title](/bar/\t\"title\")\n",
328 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
329
330 "[foo with a title](/bar/ \"title\" )\n",
331 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
332
333 "[foo with a title](/bar/ title with no quotes)\n",
334 "<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n",
335
336 "[foo]()\n",
337 "<p>[foo]()</p>\n",
338
339 "![foo](/bar/)\n",
340 "<p><img src=\"/bar/\" alt=\"foo\" />\n</p>\n",
341
342 "![foo with a title](/bar/ \"title\")\n",
343 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
344
345 "![foo with a title](/bar/\t\"title\")\n",
346 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
347
348 "![foo with a title](/bar/ \"title\" )\n",
349 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
350
351 "![foo with a title](/bar/ title with no quotes)\n",
352 "<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" />\n</p>\n",
353
354 "![foo]()\n",
355 "<p>![foo]()</p>\n",
356
357 "[a link]\t(/with_a_tab/)\n",
358 "<p><a href=\"/with_a_tab/\">a link</a></p>\n",
359
360 "[a link] (/with_spaces/)\n",
361 "<p><a href=\"/with_spaces/\">a link</a></p>\n",
362
363 "[text (with) [[nested] (brackets)]](/url/)\n",
364 "<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n",
365
366 "[text (with) [broken nested] (brackets)]](/url/)\n",
367 "<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n",
368
369 "[text\nwith a newline](/link/)\n",
370 "<p><a href=\"/link/\">text\nwith a newline</a></p>\n",
371
372 "[text in brackets] [followed](/by a link/)\n",
373 "<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n",
374
375 "[link with\\] a closing bracket](/url/)\n",
376 "<p><a href=\"/url/\">link with] a closing bracket</a></p>\n",
377
378 "[link with\\[ an opening bracket](/url/)\n",
379 "<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n",
380
381 "[link with\\) a closing paren](/url/)\n",
382 "<p><a href=\"/url/\">link with) a closing paren</a></p>\n",
383
384 "[link with\\( an opening paren](/url/)\n",
385 "<p><a href=\"/url/\">link with( an opening paren</a></p>\n",
386
387 "[link]( with whitespace)\n",
388 "<p><a href=\"with whitespace\">link</a></p>\n",
389
390 "[link]( with whitespace )\n",
391 "<p><a href=\"with whitespace\">link</a></p>\n",
392
393 "[link](url \"one quote)\n",
394 "<p><a href=\"url "one quote\">link</a></p>\n",
395
396 "[link](url 'one quote)\n",
397 "<p><a href=\"url 'one quote\">link</a></p>\n",
398
399 "[link](<url>)\n",
400 "<p><a href=\"url\">link</a></p>\n",
401
402 "[link & ampersand](/url/)\n",
403 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
404
405 "[link & ampersand](/url/)\n",
406 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
407
408 "[link](/url/&query)\n",
409 "<p><a href=\"/url/&query\">link</a></p>\n",
410 }
411 doTestsInline(t, tests)
412}
413
414func TestReferenceLink(t *testing.T) {
415 var tests = []string{
416 "[link][ref]\n",
417 "<p>[link][ref]</p>\n",
418
419 "[link][ref]\n [ref]: /url/ \"title\"\n",
420 "<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
421
422 "[link][ref]\n [ref]: /url/\n",
423 "<p><a href=\"/url/\">link</a></p>\n",
424
425 " [ref]: /url/\n",
426 "",
427
428 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
429 "",
430
431 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n [4spaces]: /url/\n",
432 "<pre><code>[4spaces]: /url/\n</code></pre>\n",
433
434 "[hmm](ref2)\n [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
435 "<p><a href=\"ref2\">hmm</a></p>\n",
436
437 "[ref]\n",
438 "<p>[ref]</p>\n",
439
440 "[ref]\n [ref]: /url/ \"title\"\n",
441 "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
442 }
443 doTestsInline(t, tests)
444}
445
446func TestTags(t *testing.T) {
447 var tests = []string{
448 "a <span>tag</span>\n",
449 "<p>a <span>tag</span></p>\n",
450
451 "<span>tag</span>\n",
452 "<p><span>tag</span></p>\n",
453
454 "<span>mismatch</spandex>\n",
455 "<p><span>mismatch</spandex></p>\n",
456
457 "a <singleton /> tag\n",
458 "<p>a <singleton /> tag</p>\n",
459 }
460 doTestsInline(t, tests)
461}
462
463func TestAutoLink(t *testing.T) {
464 var tests = []string{
465 "go to <http://foo.com/>\n",
466 "<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
467
468 "a secure <https://link.org>\n",
469 "<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
470
471 "an email <mailto:some@one.com>\n",
472 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
473
474 "an email <mailto://some@one.com>\n",
475 "<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
476
477 "an email <some@one.com>\n",
478 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
479
480 "an ftp <ftp://old.com>\n",
481 "<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
482
483 "an ftp <ftp:old.com>\n",
484 "<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
485
486 "a link with <http://new.com?query=foo&bar>\n",
487 "<p>a link with <a href=\"http://new.com?query=foo&bar\">" +
488 "http://new.com?query=foo&bar</a></p>\n",
489
490 "quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
491 "<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
492
493 "quotes mean a tag <http://new.com?query='foo'&bar>\n",
494 "<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
495
496 "unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
497 "<p>unless escaped <a href=\"http://new.com?query="foo"&bar\">" +
498 "http://new.com?query="foo"&bar</a></p>\n",
499
500 "even a > can be escaped <http://new.com?q=\\>&etc>\n",
501 "<p>even a > can be escaped <a href=\"http://new.com?q=>&etc\">" +
502 "http://new.com?q=>&etc</a></p>\n",
503 }
504 doTestsInline(t, tests)
505}
506
507func TestFootnotes(t *testing.T) {
508 tests := []string{
509 "testing footnotes.[^a]\n\n[^a]: This is the note\n",
510 `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
511<div class="footnotes">
512
513<hr />
514
515<ol>
516<li id="fn:a">This is the note
517</li>
518</ol>
519</div>
520`,
521
522 `testing long[^b] notes.
523
524[^b]: Paragraph 1
525
526 Paragraph 2
527
528 ` + "```\n\tsome code\n\t```" + `
529
530 Paragraph 3
531
532No longer in the footnote
533`,
534 `<p>testing long<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">1</a></sup> notes.</p>
535
536<p>No longer in the footnote</p>
537<div class="footnotes">
538
539<hr />
540
541<ol>
542<li id="fn:b"><p>Paragraph 1</p>
543
544<p>Paragraph 2</p>
545
546<p><code>
547some code
548</code></p>
549
550<p>Paragraph 3</p>
551</li>
552</ol>
553</div>
554`,
555
556 `testing[^c] multiple[^d] notes.
557
558[^c]: this is [note] c
559
560
561omg
562
563[^d]: this is note d
564
565what happens here
566
567[note]: /link/c
568
569`,
570 `<p>testing<sup class="footnote-ref" id="fnref:c"><a rel="footnote" href="#fn:c">1</a></sup> multiple<sup class="footnote-ref" id="fnref:d"><a rel="footnote" href="#fn:d">2</a></sup> notes.</p>
571
572<p>omg</p>
573
574<p>what happens here</p>
575<div class="footnotes">
576
577<hr />
578
579<ol>
580<li id="fn:c">this is <a href="/link/c">note</a> c
581</li>
582<li id="fn:d">this is note d
583</li>
584</ol>
585</div>
586`,
587
588 "testing inline^[this is the note] notes.\n",
589 `<p>testing inline<sup class="footnote-ref" id="fnref:this-is-the-note"><a rel="footnote" href="#fn:this-is-the-note">1</a></sup> notes.</p>
590<div class="footnotes">
591
592<hr />
593
594<ol>
595<li id="fn:this-is-the-note">this is the note</li>
596</ol>
597</div>
598`,
599
600 "testing multiple[^1] types^[inline note] of notes[^2]\n\n[^2]: the second deferred note\n[^1]: the first deferred note\n\n\twhich happens to be a block\n",
601 `<p>testing multiple<sup class="footnote-ref" id="fnref:1"><a rel="footnote" href="#fn:1">1</a></sup> types<sup class="footnote-ref" id="fnref:inline-note"><a rel="footnote" href="#fn:inline-note">2</a></sup> of notes<sup class="footnote-ref" id="fnref:2"><a rel="footnote" href="#fn:2">3</a></sup></p>
602<div class="footnotes">
603
604<hr />
605
606<ol>
607<li id="fn:1"><p>the first deferred note</p>
608
609<p>which happens to be a block</p>
610</li>
611<li id="fn:inline-note">inline note</li>
612<li id="fn:2">the second deferred note
613</li>
614</ol>
615</div>
616`,
617
618 `This is a footnote[^1]^[and this is an inline footnote]
619
620[^1]: the footnote text.
621
622 may be multiple paragraphs.
623`,
624 `<p>This is a footnote<sup class="footnote-ref" id="fnref:1"><a rel="footnote" href="#fn:1">1</a></sup><sup class="footnote-ref" id="fnref:and-this-is-an-i"><a rel="footnote" href="#fn:and-this-is-an-i">2</a></sup></p>
625<div class="footnotes">
626
627<hr />
628
629<ol>
630<li id="fn:1"><p>the footnote text.</p>
631
632<p>may be multiple paragraphs.</p>
633</li>
634<li id="fn:and-this-is-an-i">and this is an inline footnote</li>
635</ol>
636</div>
637`,
638
639 "empty footnote[^]\n\n[^]: fn text",
640 "<p>empty footnote<sup class=\"footnote-ref\" id=\"fnref:\"><a rel=\"footnote\" href=\"#fn:\">1</a></sup></p>\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:\">fn text\n</li>\n</ol>\n</div>\n",
641 }
642
643 doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, 0)
644}