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 "regexp"
18 "testing"
19
20 "strings"
21)
22
23func TestEmphasis(t *testing.T) {
24 var tests = []string{
25 "nothing inline\n",
26 "<p>nothing inline</p>\n",
27
28 "simple *inline* test\n",
29 "<p>simple <em>inline</em> test</p>\n",
30
31 "*at the* beginning\n",
32 "<p><em>at the</em> beginning</p>\n",
33
34 "at the *end*\n",
35 "<p>at the <em>end</em></p>\n",
36
37 "*try two* in *one line*\n",
38 "<p><em>try two</em> in <em>one line</em></p>\n",
39
40 "over *two\nlines* test\n",
41 "<p>over <em>two\nlines</em> test</p>\n",
42
43 "odd *number of* markers* here\n",
44 "<p>odd <em>number of</em> markers* here</p>\n",
45
46 "odd *number\nof* markers* here\n",
47 "<p>odd <em>number\nof</em> markers* here</p>\n",
48
49 "simple _inline_ test\n",
50 "<p>simple <em>inline</em> test</p>\n",
51
52 "_at the_ beginning\n",
53 "<p><em>at the</em> beginning</p>\n",
54
55 "at the _end_\n",
56 "<p>at the <em>end</em></p>\n",
57
58 "_try two_ in _one line_\n",
59 "<p><em>try two</em> in <em>one line</em></p>\n",
60
61 "over _two\nlines_ test\n",
62 "<p>over <em>two\nlines</em> test</p>\n",
63
64 "odd _number of_ markers_ here\n",
65 "<p>odd <em>number of</em> markers_ here</p>\n",
66
67 "odd _number\nof_ markers_ here\n",
68 "<p>odd <em>number\nof</em> markers_ here</p>\n",
69
70 "mix of *markers_\n",
71 "<p>mix of *markers_</p>\n",
72
73 "*What is A\\* algorithm?*\n",
74 "<p><em>What is A* algorithm?</em></p>\n",
75 }
76 doTestsInline(t, tests)
77}
78
79func TestReferenceOverride(t *testing.T) {
80 var tests = []string{
81 "test [ref1][]\n",
82 "<p>test <a href=\"http://www.ref1.com/\" title=\"Reference 1\">ref1</a></p>\n",
83
84 "test [my ref][ref1]\n",
85 "<p>test <a href=\"http://www.ref1.com/\" title=\"Reference 1\">my ref</a></p>\n",
86
87 "test [ref2][]\n\n[ref2]: http://www.leftalone.com/ (Ref left alone)\n",
88 "<p>test <a href=\"http://www.overridden.com/\" title=\"Reference Overridden\">ref2</a></p>\n",
89
90 "test [ref3][]\n\n[ref3]: http://www.leftalone.com/ (Ref left alone)\n",
91 "<p>test <a href=\"http://www.leftalone.com/\" title=\"Ref left alone\">ref3</a></p>\n",
92
93 "test [ref4][]\n\n[ref4]: http://zombo.com/ (You can do anything)\n",
94 "<p>test [ref4][]</p>\n",
95
96 "test [!(*http.ServeMux).ServeHTTP][] complicated ref\n",
97 "<p>test <a href=\"http://localhost:6060/pkg/net/http/#ServeMux.ServeHTTP\" title=\"ServeHTTP docs\">!(*http.ServeMux).ServeHTTP</a> complicated ref</p>\n",
98
99 "test [ref5][]\n",
100 "<p>test <a href=\"http://www.ref5.com/\" title=\"Reference 5\">Moo</a></p>\n",
101 }
102 doTestsInlineParam(t, tests, TestParams{
103 Options: Options{
104 ReferenceOverride: func(reference string) (rv *Reference, overridden bool) {
105 switch reference {
106 case "ref1":
107 // just an overriden reference exists without definition
108 return &Reference{
109 Link: "http://www.ref1.com/",
110 Title: "Reference 1"}, true
111 case "ref2":
112 // overridden exists and reference defined
113 return &Reference{
114 Link: "http://www.overridden.com/",
115 Title: "Reference Overridden"}, true
116 case "ref3":
117 // not overridden and reference defined
118 return nil, false
119 case "ref4":
120 // overridden missing and defined
121 return nil, true
122 case "!(*http.ServeMux).ServeHTTP":
123 return &Reference{
124 Link: "http://localhost:6060/pkg/net/http/#ServeMux.ServeHTTP",
125 Title: "ServeHTTP docs"}, true
126 case "ref5":
127 return &Reference{
128 Link: "http://www.ref5.com/",
129 Title: "Reference 5",
130 Text: "Moo",
131 }, true
132 }
133 return nil, false
134 },
135 },
136 })
137}
138
139func TestStrong(t *testing.T) {
140 var tests = []string{
141 "nothing inline\n",
142 "<p>nothing inline</p>\n",
143
144 "simple **inline** test\n",
145 "<p>simple <strong>inline</strong> test</p>\n",
146
147 "**at the** beginning\n",
148 "<p><strong>at the</strong> beginning</p>\n",
149
150 "at the **end**\n",
151 "<p>at the <strong>end</strong></p>\n",
152
153 "**try two** in **one line**\n",
154 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
155
156 "over **two\nlines** test\n",
157 "<p>over <strong>two\nlines</strong> test</p>\n",
158
159 "odd **number of** markers** here\n",
160 "<p>odd <strong>number of</strong> markers** here</p>\n",
161
162 "odd **number\nof** markers** here\n",
163 "<p>odd <strong>number\nof</strong> markers** here</p>\n",
164
165 "simple __inline__ test\n",
166 "<p>simple <strong>inline</strong> test</p>\n",
167
168 "__at the__ beginning\n",
169 "<p><strong>at the</strong> beginning</p>\n",
170
171 "at the __end__\n",
172 "<p>at the <strong>end</strong></p>\n",
173
174 "__try two__ in __one line__\n",
175 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
176
177 "over __two\nlines__ test\n",
178 "<p>over <strong>two\nlines</strong> test</p>\n",
179
180 "odd __number of__ markers__ here\n",
181 "<p>odd <strong>number of</strong> markers__ here</p>\n",
182
183 "odd __number\nof__ markers__ here\n",
184 "<p>odd <strong>number\nof</strong> markers__ here</p>\n",
185
186 "mix of **markers__\n",
187 "<p>mix of **markers__</p>\n",
188
189 "**`/usr`** : this folder is named `usr`\n",
190 "<p><strong><code>/usr</code></strong> : this folder is named <code>usr</code></p>\n",
191
192 "**`/usr`** :\n\n this folder is named `usr`\n",
193 "<p><strong><code>/usr</code></strong> :</p>\n\n<p>this folder is named <code>usr</code></p>\n",
194 }
195 doTestsInline(t, tests)
196}
197
198func TestEmphasisMix(t *testing.T) {
199 var tests = []string{
200 "***triple emphasis***\n",
201 "<p><strong><em>triple emphasis</em></strong></p>\n",
202
203 "***triple\nemphasis***\n",
204 "<p><strong><em>triple\nemphasis</em></strong></p>\n",
205
206 "___triple emphasis___\n",
207 "<p><strong><em>triple emphasis</em></strong></p>\n",
208
209 "***triple emphasis___\n",
210 "<p>***triple emphasis___</p>\n",
211
212 "*__triple emphasis__*\n",
213 "<p><em><strong>triple emphasis</strong></em></p>\n",
214
215 "__*triple emphasis*__\n",
216 "<p><strong><em>triple emphasis</em></strong></p>\n",
217
218 "**improper *nesting** is* bad\n",
219 "<p><strong>improper *nesting</strong> is* bad</p>\n",
220
221 "*improper **nesting* is** bad\n",
222 "<p>*improper <strong>nesting* is</strong> bad</p>\n",
223 }
224 doTestsInline(t, tests)
225}
226
227func TestEmphasisLink(t *testing.T) {
228 var tests = []string{
229 "[first](before) *text[second] (inside)text* [third](after)\n",
230 "<p><a href=\"before\">first</a> <em>text<a href=\"inside\">second</a>text</em> <a href=\"after\">third</a></p>\n",
231
232 "*incomplete [link] definition*\n",
233 "<p><em>incomplete [link] definition</em></p>\n",
234
235 "*it's [emphasis*] (not link)\n",
236 "<p><em>it's [emphasis</em>] (not link)</p>\n",
237
238 "*it's [emphasis*] and *[asterisk]\n",
239 "<p><em>it's [emphasis</em>] and *[asterisk]</p>\n",
240 }
241 doTestsInline(t, tests)
242}
243
244func TestStrikeThrough(t *testing.T) {
245 var tests = []string{
246 "nothing inline\n",
247 "<p>nothing inline</p>\n",
248
249 "simple ~~inline~~ test\n",
250 "<p>simple <del>inline</del> test</p>\n",
251
252 "~~at the~~ beginning\n",
253 "<p><del>at the</del> beginning</p>\n",
254
255 "at the ~~end~~\n",
256 "<p>at the <del>end</del></p>\n",
257
258 "~~try two~~ in ~~one line~~\n",
259 "<p><del>try two</del> in <del>one line</del></p>\n",
260
261 "over ~~two\nlines~~ test\n",
262 "<p>over <del>two\nlines</del> test</p>\n",
263
264 "odd ~~number of~~ markers~~ here\n",
265 "<p>odd <del>number of</del> markers~~ here</p>\n",
266
267 "odd ~~number\nof~~ markers~~ here\n",
268 "<p>odd <del>number\nof</del> markers~~ here</p>\n",
269 }
270 doTestsInline(t, tests)
271}
272
273func TestCodeSpan(t *testing.T) {
274 var tests = []string{
275 "`source code`\n",
276 "<p><code>source code</code></p>\n",
277
278 "` source code with spaces `\n",
279 "<p><code>source code with spaces</code></p>\n",
280
281 "` source code with spaces `not here\n",
282 "<p><code>source code with spaces</code>not here</p>\n",
283
284 "a `single marker\n",
285 "<p>a `single marker</p>\n",
286
287 "a single multi-tick marker with ``` no text\n",
288 "<p>a single multi-tick marker with ``` no text</p>\n",
289
290 "markers with ` ` a space\n",
291 "<p>markers with a space</p>\n",
292
293 "`source code` and a `stray\n",
294 "<p><code>source code</code> and a `stray</p>\n",
295
296 "`source *with* _awkward characters_ in it`\n",
297 "<p><code>source *with* _awkward characters_ in it</code></p>\n",
298
299 "`split over\ntwo lines`\n",
300 "<p><code>split over\ntwo lines</code></p>\n",
301
302 "```multiple ticks``` for the marker\n",
303 "<p><code>multiple ticks</code> for the marker</p>\n",
304
305 "```multiple ticks `with` ticks inside```\n",
306 "<p><code>multiple ticks `with` ticks inside</code></p>\n",
307 }
308 doTestsInline(t, tests)
309}
310
311func TestLineBreak(t *testing.T) {
312 var tests = []string{
313 "this line \nhas a break\n",
314 "<p>this line<br />\nhas a break</p>\n",
315
316 "this line \ndoes not\n",
317 "<p>this line\ndoes not</p>\n",
318
319 "this line\\\ndoes not\n",
320 "<p>this line\\\ndoes not</p>\n",
321
322 "this line\\ \ndoes not\n",
323 "<p>this line\\\ndoes not</p>\n",
324
325 "this has an \nextra space\n",
326 "<p>this has an<br />\nextra space</p>\n",
327 }
328 doTestsInline(t, tests)
329
330 tests = []string{
331 "this line \nhas a break\n",
332 "<p>this line<br />\nhas a break</p>\n",
333
334 "this line \ndoes not\n",
335 "<p>this line\ndoes not</p>\n",
336
337 "this line\\\nhas a break\n",
338 "<p>this line<br />\nhas a break</p>\n",
339
340 "this line\\ \ndoes not\n",
341 "<p>this line\\\ndoes not</p>\n",
342
343 "this has an \nextra space\n",
344 "<p>this has an<br />\nextra space</p>\n",
345 }
346 doTestsInlineParam(t, tests, TestParams{Options: Options{
347 Extensions: BackslashLineBreak}})
348}
349
350func TestInlineLink(t *testing.T) {
351 var tests = []string{
352 "[foo](/bar/)\n",
353 "<p><a href=\"/bar/\">foo</a></p>\n",
354
355 "[foo with a title](/bar/ \"title\")\n",
356 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
357
358 "[foo with a title](/bar/\t\"title\")\n",
359 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
360
361 "[foo with a title](/bar/ \"title\" )\n",
362 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
363
364 "[foo with a title](/bar/ title with no quotes)\n",
365 "<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n",
366
367 "[foo]()\n",
368 "<p>[foo]()</p>\n",
369
370 "![foo](/bar/)\n",
371 "<p><img src=\"/bar/\" alt=\"foo\" /></p>\n",
372
373 "![foo with a title](/bar/ \"title\")\n",
374 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" /></p>\n",
375
376 "![foo with a title](/bar/\t\"title\")\n",
377 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" /></p>\n",
378
379 "![foo with a title](/bar/ \"title\" )\n",
380 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" /></p>\n",
381
382 "![foo with a title](/bar/ title with no quotes)\n",
383 "<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" /></p>\n",
384
385 "![](img.jpg)\n",
386 "<p><img src=\"img.jpg\" alt=\"\" /></p>\n",
387
388 "[link](url)\n",
389 "<p><a href=\"url\">link</a></p>\n",
390
391 "![foo]()\n",
392 "<p>![foo]()</p>\n",
393
394 "[a link]\t(/with_a_tab/)\n",
395 "<p><a href=\"/with_a_tab/\">a link</a></p>\n",
396
397 "[a link] (/with_spaces/)\n",
398 "<p><a href=\"/with_spaces/\">a link</a></p>\n",
399
400 "[text (with) [[nested] (brackets)]](/url/)\n",
401 "<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n",
402
403 "[text (with) [broken nested] (brackets)]](/url/)\n",
404 "<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n",
405
406 "[text\nwith a newline](/link/)\n",
407 "<p><a href=\"/link/\">text\nwith a newline</a></p>\n",
408
409 "[text in brackets] [followed](/by a link/)\n",
410 "<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n",
411
412 "[link with\\] a closing bracket](/url/)\n",
413 "<p><a href=\"/url/\">link with] a closing bracket</a></p>\n",
414
415 "[link with\\[ an opening bracket](/url/)\n",
416 "<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n",
417
418 "[link with\\) a closing paren](/url/)\n",
419 "<p><a href=\"/url/\">link with) a closing paren</a></p>\n",
420
421 "[link with\\( an opening paren](/url/)\n",
422 "<p><a href=\"/url/\">link with( an opening paren</a></p>\n",
423
424 "[link]( with whitespace)\n",
425 "<p><a href=\"with whitespace\">link</a></p>\n",
426
427 "[link]( with whitespace )\n",
428 "<p><a href=\"with whitespace\">link</a></p>\n",
429
430 "[![image](someimage)](with image)\n",
431 "<p><a href=\"with image\"><img src=\"someimage\" alt=\"image\" /></a></p>\n",
432
433 "[link](url \"one quote)\n",
434 "<p><a href=\"url "one quote\">link</a></p>\n",
435
436 "[link](url 'one quote)\n",
437 "<p><a href=\"url 'one quote\">link</a></p>\n",
438
439 "[link](<url>)\n",
440 "<p><a href=\"url\">link</a></p>\n",
441
442 "[link & ampersand](/url/)\n",
443 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
444
445 "[link & ampersand](/url/)\n",
446 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
447
448 "[link](/url/&query)\n",
449 "<p><a href=\"/url/&query\">link</a></p>\n",
450
451 "[[t]](/t)\n",
452 "<p><a href=\"/t\">[t]</a></p>\n",
453
454 "[link](</>)\n",
455 "<p><a href=\"/\">link</a></p>\n",
456
457 "[link](<./>)\n",
458 "<p><a href=\"./\">link</a></p>\n",
459
460 "[link](<../>)\n",
461 "<p><a href=\"../\">link</a></p>\n",
462 }
463 doLinkTestsInline(t, tests)
464
465}
466
467func TestRelAttrLink(t *testing.T) {
468 var nofollowTests = []string{
469 "[foo](http://bar.com/foo/)\n",
470 "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n",
471
472 "[foo](/bar/)\n",
473 "<p><a href=\"/bar/\">foo</a></p>\n",
474
475 "[foo](/)\n",
476 "<p><a href=\"/\">foo</a></p>\n",
477
478 "[foo](./)\n",
479 "<p><a href=\"./\">foo</a></p>\n",
480
481 "[foo](../)\n",
482 "<p><a href=\"../\">foo</a></p>\n",
483
484 "[foo](../bar)\n",
485 "<p><a href=\"../bar\">foo</a></p>\n",
486 }
487 doTestsInlineParam(t, nofollowTests, TestParams{
488 HTMLFlags: Safelink | NofollowLinks,
489 })
490
491 var noreferrerTests = []string{
492 "[foo](http://bar.com/foo/)\n",
493 "<p><a href=\"http://bar.com/foo/\" rel=\"noreferrer\">foo</a></p>\n",
494
495 "[foo](/bar/)\n",
496 "<p><a href=\"/bar/\">foo</a></p>\n",
497 }
498 doTestsInlineParam(t, noreferrerTests, TestParams{
499 HTMLFlags: Safelink | NoreferrerLinks,
500 })
501
502 var nofollownoreferrerTests = []string{
503 "[foo](http://bar.com/foo/)\n",
504 "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow noreferrer\">foo</a></p>\n",
505
506 "[foo](/bar/)\n",
507 "<p><a href=\"/bar/\">foo</a></p>\n",
508 }
509 doTestsInlineParam(t, nofollownoreferrerTests, TestParams{
510 HTMLFlags: Safelink | NofollowLinks | NoreferrerLinks,
511 })
512}
513
514func TestHrefTargetBlank(t *testing.T) {
515 var tests = []string{
516 // internal link
517 "[foo](/bar/)\n",
518 "<p><a href=\"/bar/\">foo</a></p>\n",
519
520 "[foo](/)\n",
521 "<p><a href=\"/\">foo</a></p>\n",
522
523 "[foo](./)\n",
524 "<p><a href=\"./\">foo</a></p>\n",
525
526 "[foo](./bar)\n",
527 "<p><a href=\"./bar\">foo</a></p>\n",
528
529 "[foo](../)\n",
530 "<p><a href=\"../\">foo</a></p>\n",
531
532 "[foo](../bar)\n",
533 "<p><a href=\"../bar\">foo</a></p>\n",
534
535 "[foo](http://example.com)\n",
536 "<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n",
537 }
538 doTestsInlineParam(t, tests, TestParams{
539 HTMLFlags: Safelink | HrefTargetBlank,
540 })
541}
542
543func TestSafeInlineLink(t *testing.T) {
544 var tests = []string{
545 "[foo](/bar/)\n",
546 "<p><a href=\"/bar/\">foo</a></p>\n",
547
548 "[foo](/)\n",
549 "<p><a href=\"/\">foo</a></p>\n",
550
551 "[foo](./)\n",
552 "<p><a href=\"./\">foo</a></p>\n",
553
554 "[foo](../)\n",
555 "<p><a href=\"../\">foo</a></p>\n",
556
557 "[foo](http://bar/)\n",
558 "<p><a href=\"http://bar/\">foo</a></p>\n",
559
560 "[foo](https://bar/)\n",
561 "<p><a href=\"https://bar/\">foo</a></p>\n",
562
563 "[foo](ftp://bar/)\n",
564 "<p><a href=\"ftp://bar/\">foo</a></p>\n",
565
566 "[foo](mailto://bar/)\n",
567 "<p><a href=\"mailto://bar/\">foo</a></p>\n",
568
569 // Not considered safe
570 "[foo](baz://bar/)\n",
571 "<p><tt>foo</tt></p>\n",
572 }
573 doSafeTestsInline(t, tests)
574}
575
576func TestReferenceLink(t *testing.T) {
577 var tests = []string{
578 "[link][ref]\n",
579 "<p>[link][ref]</p>\n",
580
581 "[link][ref]\n [ref]: /url/ \"title\"\n",
582 "<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
583
584 "[link][ref]\n [ref]: /url/\n",
585 "<p><a href=\"/url/\">link</a></p>\n",
586
587 " [ref]: /url/\n",
588 "",
589
590 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
591 "",
592
593 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n [4spaces]: /url/\n",
594 "<pre><code>[4spaces]: /url/\n</code></pre>\n",
595
596 "[hmm](ref2)\n [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
597 "<p><a href=\"ref2\">hmm</a></p>\n",
598
599 "[ref]\n",
600 "<p>[ref]</p>\n",
601
602 "[ref]\n [ref]: /url/ \"title\"\n",
603 "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
604
605 "[ref]\n [ref]: ../url/ \"title\"\n",
606 "<p><a href=\"../url/\" title=\"title\">ref</a></p>\n",
607 }
608 doLinkTestsInline(t, tests)
609}
610
611func TestTags(t *testing.T) {
612 var tests = []string{
613 "a <span>tag</span>\n",
614 "<p>a <span>tag</span></p>\n",
615
616 "<span>tag</span>\n",
617 "<p><span>tag</span></p>\n",
618
619 "<span>mismatch</spandex>\n",
620 "<p><span>mismatch</spandex></p>\n",
621
622 "a <singleton /> tag\n",
623 "<p>a <singleton /> tag</p>\n",
624 }
625 doTestsInline(t, tests)
626}
627
628func TestAutoLink(t *testing.T) {
629 var tests = []string{
630 "http://foo.com/\n",
631 "<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
632
633 "1 http://foo.com/\n",
634 "<p>1 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
635
636 "1http://foo.com/\n",
637 "<p>1<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
638
639 "1.http://foo.com/\n",
640 "<p>1.<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
641
642 "1. http://foo.com/\n",
643 "<ol>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ol>\n",
644
645 "-http://foo.com/\n",
646 "<p>-<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
647
648 "- http://foo.com/\n",
649 "<ul>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ul>\n",
650
651 "_http://foo.com/\n",
652 "<p>_<a href=\"http://foo.com/\">http://foo.com/</a></p>\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 "<p>令狐 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
659
660 "ahttp://foo.com/\n",
661 "<p>ahttp://foo.com/</p>\n",
662
663 ">http://foo.com/\n",
664 "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
665
666 "> http://foo.com/\n",
667 "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
668
669 "go to <http://foo.com/>\n",
670 "<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
671
672 "a secure <https://link.org>\n",
673 "<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
674
675 "an email <mailto:some@one.com>\n",
676 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
677
678 "an email <mailto://some@one.com>\n",
679 "<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
680
681 "an email <some@one.com>\n",
682 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
683
684 "an ftp <ftp://old.com>\n",
685 "<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
686
687 "an ftp <ftp:old.com>\n",
688 "<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
689
690 "a link with <http://new.com?query=foo&bar>\n",
691 "<p>a link with <a href=\"http://new.com?query=foo&bar\">" +
692 "http://new.com?query=foo&bar</a></p>\n",
693
694 "quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
695 "<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
696
697 "quotes mean a tag <http://new.com?query='foo'&bar>\n",
698 "<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
699
700 "unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
701 "<p>unless escaped <a href=\"http://new.com?query="foo"&bar\">" +
702 "http://new.com?query="foo"&bar</a></p>\n",
703
704 "even a > can be escaped <http://new.com?q=\\>&etc>\n",
705 "<p>even a > can be escaped <a href=\"http://new.com?q=>&etc\">" +
706 "http://new.com?q=>&etc</a></p>\n",
707
708 "<a href=\"http://fancy.com\">http://fancy.com</a>\n",
709 "<p><a href=\"http://fancy.com\">http://fancy.com</a></p>\n",
710
711 "<a href=\"http://fancy.com\">This is a link</a>\n",
712 "<p><a href=\"http://fancy.com\">This is a link</a></p>\n",
713
714 "<a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a>\n",
715 "<p><a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a></p>\n",
716
717 "(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (\n",
718 "<p>(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (</p>\n",
719
720 "(<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",
721 "<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",
722
723 "http://www.foo.com<br />\n",
724 "<p><a href=\"http://www.foo.com\">http://www.foo.com</a><br /></p>\n",
725
726 "http://foo.com/viewtopic.php?f=18&t=297",
727 "<p><a href=\"http://foo.com/viewtopic.php?f=18&t=297\">http://foo.com/viewtopic.php?f=18&t=297</a></p>\n",
728
729 "http://foo.com/viewtopic.php?param="18"zz",
730 "<p><a href=\"http://foo.com/viewtopic.php?param="18"zz\">http://foo.com/viewtopic.php?param="18"zz</a></p>\n",
731
732 "http://foo.com/viewtopic.php?param="18"",
733 "<p><a href=\"http://foo.com/viewtopic.php?param="18"\">http://foo.com/viewtopic.php?param="18"</a></p>\n",
734
735 "<a href=\"https://fancy.com\">https://fancy.com</a>\n",
736 "<p><a href=\"https://fancy.com\">https://fancy.com</a></p>\n",
737 }
738 doLinkTestsInline(t, tests)
739}
740
741var footnoteTests = []string{
742 "testing footnotes.[^a]\n\n[^a]: This is the note\n",
743 `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
744
745<div class="footnotes">
746
747<hr />
748
749<ol>
750<li id="fn:a">This is the note</li>
751</ol>
752
753</div>
754`,
755
756 `testing long[^b] notes.
757
758[^b]: Paragraph 1
759
760 Paragraph 2
761
762 ` + "```\n\tsome code\n\t```" + `
763
764 Paragraph 3
765
766No longer in the footnote
767`,
768 `<p>testing long<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">1</a></sup> notes.</p>
769
770<p>No longer in the footnote</p>
771
772<div class="footnotes">
773
774<hr />
775
776<ol>
777<li id="fn:b"><p>Paragraph 1</p>
778
779<p>Paragraph 2</p>
780
781<p><code>
782some code
783</code></p>
784
785<p>Paragraph 3</p></li>
786</ol>
787
788</div>
789`,
790
791 `testing[^c] multiple[^d] notes.
792
793[^c]: this is [note] c
794
795
796omg
797
798[^d]: this is note d
799
800what happens here
801
802[note]: /link/c
803
804`,
805 `<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>
806
807<p>omg</p>
808
809<p>what happens here</p>
810
811<div class="footnotes">
812
813<hr />
814
815<ol>
816<li id="fn:c">this is <a href="/link/c">note</a> c</li>
817
818<li id="fn:d">this is note d</li>
819</ol>
820
821</div>
822`,
823
824 "testing inline^[this is the note] notes.\n",
825 `<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>
826
827<div class="footnotes">
828
829<hr />
830
831<ol>
832<li id="fn:this-is-the-note">this is the note</li>
833</ol>
834
835</div>
836`,
837
838 "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",
839 `<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>
840
841<div class="footnotes">
842
843<hr />
844
845<ol>
846<li id="fn:1"><p>the first deferred note</p>
847
848<p>which happens to be a block</p></li>
849
850<li id="fn:inline-note">inline note</li>
851
852<li id="fn:2">the second deferred note</li>
853</ol>
854
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
866<div class="footnotes">
867
868<hr />
869
870<ol>
871<li id="fn:1"><p>the footnote text.</p>
872
873<p>may be multiple paragraphs.</p></li>
874
875<li id="fn:and-this-is-an-i">and this is an inline footnote</li>
876</ol>
877
878</div>
879`,
880
881 "empty footnote[^]\n\n[^]: fn text",
882 "<p>empty footnote<sup class=\"footnote-ref\" id=\"fnref:\"><a rel=\"footnote\" href=\"#fn:\">1</a></sup></p>\n\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:\">fn text</li>\n</ol>\n\n</div>\n",
883
884 "Some text.[^note1]\n\n[^note1]: fn1",
885 "<p>Some text.<sup class=\"footnote-ref\" id=\"fnref:note1\"><a rel=\"footnote\" href=\"#fn:note1\">1</a></sup></p>\n\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:note1\">fn1</li>\n</ol>\n\n</div>\n",
886
887 "Some text.[^note1][^note2]\n\n[^note1]: fn1\n[^note2]: fn2\n",
888 "<p>Some text.<sup class=\"footnote-ref\" id=\"fnref:note1\"><a rel=\"footnote\" href=\"#fn:note1\">1</a></sup><sup class=\"footnote-ref\" id=\"fnref:note2\"><a rel=\"footnote\" href=\"#fn:note2\">2</a></sup></p>\n\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:note1\">fn1</li>\n\n<li id=\"fn:note2\">fn2</li>\n</ol>\n\n</div>\n",
889
890 `Bla bla [^1] [WWW][w3]
891
892[^1]: This is a footnote
893
894[w3]: http://www.w3.org/
895`,
896 `<p>Bla bla <sup class="footnote-ref" id="fnref:1"><a rel="footnote" href="#fn:1">1</a></sup> <a href="http://www.w3.org/">WWW</a></p>
897
898<div class="footnotes">
899
900<hr />
901
902<ol>
903<li id="fn:1">This is a footnote</li>
904</ol>
905
906</div>
907`,
908
909 `This is exciting![^fn1]
910
911[^fn1]: Fine print
912`,
913 `<p>This is exciting!<sup class="footnote-ref" id="fnref:fn1"><a rel="footnote" href="#fn:fn1">1</a></sup></p>
914
915<div class="footnotes">
916
917<hr />
918
919<ol>
920<li id="fn:fn1">Fine print</li>
921</ol>
922
923</div>
924`,
925}
926
927func TestFootnotes(t *testing.T) {
928 doTestsInlineParam(t, footnoteTests, TestParams{
929 Options: Options{Extensions: Footnotes},
930 })
931}
932
933func TestFootnotesWithParameters(t *testing.T) {
934 tests := make([]string, len(footnoteTests))
935
936 prefix := "testPrefix"
937 returnText := "ret"
938 re := regexp.MustCompile(`(?ms)<li id="fn:(\S+?)">(.*?)</li>`)
939
940 // Transform the test expectations to match the parameters we're using.
941 for i, test := range footnoteTests {
942 if i%2 == 1 {
943 test = strings.Replace(test, "fn:", "fn:"+prefix, -1)
944 test = strings.Replace(test, "fnref:", "fnref:"+prefix, -1)
945 test = re.ReplaceAllString(test, `<li id="fn:$1">$2 <a class="footnote-return" href="#fnref:$1">ret</a></li>`)
946 }
947 tests[i] = test
948 }
949
950 params := HTMLRendererParameters{
951 FootnoteAnchorPrefix: prefix,
952 FootnoteReturnLinkContents: returnText,
953 }
954
955 doTestsInlineParam(t, tests, TestParams{
956 Options: Options{Extensions: Footnotes},
957 HTMLFlags: FootnoteReturnLinks,
958 HTMLRendererParameters: params,
959 })
960}
961
962func TestNestedFootnotes(t *testing.T) {
963 var tests = []string{
964 `Paragraph.[^fn1]
965
966[^fn1]:
967 Asterisk[^fn2]
968
969[^fn2]:
970 Obelisk`,
971 `<p>Paragraph.<sup class="footnote-ref" id="fnref:fn1"><a rel="footnote" href="#fn:fn1">1</a></sup></p>
972
973<div class="footnotes">
974
975<hr />
976
977<ol>
978<li id="fn:fn1">Asterisk<sup class="footnote-ref" id="fnref:fn2"><a rel="footnote" href="#fn:fn2">2</a></sup></li>
979
980<li id="fn:fn2">Obelisk</li>
981</ol>
982
983</div>
984`,
985 }
986 doTestsInlineParam(t, tests, TestParams{Options: Options{Extensions: Footnotes}})
987}
988
989func TestInlineComments(t *testing.T) {
990 var tests = []string{
991 "Hello <!-- there ->\n",
992 "<p>Hello <!— there –></p>\n",
993
994 "Hello <!-- there -->\n",
995 "<p>Hello <!-- there --></p>\n",
996
997 "Hello <!-- there -->",
998 "<p>Hello <!-- there --></p>\n",
999
1000 "Hello <!---->\n",
1001 "<p>Hello <!----></p>\n",
1002
1003 "Hello <!-- there -->\na",
1004 "<p>Hello <!-- there -->\na</p>\n",
1005
1006 "* list <!-- item -->\n",
1007 "<ul>\n<li>list <!-- item --></li>\n</ul>\n",
1008
1009 "<!-- Front --> comment\n",
1010 "<p><!-- Front --> comment</p>\n",
1011
1012 "blahblah\n<!--- foo -->\nrhubarb\n",
1013 "<p>blahblah\n<!--- foo -->\nrhubarb</p>\n",
1014 }
1015 doTestsInlineParam(t, tests, TestParams{
1016 Options: Options{
1017 Extensions: Smartypants | SmartypantsDashes,
1018 },
1019 })
1020}
1021
1022func TestSmartDoubleQuotes(t *testing.T) {
1023 var tests = []string{
1024 "this should be normal \"quoted\" text.\n",
1025 "<p>this should be normal “quoted” text.</p>\n",
1026 "this \" single double\n",
1027 "<p>this “ single double</p>\n",
1028 "two pair of \"some\" quoted \"text\".\n",
1029 "<p>two pair of “some” quoted “text”.</p>\n"}
1030
1031 doTestsInlineParam(t, tests, TestParams{
1032 Options: Options{
1033 Extensions: Smartypants,
1034 },
1035 })
1036}
1037
1038func TestSmartAngledDoubleQuotes(t *testing.T) {
1039 var tests = []string{
1040 "this should be angled \"quoted\" text.\n",
1041 "<p>this should be angled «quoted» text.</p>\n",
1042 "this \" single double\n",
1043 "<p>this « single double</p>\n",
1044 "two pair of \"some\" quoted \"text\".\n",
1045 "<p>two pair of «some» quoted «text».</p>\n"}
1046
1047 doTestsInlineParam(t, tests, TestParams{
1048 Options: Options{
1049 Extensions: Smartypants | SmartypantsAngledQuotes,
1050 },
1051 })
1052}
1053
1054func TestSmartFractions(t *testing.T) {
1055 var tests = []string{
1056 "1/2, 1/4 and 3/4; 1/4th and 3/4ths\n",
1057 "<p>½, ¼ and ¾; ¼th and ¾ths</p>\n",
1058 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
1059 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
1060
1061 doTestsInlineParam(t, tests, TestParams{
1062 Options: Options{
1063 Extensions: Smartypants,
1064 },
1065 })
1066
1067 tests = []string{
1068 "1/2, 2/3, 81/100 and 1000000/1048576.\n",
1069 "<p><sup>1</sup>⁄<sub>2</sub>, <sup>2</sup>⁄<sub>3</sub>, <sup>81</sup>⁄<sub>100</sub> and <sup>1000000</sup>⁄<sub>1048576</sub>.</p>\n",
1070 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
1071 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
1072
1073 doTestsInlineParam(t, tests, TestParams{
1074 Options: Options{
1075 Extensions: Smartypants | SmartypantsFractions,
1076 },
1077 })
1078}
1079
1080func TestDisableSmartDashes(t *testing.T) {
1081 doTestsInlineParam(t, []string{
1082 "foo - bar\n",
1083 "<p>foo - bar</p>\n",
1084 "foo -- bar\n",
1085 "<p>foo -- bar</p>\n",
1086 "foo --- bar\n",
1087 "<p>foo --- bar</p>\n",
1088 }, TestParams{})
1089 doTestsInlineParam(t, []string{
1090 "foo - bar\n",
1091 "<p>foo – bar</p>\n",
1092 "foo -- bar\n",
1093 "<p>foo — bar</p>\n",
1094 "foo --- bar\n",
1095 "<p>foo —– bar</p>\n",
1096 }, TestParams{Options: Options{Extensions: Smartypants | SmartypantsDashes}})
1097 doTestsInlineParam(t, []string{
1098 "foo - bar\n",
1099 "<p>foo - bar</p>\n",
1100 "foo -- bar\n",
1101 "<p>foo – bar</p>\n",
1102 "foo --- bar\n",
1103 "<p>foo — bar</p>\n",
1104 }, TestParams{
1105 Options: Options{
1106 Extensions: Smartypants | SmartypantsLatexDashes | SmartypantsDashes,
1107 },
1108 })
1109 doTestsInlineParam(t, []string{
1110 "foo - bar\n",
1111 "<p>foo - bar</p>\n",
1112 "foo -- bar\n",
1113 "<p>foo -- bar</p>\n",
1114 "foo --- bar\n",
1115 "<p>foo --- bar</p>\n",
1116 }, TestParams{
1117 Options: Options{Extensions: Smartypants | SmartypantsLatexDashes},
1118 })
1119}
1120
1121func TestSkipLinks(t *testing.T) {
1122 doTestsInlineParam(t, []string{
1123 "[foo](gopher://foo.bar)",
1124 "<p><tt>foo</tt></p>\n",
1125
1126 "[foo](mailto://bar/)\n",
1127 "<p><tt>foo</tt></p>\n",
1128 }, TestParams{
1129 HTMLFlags: SkipLinks,
1130 })
1131}
1132
1133func TestUseXHTML(t *testing.T) {
1134 doTestsParam(t, []string{
1135 "---",
1136 "<hr>\n",
1137 }, TestParams{})
1138 doTestsParam(t, []string{
1139 "---",
1140 "<hr />\n",
1141 }, TestParams{HTMLFlags: UseXHTML})
1142}