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 noopenerTests = []string{
501 "[foo](http://bar.com/foo/)\n",
502 "<p><a href=\"http://bar.com/foo/\" rel=\"noopener\">foo</a></p>\n",
503
504 "[foo](/bar/)\n",
505 "<p><a href=\"/bar/\">foo</a></p>\n",
506 }
507 doTestsInlineParam(t, noopenerTests, TestParams{
508 HTMLFlags: Safelink | NoopenerLinks,
509 })
510
511 var nofollownoreferrernoopenerTests = []string{
512 "[foo](http://bar.com/foo/)\n",
513 "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow noreferrer noopener\">foo</a></p>\n",
514
515 "[foo](/bar/)\n",
516 "<p><a href=\"/bar/\">foo</a></p>\n",
517 }
518 doTestsInlineParam(t, nofollownoreferrernoopenerTests, TestParams{
519 HTMLFlags: Safelink | NofollowLinks | NoreferrerLinks | NoopenerLinks,
520 })
521}
522
523func TestHrefTargetBlank(t *testing.T) {
524 var tests = []string{
525 // internal link
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](./)\n",
533 "<p><a href=\"./\">foo</a></p>\n",
534
535 "[foo](./bar)\n",
536 "<p><a href=\"./bar\">foo</a></p>\n",
537
538 "[foo](../)\n",
539 "<p><a href=\"../\">foo</a></p>\n",
540
541 "[foo](../bar)\n",
542 "<p><a href=\"../bar\">foo</a></p>\n",
543
544 "[foo](http://example.com)\n",
545 "<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n",
546 }
547 doTestsInlineParam(t, tests, TestParams{
548 HTMLFlags: Safelink | HrefTargetBlank,
549 })
550}
551
552func TestSafeInlineLink(t *testing.T) {
553 var tests = []string{
554 "[foo](/bar/)\n",
555 "<p><a href=\"/bar/\">foo</a></p>\n",
556
557 "[foo](/)\n",
558 "<p><a href=\"/\">foo</a></p>\n",
559
560 "[foo](./)\n",
561 "<p><a href=\"./\">foo</a></p>\n",
562
563 "[foo](../)\n",
564 "<p><a href=\"../\">foo</a></p>\n",
565
566 "[foo](http://bar/)\n",
567 "<p><a href=\"http://bar/\">foo</a></p>\n",
568
569 "[foo](https://bar/)\n",
570 "<p><a href=\"https://bar/\">foo</a></p>\n",
571
572 "[foo](ftp://bar/)\n",
573 "<p><a href=\"ftp://bar/\">foo</a></p>\n",
574
575 "[foo](mailto://bar/)\n",
576 "<p><a href=\"mailto://bar/\">foo</a></p>\n",
577
578 // Not considered safe
579 "[foo](baz://bar/)\n",
580 "<p><tt>foo</tt></p>\n",
581 }
582 doSafeTestsInline(t, tests)
583}
584
585func TestReferenceLink(t *testing.T) {
586 var tests = []string{
587 "[link][ref]\n",
588 "<p>[link][ref]</p>\n",
589
590 "[link][ref]\n [ref]: /url/ \"title\"\n",
591 "<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
592
593 "[link][ref]\n [ref]: /url/\n",
594 "<p><a href=\"/url/\">link</a></p>\n",
595
596 " [ref]: /url/\n",
597 "",
598
599 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
600 "",
601
602 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n [4spaces]: /url/\n",
603 "<pre><code>[4spaces]: /url/\n</code></pre>\n",
604
605 "[hmm](ref2)\n [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
606 "<p><a href=\"ref2\">hmm</a></p>\n",
607
608 "[ref]\n",
609 "<p>[ref]</p>\n",
610
611 "[ref]\n [ref]: /url/ \"title\"\n",
612 "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
613
614 "[ref]\n [ref]: ../url/ \"title\"\n",
615 "<p><a href=\"../url/\" title=\"title\">ref</a></p>\n",
616
617 "[link][ref]\n [ref]: /url/",
618 "<p><a href=\"/url/\">link</a></p>\n",
619 }
620 doLinkTestsInline(t, tests)
621}
622
623func TestTags(t *testing.T) {
624 var tests = []string{
625 "a <span>tag</span>\n",
626 "<p>a <span>tag</span></p>\n",
627
628 "<span>tag</span>\n",
629 "<p><span>tag</span></p>\n",
630
631 "<span>mismatch</spandex>\n",
632 "<p><span>mismatch</spandex></p>\n",
633
634 "a <singleton /> tag\n",
635 "<p>a <singleton /> tag</p>\n",
636 }
637 doTestsInline(t, tests)
638}
639
640func TestAutoLink(t *testing.T) {
641 var tests = []string{
642 "http://foo.com/\n",
643 "<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
644
645 "1 http://foo.com/\n",
646 "<p>1 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
647
648 "1http://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 "<p>1.<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
653
654 "1. http://foo.com/\n",
655 "<ol>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ol>\n",
656
657 "-http://foo.com/\n",
658 "<p>-<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
659
660 "- http://foo.com/\n",
661 "<ul>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ul>\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 "令狐 http://foo.com/\n",
670 "<p>令狐 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
671
672 "ahttp://foo.com/\n",
673 "<p>ahttp://foo.com/</p>\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 "> http://foo.com/\n",
679 "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
680
681 "go to <http://foo.com/>\n",
682 "<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
683
684 "a secure <https://link.org>\n",
685 "<p>a secure <a href=\"https://link.org\">https://link.org</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 <mailto://some@one.com>\n",
691 "<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
692
693 "an email <some@one.com>\n",
694 "<p>an email <a href=\"mailto:some@one.com\">some@one.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 "an ftp <ftp:old.com>\n",
700 "<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
701
702 "a link with <http://new.com?query=foo&bar>\n",
703 "<p>a link with <a href=\"http://new.com?query=foo&bar\">" +
704 "http://new.com?query=foo&bar</a></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 "quotes mean a tag <http://new.com?query='foo'&bar>\n",
710 "<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
711
712 "unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
713 "<p>unless escaped <a href=\"http://new.com?query="foo"&bar\">" +
714 "http://new.com?query="foo"&bar</a></p>\n",
715
716 "even a > can be escaped <http://new.com?q=\\>&etc>\n",
717 "<p>even a > can be escaped <a href=\"http://new.com?q=>&etc\">" +
718 "http://new.com?q=>&etc</a></p>\n",
719
720 "<a href=\"http://fancy.com\">http://fancy.com</a>\n",
721 "<p><a href=\"http://fancy.com\">http://fancy.com</a></p>\n",
722
723 "<a href=\"http://fancy.com\">This is a link</a>\n",
724 "<p><a href=\"http://fancy.com\">This is a link</a></p>\n",
725
726 "<a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a>\n",
727 "<p><a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a></p>\n",
728
729 "(<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> (</p>\n",
731
732 "(<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",
733 "<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",
734
735 "http://www.foo.com<br />\n",
736 "<p><a href=\"http://www.foo.com\">http://www.foo.com</a><br /></p>\n",
737
738 "http://foo.com/viewtopic.php?f=18&t=297",
739 "<p><a href=\"http://foo.com/viewtopic.php?f=18&t=297\">http://foo.com/viewtopic.php?f=18&t=297</a></p>\n",
740
741 "http://foo.com/viewtopic.php?param="18"zz",
742 "<p><a href=\"http://foo.com/viewtopic.php?param="18"zz\">http://foo.com/viewtopic.php?param="18"zz</a></p>\n",
743
744 "http://foo.com/viewtopic.php?param="18"",
745 "<p><a href=\"http://foo.com/viewtopic.php?param="18"\">http://foo.com/viewtopic.php?param="18"</a></p>\n",
746
747 "<a href=\"https://fancy.com\">https://fancy.com</a>\n",
748 "<p><a href=\"https://fancy.com\">https://fancy.com</a></p>\n",
749 }
750 doLinkTestsInline(t, tests)
751}
752
753var footnoteTests = []string{
754 "testing footnotes.[^a]\n\n[^a]: This is the note\n",
755 `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a href="#fn:a">1</a></sup></p>
756
757<div class="footnotes">
758
759<hr />
760
761<ol>
762<li id="fn:a">This is the note</li>
763</ol>
764
765</div>
766`,
767
768 `testing long[^b] notes.
769
770[^b]: Paragraph 1
771
772 Paragraph 2
773
774 ` + "```\n\tsome code\n\t```" + `
775
776 Paragraph 3
777
778No longer in the footnote
779`,
780 `<p>testing long<sup class="footnote-ref" id="fnref:b"><a href="#fn:b">1</a></sup> notes.</p>
781
782<p>No longer in the footnote</p>
783
784<div class="footnotes">
785
786<hr />
787
788<ol>
789<li id="fn:b"><p>Paragraph 1</p>
790
791<p>Paragraph 2</p>
792
793<p><code>
794some code
795</code></p>
796
797<p>Paragraph 3</p></li>
798</ol>
799
800</div>
801`,
802
803 `testing[^c] multiple[^d] notes.
804
805[^c]: this is [note] c
806
807
808omg
809
810[^d]: this is note d
811
812what happens here
813
814[note]: /link/c
815
816`,
817 `<p>testing<sup class="footnote-ref" id="fnref:c"><a href="#fn:c">1</a></sup> multiple<sup class="footnote-ref" id="fnref:d"><a href="#fn:d">2</a></sup> notes.</p>
818
819<p>omg</p>
820
821<p>what happens here</p>
822
823<div class="footnotes">
824
825<hr />
826
827<ol>
828<li id="fn:c">this is <a href="/link/c">note</a> c</li>
829
830<li id="fn:d">this is note d</li>
831</ol>
832
833</div>
834`,
835
836 "testing inline^[this is the note] notes.\n",
837 `<p>testing inline<sup class="footnote-ref" id="fnref:this-is-the-note"><a href="#fn:this-is-the-note">1</a></sup> notes.</p>
838
839<div class="footnotes">
840
841<hr />
842
843<ol>
844<li id="fn:this-is-the-note">this is the note</li>
845</ol>
846
847</div>
848`,
849
850 "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",
851 `<p>testing multiple<sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup> types<sup class="footnote-ref" id="fnref:inline-note"><a href="#fn:inline-note">2</a></sup> of notes<sup class="footnote-ref" id="fnref:2"><a href="#fn:2">3</a></sup></p>
852
853<div class="footnotes">
854
855<hr />
856
857<ol>
858<li id="fn:1"><p>the first deferred note</p>
859
860<p>which happens to be a block</p></li>
861
862<li id="fn:inline-note">inline note</li>
863
864<li id="fn:2">the second deferred note</li>
865</ol>
866
867</div>
868`,
869
870 `This is a footnote[^1]^[and this is an inline footnote]
871
872[^1]: the footnote text.
873
874 may be multiple paragraphs.
875`,
876 `<p>This is a footnote<sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup><sup class="footnote-ref" id="fnref:and-this-is-an-i"><a href="#fn:and-this-is-an-i">2</a></sup></p>
877
878<div class="footnotes">
879
880<hr />
881
882<ol>
883<li id="fn:1"><p>the footnote text.</p>
884
885<p>may be multiple paragraphs.</p></li>
886
887<li id="fn:and-this-is-an-i">and this is an inline footnote</li>
888</ol>
889
890</div>
891`,
892
893 "empty footnote[^]\n\n[^]: fn text",
894 "<p>empty footnote<sup class=\"footnote-ref\" id=\"fnref:\"><a 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",
895
896 "Some text.[^note1]\n\n[^note1]: fn1",
897 "<p>Some text.<sup class=\"footnote-ref\" id=\"fnref:note1\"><a 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",
898
899 "Some text.[^note1][^note2]\n\n[^note1]: fn1\n[^note2]: fn2\n",
900 "<p>Some text.<sup class=\"footnote-ref\" id=\"fnref:note1\"><a href=\"#fn:note1\">1</a></sup><sup class=\"footnote-ref\" id=\"fnref:note2\"><a 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",
901
902 `Bla bla [^1] [WWW][w3]
903
904[^1]: This is a footnote
905
906[w3]: http://www.w3.org/
907`,
908 `<p>Bla bla <sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup> <a href="http://www.w3.org/">WWW</a></p>
909
910<div class="footnotes">
911
912<hr />
913
914<ol>
915<li id="fn:1">This is a footnote</li>
916</ol>
917
918</div>
919`,
920
921 `This is exciting![^fn1]
922
923[^fn1]: Fine print
924`,
925 `<p>This is exciting!<sup class="footnote-ref" id="fnref:fn1"><a href="#fn:fn1">1</a></sup></p>
926
927<div class="footnotes">
928
929<hr />
930
931<ol>
932<li id="fn:fn1">Fine print</li>
933</ol>
934
935</div>
936`,
937
938 `This text does not reference a footnote.
939
940[^footnote]: But it has a footnote! And it gets omitted.
941`,
942 "<p>This text does not reference a footnote.</p>\n",
943}
944
945func TestFootnotes(t *testing.T) {
946 doTestsInlineParam(t, footnoteTests, TestParams{
947 extensions: Footnotes,
948 })
949}
950
951func TestFootnotesWithParameters(t *testing.T) {
952 tests := make([]string, len(footnoteTests))
953
954 prefix := "testPrefix"
955 returnText := "ret"
956 re := regexp.MustCompile(`(?ms)<li id="fn:(\S+?)">(.*?)</li>`)
957
958 // Transform the test expectations to match the parameters we're using.
959 for i, test := range footnoteTests {
960 if i%2 == 1 {
961 test = strings.Replace(test, "fn:", "fn:"+prefix, -1)
962 test = strings.Replace(test, "fnref:", "fnref:"+prefix, -1)
963 test = re.ReplaceAllString(test, `<li id="fn:$1">$2 <a class="footnote-return" href="#fnref:$1">ret</a></li>`)
964 }
965 tests[i] = test
966 }
967
968 params := HTMLRendererParameters{
969 FootnoteAnchorPrefix: prefix,
970 FootnoteReturnLinkContents: returnText,
971 }
972
973 doTestsInlineParam(t, tests, TestParams{
974 extensions: Footnotes,
975 HTMLFlags: FootnoteReturnLinks,
976 HTMLRendererParameters: params,
977 })
978}
979
980func TestNestedFootnotes(t *testing.T) {
981 var tests = []string{
982 `Paragraph.[^fn1]
983
984[^fn1]:
985 Asterisk[^fn2]
986
987[^fn2]:
988 Obelisk`,
989 `<p>Paragraph.<sup class="footnote-ref" id="fnref:fn1"><a href="#fn:fn1">1</a></sup></p>
990
991<div class="footnotes">
992
993<hr />
994
995<ol>
996<li id="fn:fn1">Asterisk<sup class="footnote-ref" id="fnref:fn2"><a href="#fn:fn2">2</a></sup></li>
997
998<li id="fn:fn2">Obelisk</li>
999</ol>
1000
1001</div>
1002`,
1003 }
1004 doTestsInlineParam(t, tests, TestParams{extensions: Footnotes})
1005}
1006
1007func TestInlineComments(t *testing.T) {
1008 var tests = []string{
1009 "Hello <!-- there ->\n",
1010 "<p>Hello <!— there –></p>\n",
1011
1012 "Hello <!-- there -->\n",
1013 "<p>Hello <!-- there --></p>\n",
1014
1015 "Hello <!-- there -->",
1016 "<p>Hello <!-- there --></p>\n",
1017
1018 "Hello <!---->\n",
1019 "<p>Hello <!----></p>\n",
1020
1021 "Hello <!-- there -->\na",
1022 "<p>Hello <!-- there -->\na</p>\n",
1023
1024 "* list <!-- item -->\n",
1025 "<ul>\n<li>list <!-- item --></li>\n</ul>\n",
1026
1027 "<!-- Front --> comment\n",
1028 "<p><!-- Front --> comment</p>\n",
1029
1030 "blahblah\n<!--- foo -->\nrhubarb\n",
1031 "<p>blahblah\n<!--- foo -->\nrhubarb</p>\n",
1032 }
1033 doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsDashes})
1034}
1035
1036func TestSmartDoubleQuotes(t *testing.T) {
1037 var tests = []string{
1038 "this should be normal \"quoted\" text.\n",
1039 "<p>this should be normal “quoted” text.</p>\n",
1040 "this \" single double\n",
1041 "<p>this “ single double</p>\n",
1042 "two pair of \"some\" quoted \"text\".\n",
1043 "<p>two pair of “some” quoted “text”.</p>\n"}
1044
1045 doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants})
1046}
1047
1048func TestSmartDoubleQuotesNBSP(t *testing.T) {
1049 var tests = []string{
1050 "this should be normal \"quoted\" text.\n",
1051 "<p>this should be normal “ quoted ” text.</p>\n",
1052 "this \" single double\n",
1053 "<p>this “ single double</p>\n",
1054 "two pair of \"some\" quoted \"text\".\n",
1055 "<p>two pair of “ some ” quoted “ text ”.</p>\n"}
1056
1057 doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsQuotesNBSP})
1058}
1059
1060func TestSmartAngledDoubleQuotes(t *testing.T) {
1061 var tests = []string{
1062 "this should be angled \"quoted\" text.\n",
1063 "<p>this should be angled «quoted» text.</p>\n",
1064 "this \" single double\n",
1065 "<p>this « single double</p>\n",
1066 "two pair of \"some\" quoted \"text\".\n",
1067 "<p>two pair of «some» quoted «text».</p>\n"}
1068
1069 doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsAngledQuotes})
1070}
1071
1072func TestSmartAngledDoubleQuotesNBSP(t *testing.T) {
1073 var tests = []string{
1074 "this should be angled \"quoted\" text.\n",
1075 "<p>this should be angled « quoted » text.</p>\n",
1076 "this \" single double\n",
1077 "<p>this « single double</p>\n",
1078 "two pair of \"some\" quoted \"text\".\n",
1079 "<p>two pair of « some » quoted « text ».</p>\n"}
1080
1081 doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsAngledQuotes | SmartypantsQuotesNBSP})
1082}
1083
1084func TestSmartFractions(t *testing.T) {
1085 var tests = []string{
1086 "1/2, 1/4 and 3/4; 1/4th and 3/4ths\n",
1087 "<p>½, ¼ and ¾; ¼th and ¾ths</p>\n",
1088 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
1089 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
1090
1091 doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants})
1092
1093 tests = []string{
1094 "1/2, 2/3, 81/100 and 1000000/1048576.\n",
1095 "<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",
1096 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
1097 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
1098
1099 doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsFractions})
1100}
1101
1102func TestDisableSmartDashes(t *testing.T) {
1103 doTestsInlineParam(t, []string{
1104 "foo - bar\n",
1105 "<p>foo - bar</p>\n",
1106 "foo -- bar\n",
1107 "<p>foo -- bar</p>\n",
1108 "foo --- bar\n",
1109 "<p>foo --- bar</p>\n",
1110 }, TestParams{})
1111 doTestsInlineParam(t, []string{
1112 "foo - bar\n",
1113 "<p>foo – bar</p>\n",
1114 "foo -- bar\n",
1115 "<p>foo — bar</p>\n",
1116 "foo --- bar\n",
1117 "<p>foo —– bar</p>\n",
1118 }, TestParams{HTMLFlags: Smartypants | SmartypantsDashes})
1119 doTestsInlineParam(t, []string{
1120 "foo - bar\n",
1121 "<p>foo - bar</p>\n",
1122 "foo -- bar\n",
1123 "<p>foo – bar</p>\n",
1124 "foo --- bar\n",
1125 "<p>foo — bar</p>\n",
1126 }, TestParams{HTMLFlags: Smartypants | SmartypantsLatexDashes | SmartypantsDashes})
1127 doTestsInlineParam(t, []string{
1128 "foo - bar\n",
1129 "<p>foo - bar</p>\n",
1130 "foo -- bar\n",
1131 "<p>foo -- bar</p>\n",
1132 "foo --- bar\n",
1133 "<p>foo --- bar</p>\n",
1134 }, TestParams{HTMLFlags: Smartypants | SmartypantsLatexDashes})
1135}
1136
1137func TestSkipLinks(t *testing.T) {
1138 doTestsInlineParam(t, []string{
1139 "[foo](gopher://foo.bar)",
1140 "<p><tt>foo</tt></p>\n",
1141
1142 "[foo](mailto://bar/)\n",
1143 "<p><tt>foo</tt></p>\n",
1144 }, TestParams{
1145 HTMLFlags: SkipLinks,
1146 })
1147}
1148
1149func TestSkipImages(t *testing.T) {
1150 doTestsInlineParam(t, []string{
1151 "![foo](/bar/)\n",
1152 "<p></p>\n",
1153 }, TestParams{
1154 HTMLFlags: SkipImages,
1155 })
1156}
1157
1158func TestUseXHTML(t *testing.T) {
1159 doTestsParam(t, []string{
1160 "---",
1161 "<hr>\n",
1162 }, TestParams{})
1163 doTestsParam(t, []string{
1164 "---",
1165 "<hr />\n",
1166 }, TestParams{HTMLFlags: UseXHTML})
1167}
1168
1169func TestSkipHTML(t *testing.T) {
1170 doTestsParam(t, []string{
1171 "<div class=\"foo\"></div>\n\ntext\n\n<form>the form</form>",
1172 "<p>text</p>\n\n<p>the form</p>\n",
1173
1174 "text <em>inline html</em> more text",
1175 "<p>text inline html more text</p>\n",
1176 }, TestParams{HTMLFlags: SkipHTML})
1177}
1178
1179func BenchmarkSmartDoubleQuotes(b *testing.B) {
1180 params := TestParams{HTMLFlags: Smartypants}
1181 params.extensions |= Autolink | Strikethrough
1182 params.HTMLFlags |= UseXHTML
1183
1184 for i := 0; i < b.N; i++ {
1185 runMarkdown("this should be normal \"quoted\" text.\n", params)
1186 }
1187}