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