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