all repos — grayfriday @ 5d3d5c198ebf779798f95e73e990ecc271db3735

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