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, extensions, htmlFlags int, params HtmlRendererParameters) string {
24 extensions |= EXTENSION_AUTOLINK
25 extensions |= EXTENSION_STRIKETHROUGH
26
27 htmlFlags |= HTML_USE_XHTML
28
29 renderer := HtmlRendererWithParameters(htmlFlags, "", "", params)
30
31 return string(Markdown([]byte(input), renderer, extensions))
32}
33
34func doTestsInline(t *testing.T, tests []string) {
35 doTestsInlineParam(t, tests, 0, 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, 0, 0, params)
45 doTestsInlineParam(t, transformTests, 0, commonHtmlFlags, params)
46}
47
48func doSafeTestsInline(t *testing.T, tests []string) {
49 doTestsInlineParam(t, tests, 0, 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, 0, HTML_SAFELINK, params)
57}
58
59func doTestsInlineParam(t *testing.T, tests []string, extensions, 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
76 actual := runMarkdownInline(candidate, extensions, htmlFlags, params)
77 if actual != expected {
78 t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
79 candidate, expected, actual)
80 }
81
82 // now test every substring to stress test bounds checking
83 if !testing.Short() {
84 for start := 0; start < len(input); start++ {
85 for end := start + 1; end <= len(input); end++ {
86 candidate = input[start:end]
87 _ = runMarkdownInline(candidate, extensions, htmlFlags, params)
88 }
89 }
90 }
91 }
92}
93
94func transformLinks(tests []string, prefix string) []string {
95 newTests := make([]string, len(tests))
96 anchorRe := regexp.MustCompile(`<a href="/(.*?)"`)
97 imgRe := regexp.MustCompile(`<img src="/(.*?)"`)
98 for i, test := range tests {
99 if i%2 == 1 {
100 test = anchorRe.ReplaceAllString(test, `<a href="`+prefix+`/$1"`)
101 test = imgRe.ReplaceAllString(test, `<img src="`+prefix+`/$1"`)
102 }
103 newTests[i] = test
104 }
105 return newTests
106}
107
108func TestEmphasis(t *testing.T) {
109 var tests = []string{
110 "nothing inline\n",
111 "<p>nothing inline</p>\n",
112
113 "simple *inline* test\n",
114 "<p>simple <em>inline</em> test</p>\n",
115
116 "*at the* beginning\n",
117 "<p><em>at the</em> beginning</p>\n",
118
119 "at the *end*\n",
120 "<p>at the <em>end</em></p>\n",
121
122 "*try two* in *one line*\n",
123 "<p><em>try two</em> in <em>one line</em></p>\n",
124
125 "over *two\nlines* test\n",
126 "<p>over <em>two\nlines</em> test</p>\n",
127
128 "odd *number of* markers* here\n",
129 "<p>odd <em>number of</em> markers* here</p>\n",
130
131 "odd *number\nof* markers* here\n",
132 "<p>odd <em>number\nof</em> markers* here</p>\n",
133
134 "simple _inline_ test\n",
135 "<p>simple <em>inline</em> test</p>\n",
136
137 "_at the_ beginning\n",
138 "<p><em>at the</em> beginning</p>\n",
139
140 "at the _end_\n",
141 "<p>at the <em>end</em></p>\n",
142
143 "_try two_ in _one line_\n",
144 "<p><em>try two</em> in <em>one line</em></p>\n",
145
146 "over _two\nlines_ test\n",
147 "<p>over <em>two\nlines</em> test</p>\n",
148
149 "odd _number of_ markers_ here\n",
150 "<p>odd <em>number of</em> markers_ here</p>\n",
151
152 "odd _number\nof_ markers_ here\n",
153 "<p>odd <em>number\nof</em> markers_ here</p>\n",
154
155 "mix of *markers_\n",
156 "<p>mix of *markers_</p>\n",
157 }
158 doTestsInline(t, tests)
159}
160
161func TestStrong(t *testing.T) {
162 var tests = []string{
163 "nothing inline\n",
164 "<p>nothing inline</p>\n",
165
166 "simple **inline** test\n",
167 "<p>simple <strong>inline</strong> test</p>\n",
168
169 "**at the** beginning\n",
170 "<p><strong>at the</strong> beginning</p>\n",
171
172 "at the **end**\n",
173 "<p>at the <strong>end</strong></p>\n",
174
175 "**try two** in **one line**\n",
176 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
177
178 "over **two\nlines** test\n",
179 "<p>over <strong>two\nlines</strong> test</p>\n",
180
181 "odd **number of** markers** here\n",
182 "<p>odd <strong>number of</strong> markers** here</p>\n",
183
184 "odd **number\nof** markers** here\n",
185 "<p>odd <strong>number\nof</strong> markers** here</p>\n",
186
187 "simple __inline__ test\n",
188 "<p>simple <strong>inline</strong> test</p>\n",
189
190 "__at the__ beginning\n",
191 "<p><strong>at the</strong> beginning</p>\n",
192
193 "at the __end__\n",
194 "<p>at the <strong>end</strong></p>\n",
195
196 "__try two__ in __one line__\n",
197 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
198
199 "over __two\nlines__ test\n",
200 "<p>over <strong>two\nlines</strong> test</p>\n",
201
202 "odd __number of__ markers__ here\n",
203 "<p>odd <strong>number of</strong> markers__ here</p>\n",
204
205 "odd __number\nof__ markers__ here\n",
206 "<p>odd <strong>number\nof</strong> markers__ here</p>\n",
207
208 "mix of **markers__\n",
209 "<p>mix of **markers__</p>\n",
210 }
211 doTestsInline(t, tests)
212}
213
214func TestEmphasisMix(t *testing.T) {
215 var tests = []string{
216 "***triple emphasis***\n",
217 "<p><strong><em>triple emphasis</em></strong></p>\n",
218
219 "***triple\nemphasis***\n",
220 "<p><strong><em>triple\nemphasis</em></strong></p>\n",
221
222 "___triple emphasis___\n",
223 "<p><strong><em>triple emphasis</em></strong></p>\n",
224
225 "***triple emphasis___\n",
226 "<p>***triple emphasis___</p>\n",
227
228 "*__triple emphasis__*\n",
229 "<p><em><strong>triple emphasis</strong></em></p>\n",
230
231 "__*triple emphasis*__\n",
232 "<p><strong><em>triple emphasis</em></strong></p>\n",
233
234 "**improper *nesting** is* bad\n",
235 "<p><strong>improper *nesting</strong> is* bad</p>\n",
236
237 "*improper **nesting* is** bad\n",
238 "<p><em>improper **nesting</em> is** bad</p>\n",
239 }
240 doTestsInline(t, tests)
241}
242
243func TestEmphasisLink(t *testing.T) {
244 var tests = []string{
245 "[first](before) *text[second] (inside)text* [third](after)\n",
246 "<p><a href=\"before\">first</a> <em>text<a href=\"inside\">second</a>text</em> <a href=\"after\">third</a></p>\n",
247
248 "*incomplete [link] definition*\n",
249 "<p><em>incomplete [link] definition</em></p>\n",
250
251 "*it's [emphasis*] (not link)\n",
252 "<p><em>it's [emphasis</em>] (not link)</p>\n",
253
254 "*it's [emphasis*] and *[asterisk]\n",
255 "<p><em>it's [emphasis</em>] and *[asterisk]</p>\n",
256 }
257 doTestsInline(t, tests)
258}
259
260func TestStrikeThrough(t *testing.T) {
261 var tests = []string{
262 "nothing inline\n",
263 "<p>nothing inline</p>\n",
264
265 "simple ~~inline~~ test\n",
266 "<p>simple <del>inline</del> test</p>\n",
267
268 "~~at the~~ beginning\n",
269 "<p><del>at the</del> beginning</p>\n",
270
271 "at the ~~end~~\n",
272 "<p>at the <del>end</del></p>\n",
273
274 "~~try two~~ in ~~one line~~\n",
275 "<p><del>try two</del> in <del>one line</del></p>\n",
276
277 "over ~~two\nlines~~ test\n",
278 "<p>over <del>two\nlines</del> test</p>\n",
279
280 "odd ~~number of~~ markers~~ here\n",
281 "<p>odd <del>number of</del> markers~~ here</p>\n",
282
283 "odd ~~number\nof~~ markers~~ here\n",
284 "<p>odd <del>number\nof</del> markers~~ here</p>\n",
285 }
286 doTestsInline(t, tests)
287}
288
289func TestCodeSpan(t *testing.T) {
290 var tests = []string{
291 "`source code`\n",
292 "<p><code>source code</code></p>\n",
293
294 "` source code with spaces `\n",
295 "<p><code>source code with spaces</code></p>\n",
296
297 "` source code with spaces `not here\n",
298 "<p><code>source code with spaces</code>not here</p>\n",
299
300 "a `single marker\n",
301 "<p>a `single marker</p>\n",
302
303 "a single multi-tick marker with ``` no text\n",
304 "<p>a single multi-tick marker with ``` no text</p>\n",
305
306 "markers with ` ` a space\n",
307 "<p>markers with a space</p>\n",
308
309 "`source code` and a `stray\n",
310 "<p><code>source code</code> and a `stray</p>\n",
311
312 "`source *with* _awkward characters_ in it`\n",
313 "<p><code>source *with* _awkward characters_ in it</code></p>\n",
314
315 "`split over\ntwo lines`\n",
316 "<p><code>split over\ntwo lines</code></p>\n",
317
318 "```multiple ticks``` for the marker\n",
319 "<p><code>multiple ticks</code> for the marker</p>\n",
320
321 "```multiple ticks `with` ticks inside```\n",
322 "<p><code>multiple ticks `with` ticks inside</code></p>\n",
323 }
324 doTestsInline(t, tests)
325}
326
327func TestLineBreak(t *testing.T) {
328 var 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\\\ndoes not\n",
336 "<p>this line\\\ndoes not</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 doTestsInline(t, tests)
345
346 tests = []string{
347 "this line \nhas a break\n",
348 "<p>this line<br />\nhas a break</p>\n",
349
350 "this line \ndoes not\n",
351 "<p>this line\ndoes not</p>\n",
352
353 "this line\\\nhas a break\n",
354 "<p>this line<br />\nhas a break</p>\n",
355
356 "this line\\ \ndoes not\n",
357 "<p>this line\\\ndoes not</p>\n",
358
359 "this has an \nextra space\n",
360 "<p>this has an<br />\nextra space</p>\n",
361 }
362 doTestsInlineParam(t, tests, EXTENSION_BACKSLASH_LINE_BREAK, 0, HtmlRendererParameters{})
363}
364
365func TestInlineLink(t *testing.T) {
366 var tests = []string{
367 "[foo](/bar/)\n",
368 "<p><a href=\"/bar/\">foo</a></p>\n",
369
370 "[foo with a title](/bar/ \"title\")\n",
371 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
372
373 "[foo with a title](/bar/\t\"title\")\n",
374 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
375
376 "[foo with a title](/bar/ \"title\" )\n",
377 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
378
379 "[foo with a title](/bar/ title with no quotes)\n",
380 "<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n",
381
382 "[foo]()\n",
383 "<p>[foo]()</p>\n",
384
385 "![foo](/bar/)\n",
386 "<p><img src=\"/bar/\" alt=\"foo\" />\n</p>\n",
387
388 "![foo with a title](/bar/ \"title\")\n",
389 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
390
391 "![foo with a title](/bar/\t\"title\")\n",
392 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
393
394 "![foo with a title](/bar/ \"title\" )\n",
395 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
396
397 "![foo with a title](/bar/ title with no quotes)\n",
398 "<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" />\n</p>\n",
399
400 "![](img.jpg)\n",
401 "<p><img src=\"img.jpg\" alt=\"\" />\n</p>\n",
402
403 "[link](url)\n",
404 "<p><a href=\"url\">link</a></p>\n",
405
406 "![foo]()\n",
407 "<p>![foo]()</p>\n",
408
409 "[a link]\t(/with_a_tab/)\n",
410 "<p><a href=\"/with_a_tab/\">a link</a></p>\n",
411
412 "[a link] (/with_spaces/)\n",
413 "<p><a href=\"/with_spaces/\">a link</a></p>\n",
414
415 "[text (with) [[nested] (brackets)]](/url/)\n",
416 "<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n",
417
418 "[text (with) [broken nested] (brackets)]](/url/)\n",
419 "<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n",
420
421 "[text\nwith a newline](/link/)\n",
422 "<p><a href=\"/link/\">text\nwith a newline</a></p>\n",
423
424 "[text in brackets] [followed](/by a link/)\n",
425 "<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n",
426
427 "[link with\\] a closing bracket](/url/)\n",
428 "<p><a href=\"/url/\">link with] a closing bracket</a></p>\n",
429
430 "[link with\\[ an opening bracket](/url/)\n",
431 "<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n",
432
433 "[link with\\) a closing paren](/url/)\n",
434 "<p><a href=\"/url/\">link with) a closing paren</a></p>\n",
435
436 "[link with\\( an opening paren](/url/)\n",
437 "<p><a href=\"/url/\">link with( an opening paren</a></p>\n",
438
439 "[link]( with whitespace)\n",
440 "<p><a href=\"with whitespace\">link</a></p>\n",
441
442 "[link]( with whitespace )\n",
443 "<p><a href=\"with whitespace\">link</a></p>\n",
444
445 "[![image](someimage)](with image)\n",
446 "<p><a href=\"with image\"><img src=\"someimage\" alt=\"image\" />\n</a></p>\n",
447
448 "[link](url \"one quote)\n",
449 "<p><a href=\"url "one quote\">link</a></p>\n",
450
451 "[link](url 'one quote)\n",
452 "<p><a href=\"url 'one quote\">link</a></p>\n",
453
454 "[link](<url>)\n",
455 "<p><a href=\"url\">link</a></p>\n",
456
457 "[link & ampersand](/url/)\n",
458 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
459
460 "[link & ampersand](/url/)\n",
461 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
462
463 "[link](/url/&query)\n",
464 "<p><a href=\"/url/&query\">link</a></p>\n",
465
466 "[[t]](/t)\n",
467 "<p><a href=\"/t\">[t]</a></p>\n",
468
469 "[link](</>)\n",
470 "<p><a href=\"/\">link</a></p>\n",
471
472 "[link](<./>)\n",
473 "<p><a href=\"./\">link</a></p>\n",
474
475 "[link](<../>)\n",
476 "<p><a href=\"../\">link</a></p>\n",
477 }
478 doLinkTestsInline(t, tests)
479
480}
481
482func TestRelAttrLink(t *testing.T) {
483 var nofollowTests = []string{
484 "[foo](http://bar.com/foo/)\n",
485 "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n",
486
487 "[foo](/bar/)\n",
488 "<p><a href=\"/bar/\">foo</a></p>\n",
489
490 "[foo](/)\n",
491 "<p><a href=\"/\">foo</a></p>\n",
492
493 "[foo](./)\n",
494 "<p><a href=\"./\">foo</a></p>\n",
495
496 "[foo](../)\n",
497 "<p><a href=\"../\">foo</a></p>\n",
498
499 "[foo](../bar)\n",
500 "<p><a href=\"../bar\">foo</a></p>\n",
501 }
502 doTestsInlineParam(t, nofollowTests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS,
503 HtmlRendererParameters{})
504
505 var noreferrerTests = []string{
506 "[foo](http://bar.com/foo/)\n",
507 "<p><a href=\"http://bar.com/foo/\" rel=\"noreferrer\">foo</a></p>\n",
508
509 "[foo](/bar/)\n",
510 "<p><a href=\"/bar/\">foo</a></p>\n",
511 }
512 doTestsInlineParam(t, noreferrerTests, 0, HTML_SAFELINK|HTML_NOREFERRER_LINKS,
513 HtmlRendererParameters{})
514
515 var nofollownoreferrerTests = []string{
516 "[foo](http://bar.com/foo/)\n",
517 "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow noreferrer\">foo</a></p>\n",
518
519 "[foo](/bar/)\n",
520 "<p><a href=\"/bar/\">foo</a></p>\n",
521 }
522 doTestsInlineParam(t, nofollownoreferrerTests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_NOREFERRER_LINKS,
523 HtmlRendererParameters{})
524}
525
526func TestHrefTargetBlank(t *testing.T) {
527 var tests = []string{
528 // internal link
529 "[foo](/bar/)\n",
530 "<p><a href=\"/bar/\">foo</a></p>\n",
531
532 "[foo](/)\n",
533 "<p><a href=\"/\">foo</a></p>\n",
534
535 "[foo](./)\n",
536 "<p><a href=\"./\">foo</a></p>\n",
537
538 "[foo](./bar)\n",
539 "<p><a href=\"./bar\">foo</a></p>\n",
540
541 "[foo](../)\n",
542 "<p><a href=\"../\">foo</a></p>\n",
543
544 "[foo](../bar)\n",
545 "<p><a href=\"../bar\">foo</a></p>\n",
546
547 "[foo](http://example.com)\n",
548 "<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n",
549 }
550 doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_HREF_TARGET_BLANK, HtmlRendererParameters{})
551}
552
553func TestSafeInlineLink(t *testing.T) {
554 var tests = []string{
555 "[foo](/bar/)\n",
556 "<p><a href=\"/bar/\">foo</a></p>\n",
557
558 "[foo](/)\n",
559 "<p><a href=\"/\">foo</a></p>\n",
560
561 "[foo](./)\n",
562 "<p><a href=\"./\">foo</a></p>\n",
563
564 "[foo](../)\n",
565 "<p><a href=\"../\">foo</a></p>\n",
566
567 "[foo](http://bar/)\n",
568 "<p><a href=\"http://bar/\">foo</a></p>\n",
569
570 "[foo](https://bar/)\n",
571 "<p><a href=\"https://bar/\">foo</a></p>\n",
572
573 "[foo](ftp://bar/)\n",
574 "<p><a href=\"ftp://bar/\">foo</a></p>\n",
575
576 "[foo](mailto://bar/)\n",
577 "<p><a href=\"mailto://bar/\">foo</a></p>\n",
578
579 // Not considered safe
580 "[foo](baz://bar/)\n",
581 "<p><tt>foo</tt></p>\n",
582 }
583 doSafeTestsInline(t, tests)
584}
585
586func TestReferenceLink(t *testing.T) {
587 var tests = []string{
588 "[link][ref]\n",
589 "<p>[link][ref]</p>\n",
590
591 "[link][ref]\n [ref]: /url/ \"title\"\n",
592 "<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
593
594 "[link][ref]\n [ref]: /url/\n",
595 "<p><a href=\"/url/\">link</a></p>\n",
596
597 " [ref]: /url/\n",
598 "",
599
600 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
601 "",
602
603 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n [4spaces]: /url/\n",
604 "<pre><code>[4spaces]: /url/\n</code></pre>\n",
605
606 "[hmm](ref2)\n [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
607 "<p><a href=\"ref2\">hmm</a></p>\n",
608
609 "[ref]\n",
610 "<p>[ref]</p>\n",
611
612 "[ref]\n [ref]: /url/ \"title\"\n",
613 "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
614
615 "[ref]\n [ref]: ../url/ \"title\"\n",
616 "<p><a href=\"../url/\" title=\"title\">ref</a></p>\n",
617 }
618 doLinkTestsInline(t, tests)
619}
620
621func TestTags(t *testing.T) {
622 var tests = []string{
623 "a <span>tag</span>\n",
624 "<p>a <span>tag</span></p>\n",
625
626 "<span>tag</span>\n",
627 "<p><span>tag</span></p>\n",
628
629 "<span>mismatch</spandex>\n",
630 "<p><span>mismatch</spandex></p>\n",
631
632 "a <singleton /> tag\n",
633 "<p>a <singleton /> tag</p>\n",
634 }
635 doTestsInline(t, tests)
636}
637
638func TestAutoLink(t *testing.T) {
639 var tests = []string{
640 "http://foo.com/\n",
641 "<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
642
643 "1 http://foo.com/\n",
644 "<p>1 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
645
646 "1http://foo.com/\n",
647 "<p>1<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
648
649 "1.http://foo.com/\n",
650 "<p>1.<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
651
652 "1. http://foo.com/\n",
653 "<ol>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ol>\n",
654
655 "-http://foo.com/\n",
656 "<p>-<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
657
658 "- http://foo.com/\n",
659 "<ul>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ul>\n",
660
661 "_http://foo.com/\n",
662 "<p>_<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
663
664 "令狐http://foo.com/\n",
665 "<p>令狐<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
666
667 "令狐 http://foo.com/\n",
668 "<p>令狐 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
669
670 "ahttp://foo.com/\n",
671 "<p>ahttp://foo.com/</p>\n",
672
673 ">http://foo.com/\n",
674 "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
675
676 "> http://foo.com/\n",
677 "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
678
679 "go to <http://foo.com/>\n",
680 "<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
681
682 "a secure <https://link.org>\n",
683 "<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
684
685 "an email <mailto:some@one.com>\n",
686 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
687
688 "an email <mailto://some@one.com>\n",
689 "<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
690
691 "an email <some@one.com>\n",
692 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
693
694 "an ftp <ftp://old.com>\n",
695 "<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
696
697 "an ftp <ftp:old.com>\n",
698 "<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
699
700 "a link with <http://new.com?query=foo&bar>\n",
701 "<p>a link with <a href=\"http://new.com?query=foo&bar\">" +
702 "http://new.com?query=foo&bar</a></p>\n",
703
704 "quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
705 "<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
706
707 "quotes mean a tag <http://new.com?query='foo'&bar>\n",
708 "<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
709
710 "unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
711 "<p>unless escaped <a href=\"http://new.com?query="foo"&bar\">" +
712 "http://new.com?query="foo"&bar</a></p>\n",
713
714 "even a > can be escaped <http://new.com?q=\\>&etc>\n",
715 "<p>even a > can be escaped <a href=\"http://new.com?q=>&etc\">" +
716 "http://new.com?q=>&etc</a></p>\n",
717
718 "<a href=\"http://fancy.com\">http://fancy.com</a>\n",
719 "<p><a href=\"http://fancy.com\">http://fancy.com</a></p>\n",
720
721 "<a href=\"http://fancy.com\">This is a link</a>\n",
722 "<p><a href=\"http://fancy.com\">This is a link</a></p>\n",
723
724 "<a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a>\n",
725 "<p><a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a></p>\n",
726
727 "(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (\n",
728 "<p>(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (</p>\n",
729
730 "(<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",
731 "<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",
732
733 "http://www.foo.com<br />\n",
734 "<p><a href=\"http://www.foo.com\">http://www.foo.com</a><br /></p>\n",
735
736 "http://foo.com/viewtopic.php?f=18&t=297",
737 "<p><a href=\"http://foo.com/viewtopic.php?f=18&t=297\">http://foo.com/viewtopic.php?f=18&t=297</a></p>\n",
738
739 "http://foo.com/viewtopic.php?param="18"zz",
740 "<p><a href=\"http://foo.com/viewtopic.php?param="18"zz\">http://foo.com/viewtopic.php?param="18"zz</a></p>\n",
741
742 "http://foo.com/viewtopic.php?param="18"",
743 "<p><a href=\"http://foo.com/viewtopic.php?param="18"\">http://foo.com/viewtopic.php?param="18"</a></p>\n",
744 }
745 doLinkTestsInline(t, tests)
746}
747
748var footnoteTests = []string{
749 "testing footnotes.[^a]\n\n[^a]: This is the note\n",
750 `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
751<div class="footnotes">
752
753<hr />
754
755<ol>
756<li id="fn:a">This is the note
757</li>
758</ol>
759</div>
760`,
761
762 `testing long[^b] notes.
763
764[^b]: Paragraph 1
765
766 Paragraph 2
767
768 ` + "```\n\tsome code\n\t```" + `
769
770 Paragraph 3
771
772No longer in the footnote
773`,
774 `<p>testing long<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">1</a></sup> notes.</p>
775
776<p>No longer in the footnote</p>
777<div class="footnotes">
778
779<hr />
780
781<ol>
782<li id="fn:b"><p>Paragraph 1</p>
783
784<p>Paragraph 2</p>
785
786<p><code>
787some code
788</code></p>
789
790<p>Paragraph 3</p>
791</li>
792</ol>
793</div>
794`,
795
796 `testing[^c] multiple[^d] notes.
797
798[^c]: this is [note] c
799
800
801omg
802
803[^d]: this is note d
804
805what happens here
806
807[note]: /link/c
808
809`,
810 `<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>
811
812<p>omg</p>
813
814<p>what happens here</p>
815<div class="footnotes">
816
817<hr />
818
819<ol>
820<li id="fn:c">this is <a href="/link/c">note</a> c
821</li>
822<li id="fn:d">this is note d
823</li>
824</ol>
825</div>
826`,
827
828 "testing inline^[this is the note] notes.\n",
829 `<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>
830<div class="footnotes">
831
832<hr />
833
834<ol>
835<li id="fn:this-is-the-note">this is the note</li>
836</ol>
837</div>
838`,
839
840 "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",
841 `<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>
842<div class="footnotes">
843
844<hr />
845
846<ol>
847<li id="fn:1"><p>the first deferred note</p>
848
849<p>which happens to be a block</p>
850</li>
851<li id="fn:inline-note">inline note</li>
852<li id="fn:2">the second deferred note
853</li>
854</ol>
855</div>
856`,
857
858 `This is a footnote[^1]^[and this is an inline footnote]
859
860[^1]: the footnote text.
861
862 may be multiple paragraphs.
863`,
864 `<p>This is a footnote<sup class="footnote-ref" id="fnref:1"><a rel="footnote" href="#fn:1">1</a></sup><sup class="footnote-ref" id="fnref:and-this-is-an-i"><a rel="footnote" href="#fn:and-this-is-an-i">2</a></sup></p>
865<div class="footnotes">
866
867<hr />
868
869<ol>
870<li id="fn:1"><p>the footnote text.</p>
871
872<p>may be multiple paragraphs.</p>
873</li>
874<li id="fn:and-this-is-an-i">and this is an inline footnote</li>
875</ol>
876</div>
877`,
878
879 "empty footnote[^]\n\n[^]: fn text",
880 "<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",
881
882 "Some text.[^note1]\n\n[^note1]: fn1",
883 "<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",
884
885 "Some text.[^note1][^note2]\n\n[^note1]: fn1\n[^note2]: fn2\n",
886 "<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",
887}
888
889func TestFootnotes(t *testing.T) {
890 doTestsInlineParam(t, footnoteTests, EXTENSION_FOOTNOTES, 0, HtmlRendererParameters{})
891}
892
893func TestFootnotesWithParameters(t *testing.T) {
894 tests := make([]string, len(footnoteTests))
895
896 prefix := "testPrefix"
897 returnText := "ret"
898 re := regexp.MustCompile(`(?ms)<li id="fn:(\S+?)">(.*?)</li>`)
899
900 // Transform the test expectations to match the parameters we're using.
901 for i, test := range footnoteTests {
902 if i%2 == 1 {
903 test = strings.Replace(test, "fn:", "fn:"+prefix, -1)
904 test = strings.Replace(test, "fnref:", "fnref:"+prefix, -1)
905 test = re.ReplaceAllString(test, `<li id="fn:$1">$2 <a class="footnote-return" href="#fnref:$1">ret</a></li>`)
906 }
907 tests[i] = test
908 }
909
910 params := HtmlRendererParameters{
911 FootnoteAnchorPrefix: prefix,
912 FootnoteReturnLinkContents: returnText,
913 }
914
915 doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, HTML_FOOTNOTE_RETURN_LINKS, params)
916}
917
918func TestSmartDoubleQuotes(t *testing.T) {
919 var tests = []string{
920 "this should be normal \"quoted\" text.\n",
921 "<p>this should be normal “quoted” text.</p>\n",
922 "this \" single double\n",
923 "<p>this “ single double</p>\n",
924 "two pair of \"some\" quoted \"text\".\n",
925 "<p>two pair of “some” quoted “text”.</p>\n"}
926
927 doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS, HtmlRendererParameters{})
928}
929
930func TestSmartAngledDoubleQuotes(t *testing.T) {
931 var tests = []string{
932 "this should be angled \"quoted\" text.\n",
933 "<p>this should be angled «quoted» text.</p>\n",
934 "this \" single double\n",
935 "<p>this « single double</p>\n",
936 "two pair of \"some\" quoted \"text\".\n",
937 "<p>two pair of «some» quoted «text».</p>\n"}
938
939 doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_ANGLED_QUOTES, HtmlRendererParameters{})
940}
941
942func TestSmartFractions(t *testing.T) {
943 var tests = []string{
944 "1/2, 1/4 and 3/4; 1/4th and 3/4ths\n",
945 "<p>½, ¼ and ¾; ¼th and ¾ths</p>\n",
946 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
947 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
948
949 doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS, HtmlRendererParameters{})
950
951 tests = []string{
952 "1/2, 2/3, 81/100 and 1000000/1048576.\n",
953 "<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",
954 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
955 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
956
957 doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_FRACTIONS, HtmlRendererParameters{})
958}