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 int, params HtmlRendererParameters) string {
24 opts.Extensions |= EXTENSION_AUTOLINK
25 opts.Extensions |= EXTENSION_STRIKETHROUGH
26
27 htmlFlags |= HTML_USE_XHTML
28
29 renderer := HtmlRendererWithParameters(htmlFlags, "", "", 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{}, HTML_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{}, HTML_SAFELINK, params)
57}
58
59func doTestsInlineParam(t *testing.T, tests []string, opts Options, htmlFlags int,
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: EXTENSION_BACKSLASH_LINE_BREAK},
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{}, HTML_SAFELINK|HTML_NOFOLLOW_LINKS,
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{}, HTML_SAFELINK|HTML_NOREFERRER_LINKS,
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{}, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_NOREFERRER_LINKS,
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{}, HTML_SAFELINK|HTML_HREF_TARGET_BLANK, 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 doLinkTestsInline(t, tests)
813}
814
815var footnoteTests = []string{
816 "testing footnotes.[^a]\n\n[^a]: This is the note\n",
817 `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
818<div class="footnotes">
819
820<hr />
821
822<ol>
823<li id="fn:a">This is the note
824</li>
825</ol>
826</div>
827`,
828
829 `testing long[^b] notes.
830
831[^b]: Paragraph 1
832
833 Paragraph 2
834
835 ` + "```\n\tsome code\n\t```" + `
836
837 Paragraph 3
838
839No longer in the footnote
840`,
841 `<p>testing long<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">1</a></sup> notes.</p>
842
843<p>No longer in the footnote</p>
844<div class="footnotes">
845
846<hr />
847
848<ol>
849<li id="fn:b"><p>Paragraph 1</p>
850
851<p>Paragraph 2</p>
852
853<p><code>
854some code
855</code></p>
856
857<p>Paragraph 3</p>
858</li>
859</ol>
860</div>
861`,
862
863 `testing[^c] multiple[^d] notes.
864
865[^c]: this is [note] c
866
867
868omg
869
870[^d]: this is note d
871
872what happens here
873
874[note]: /link/c
875
876`,
877 `<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>
878
879<p>omg</p>
880
881<p>what happens here</p>
882<div class="footnotes">
883
884<hr />
885
886<ol>
887<li id="fn:c">this is <a href="/link/c">note</a> c
888</li>
889<li id="fn:d">this is note d
890</li>
891</ol>
892</div>
893`,
894
895 "testing inline^[this is the note] notes.\n",
896 `<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>
897<div class="footnotes">
898
899<hr />
900
901<ol>
902<li id="fn:this-is-the-note">this is the note</li>
903</ol>
904</div>
905`,
906
907 "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",
908 `<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>
909<div class="footnotes">
910
911<hr />
912
913<ol>
914<li id="fn:1"><p>the first deferred note</p>
915
916<p>which happens to be a block</p>
917</li>
918<li id="fn:inline-note">inline note</li>
919<li id="fn:2">the second deferred note
920</li>
921</ol>
922</div>
923`,
924
925 `This is a footnote[^1]^[and this is an inline footnote]
926
927[^1]: the footnote text.
928
929 may be multiple paragraphs.
930`,
931 `<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>
932<div class="footnotes">
933
934<hr />
935
936<ol>
937<li id="fn:1"><p>the footnote text.</p>
938
939<p>may be multiple paragraphs.</p>
940</li>
941<li id="fn:and-this-is-an-i">and this is an inline footnote</li>
942</ol>
943</div>
944`,
945
946 "empty footnote[^]\n\n[^]: fn text",
947 "<p>empty footnote<sup class=\"footnote-ref\" id=\"fnref:\"><a rel=\"footnote\" href=\"#fn:\">1</a></sup></p>\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:\">fn text\n</li>\n</ol>\n</div>\n",
948
949 "Some text.[^note1]\n\n[^note1]: fn1",
950 "<p>Some text.<sup class=\"footnote-ref\" id=\"fnref:note1\"><a rel=\"footnote\" href=\"#fn:note1\">1</a></sup></p>\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:note1\">fn1\n</li>\n</ol>\n</div>\n",
951
952 "Some text.[^note1][^note2]\n\n[^note1]: fn1\n[^note2]: fn2\n",
953 "<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<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:note1\">fn1\n</li>\n<li id=\"fn:note2\">fn2\n</li>\n</ol>\n</div>\n",
954
955 `Bla bla [^1] [WWW][w3]
956
957[^1]: This is a footnote
958
959[w3]: http://www.w3.org/
960`,
961 `<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>
962<div class="footnotes">
963
964<hr />
965
966<ol>
967<li id="fn:1">This is a footnote
968</li>
969</ol>
970</div>
971`,
972
973 `This is exciting![^fn1]
974
975[^fn1]: Fine print
976`,
977 `<p>This is exciting!<sup class="footnote-ref" id="fnref:fn1"><a rel="footnote" href="#fn:fn1">1</a></sup></p>
978<div class="footnotes">
979
980<hr />
981
982<ol>
983<li id="fn:fn1">Fine print
984</li>
985</ol>
986</div>
987`,
988}
989
990func TestFootnotes(t *testing.T) {
991 doTestsInlineParam(t, footnoteTests, Options{Extensions: EXTENSION_FOOTNOTES}, 0, HtmlRendererParameters{})
992}
993
994func TestFootnotesWithParameters(t *testing.T) {
995 tests := make([]string, len(footnoteTests))
996
997 prefix := "testPrefix"
998 returnText := "ret"
999 re := regexp.MustCompile(`(?ms)<li id="fn:(\S+?)">(.*?)</li>`)
1000
1001 // Transform the test expectations to match the parameters we're using.
1002 for i, test := range footnoteTests {
1003 if i%2 == 1 {
1004 test = strings.Replace(test, "fn:", "fn:"+prefix, -1)
1005 test = strings.Replace(test, "fnref:", "fnref:"+prefix, -1)
1006 test = re.ReplaceAllString(test, `<li id="fn:$1">$2 <a class="footnote-return" href="#fnref:$1">ret</a></li>`)
1007 }
1008 tests[i] = test
1009 }
1010
1011 params := HtmlRendererParameters{
1012 FootnoteAnchorPrefix: prefix,
1013 FootnoteReturnLinkContents: returnText,
1014 }
1015
1016 doTestsInlineParam(t, tests, Options{Extensions: EXTENSION_FOOTNOTES}, HTML_FOOTNOTE_RETURN_LINKS, params)
1017}
1018
1019func TestNestedFootnotes(t *testing.T) {
1020 var tests = []string{
1021 `Paragraph.[^fn1]
1022
1023[^fn1]:
1024 Asterisk[^fn2]
1025
1026[^fn2]:
1027 Obelisk`,
1028 `<p>Paragraph.<sup class="footnote-ref" id="fnref:fn1"><a rel="footnote" href="#fn:fn1">1</a></sup></p>
1029<div class="footnotes">
1030
1031<hr />
1032
1033<ol>
1034<li id="fn:fn1">Asterisk<sup class="footnote-ref" id="fnref:fn2"><a rel="footnote" href="#fn:fn2">2</a></sup>
1035</li>
1036<li id="fn:fn2">Obelisk
1037</li>
1038</ol>
1039</div>
1040`,
1041 }
1042 doTestsInlineParam(t, tests, Options{Extensions: EXTENSION_FOOTNOTES}, 0,
1043 HtmlRendererParameters{})
1044}
1045
1046func TestInlineComments(t *testing.T) {
1047 var tests = []string{
1048 "Hello <!-- there ->\n",
1049 "<p>Hello <!— there –></p>\n",
1050
1051 "Hello <!-- there -->\n",
1052 "<p>Hello <!-- there --></p>\n",
1053
1054 "Hello <!-- there -->",
1055 "<p>Hello <!-- there --></p>\n",
1056
1057 "Hello <!---->\n",
1058 "<p>Hello <!----></p>\n",
1059
1060 "Hello <!-- there -->\na",
1061 "<p>Hello <!-- there -->\na</p>\n",
1062
1063 "* list <!-- item -->\n",
1064 "<ul>\n<li>list <!-- item --></li>\n</ul>\n",
1065
1066 "<!-- Front --> comment\n",
1067 "<p><!-- Front --> comment</p>\n",
1068
1069 "blahblah\n<!--- foo -->\nrhubarb\n",
1070 "<p>blahblah\n<!--- foo -->\nrhubarb</p>\n",
1071 }
1072 doTestsInlineParam(t, tests, Options{}, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_DASHES, HtmlRendererParameters{})
1073}
1074
1075func TestSmartDoubleQuotes(t *testing.T) {
1076 var tests = []string{
1077 "this should be normal \"quoted\" text.\n",
1078 "<p>this should be normal “quoted” text.</p>\n",
1079 "this \" single double\n",
1080 "<p>this “ single double</p>\n",
1081 "two pair of \"some\" quoted \"text\".\n",
1082 "<p>two pair of “some” quoted “text”.</p>\n"}
1083
1084 doTestsInlineParam(t, tests, Options{}, HTML_USE_SMARTYPANTS, HtmlRendererParameters{})
1085}
1086
1087func TestSmartAngledDoubleQuotes(t *testing.T) {
1088 var tests = []string{
1089 "this should be angled \"quoted\" text.\n",
1090 "<p>this should be angled «quoted» text.</p>\n",
1091 "this \" single double\n",
1092 "<p>this « single double</p>\n",
1093 "two pair of \"some\" quoted \"text\".\n",
1094 "<p>two pair of «some» quoted «text».</p>\n"}
1095
1096 doTestsInlineParam(t, tests, Options{}, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_ANGLED_QUOTES, HtmlRendererParameters{})
1097}
1098
1099func TestSmartFractions(t *testing.T) {
1100 var tests = []string{
1101 "1/2, 1/4 and 3/4; 1/4th and 3/4ths\n",
1102 "<p>½, ¼ and ¾; ¼th and ¾ths</p>\n",
1103 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
1104 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
1105
1106 doTestsInlineParam(t, tests, Options{}, HTML_USE_SMARTYPANTS, HtmlRendererParameters{})
1107
1108 tests = []string{
1109 "1/2, 2/3, 81/100 and 1000000/1048576.\n",
1110 "<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",
1111 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
1112 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
1113
1114 doTestsInlineParam(t, tests, Options{}, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_FRACTIONS, HtmlRendererParameters{})
1115}
1116
1117func TestDisableSmartDashes(t *testing.T) {
1118 doTestsInlineParam(t, []string{
1119 "foo - bar\n",
1120 "<p>foo - bar</p>\n",
1121 "foo -- bar\n",
1122 "<p>foo -- bar</p>\n",
1123 "foo --- bar\n",
1124 "<p>foo --- bar</p>\n",
1125 }, Options{}, 0, HtmlRendererParameters{})
1126 doTestsInlineParam(t, []string{
1127 "foo - bar\n",
1128 "<p>foo – bar</p>\n",
1129 "foo -- bar\n",
1130 "<p>foo — bar</p>\n",
1131 "foo --- bar\n",
1132 "<p>foo —– bar</p>\n",
1133 }, Options{}, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_DASHES, HtmlRendererParameters{})
1134 doTestsInlineParam(t, []string{
1135 "foo - bar\n",
1136 "<p>foo - bar</p>\n",
1137 "foo -- bar\n",
1138 "<p>foo – bar</p>\n",
1139 "foo --- bar\n",
1140 "<p>foo — bar</p>\n",
1141 }, Options{}, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_LATEX_DASHES|HTML_SMARTYPANTS_DASHES,
1142 HtmlRendererParameters{})
1143 doTestsInlineParam(t, []string{
1144 "foo - bar\n",
1145 "<p>foo - bar</p>\n",
1146 "foo -- bar\n",
1147 "<p>foo -- bar</p>\n",
1148 "foo --- bar\n",
1149 "<p>foo --- bar</p>\n",
1150 }, Options{},
1151 HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_LATEX_DASHES,
1152 HtmlRendererParameters{})
1153}