all repos — grayfriday @ 36787eca3aa824e3c17b9c00feaf307c543fbe97

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