all repos — grayfriday @ 9c061de92b235695e9e2220384c93093922b4153

blackfriday fork with a few changes

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		actual := runMarkdownInline(candidate, extensions, 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, extensions, 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	doTestsInline(t, tests)
158}
159
160func TestStrong(t *testing.T) {
161	var tests = []string{
162		"nothing inline\n",
163		"<p>nothing inline</p>\n",
164
165		"simple **inline** test\n",
166		"<p>simple <strong>inline</strong> test</p>\n",
167
168		"**at the** beginning\n",
169		"<p><strong>at the</strong> beginning</p>\n",
170
171		"at the **end**\n",
172		"<p>at the <strong>end</strong></p>\n",
173
174		"**try two** in **one line**\n",
175		"<p><strong>try two</strong> in <strong>one line</strong></p>\n",
176
177		"over **two\nlines** test\n",
178		"<p>over <strong>two\nlines</strong> test</p>\n",
179
180		"odd **number of** markers** here\n",
181		"<p>odd <strong>number of</strong> markers** here</p>\n",
182
183		"odd **number\nof** markers** here\n",
184		"<p>odd <strong>number\nof</strong> markers** here</p>\n",
185
186		"simple __inline__ test\n",
187		"<p>simple <strong>inline</strong> test</p>\n",
188
189		"__at the__ beginning\n",
190		"<p><strong>at the</strong> beginning</p>\n",
191
192		"at the __end__\n",
193		"<p>at the <strong>end</strong></p>\n",
194
195		"__try two__ in __one line__\n",
196		"<p><strong>try two</strong> in <strong>one line</strong></p>\n",
197
198		"over __two\nlines__ test\n",
199		"<p>over <strong>two\nlines</strong> test</p>\n",
200
201		"odd __number of__ markers__ here\n",
202		"<p>odd <strong>number of</strong> markers__ here</p>\n",
203
204		"odd __number\nof__ markers__ here\n",
205		"<p>odd <strong>number\nof</strong> markers__ here</p>\n",
206
207		"mix of **markers__\n",
208		"<p>mix of **markers__</p>\n",
209	}
210	doTestsInline(t, tests)
211}
212
213func TestEmphasisMix(t *testing.T) {
214	var tests = []string{
215		"***triple emphasis***\n",
216		"<p><strong><em>triple emphasis</em></strong></p>\n",
217
218		"***triple\nemphasis***\n",
219		"<p><strong><em>triple\nemphasis</em></strong></p>\n",
220
221		"___triple emphasis___\n",
222		"<p><strong><em>triple emphasis</em></strong></p>\n",
223
224		"***triple emphasis___\n",
225		"<p>***triple emphasis___</p>\n",
226
227		"*__triple emphasis__*\n",
228		"<p><em><strong>triple emphasis</strong></em></p>\n",
229
230		"__*triple emphasis*__\n",
231		"<p><strong><em>triple emphasis</em></strong></p>\n",
232
233		"**improper *nesting** is* bad\n",
234		"<p><strong>improper *nesting</strong> is* bad</p>\n",
235
236		"*improper **nesting* is** bad\n",
237		"<p><em>improper **nesting</em> is** bad</p>\n",
238	}
239	doTestsInline(t, tests)
240}
241
242func TestStrikeThrough(t *testing.T) {
243	var tests = []string{
244		"nothing inline\n",
245		"<p>nothing inline</p>\n",
246
247		"simple ~~inline~~ test\n",
248		"<p>simple <del>inline</del> test</p>\n",
249
250		"~~at the~~ beginning\n",
251		"<p><del>at the</del> beginning</p>\n",
252
253		"at the ~~end~~\n",
254		"<p>at the <del>end</del></p>\n",
255
256		"~~try two~~ in ~~one line~~\n",
257		"<p><del>try two</del> in <del>one line</del></p>\n",
258
259		"over ~~two\nlines~~ test\n",
260		"<p>over <del>two\nlines</del> test</p>\n",
261
262		"odd ~~number of~~ markers~~ here\n",
263		"<p>odd <del>number of</del> markers~~ here</p>\n",
264
265		"odd ~~number\nof~~ markers~~ here\n",
266		"<p>odd <del>number\nof</del> markers~~ here</p>\n",
267	}
268	doTestsInline(t, tests)
269}
270
271func TestCodeSpan(t *testing.T) {
272	var tests = []string{
273		"`source code`\n",
274		"<p><code>source code</code></p>\n",
275
276		"` source code with spaces `\n",
277		"<p><code>source code with spaces</code></p>\n",
278
279		"` source code with spaces `not here\n",
280		"<p><code>source code with spaces</code>not here</p>\n",
281
282		"a `single marker\n",
283		"<p>a `single marker</p>\n",
284
285		"a single multi-tick marker with ``` no text\n",
286		"<p>a single multi-tick marker with ``` no text</p>\n",
287
288		"markers with ` ` a space\n",
289		"<p>markers with  a space</p>\n",
290
291		"`source code` and a `stray\n",
292		"<p><code>source code</code> and a `stray</p>\n",
293
294		"`source *with* _awkward characters_ in it`\n",
295		"<p><code>source *with* _awkward characters_ in it</code></p>\n",
296
297		"`split over\ntwo lines`\n",
298		"<p><code>split over\ntwo lines</code></p>\n",
299
300		"```multiple ticks``` for the marker\n",
301		"<p><code>multiple ticks</code> for the marker</p>\n",
302
303		"```multiple ticks `with` ticks inside```\n",
304		"<p><code>multiple ticks `with` ticks inside</code></p>\n",
305	}
306	doTestsInline(t, tests)
307}
308
309func TestLineBreak(t *testing.T) {
310	var tests = []string{
311		"this line  \nhas a break\n",
312		"<p>this line<br />\nhas a break</p>\n",
313
314		"this line \ndoes not\n",
315		"<p>this line\ndoes not</p>\n",
316
317		"this has an   \nextra space\n",
318		"<p>this has an<br />\nextra space</p>\n",
319	}
320	doTestsInline(t, tests)
321}
322
323func TestInlineLink(t *testing.T) {
324	var tests = []string{
325		"[foo](/bar/)\n",
326		"<p><a href=\"/bar/\">foo</a></p>\n",
327
328		"[foo with a title](/bar/ \"title\")\n",
329		"<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
330
331		"[foo with a title](/bar/\t\"title\")\n",
332		"<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
333
334		"[foo with a title](/bar/ \"title\"  )\n",
335		"<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
336
337		"[foo with a title](/bar/ title with no quotes)\n",
338		"<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n",
339
340		"[foo]()\n",
341		"<p>[foo]()</p>\n",
342
343		"![foo](/bar/)\n",
344		"<p><img src=\"/bar/\" alt=\"foo\" />\n</p>\n",
345
346		"![foo with a title](/bar/ \"title\")\n",
347		"<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
348
349		"![foo with a title](/bar/\t\"title\")\n",
350		"<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
351
352		"![foo with a title](/bar/ \"title\"  )\n",
353		"<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
354
355		"![foo with a title](/bar/ title with no quotes)\n",
356		"<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" />\n</p>\n",
357
358		"![](img.jpg)\n",
359		"<p><img src=\"img.jpg\" alt=\"\" />\n</p>\n",
360
361		"[link](url)\n",
362		"<p><a href=\"url\">link</a></p>\n",
363
364		"![foo]()\n",
365		"<p>![foo]()</p>\n",
366
367		"[a link]\t(/with_a_tab/)\n",
368		"<p><a href=\"/with_a_tab/\">a link</a></p>\n",
369
370		"[a link]  (/with_spaces/)\n",
371		"<p><a href=\"/with_spaces/\">a link</a></p>\n",
372
373		"[text (with) [[nested] (brackets)]](/url/)\n",
374		"<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n",
375
376		"[text (with) [broken nested] (brackets)]](/url/)\n",
377		"<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n",
378
379		"[text\nwith a newline](/link/)\n",
380		"<p><a href=\"/link/\">text\nwith a newline</a></p>\n",
381
382		"[text in brackets] [followed](/by a link/)\n",
383		"<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n",
384
385		"[link with\\] a closing bracket](/url/)\n",
386		"<p><a href=\"/url/\">link with] a closing bracket</a></p>\n",
387
388		"[link with\\[ an opening bracket](/url/)\n",
389		"<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n",
390
391		"[link with\\) a closing paren](/url/)\n",
392		"<p><a href=\"/url/\">link with) a closing paren</a></p>\n",
393
394		"[link with\\( an opening paren](/url/)\n",
395		"<p><a href=\"/url/\">link with( an opening paren</a></p>\n",
396
397		"[link](  with whitespace)\n",
398		"<p><a href=\"with whitespace\">link</a></p>\n",
399
400		"[link](  with whitespace   )\n",
401		"<p><a href=\"with whitespace\">link</a></p>\n",
402
403		"[![image](someimage)](with image)\n",
404		"<p><a href=\"with image\"><img src=\"someimage\" alt=\"image\" />\n</a></p>\n",
405
406		"[link](url \"one quote)\n",
407		"<p><a href=\"url &quot;one quote\">link</a></p>\n",
408
409		"[link](url 'one quote)\n",
410		"<p><a href=\"url 'one quote\">link</a></p>\n",
411
412		"[link](<url>)\n",
413		"<p><a href=\"url\">link</a></p>\n",
414
415		"[link & ampersand](/url/)\n",
416		"<p><a href=\"/url/\">link &amp; ampersand</a></p>\n",
417
418		"[link &amp; ampersand](/url/)\n",
419		"<p><a href=\"/url/\">link &amp; ampersand</a></p>\n",
420
421		"[link](/url/&query)\n",
422		"<p><a href=\"/url/&amp;query\">link</a></p>\n",
423
424		"[[t]](/t)\n",
425		"<p><a href=\"/t\">[t]</a></p>\n",
426	}
427	doLinkTestsInline(t, tests)
428
429}
430
431func TestNofollowLink(t *testing.T) {
432	var tests = []string{
433		"[foo](http://bar.com/foo/)\n",
434		"<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n",
435
436		"[foo](/bar/)\n",
437		"<p><a href=\"/bar/\">foo</a></p>\n",
438	}
439	doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS,
440		HtmlRendererParameters{})
441}
442
443func TestHrefTargetBlank(t *testing.T) {
444	var tests = []string{
445		// internal link
446		"[foo](/bar/)\n",
447		"<p><a href=\"/bar/\">foo</a></p>\n",
448
449		"[foo](http://example.com)\n",
450		"<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n",
451	}
452	doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_HREF_TARGET_BLANK, HtmlRendererParameters{})
453}
454
455func TestSafeInlineLink(t *testing.T) {
456	var tests = []string{
457		"[foo](/bar/)\n",
458		"<p><a href=\"/bar/\">foo</a></p>\n",
459
460		"[foo](http://bar/)\n",
461		"<p><a href=\"http://bar/\">foo</a></p>\n",
462
463		"[foo](https://bar/)\n",
464		"<p><a href=\"https://bar/\">foo</a></p>\n",
465
466		"[foo](ftp://bar/)\n",
467		"<p><a href=\"ftp://bar/\">foo</a></p>\n",
468
469		"[foo](mailto://bar/)\n",
470		"<p><a href=\"mailto://bar/\">foo</a></p>\n",
471
472		// Not considered safe
473		"[foo](baz://bar/)\n",
474		"<p><tt>foo</tt></p>\n",
475	}
476	doSafeTestsInline(t, tests)
477}
478
479func TestReferenceLink(t *testing.T) {
480	var tests = []string{
481		"[link][ref]\n",
482		"<p>[link][ref]</p>\n",
483
484		"[link][ref]\n   [ref]: /url/ \"title\"\n",
485		"<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
486
487		"[link][ref]\n   [ref]: /url/\n",
488		"<p><a href=\"/url/\">link</a></p>\n",
489
490		"   [ref]: /url/\n",
491		"",
492
493		"   [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
494		"",
495
496		"   [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n    [4spaces]: /url/\n",
497		"<pre><code>[4spaces]: /url/\n</code></pre>\n",
498
499		"[hmm](ref2)\n   [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
500		"<p><a href=\"ref2\">hmm</a></p>\n",
501
502		"[ref]\n",
503		"<p>[ref]</p>\n",
504
505		"[ref]\n   [ref]: /url/ \"title\"\n",
506		"<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
507	}
508	doLinkTestsInline(t, tests)
509}
510
511func TestTags(t *testing.T) {
512	var tests = []string{
513		"a <span>tag</span>\n",
514		"<p>a <span>tag</span></p>\n",
515
516		"<span>tag</span>\n",
517		"<p><span>tag</span></p>\n",
518
519		"<span>mismatch</spandex>\n",
520		"<p><span>mismatch</spandex></p>\n",
521
522		"a <singleton /> tag\n",
523		"<p>a <singleton /> tag</p>\n",
524	}
525	doTestsInline(t, tests)
526}
527
528func TestAutoLink(t *testing.T) {
529	var tests = []string{
530		"http://foo.com/\n",
531		"<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
532
533		"1 http://foo.com/\n",
534		"<p>1 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
535
536		"1http://foo.com/\n",
537		"<p>1<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
538
539		"1.http://foo.com/\n",
540		"<p>1.<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
541
542		"1. http://foo.com/\n",
543		"<ol>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ol>\n",
544
545		"-http://foo.com/\n",
546		"<p>-<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
547
548		"- http://foo.com/\n",
549		"<ul>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ul>\n",
550
551		"_http://foo.com/\n",
552		"<p>_<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
553
554		"令狐http://foo.com/\n",
555		"<p>令狐<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
556
557		"令狐 http://foo.com/\n",
558		"<p>令狐 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
559
560		"ahttp://foo.com/\n",
561		"<p>ahttp://foo.com/</p>\n",
562
563		">http://foo.com/\n",
564		"<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
565
566		"> http://foo.com/\n",
567		"<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
568
569		"go to <http://foo.com/>\n",
570		"<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
571
572		"a secure <https://link.org>\n",
573		"<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
574
575		"an email <mailto:some@one.com>\n",
576		"<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
577
578		"an email <mailto://some@one.com>\n",
579		"<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
580
581		"an email <some@one.com>\n",
582		"<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
583
584		"an ftp <ftp://old.com>\n",
585		"<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
586
587		"an ftp <ftp:old.com>\n",
588		"<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
589
590		"a link with <http://new.com?query=foo&bar>\n",
591		"<p>a link with <a href=\"http://new.com?query=foo&amp;bar\">" +
592			"http://new.com?query=foo&amp;bar</a></p>\n",
593
594		"quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
595		"<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
596
597		"quotes mean a tag <http://new.com?query='foo'&bar>\n",
598		"<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
599
600		"unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
601		"<p>unless escaped <a href=\"http://new.com?query=&quot;foo&quot;&amp;bar\">" +
602			"http://new.com?query=&quot;foo&quot;&amp;bar</a></p>\n",
603
604		"even a > can be escaped <http://new.com?q=\\>&etc>\n",
605		"<p>even a &gt; can be escaped <a href=\"http://new.com?q=&gt;&amp;etc\">" +
606			"http://new.com?q=&gt;&amp;etc</a></p>\n",
607
608		"<a href=\"http://fancy.com\">http://fancy.com</a>\n",
609		"<p><a href=\"http://fancy.com\">http://fancy.com</a></p>\n",
610
611		"<a href=\"http://fancy.com\">This is a link</a>\n",
612		"<p><a href=\"http://fancy.com\">This is a link</a></p>\n",
613
614		"<a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a>\n",
615		"<p><a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a></p>\n",
616
617		"(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (\n",
618		"<p>(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (</p>\n",
619
620		"(<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",
621		"<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",
622
623		"http://www.foo.com<br />\n",
624		"<p><a href=\"http://www.foo.com\">http://www.foo.com</a><br /></p>\n",
625
626		"http://foo.com/viewtopic.php?f=18&amp;t=297",
627		"<p><a href=\"http://foo.com/viewtopic.php?f=18&amp;t=297\">http://foo.com/viewtopic.php?f=18&amp;t=297</a></p>\n",
628
629		"http://foo.com/viewtopic.php?param=&quot;18&quot;zz",
630		"<p><a href=\"http://foo.com/viewtopic.php?param=&quot;18&quot;zz\">http://foo.com/viewtopic.php?param=&quot;18&quot;zz</a></p>\n",
631
632		"http://foo.com/viewtopic.php?param=&quot;18&quot;",
633		"<p><a href=\"http://foo.com/viewtopic.php?param=&quot;18&quot;\">http://foo.com/viewtopic.php?param=&quot;18&quot;</a></p>\n",
634	}
635	doLinkTestsInline(t, tests)
636}
637
638var footnoteTests = []string{
639	"testing footnotes.[^a]\n\n[^a]: This is the note\n",
640	`<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
641<div class="footnotes">
642
643<hr />
644
645<ol>
646<li id="fn:a">This is the note
647</li>
648</ol>
649</div>
650`,
651
652	`testing long[^b] notes.
653
654[^b]: Paragraph 1
655
656	Paragraph 2
657
658	` + "```\n\tsome code\n\t```" + `
659
660	Paragraph 3
661
662No longer in the footnote
663`,
664	`<p>testing long<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">1</a></sup> notes.</p>
665
666<p>No longer in the footnote</p>
667<div class="footnotes">
668
669<hr />
670
671<ol>
672<li id="fn:b"><p>Paragraph 1</p>
673
674<p>Paragraph 2</p>
675
676<p><code>
677some code
678</code></p>
679
680<p>Paragraph 3</p>
681</li>
682</ol>
683</div>
684`,
685
686	`testing[^c] multiple[^d] notes.
687
688[^c]: this is [note] c
689
690
691omg
692
693[^d]: this is note d
694
695what happens here
696
697[note]: /link/c
698
699`,
700	`<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>
701
702<p>omg</p>
703
704<p>what happens here</p>
705<div class="footnotes">
706
707<hr />
708
709<ol>
710<li id="fn:c">this is <a href="/link/c">note</a> c
711</li>
712<li id="fn:d">this is note d
713</li>
714</ol>
715</div>
716`,
717
718	"testing inline^[this is the note] notes.\n",
719	`<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>
720<div class="footnotes">
721
722<hr />
723
724<ol>
725<li id="fn:this-is-the-note">this is the note</li>
726</ol>
727</div>
728`,
729
730	"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",
731	`<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>
732<div class="footnotes">
733
734<hr />
735
736<ol>
737<li id="fn:1"><p>the first deferred note</p>
738
739<p>which happens to be a block</p>
740</li>
741<li id="fn:inline-note">inline note</li>
742<li id="fn:2">the second deferred note
743</li>
744</ol>
745</div>
746`,
747
748	`This is a footnote[^1]^[and this is an inline footnote]
749
750[^1]: the footnote text.
751
752    may be multiple paragraphs.
753`,
754	`<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>
755<div class="footnotes">
756
757<hr />
758
759<ol>
760<li id="fn:1"><p>the footnote text.</p>
761
762<p>may be multiple paragraphs.</p>
763</li>
764<li id="fn:and-this-is-an-i">and this is an inline footnote</li>
765</ol>
766</div>
767`,
768
769	"empty footnote[^]\n\n[^]: fn text",
770	"<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",
771}
772
773func TestFootnotes(t *testing.T) {
774	doTestsInlineParam(t, footnoteTests, EXTENSION_FOOTNOTES, 0, HtmlRendererParameters{})
775}
776
777func TestFootnotesWithParameters(t *testing.T) {
778	tests := make([]string, len(footnoteTests))
779
780	prefix := "testPrefix"
781	returnText := "ret"
782	re := regexp.MustCompile(`(?ms)<li id="fn:(\S+?)">(.*?)</li>`)
783
784	// Transform the test expectations to match the parameters we're using.
785	for i, test := range footnoteTests {
786		if i%2 == 1 {
787			test = strings.Replace(test, "fn:", "fn:"+prefix, -1)
788			test = strings.Replace(test, "fnref:", "fnref:"+prefix, -1)
789			test = re.ReplaceAllString(test, `<li id="fn:$1">$2 <a class="footnote-return" href="#fnref:$1">ret</a></li>`)
790		}
791		tests[i] = test
792	}
793
794	params := HtmlRendererParameters{
795		FootnoteAnchorPrefix:       prefix,
796		FootnoteReturnLinkContents: returnText,
797	}
798
799	doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, HTML_FOOTNOTE_RETURN_LINKS, params)
800}