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 doSafeTestsInline(t *testing.T, tests []string) {
36 doTestsInlineParam(t, tests, 0, HTML_SAFELINK)
37}
38
39func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int) {
40 // catch and report panics
41 var candidate string
42 /*
43 defer func() {
44 if err := recover(); err != nil {
45 t.Errorf("\npanic while processing [%#v] (%v)\n", candidate, err)
46 }
47 }()
48 */
49
50 for i := 0; i+1 < len(tests); i += 2 {
51 input := tests[i]
52 candidate = input
53 expected := tests[i+1]
54 actual := runMarkdownInline(candidate, extensions, htmlFlags)
55 if actual != expected {
56 t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
57 candidate, expected, actual)
58 }
59
60 // now test every substring to stress test bounds checking
61 if !testing.Short() {
62 for start := 0; start < len(input); start++ {
63 for end := start + 1; end <= len(input); end++ {
64 candidate = input[start:end]
65 _ = runMarkdownInline(candidate, extensions, htmlFlags)
66 }
67 }
68 }
69 }
70}
71
72func TestRawHtmlTag(t *testing.T) {
73 tests := []string{
74 "zz <style>p {}</style>\n",
75 "<p>zz <style>p {}</style></p>\n",
76
77 "zz <STYLE>p {}</STYLE>\n",
78 "<p>zz <style>p {}</style></p>\n",
79
80 "<SCRIPT>alert()</SCRIPT>\n",
81 "<p><script>alert()</script></p>\n",
82
83 "zz <SCRIPT>alert()</SCRIPT>\n",
84 "<p>zz <script>alert()</script></p>\n",
85
86 "zz <script>alert()</script>\n",
87 "<p>zz <script>alert()</script></p>\n",
88
89 " <script>alert()</script>\n",
90 "<p><script>alert()</script></p>\n",
91
92 "<script>alert()</script>\n",
93 "<script>alert()</script>\n",
94
95 "<script src='foo'></script>\n",
96 "<script src='foo'></script>\n",
97
98 "<script src='a>b'></script>\n",
99 "<script src='a>b'></script>\n",
100
101 "zz <script src='foo'></script>\n",
102 "<p>zz <script src='foo'></script></p>\n",
103
104 "zz <script src=foo></script>\n",
105 "<p>zz <script src=foo></script></p>\n",
106
107 `<script><script src="http://example.com/exploit.js"></SCRIPT></script>`,
108 "<script><script src="http://example.com/exploit.js"></script></script>\n",
109
110 `'';!--"<XSS>=&{()}`,
111 "<p>'';!--"<xss>=&{()}</p>\n",
112
113 "<SCRIPT SRC=http://ha.ckers.org/xss.js></SCRIPT>",
114 "<p><script SRC=http://ha.ckers.org/xss.js></script></p>\n",
115
116 "<SCRIPT \nSRC=http://ha.ckers.org/xss.js></SCRIPT>",
117 "<p><script \nSRC=http://ha.ckers.org/xss.js></script></p>\n",
118
119 `<IMG SRC="javascript:alert('XSS');">`,
120 "<p><img></p>\n",
121
122 "<IMG SRC=javascript:alert('XSS')>",
123 "<p><img></p>\n",
124
125 "<IMG SRC=JaVaScRiPt:alert('XSS')>",
126 "<p><img></p>\n",
127
128 "<IMG SRC=`javascript:alert(\"RSnake says, 'XSS'\")`>",
129 "<p><img></p>\n",
130
131 `<a onmouseover="alert(document.cookie)">xss link</a>`,
132 "<p><a>xss link</a></p>\n",
133
134 "<a onmouseover=alert(document.cookie)>xss link</a>",
135 "<p><a>xss link</a></p>\n",
136
137 `<IMG """><SCRIPT>alert("XSS")</SCRIPT>">`,
138 "<p><img><script>alert(&quot;XSS&quot;)</script>"></p>\n",
139
140 "<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>",
141 "<p><img></p>\n",
142
143 `<IMG SRC=# onmouseover="alert('xxs')">`,
144 "<p><img></p>\n",
145
146 `<IMG SRC= onmouseover="alert('xxs')">`,
147 "<p><img></p>\n",
148
149 `<IMG onmouseover="alert('xxs')">`,
150 "<p><img></p>\n",
151
152 "<IMG SRC=javascript:alert('XSS')>",
153 "<p><img></p>\n",
154
155 "<IMG SRC=javascript:alert('XSS')>",
156 "<p><img></p>\n",
157
158 "<IMG SRC=javascript:alert('XSS')>",
159 "<p><img></p>\n",
160
161 `<IMG SRC="javascriptascript:alert('XSS');">`,
162 "<p><img></p>\n",
163
164 `<IMG SRC="jav	ascript:alert('XSS');">`,
165 "<p><img></p>\n",
166
167 `<IMG SRC="jav
ascript:alert('XSS');">`,
168 "<p><img></p>\n",
169
170 `<IMG SRC="jav
ascript:alert('XSS');">`,
171 "<p><img></p>\n",
172
173 `<IMG SRC="  javascript:alert('XSS');">`,
174 "<p><img></p>\n",
175
176 `<SCRIPT/XSS SRC="http://ha.ckers.org/xss.js"></SCRIPT>`,
177 "<p><script/XSS SRC="http://ha.ckers.org/xss.js"></script></p>\n",
178
179 "<BODY onload!#$%&()*~+-_.,:;?@[/|\\]^`=alert(\"XSS\")>",
180 "<p><body onload!#$%&()*~+-_.,:;?@[/|\\]^`=alert("XSS")></p>\n",
181
182 `<SCRIPT/SRC="http://ha.ckers.org/xss.js"></SCRIPT>`,
183 "<p><script/SRC="http://ha.ckers.org/xss.js"></script></p>\n",
184
185 // HTML5 interprets the <script> tag contents as raw test, thus the end
186 // result has double-escaped &quot;
187 `<<SCRIPT>alert("XSS");//<</SCRIPT>`,
188 "<p><<script>alert(&quot;XSS&quot;);//&lt;</script></p>\n",
189
190 // HTML5 parses the </p> within an unclosed <script> tag as text.
191 // Same for the following tests.
192 "<SCRIPT SRC=http://ha.ckers.org/xss.js?< B >",
193 "<p><script SRC=http://ha.ckers.org/xss.js?< B ></p>\n",
194
195 "<SCRIPT SRC=//ha.ckers.org/.j>",
196 "<p><script SRC=//ha.ckers.org/.j></p>\n",
197
198 `<IMG SRC="javascript:alert('XSS')"`,
199 "<p><IMG SRC="javascript:alert('XSS')"</p>\n",
200
201 "<iframe src=http://ha.ckers.org/scriptlet.html <",
202 // The hyperlink gets linkified, the <iframe> gets escaped
203 "<p><iframe src=<a href=\"http://ha.ckers.org/scriptlet.html\">http://ha.ckers.org/scriptlet.html</a> <</p>\n",
204
205 // Additonal token types: SelfClosing, Comment, DocType.
206 "<br/>",
207 "<p><br></p>\n",
208
209 "<!-- Comment -->",
210 "<!-- Comment -->\n",
211
212 "<!DOCTYPE test>",
213 "<p><!DOCTYPE test></p>\n",
214
215 "<hr>",
216 "<hr>\n",
217 }
218 doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE|HTML_SANITIZE_OUTPUT)
219}
220
221func TestEmphasis(t *testing.T) {
222 var tests = []string{
223 "nothing inline\n",
224 "<p>nothing inline</p>\n",
225
226 "simple *inline* test\n",
227 "<p>simple <em>inline</em> test</p>\n",
228
229 "*at the* beginning\n",
230 "<p><em>at the</em> beginning</p>\n",
231
232 "at the *end*\n",
233 "<p>at the <em>end</em></p>\n",
234
235 "*try two* in *one line*\n",
236 "<p><em>try two</em> in <em>one line</em></p>\n",
237
238 "over *two\nlines* test\n",
239 "<p>over <em>two\nlines</em> test</p>\n",
240
241 "odd *number of* markers* here\n",
242 "<p>odd <em>number of</em> markers* here</p>\n",
243
244 "odd *number\nof* markers* here\n",
245 "<p>odd <em>number\nof</em> markers* here</p>\n",
246
247 "simple _inline_ test\n",
248 "<p>simple <em>inline</em> test</p>\n",
249
250 "_at the_ beginning\n",
251 "<p><em>at the</em> beginning</p>\n",
252
253 "at the _end_\n",
254 "<p>at the <em>end</em></p>\n",
255
256 "_try two_ in _one line_\n",
257 "<p><em>try two</em> in <em>one line</em></p>\n",
258
259 "over _two\nlines_ test\n",
260 "<p>over <em>two\nlines</em> test</p>\n",
261
262 "odd _number of_ markers_ here\n",
263 "<p>odd <em>number of</em> markers_ here</p>\n",
264
265 "odd _number\nof_ markers_ here\n",
266 "<p>odd <em>number\nof</em> markers_ here</p>\n",
267
268 "mix of *markers_\n",
269 "<p>mix of *markers_</p>\n",
270 }
271 doTestsInline(t, tests)
272}
273
274func TestStrong(t *testing.T) {
275 var tests = []string{
276 "nothing inline\n",
277 "<p>nothing inline</p>\n",
278
279 "simple **inline** test\n",
280 "<p>simple <strong>inline</strong> test</p>\n",
281
282 "**at the** beginning\n",
283 "<p><strong>at the</strong> beginning</p>\n",
284
285 "at the **end**\n",
286 "<p>at the <strong>end</strong></p>\n",
287
288 "**try two** in **one line**\n",
289 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
290
291 "over **two\nlines** test\n",
292 "<p>over <strong>two\nlines</strong> test</p>\n",
293
294 "odd **number of** markers** here\n",
295 "<p>odd <strong>number of</strong> markers** here</p>\n",
296
297 "odd **number\nof** markers** here\n",
298 "<p>odd <strong>number\nof</strong> markers** here</p>\n",
299
300 "simple __inline__ test\n",
301 "<p>simple <strong>inline</strong> test</p>\n",
302
303 "__at the__ beginning\n",
304 "<p><strong>at the</strong> beginning</p>\n",
305
306 "at the __end__\n",
307 "<p>at the <strong>end</strong></p>\n",
308
309 "__try two__ in __one line__\n",
310 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
311
312 "over __two\nlines__ test\n",
313 "<p>over <strong>two\nlines</strong> test</p>\n",
314
315 "odd __number of__ markers__ here\n",
316 "<p>odd <strong>number of</strong> markers__ here</p>\n",
317
318 "odd __number\nof__ markers__ here\n",
319 "<p>odd <strong>number\nof</strong> markers__ here</p>\n",
320
321 "mix of **markers__\n",
322 "<p>mix of **markers__</p>\n",
323 }
324 doTestsInline(t, tests)
325}
326
327func TestEmphasisMix(t *testing.T) {
328 var tests = []string{
329 "***triple emphasis***\n",
330 "<p><strong><em>triple emphasis</em></strong></p>\n",
331
332 "***triple\nemphasis***\n",
333 "<p><strong><em>triple\nemphasis</em></strong></p>\n",
334
335 "___triple emphasis___\n",
336 "<p><strong><em>triple emphasis</em></strong></p>\n",
337
338 "***triple emphasis___\n",
339 "<p>***triple emphasis___</p>\n",
340
341 "*__triple emphasis__*\n",
342 "<p><em><strong>triple emphasis</strong></em></p>\n",
343
344 "__*triple emphasis*__\n",
345 "<p><strong><em>triple emphasis</em></strong></p>\n",
346
347 "**improper *nesting** is* bad\n",
348 "<p><strong>improper *nesting</strong> is* bad</p>\n",
349
350 "*improper **nesting* is** bad\n",
351 "<p><em>improper **nesting</em> is** bad</p>\n",
352 }
353 doTestsInline(t, tests)
354}
355
356func TestStrikeThrough(t *testing.T) {
357 var tests = []string{
358 "nothing inline\n",
359 "<p>nothing inline</p>\n",
360
361 "simple ~~inline~~ test\n",
362 "<p>simple <del>inline</del> test</p>\n",
363
364 "~~at the~~ beginning\n",
365 "<p><del>at the</del> beginning</p>\n",
366
367 "at the ~~end~~\n",
368 "<p>at the <del>end</del></p>\n",
369
370 "~~try two~~ in ~~one line~~\n",
371 "<p><del>try two</del> in <del>one line</del></p>\n",
372
373 "over ~~two\nlines~~ test\n",
374 "<p>over <del>two\nlines</del> test</p>\n",
375
376 "odd ~~number of~~ markers~~ here\n",
377 "<p>odd <del>number of</del> markers~~ here</p>\n",
378
379 "odd ~~number\nof~~ markers~~ here\n",
380 "<p>odd <del>number\nof</del> markers~~ here</p>\n",
381 }
382 doTestsInline(t, tests)
383}
384
385func TestCodeSpan(t *testing.T) {
386 var tests = []string{
387 "`source code`\n",
388 "<p><code>source code</code></p>\n",
389
390 "` source code with spaces `\n",
391 "<p><code>source code with spaces</code></p>\n",
392
393 "` source code with spaces `not here\n",
394 "<p><code>source code with spaces</code>not here</p>\n",
395
396 "a `single marker\n",
397 "<p>a `single marker</p>\n",
398
399 "a single multi-tick marker with ``` no text\n",
400 "<p>a single multi-tick marker with ``` no text</p>\n",
401
402 "markers with ` ` a space\n",
403 "<p>markers with a space</p>\n",
404
405 "`source code` and a `stray\n",
406 "<p><code>source code</code> and a `stray</p>\n",
407
408 "`source *with* _awkward characters_ in it`\n",
409 "<p><code>source *with* _awkward characters_ in it</code></p>\n",
410
411 "`split over\ntwo lines`\n",
412 "<p><code>split over\ntwo lines</code></p>\n",
413
414 "```multiple ticks``` for the marker\n",
415 "<p><code>multiple ticks</code> for the marker</p>\n",
416
417 "```multiple ticks `with` ticks inside```\n",
418 "<p><code>multiple ticks `with` ticks inside</code></p>\n",
419 }
420 doTestsInline(t, tests)
421}
422
423func TestLineBreak(t *testing.T) {
424 var tests = []string{
425 "this line \nhas a break\n",
426 "<p>this line<br />\nhas a break</p>\n",
427
428 "this line \ndoes not\n",
429 "<p>this line\ndoes not</p>\n",
430
431 "this has an \nextra space\n",
432 "<p>this has an<br />\nextra space</p>\n",
433 }
434 doTestsInline(t, tests)
435}
436
437func TestInlineLink(t *testing.T) {
438 var tests = []string{
439 "[foo](/bar/)\n",
440 "<p><a href=\"/bar/\">foo</a></p>\n",
441
442 "[foo with a title](/bar/ \"title\")\n",
443 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
444
445 "[foo with a title](/bar/\t\"title\")\n",
446 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
447
448 "[foo with a title](/bar/ \"title\" )\n",
449 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
450
451 "[foo with a title](/bar/ title with no quotes)\n",
452 "<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n",
453
454 "[foo]()\n",
455 "<p>[foo]()</p>\n",
456
457 "![foo](/bar/)\n",
458 "<p><img src=\"/bar/\" alt=\"foo\" />\n</p>\n",
459
460 "![foo with a title](/bar/ \"title\")\n",
461 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
462
463 "![foo with a title](/bar/\t\"title\")\n",
464 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
465
466 "![foo with a title](/bar/ \"title\" )\n",
467 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
468
469 "![foo with a title](/bar/ title with no quotes)\n",
470 "<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" />\n</p>\n",
471
472 "![foo]()\n",
473 "<p>![foo]()</p>\n",
474
475 "[a link]\t(/with_a_tab/)\n",
476 "<p><a href=\"/with_a_tab/\">a link</a></p>\n",
477
478 "[a link] (/with_spaces/)\n",
479 "<p><a href=\"/with_spaces/\">a link</a></p>\n",
480
481 "[text (with) [[nested] (brackets)]](/url/)\n",
482 "<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n",
483
484 "[text (with) [broken nested] (brackets)]](/url/)\n",
485 "<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n",
486
487 "[text\nwith a newline](/link/)\n",
488 "<p><a href=\"/link/\">text\nwith a newline</a></p>\n",
489
490 "[text in brackets] [followed](/by a link/)\n",
491 "<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n",
492
493 "[link with\\] a closing bracket](/url/)\n",
494 "<p><a href=\"/url/\">link with] a closing bracket</a></p>\n",
495
496 "[link with\\[ an opening bracket](/url/)\n",
497 "<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n",
498
499 "[link with\\) a closing paren](/url/)\n",
500 "<p><a href=\"/url/\">link with) a closing paren</a></p>\n",
501
502 "[link with\\( an opening paren](/url/)\n",
503 "<p><a href=\"/url/\">link with( an opening paren</a></p>\n",
504
505 "[link]( with whitespace)\n",
506 "<p><a href=\"with whitespace\">link</a></p>\n",
507
508 "[link]( with whitespace )\n",
509 "<p><a href=\"with whitespace\">link</a></p>\n",
510
511 "[![image](someimage)](with image)\n",
512 "<p><a href=\"with image\"><img src=\"someimage\" alt=\"image\" />\n</a></p>\n",
513
514 "[link](url \"one quote)\n",
515 "<p><a href=\"url "one quote\">link</a></p>\n",
516
517 "[link](url 'one quote)\n",
518 "<p><a href=\"url 'one quote\">link</a></p>\n",
519
520 "[link](<url>)\n",
521 "<p><a href=\"url\">link</a></p>\n",
522
523 "[link & ampersand](/url/)\n",
524 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
525
526 "[link & ampersand](/url/)\n",
527 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
528
529 "[link](/url/&query)\n",
530 "<p><a href=\"/url/&query\">link</a></p>\n",
531
532 "[[t]](/t)\n",
533 "<p><a href=\"/t\">[t]</a></p>\n",
534 }
535 doTestsInline(t, tests)
536}
537
538func TestNofollowLink(t *testing.T) {
539 var tests = []string{
540 "[foo](http://bar.com/foo/)\n",
541 "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n",
542 }
543 doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_SANITIZE_OUTPUT)
544 // HTML_SANITIZE_OUTPUT won't allow relative links, so test that separately:
545 tests = []string{
546 "[foo](/bar/)\n",
547 "<p><a href=\"/bar/\">foo</a></p>\n",
548 }
549 doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS)
550}
551
552func TestHrefTargetBlank(t *testing.T) {
553 var tests = []string{
554 // internal link
555 "[foo](/bar/)\n",
556 "<p><a href=\"/bar/\">foo</a></p>\n",
557
558 "[foo](http://example.com)\n",
559 "<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n",
560 }
561 doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_HREF_TARGET_BLANK)
562}
563
564func TestSafeInlineLink(t *testing.T) {
565 var tests = []string{
566 "[foo](/bar/)\n",
567 "<p><a href=\"/bar/\">foo</a></p>\n",
568
569 "[foo](http://bar/)\n",
570 "<p><a href=\"http://bar/\">foo</a></p>\n",
571
572 "[foo](https://bar/)\n",
573 "<p><a href=\"https://bar/\">foo</a></p>\n",
574
575 "[foo](ftp://bar/)\n",
576 "<p><a href=\"ftp://bar/\">foo</a></p>\n",
577
578 "[foo](mailto://bar/)\n",
579 "<p><a href=\"mailto://bar/\">foo</a></p>\n",
580
581 // Not considered safe
582 "[foo](baz://bar/)\n",
583 "<p><tt>foo</tt></p>\n",
584 }
585 doSafeTestsInline(t, tests)
586}
587
588func TestReferenceLink(t *testing.T) {
589 var tests = []string{
590 "[link][ref]\n",
591 "<p>[link][ref]</p>\n",
592
593 "[link][ref]\n [ref]: /url/ \"title\"\n",
594 "<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
595
596 "[link][ref]\n [ref]: /url/\n",
597 "<p><a href=\"/url/\">link</a></p>\n",
598
599 " [ref]: /url/\n",
600 "",
601
602 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
603 "",
604
605 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n [4spaces]: /url/\n",
606 "<pre><code>[4spaces]: /url/\n</code></pre>\n",
607
608 "[hmm](ref2)\n [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
609 "<p><a href=\"ref2\">hmm</a></p>\n",
610
611 "[ref]\n",
612 "<p>[ref]</p>\n",
613
614 "[ref]\n [ref]: /url/ \"title\"\n",
615 "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
616 }
617 doTestsInline(t, tests)
618}
619
620func TestTags(t *testing.T) {
621 var tests = []string{
622 "a <span>tag</span>\n",
623 "<p>a <span>tag</span></p>\n",
624
625 "<span>tag</span>\n",
626 "<p><span>tag</span></p>\n",
627
628 "<span>mismatch</spandex>\n",
629 "<p><span>mismatch</spandex></p>\n",
630
631 "a <singleton /> tag\n",
632 "<p>a <singleton /> tag</p>\n",
633 }
634 doTestsInline(t, tests)
635}
636
637func TestAutoLink(t *testing.T) {
638 var tests = []string{
639 "http://foo.com/\n",
640 "<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
641
642 "1 http://foo.com/\n",
643 "<p>1 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
644
645 "1http://foo.com/\n",
646 "<p>1<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
647
648 "1.http://foo.com/\n",
649 "<p>1.<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
650
651 "1. http://foo.com/\n",
652 "<ol>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ol>\n",
653
654 "-http://foo.com/\n",
655 "<p>-<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
656
657 "- http://foo.com/\n",
658 "<ul>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ul>\n",
659
660 "_http://foo.com/\n",
661 "<p>_<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
662
663 "令狐http://foo.com/\n",
664 "<p>令狐<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
665
666 "令狐 http://foo.com/\n",
667 "<p>令狐 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
668
669 "ahttp://foo.com/\n",
670 "<p>ahttp://foo.com/</p>\n",
671
672 ">http://foo.com/\n",
673 "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
674
675 "> http://foo.com/\n",
676 "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
677
678 "go to <http://foo.com/>\n",
679 "<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
680
681 "a secure <https://link.org>\n",
682 "<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
683
684 "an email <mailto:some@one.com>\n",
685 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
686
687 "an email <mailto://some@one.com>\n",
688 "<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
689
690 "an email <some@one.com>\n",
691 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
692
693 "an ftp <ftp://old.com>\n",
694 "<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
695
696 "an ftp <ftp:old.com>\n",
697 "<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
698
699 "a link with <http://new.com?query=foo&bar>\n",
700 "<p>a link with <a href=\"http://new.com?query=foo&bar\">" +
701 "http://new.com?query=foo&bar</a></p>\n",
702
703 "quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
704 "<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
705
706 "quotes mean a tag <http://new.com?query='foo'&bar>\n",
707 "<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
708
709 "unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
710 "<p>unless escaped <a href=\"http://new.com?query="foo"&bar\">" +
711 "http://new.com?query="foo"&bar</a></p>\n",
712
713 "even a > can be escaped <http://new.com?q=\\>&etc>\n",
714 "<p>even a > can be escaped <a href=\"http://new.com?q=>&etc\">" +
715 "http://new.com?q=>&etc</a></p>\n",
716
717 "<a href=\"http://fancy.com\">http://fancy.com</a>\n",
718 "<p><a href=\"http://fancy.com\">http://fancy.com</a></p>\n",
719
720 "<a href=\"http://fancy.com\">This is a link</a>\n",
721 "<p><a href=\"http://fancy.com\">This is a link</a></p>\n",
722
723 "<a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a>\n",
724 "<p><a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a></p>\n",
725
726 "(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (\n",
727 "<p>(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (</p>\n",
728
729 "(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (part two: <a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a>)).\n",
730 "<p>(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (part two: <a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a>)).</p>\n",
731
732 "http://www.foo.com<br />\n",
733 "<p><a href=\"http://www.foo.com\">http://www.foo.com</a><br /></p>\n",
734
735 "http://foo.com/viewtopic.php?f=18&t=297",
736 "<p><a href=\"http://foo.com/viewtopic.php?f=18&t=297\">http://foo.com/viewtopic.php?f=18&t=297</a></p>\n",
737
738 "http://foo.com/viewtopic.php?param="18"zz",
739 "<p><a href=\"http://foo.com/viewtopic.php?param="18"zz\">http://foo.com/viewtopic.php?param="18"zz</a></p>\n",
740
741 "http://foo.com/viewtopic.php?param="18"",
742 "<p><a href=\"http://foo.com/viewtopic.php?param="18"\">http://foo.com/viewtopic.php?param="18"</a></p>\n",
743 }
744 doTestsInline(t, tests)
745}
746
747func TestFootnotes(t *testing.T) {
748 tests := []string{
749 "testing footnotes.[^a]\n\n[^a]: This is the note\n",
750 `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
751<div class="footnotes">
752
753<hr />
754
755<ol>
756<li id="fn:a">This is the note
757</li>
758</ol>
759</div>
760`,
761
762 `testing long[^b] notes.
763
764[^b]: Paragraph 1
765
766 Paragraph 2
767
768 ` + "```\n\tsome code\n\t```" + `
769
770 Paragraph 3
771
772No longer in the footnote
773`,
774 `<p>testing long<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">1</a></sup> notes.</p>
775
776<p>No longer in the footnote</p>
777<div class="footnotes">
778
779<hr />
780
781<ol>
782<li id="fn:b"><p>Paragraph 1</p>
783
784<p>Paragraph 2</p>
785
786<p><code>
787some code
788</code></p>
789
790<p>Paragraph 3</p>
791</li>
792</ol>
793</div>
794`,
795
796 `testing[^c] multiple[^d] notes.
797
798[^c]: this is [note] c
799
800
801omg
802
803[^d]: this is note d
804
805what happens here
806
807[note]: /link/c
808
809`,
810 `<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>
811
812<p>omg</p>
813
814<p>what happens here</p>
815<div class="footnotes">
816
817<hr />
818
819<ol>
820<li id="fn:c">this is <a href="/link/c">note</a> c
821</li>
822<li id="fn:d">this is note d
823</li>
824</ol>
825</div>
826`,
827
828 "testing inline^[this is the note] notes.\n",
829 `<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>
830<div class="footnotes">
831
832<hr />
833
834<ol>
835<li id="fn:this-is-the-note">this is the note</li>
836</ol>
837</div>
838`,
839
840 "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",
841 `<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>
842<div class="footnotes">
843
844<hr />
845
846<ol>
847<li id="fn:1"><p>the first deferred note</p>
848
849<p>which happens to be a block</p>
850</li>
851<li id="fn:inline-note">inline note</li>
852<li id="fn:2">the second deferred note
853</li>
854</ol>
855</div>
856`,
857
858 `This is a footnote[^1]^[and this is an inline footnote]
859
860[^1]: the footnote text.
861
862 may be multiple paragraphs.
863`,
864 `<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>
865<div class="footnotes">
866
867<hr />
868
869<ol>
870<li id="fn:1"><p>the footnote text.</p>
871
872<p>may be multiple paragraphs.</p>
873</li>
874<li id="fn:and-this-is-an-i">and this is an inline footnote</li>
875</ol>
876</div>
877`,
878
879 "empty footnote[^]\n\n[^]: fn text",
880 "<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",
881 }
882
883 doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, 0)
884}