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