all repos — grayfriday @ eeb2c39339af374e266bb96592b4313ae1f3b1c9

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