all repos — grayfriday @ 3dba5bc56e043394d9d15282f94891f7d9be229b

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	"testing"
 18)
 19
 20func runMarkdownInline(input string, extensions, htmlFlags int) string {
 21	extensions |= EXTENSION_AUTOLINK
 22	extensions |= EXTENSION_STRIKETHROUGH
 23
 24	htmlFlags |= HTML_USE_XHTML
 25
 26	renderer := HtmlRenderer(htmlFlags, "", "")
 27
 28	return string(Markdown([]byte(input), renderer, extensions))
 29}
 30
 31func doTestsInline(t *testing.T, tests []string) {
 32	doTestsInlineParam(t, tests, 0, 0)
 33}
 34
 35func doSafeTestsInline(t *testing.T, tests []string) {
 36	doTestsInlineParam(t, tests, 0, HTML_SAFELINK)
 37}
 38
 39func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int) {
 40	// catch and report panics
 41	var candidate string
 42	/*
 43		defer func() {
 44			if err := recover(); err != nil {
 45				t.Errorf("\npanic while processing [%#v] (%v)\n", candidate, err)
 46			}
 47		}()
 48	*/
 49
 50	for i := 0; i+1 < len(tests); i += 2 {
 51		input := tests[i]
 52		candidate = input
 53		expected := tests[i+1]
 54		actual := runMarkdownInline(candidate, extensions, htmlFlags)
 55		if actual != expected {
 56			t.Errorf("\nInput   [%#v]\nExpected[%#v]\nActual  [%#v]",
 57				candidate, expected, actual)
 58		}
 59
 60		// now test every substring to stress test bounds checking
 61		if !testing.Short() {
 62			for start := 0; start < len(input); start++ {
 63				for end := start + 1; end <= len(input); end++ {
 64					candidate = input[start:end]
 65					_ = runMarkdownInline(candidate, extensions, htmlFlags)
 66				}
 67			}
 68		}
 69	}
 70}
 71
 72func TestRawHtmlTag(t *testing.T) {
 73	tests := []string{
 74		"zz <style>p {}</style>\n",
 75		"<p>zz &lt;style&gt;p {}&lt;/style&gt;</p>\n",
 76
 77		"zz <STYLE>p {}</STYLE>\n",
 78		"<p>zz &lt;style&gt;p {}&lt;/style&gt;</p>\n",
 79
 80		"<SCRIPT>alert()</SCRIPT>\n",
 81		"<p>&lt;script&gt;alert()&lt;/script&gt;</p>\n",
 82
 83		"zz <SCRIPT>alert()</SCRIPT>\n",
 84		"<p>zz &lt;script&gt;alert()&lt;/script&gt;</p>\n",
 85
 86		"zz <script>alert()</script>\n",
 87		"<p>zz &lt;script&gt;alert()&lt;/script&gt;</p>\n",
 88
 89		" <script>alert()</script>\n",
 90		"<p>&lt;script&gt;alert()&lt;/script&gt;</p>\n",
 91
 92		"<script>alert()</script>\n",
 93		"&lt;script&gt;alert()&lt;/script&gt;\n",
 94
 95		"<script src='foo'></script>\n",
 96		"&lt;script src=&#39;foo&#39;&gt;&lt;/script&gt;\n",
 97
 98		"<script src='a>b'></script>\n",
 99		"&lt;script src=&#39;a&gt;b&#39;&gt;&lt;/script&gt;\n",
100
101		"zz <script src='foo'></script>\n",
102		"<p>zz &lt;script src=&#39;foo&#39;&gt;&lt;/script&gt;</p>\n",
103
104		"zz <script src=foo></script>\n",
105		"<p>zz &lt;script src=foo&gt;&lt;/script&gt;</p>\n",
106
107		`<script><script src="http://example.com/exploit.js"></SCRIPT></script>`,
108		"&lt;script&gt;&lt;script src=&#34;http://example.com/exploit.js&#34;&gt;&lt;/script&gt;&lt;/script&gt;\n",
109
110		`'';!--"<XSS>=&{()}`,
111		"<p>&#39;&#39;;!--&#34;&lt;xss&gt;=&amp;{()}</p>\n",
112
113		"<SCRIPT SRC=http://ha.ckers.org/xss.js></SCRIPT>",
114		"<p>&lt;script SRC=http://ha.ckers.org/xss.js&gt;&lt;/script&gt;</p>\n",
115
116		"<SCRIPT \nSRC=http://ha.ckers.org/xss.js></SCRIPT>",
117		"<p>&lt;script \nSRC=http://ha.ckers.org/xss.js&gt;&lt;/script&gt;</p>\n",
118
119		`<IMG SRC="javascript:alert('XSS');">`,
120		"<p><img></p>\n",
121
122		"<IMG SRC=javascript:alert('XSS')>",
123		"<p><img></p>\n",
124
125		"<IMG SRC=JaVaScRiPt:alert('XSS')>",
126		"<p><img></p>\n",
127
128		"<IMG SRC=`javascript:alert(\"RSnake says, 'XSS'\")`>",
129		"<p><img></p>\n",
130
131		`<a onmouseover="alert(document.cookie)">xss link</a>`,
132		"<p><a>xss link</a></p>\n",
133
134		"<a onmouseover=alert(document.cookie)>xss link</a>",
135		"<p><a>xss link</a></p>\n",
136
137		`<IMG """><SCRIPT>alert("XSS")</SCRIPT>">`,
138		"<p><img>&lt;script&gt;alert(&amp;quot;XSS&amp;quot;)&lt;/script&gt;&#34;&gt;</p>\n",
139
140		"<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>",
141		"<p><img></p>\n",
142
143		`<IMG SRC=# onmouseover="alert('xxs')">`,
144		"<p><img></p>\n",
145
146		`<IMG SRC= onmouseover="alert('xxs')">`,
147		"<p><img></p>\n",
148
149		`<IMG onmouseover="alert('xxs')">`,
150		"<p><img></p>\n",
151
152		"<IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>",
153		"<p><img></p>\n",
154
155		"<IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>",
156		"<p><img></p>\n",
157
158		"<IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>",
159		"<p><img></p>\n",
160
161		`<IMG SRC="javascriptascript:alert('XSS');">`,
162		"<p><img></p>\n",
163
164		`<IMG SRC="jav&#x09;ascript:alert('XSS');">`,
165		"<p><img></p>\n",
166
167		`<IMG SRC="jav&#x0A;ascript:alert('XSS');">`,
168		"<p><img></p>\n",
169
170		`<IMG SRC="jav&#x0D;ascript:alert('XSS');">`,
171		"<p><img></p>\n",
172
173		`<IMG SRC=" &#14;  javascript:alert('XSS');">`,
174		"<p><img></p>\n",
175
176		`<SCRIPT/XSS SRC="http://ha.ckers.org/xss.js"></SCRIPT>`,
177		"<p>&lt;script/XSS SRC=&#34;http://ha.ckers.org/xss.js&#34;&gt;&lt;/script&gt;</p>\n",
178
179		"<BODY onload!#$%&()*~+-_.,:;?@[/|\\]^`=alert(\"XSS\")>",
180		"<p>&lt;body onload!#$%&amp;()*~+-_.,:;?@[/|\\]^`=alert(&#34;XSS&#34;)&gt;</p>\n",
181
182		`<SCRIPT/SRC="http://ha.ckers.org/xss.js"></SCRIPT>`,
183		"<p>&lt;script/SRC=&#34;http://ha.ckers.org/xss.js&#34;&gt;&lt;/script&gt;</p>\n",
184
185		// HTML5 interprets the <script> tag contents as raw test, thus the end
186		// result has double-escaped &amp;quot;
187		`<<SCRIPT>alert("XSS");//<</SCRIPT>`,
188		"<p>&lt;&lt;script&gt;alert(&amp;quot;XSS&amp;quot;);//&amp;lt;&lt;/script&gt;</p>\n",
189
190		// HTML5 parses the </p> within an unclosed <script> tag as text.
191		// Same for the following tests.
192		"<SCRIPT SRC=http://ha.ckers.org/xss.js?< B >",
193		"<p>&lt;script SRC=http://ha.ckers.org/xss.js?&lt; B &gt;&lt;/p&gt;\n",
194
195		"<SCRIPT SRC=//ha.ckers.org/.j>",
196		"<p>&lt;script SRC=//ha.ckers.org/.j&gt;&lt;/p&gt;\n",
197
198		`<IMG SRC="javascript:alert('XSS')"`,
199		"<p>&lt;IMG SRC=&#34;javascript:alert(&#39;XSS&#39;)&#34;</p>\n",
200
201		"<iframe src=http://ha.ckers.org/scriptlet.html <",
202		// The hyperlink gets linkified, the <iframe> gets escaped
203		"<p>&lt;iframe src=<a href=\"http://ha.ckers.org/scriptlet.html\">http://ha.ckers.org/scriptlet.html</a> &lt;</p>\n",
204	}
205	doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE|HTML_SANITIZE_OUTPUT)
206}
207
208func TestEmphasis(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 <em>inline</em> test</p>\n",
215
216		"*at the* beginning\n",
217		"<p><em>at the</em> beginning</p>\n",
218
219		"at the *end*\n",
220		"<p>at the <em>end</em></p>\n",
221
222		"*try two* in *one line*\n",
223		"<p><em>try two</em> in <em>one line</em></p>\n",
224
225		"over *two\nlines* test\n",
226		"<p>over <em>two\nlines</em> test</p>\n",
227
228		"odd *number of* markers* here\n",
229		"<p>odd <em>number of</em> markers* here</p>\n",
230
231		"odd *number\nof* markers* here\n",
232		"<p>odd <em>number\nof</em> markers* here</p>\n",
233
234		"simple _inline_ test\n",
235		"<p>simple <em>inline</em> test</p>\n",
236
237		"_at the_ beginning\n",
238		"<p><em>at the</em> beginning</p>\n",
239
240		"at the _end_\n",
241		"<p>at the <em>end</em></p>\n",
242
243		"_try two_ in _one line_\n",
244		"<p><em>try two</em> in <em>one line</em></p>\n",
245
246		"over _two\nlines_ test\n",
247		"<p>over <em>two\nlines</em> test</p>\n",
248
249		"odd _number of_ markers_ here\n",
250		"<p>odd <em>number of</em> markers_ here</p>\n",
251
252		"odd _number\nof_ markers_ here\n",
253		"<p>odd <em>number\nof</em> 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 TestStrong(t *testing.T) {
262	var tests = []string{
263		"nothing inline\n",
264		"<p>nothing inline</p>\n",
265
266		"simple **inline** test\n",
267		"<p>simple <strong>inline</strong> test</p>\n",
268
269		"**at the** beginning\n",
270		"<p><strong>at the</strong> beginning</p>\n",
271
272		"at the **end**\n",
273		"<p>at the <strong>end</strong></p>\n",
274
275		"**try two** in **one line**\n",
276		"<p><strong>try two</strong> in <strong>one line</strong></p>\n",
277
278		"over **two\nlines** test\n",
279		"<p>over <strong>two\nlines</strong> test</p>\n",
280
281		"odd **number of** markers** here\n",
282		"<p>odd <strong>number of</strong> markers** here</p>\n",
283
284		"odd **number\nof** markers** here\n",
285		"<p>odd <strong>number\nof</strong> markers** here</p>\n",
286
287		"simple __inline__ test\n",
288		"<p>simple <strong>inline</strong> test</p>\n",
289
290		"__at the__ beginning\n",
291		"<p><strong>at the</strong> beginning</p>\n",
292
293		"at the __end__\n",
294		"<p>at the <strong>end</strong></p>\n",
295
296		"__try two__ in __one line__\n",
297		"<p><strong>try two</strong> in <strong>one line</strong></p>\n",
298
299		"over __two\nlines__ test\n",
300		"<p>over <strong>two\nlines</strong> test</p>\n",
301
302		"odd __number of__ markers__ here\n",
303		"<p>odd <strong>number of</strong> markers__ here</p>\n",
304
305		"odd __number\nof__ markers__ here\n",
306		"<p>odd <strong>number\nof</strong> markers__ here</p>\n",
307
308		"mix of **markers__\n",
309		"<p>mix of **markers__</p>\n",
310	}
311	doTestsInline(t, tests)
312}
313
314func TestEmphasisMix(t *testing.T) {
315	var tests = []string{
316		"***triple emphasis***\n",
317		"<p><strong><em>triple emphasis</em></strong></p>\n",
318
319		"***triple\nemphasis***\n",
320		"<p><strong><em>triple\nemphasis</em></strong></p>\n",
321
322		"___triple emphasis___\n",
323		"<p><strong><em>triple emphasis</em></strong></p>\n",
324
325		"***triple emphasis___\n",
326		"<p>***triple emphasis___</p>\n",
327
328		"*__triple emphasis__*\n",
329		"<p><em><strong>triple emphasis</strong></em></p>\n",
330
331		"__*triple emphasis*__\n",
332		"<p><strong><em>triple emphasis</em></strong></p>\n",
333
334		"**improper *nesting** is* bad\n",
335		"<p><strong>improper *nesting</strong> is* bad</p>\n",
336
337		"*improper **nesting* is** bad\n",
338		"<p><em>improper **nesting</em> is** bad</p>\n",
339	}
340	doTestsInline(t, tests)
341}
342
343func TestStrikeThrough(t *testing.T) {
344	var tests = []string{
345		"nothing inline\n",
346		"<p>nothing inline</p>\n",
347
348		"simple ~~inline~~ test\n",
349		"<p>simple <del>inline</del> test</p>\n",
350
351		"~~at the~~ beginning\n",
352		"<p><del>at the</del> beginning</p>\n",
353
354		"at the ~~end~~\n",
355		"<p>at the <del>end</del></p>\n",
356
357		"~~try two~~ in ~~one line~~\n",
358		"<p><del>try two</del> in <del>one line</del></p>\n",
359
360		"over ~~two\nlines~~ test\n",
361		"<p>over <del>two\nlines</del> test</p>\n",
362
363		"odd ~~number of~~ markers~~ here\n",
364		"<p>odd <del>number of</del> markers~~ here</p>\n",
365
366		"odd ~~number\nof~~ markers~~ here\n",
367		"<p>odd <del>number\nof</del> markers~~ here</p>\n",
368	}
369	doTestsInline(t, tests)
370}
371
372func TestCodeSpan(t *testing.T) {
373	var tests = []string{
374		"`source code`\n",
375		"<p><code>source code</code></p>\n",
376
377		"` source code with spaces `\n",
378		"<p><code>source code with spaces</code></p>\n",
379
380		"` source code with spaces `not here\n",
381		"<p><code>source code with spaces</code>not here</p>\n",
382
383		"a `single marker\n",
384		"<p>a `single marker</p>\n",
385
386		"a single multi-tick marker with ``` no text\n",
387		"<p>a single multi-tick marker with ``` no text</p>\n",
388
389		"markers with ` ` a space\n",
390		"<p>markers with  a space</p>\n",
391
392		"`source code` and a `stray\n",
393		"<p><code>source code</code> and a `stray</p>\n",
394
395		"`source *with* _awkward characters_ in it`\n",
396		"<p><code>source *with* _awkward characters_ in it</code></p>\n",
397
398		"`split over\ntwo lines`\n",
399		"<p><code>split over\ntwo lines</code></p>\n",
400
401		"```multiple ticks``` for the marker\n",
402		"<p><code>multiple ticks</code> for the marker</p>\n",
403
404		"```multiple ticks `with` ticks inside```\n",
405		"<p><code>multiple ticks `with` ticks inside</code></p>\n",
406	}
407	doTestsInline(t, tests)
408}
409
410func TestLineBreak(t *testing.T) {
411	var tests = []string{
412		"this line  \nhas a break\n",
413		"<p>this line<br />\nhas a break</p>\n",
414
415		"this line \ndoes not\n",
416		"<p>this line\ndoes not</p>\n",
417
418		"this has an   \nextra space\n",
419		"<p>this has an<br />\nextra space</p>\n",
420	}
421	doTestsInline(t, tests)
422}
423
424func TestInlineLink(t *testing.T) {
425	var tests = []string{
426		"[foo](/bar/)\n",
427		"<p><a href=\"/bar/\">foo</a></p>\n",
428
429		"[foo with a title](/bar/ \"title\")\n",
430		"<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
431
432		"[foo with a title](/bar/\t\"title\")\n",
433		"<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
434
435		"[foo with a title](/bar/ \"title\"  )\n",
436		"<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
437
438		"[foo with a title](/bar/ title with no quotes)\n",
439		"<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n",
440
441		"[foo]()\n",
442		"<p>[foo]()</p>\n",
443
444		"![foo](/bar/)\n",
445		"<p><img src=\"/bar/\" alt=\"foo\" />\n</p>\n",
446
447		"![foo with a title](/bar/ \"title\")\n",
448		"<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
449
450		"![foo with a title](/bar/\t\"title\")\n",
451		"<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
452
453		"![foo with a title](/bar/ \"title\"  )\n",
454		"<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
455
456		"![foo with a title](/bar/ title with no quotes)\n",
457		"<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" />\n</p>\n",
458
459		"![foo]()\n",
460		"<p>![foo]()</p>\n",
461
462		"[a link]\t(/with_a_tab/)\n",
463		"<p><a href=\"/with_a_tab/\">a link</a></p>\n",
464
465		"[a link]  (/with_spaces/)\n",
466		"<p><a href=\"/with_spaces/\">a link</a></p>\n",
467
468		"[text (with) [[nested] (brackets)]](/url/)\n",
469		"<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n",
470
471		"[text (with) [broken nested] (brackets)]](/url/)\n",
472		"<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n",
473
474		"[text\nwith a newline](/link/)\n",
475		"<p><a href=\"/link/\">text\nwith a newline</a></p>\n",
476
477		"[text in brackets] [followed](/by a link/)\n",
478		"<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n",
479
480		"[link with\\] a closing bracket](/url/)\n",
481		"<p><a href=\"/url/\">link with] a closing bracket</a></p>\n",
482
483		"[link with\\[ an opening bracket](/url/)\n",
484		"<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n",
485
486		"[link with\\) a closing paren](/url/)\n",
487		"<p><a href=\"/url/\">link with) a closing paren</a></p>\n",
488
489		"[link with\\( an opening paren](/url/)\n",
490		"<p><a href=\"/url/\">link with( an opening paren</a></p>\n",
491
492		"[link](  with whitespace)\n",
493		"<p><a href=\"with whitespace\">link</a></p>\n",
494
495		"[link](  with whitespace   )\n",
496		"<p><a href=\"with whitespace\">link</a></p>\n",
497
498		"[![image](someimage)](with image)\n",
499		"<p><a href=\"with image\"><img src=\"someimage\" alt=\"image\" />\n</a></p>\n",
500
501		"[link](url \"one quote)\n",
502		"<p><a href=\"url &quot;one quote\">link</a></p>\n",
503
504		"[link](url 'one quote)\n",
505		"<p><a href=\"url 'one quote\">link</a></p>\n",
506
507		"[link](<url>)\n",
508		"<p><a href=\"url\">link</a></p>\n",
509
510		"[link & ampersand](/url/)\n",
511		"<p><a href=\"/url/\">link &amp; ampersand</a></p>\n",
512
513		"[link &amp; ampersand](/url/)\n",
514		"<p><a href=\"/url/\">link &amp; ampersand</a></p>\n",
515
516		"[link](/url/&query)\n",
517		"<p><a href=\"/url/&amp;query\">link</a></p>\n",
518
519		"[[t]](/t)\n",
520		"<p><a href=\"/t\">[t]</a></p>\n",
521	}
522	doTestsInline(t, tests)
523}
524
525func TestNofollowLink(t *testing.T) {
526	var tests = []string{
527		"[foo](http://bar.com/foo/)\n",
528		"<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n",
529	}
530	doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_SANITIZE_OUTPUT)
531	// HTML_SANITIZE_OUTPUT won't allow relative links, so test that separately:
532	tests = []string{
533		"[foo](/bar/)\n",
534		"<p><a href=\"/bar/\">foo</a></p>\n",
535	}
536	doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS)
537}
538
539func TestHrefTargetBlank(t *testing.T) {
540	var tests = []string{
541        // internal link
542		"[foo](/bar/)\n",
543		"<p><a href=\"/bar/\">foo</a></p>\n",
544
545		"[foo](http://example.com)\n",
546		"<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n",
547	}
548	doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_HREF_TARGET_BLANK)
549}
550
551func TestSafeInlineLink(t *testing.T) {
552	var tests = []string{
553		"[foo](/bar/)\n",
554		"<p><a href=\"/bar/\">foo</a></p>\n",
555
556		"[foo](http://bar/)\n",
557		"<p><a href=\"http://bar/\">foo</a></p>\n",
558
559		"[foo](https://bar/)\n",
560		"<p><a href=\"https://bar/\">foo</a></p>\n",
561
562		"[foo](ftp://bar/)\n",
563		"<p><a href=\"ftp://bar/\">foo</a></p>\n",
564
565		"[foo](mailto://bar/)\n",
566		"<p><a href=\"mailto://bar/\">foo</a></p>\n",
567
568		// Not considered safe
569		"[foo](baz://bar/)\n",
570		"<p><tt>foo</tt></p>\n",
571	}
572	doSafeTestsInline(t, tests)
573}
574
575func TestReferenceLink(t *testing.T) {
576	var tests = []string{
577		"[link][ref]\n",
578		"<p>[link][ref]</p>\n",
579
580		"[link][ref]\n   [ref]: /url/ \"title\"\n",
581		"<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
582
583		"[link][ref]\n   [ref]: /url/\n",
584		"<p><a href=\"/url/\">link</a></p>\n",
585
586		"   [ref]: /url/\n",
587		"",
588
589		"   [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
590		"",
591
592		"   [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n    [4spaces]: /url/\n",
593		"<pre><code>[4spaces]: /url/\n</code></pre>\n",
594
595		"[hmm](ref2)\n   [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
596		"<p><a href=\"ref2\">hmm</a></p>\n",
597
598		"[ref]\n",
599		"<p>[ref]</p>\n",
600
601		"[ref]\n   [ref]: /url/ \"title\"\n",
602		"<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
603	}
604	doTestsInline(t, tests)
605}
606
607func TestTags(t *testing.T) {
608	var tests = []string{
609		"a <span>tag</span>\n",
610		"<p>a <span>tag</span></p>\n",
611
612		"<span>tag</span>\n",
613		"<p><span>tag</span></p>\n",
614
615		"<span>mismatch</spandex>\n",
616		"<p><span>mismatch</spandex></p>\n",
617
618		"a <singleton /> tag\n",
619		"<p>a <singleton /> tag</p>\n",
620	}
621	doTestsInline(t, tests)
622}
623
624func TestAutoLink(t *testing.T) {
625	var tests = []string{
626		"http://foo.com/\n",
627		"<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
628
629		"1 http://foo.com/\n",
630		"<p>1 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
631
632		"1http://foo.com/\n",
633		"<p>1<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
634
635		"1.http://foo.com/\n",
636		"<p>1.<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
637
638		"1. http://foo.com/\n",
639		"<ol>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ol>\n",
640
641		"-http://foo.com/\n",
642		"<p>-<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
643
644		"- http://foo.com/\n",
645		"<ul>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ul>\n",
646
647		"_http://foo.com/\n",
648		"<p>_<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
649
650		"令狐http://foo.com/\n",
651		"<p>令狐<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
652
653		"令狐 http://foo.com/\n",
654		"<p>令狐 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
655
656		"ahttp://foo.com/\n",
657		"<p>ahttp://foo.com/</p>\n",
658
659		">http://foo.com/\n",
660		"<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
661
662		"> http://foo.com/\n",
663		"<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
664
665		"go to <http://foo.com/>\n",
666		"<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
667
668		"a secure <https://link.org>\n",
669		"<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
670
671		"an email <mailto:some@one.com>\n",
672		"<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
673
674		"an email <mailto://some@one.com>\n",
675		"<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
676
677		"an email <some@one.com>\n",
678		"<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
679
680		"an ftp <ftp://old.com>\n",
681		"<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
682
683		"an ftp <ftp:old.com>\n",
684		"<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
685
686		"a link with <http://new.com?query=foo&bar>\n",
687		"<p>a link with <a href=\"http://new.com?query=foo&amp;bar\">" +
688			"http://new.com?query=foo&amp;bar</a></p>\n",
689
690		"quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
691		"<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
692
693		"quotes mean a tag <http://new.com?query='foo'&bar>\n",
694		"<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
695
696		"unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
697		"<p>unless escaped <a href=\"http://new.com?query=&quot;foo&quot;&amp;bar\">" +
698			"http://new.com?query=&quot;foo&quot;&amp;bar</a></p>\n",
699
700		"even a > can be escaped <http://new.com?q=\\>&etc>\n",
701		"<p>even a &gt; can be escaped <a href=\"http://new.com?q=&gt;&amp;etc\">" +
702			"http://new.com?q=&gt;&amp;etc</a></p>\n",
703
704		"<a href=\"http://fancy.com\">http://fancy.com</a>\n",
705		"<p><a href=\"http://fancy.com\">http://fancy.com</a></p>\n",
706
707		"<a href=\"http://fancy.com\">This is a link</a>\n",
708		"<p><a href=\"http://fancy.com\">This is a link</a></p>\n",
709
710		"<a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a>\n",
711		"<p><a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a></p>\n",
712
713		"(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (\n",
714		"<p>(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (</p>\n",
715
716		"(<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",
717		"<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",
718
719		"http://www.foo.com<br />\n",
720		"<p><a href=\"http://www.foo.com\">http://www.foo.com</a><br /></p>\n",
721
722		"http://foo.com/viewtopic.php?f=18&amp;t=297",
723		"<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",
724
725		"http://foo.com/viewtopic.php?param=&quot;18&quot;zz",
726		"<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",
727
728		"http://foo.com/viewtopic.php?param=&quot;18&quot;",
729		"<p><a href=\"http://foo.com/viewtopic.php?param=&quot;18&quot;\">http://foo.com/viewtopic.php?param=&quot;18&quot;</a></p>\n",
730	}
731	doTestsInline(t, tests)
732}
733
734func TestFootnotes(t *testing.T) {
735	tests := []string{
736		"testing footnotes.[^a]\n\n[^a]: This is the note\n",
737		`<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
738<div class="footnotes">
739
740<hr />
741
742<ol>
743<li id="fn:a">This is the note
744</li>
745</ol>
746</div>
747`,
748
749		`testing long[^b] notes.
750
751[^b]: Paragraph 1
752
753	Paragraph 2
754
755	` + "```\n\tsome code\n\t```" + `
756
757	Paragraph 3
758
759No longer in the footnote
760`,
761		`<p>testing long<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">1</a></sup> notes.</p>
762
763<p>No longer in the footnote</p>
764<div class="footnotes">
765
766<hr />
767
768<ol>
769<li id="fn:b"><p>Paragraph 1</p>
770
771<p>Paragraph 2</p>
772
773<p><code>
774some code
775</code></p>
776
777<p>Paragraph 3</p>
778</li>
779</ol>
780</div>
781`,
782
783		`testing[^c] multiple[^d] notes.
784
785[^c]: this is [note] c
786
787
788omg
789
790[^d]: this is note d
791
792what happens here
793
794[note]: /link/c
795
796`,
797		`<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>
798
799<p>omg</p>
800
801<p>what happens here</p>
802<div class="footnotes">
803
804<hr />
805
806<ol>
807<li id="fn:c">this is <a href="/link/c">note</a> c
808</li>
809<li id="fn:d">this is note d
810</li>
811</ol>
812</div>
813`,
814
815		"testing inline^[this is the note] notes.\n",
816		`<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>
817<div class="footnotes">
818
819<hr />
820
821<ol>
822<li id="fn:this-is-the-note">this is the note</li>
823</ol>
824</div>
825`,
826
827		"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",
828		`<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>
829<div class="footnotes">
830
831<hr />
832
833<ol>
834<li id="fn:1"><p>the first deferred note</p>
835
836<p>which happens to be a block</p>
837</li>
838<li id="fn:inline-note">inline note</li>
839<li id="fn:2">the second deferred note
840</li>
841</ol>
842</div>
843`,
844
845		`This is a footnote[^1]^[and this is an inline footnote]
846
847[^1]: the footnote text.
848
849    may be multiple paragraphs.
850`,
851		`<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>
852<div class="footnotes">
853
854<hr />
855
856<ol>
857<li id="fn:1"><p>the footnote text.</p>
858
859<p>may be multiple paragraphs.</p>
860</li>
861<li id="fn:and-this-is-an-i">and this is an inline footnote</li>
862</ol>
863</div>
864`,
865
866		"empty footnote[^]\n\n[^]: fn text",
867		"<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",
868	}
869
870	doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, 0)
871}