all repos — grayfriday @ 8e10236be5dd3b43b5a38f041e500dec837b6df5

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