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