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 TestNofollowLink(t *testing.T) {
459 var tests = []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, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS,
479 HtmlRendererParameters{})
480}
481
482func TestHrefTargetBlank(t *testing.T) {
483 var tests = []string{
484 // internal link
485 "[foo](/bar/)\n",
486 "<p><a href=\"/bar/\">foo</a></p>\n",
487
488 "[foo](/)\n",
489 "<p><a href=\"/\">foo</a></p>\n",
490
491 "[foo](./)\n",
492 "<p><a href=\"./\">foo</a></p>\n",
493
494 "[foo](./bar)\n",
495 "<p><a href=\"./bar\">foo</a></p>\n",
496
497 "[foo](../)\n",
498 "<p><a href=\"../\">foo</a></p>\n",
499
500 "[foo](../bar)\n",
501 "<p><a href=\"../bar\">foo</a></p>\n",
502
503 "[foo](http://example.com)\n",
504 "<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n",
505 }
506 doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_HREF_TARGET_BLANK, HtmlRendererParameters{})
507}
508
509func TestSafeInlineLink(t *testing.T) {
510 var tests = []string{
511 "[foo](/bar/)\n",
512 "<p><a href=\"/bar/\">foo</a></p>\n",
513
514 "[foo](/)\n",
515 "<p><a href=\"/\">foo</a></p>\n",
516
517 "[foo](./)\n",
518 "<p><a href=\"./\">foo</a></p>\n",
519
520 "[foo](../)\n",
521 "<p><a href=\"../\">foo</a></p>\n",
522
523 "[foo](http://bar/)\n",
524 "<p><a href=\"http://bar/\">foo</a></p>\n",
525
526 "[foo](https://bar/)\n",
527 "<p><a href=\"https://bar/\">foo</a></p>\n",
528
529 "[foo](ftp://bar/)\n",
530 "<p><a href=\"ftp://bar/\">foo</a></p>\n",
531
532 "[foo](mailto://bar/)\n",
533 "<p><a href=\"mailto://bar/\">foo</a></p>\n",
534
535 // Not considered safe
536 "[foo](baz://bar/)\n",
537 "<p><tt>foo</tt></p>\n",
538 }
539 doSafeTestsInline(t, tests)
540}
541
542func TestReferenceLink(t *testing.T) {
543 var tests = []string{
544 "[link][ref]\n",
545 "<p>[link][ref]</p>\n",
546
547 "[link][ref]\n [ref]: /url/ \"title\"\n",
548 "<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
549
550 "[link][ref]\n [ref]: /url/\n",
551 "<p><a href=\"/url/\">link</a></p>\n",
552
553 " [ref]: /url/\n",
554 "",
555
556 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
557 "",
558
559 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n [4spaces]: /url/\n",
560 "<pre><code>[4spaces]: /url/\n</code></pre>\n",
561
562 "[hmm](ref2)\n [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
563 "<p><a href=\"ref2\">hmm</a></p>\n",
564
565 "[ref]\n",
566 "<p>[ref]</p>\n",
567
568 "[ref]\n [ref]: /url/ \"title\"\n",
569 "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
570
571 "[ref]\n [ref]: ../url/ \"title\"\n",
572 "<p><a href=\"../url/\" title=\"title\">ref</a></p>\n",
573 }
574 doLinkTestsInline(t, tests)
575}
576
577func TestTags(t *testing.T) {
578 var tests = []string{
579 "a <span>tag</span>\n",
580 "<p>a <span>tag</span></p>\n",
581
582 "<span>tag</span>\n",
583 "<p><span>tag</span></p>\n",
584
585 "<span>mismatch</spandex>\n",
586 "<p><span>mismatch</spandex></p>\n",
587
588 "a <singleton /> tag\n",
589 "<p>a <singleton /> tag</p>\n",
590 }
591 doTestsInline(t, tests)
592}
593
594func TestAutoLink(t *testing.T) {
595 var tests = []string{
596 "http://foo.com/\n",
597 "<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
598
599 "1 http://foo.com/\n",
600 "<p>1 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
601
602 "1http://foo.com/\n",
603 "<p>1<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
604
605 "1.http://foo.com/\n",
606 "<p>1.<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
607
608 "1. http://foo.com/\n",
609 "<ol>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ol>\n",
610
611 "-http://foo.com/\n",
612 "<p>-<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
613
614 "- http://foo.com/\n",
615 "<ul>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ul>\n",
616
617 "_http://foo.com/\n",
618 "<p>_<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
619
620 "令狐http://foo.com/\n",
621 "<p>令狐<a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
622
623 "令狐 http://foo.com/\n",
624 "<p>令狐 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
625
626 "ahttp://foo.com/\n",
627 "<p>ahttp://foo.com/</p>\n",
628
629 ">http://foo.com/\n",
630 "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
631
632 "> http://foo.com/\n",
633 "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n",
634
635 "go to <http://foo.com/>\n",
636 "<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
637
638 "a secure <https://link.org>\n",
639 "<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
640
641 "an email <mailto:some@one.com>\n",
642 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
643
644 "an email <mailto://some@one.com>\n",
645 "<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
646
647 "an email <some@one.com>\n",
648 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
649
650 "an ftp <ftp://old.com>\n",
651 "<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
652
653 "an ftp <ftp:old.com>\n",
654 "<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
655
656 "a link with <http://new.com?query=foo&bar>\n",
657 "<p>a link with <a href=\"http://new.com?query=foo&bar\">" +
658 "http://new.com?query=foo&bar</a></p>\n",
659
660 "quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
661 "<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
662
663 "quotes mean a tag <http://new.com?query='foo'&bar>\n",
664 "<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
665
666 "unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
667 "<p>unless escaped <a href=\"http://new.com?query="foo"&bar\">" +
668 "http://new.com?query="foo"&bar</a></p>\n",
669
670 "even a > can be escaped <http://new.com?q=\\>&etc>\n",
671 "<p>even a > can be escaped <a href=\"http://new.com?q=>&etc\">" +
672 "http://new.com?q=>&etc</a></p>\n",
673
674 "<a href=\"http://fancy.com\">http://fancy.com</a>\n",
675 "<p><a href=\"http://fancy.com\">http://fancy.com</a></p>\n",
676
677 "<a href=\"http://fancy.com\">This is a link</a>\n",
678 "<p><a href=\"http://fancy.com\">This is a link</a></p>\n",
679
680 "<a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a>\n",
681 "<p><a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a></p>\n",
682
683 "(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (\n",
684 "<p>(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (</p>\n",
685
686 "(<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",
687 "<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",
688
689 "http://www.foo.com<br />\n",
690 "<p><a href=\"http://www.foo.com\">http://www.foo.com</a><br /></p>\n",
691
692 "http://foo.com/viewtopic.php?f=18&t=297",
693 "<p><a href=\"http://foo.com/viewtopic.php?f=18&t=297\">http://foo.com/viewtopic.php?f=18&t=297</a></p>\n",
694
695 "http://foo.com/viewtopic.php?param="18"zz",
696 "<p><a href=\"http://foo.com/viewtopic.php?param="18"zz\">http://foo.com/viewtopic.php?param="18"zz</a></p>\n",
697
698 "http://foo.com/viewtopic.php?param="18"",
699 "<p><a href=\"http://foo.com/viewtopic.php?param="18"\">http://foo.com/viewtopic.php?param="18"</a></p>\n",
700 }
701 doLinkTestsInline(t, tests)
702}
703
704var footnoteTests = []string{
705 "testing footnotes.[^a]\n\n[^a]: This is the note\n",
706 `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
707<div class="footnotes">
708
709<hr />
710
711<ol>
712<li id="fn:a">This is the note
713</li>
714</ol>
715</div>
716`,
717
718 `testing long[^b] notes.
719
720[^b]: Paragraph 1
721
722 Paragraph 2
723
724 ` + "```\n\tsome code\n\t```" + `
725
726 Paragraph 3
727
728No longer in the footnote
729`,
730 `<p>testing long<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">1</a></sup> notes.</p>
731
732<p>No longer in the footnote</p>
733<div class="footnotes">
734
735<hr />
736
737<ol>
738<li id="fn:b"><p>Paragraph 1</p>
739
740<p>Paragraph 2</p>
741
742<p><code>
743some code
744</code></p>
745
746<p>Paragraph 3</p>
747</li>
748</ol>
749</div>
750`,
751
752 `testing[^c] multiple[^d] notes.
753
754[^c]: this is [note] c
755
756
757omg
758
759[^d]: this is note d
760
761what happens here
762
763[note]: /link/c
764
765`,
766 `<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>
767
768<p>omg</p>
769
770<p>what happens here</p>
771<div class="footnotes">
772
773<hr />
774
775<ol>
776<li id="fn:c">this is <a href="/link/c">note</a> c
777</li>
778<li id="fn:d">this is note d
779</li>
780</ol>
781</div>
782`,
783
784 "testing inline^[this is the note] notes.\n",
785 `<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>
786<div class="footnotes">
787
788<hr />
789
790<ol>
791<li id="fn:this-is-the-note">this is the note</li>
792</ol>
793</div>
794`,
795
796 "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",
797 `<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>
798<div class="footnotes">
799
800<hr />
801
802<ol>
803<li id="fn:1"><p>the first deferred note</p>
804
805<p>which happens to be a block</p>
806</li>
807<li id="fn:inline-note">inline note</li>
808<li id="fn:2">the second deferred note
809</li>
810</ol>
811</div>
812`,
813
814 `This is a footnote[^1]^[and this is an inline footnote]
815
816[^1]: the footnote text.
817
818 may be multiple paragraphs.
819`,
820 `<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>
821<div class="footnotes">
822
823<hr />
824
825<ol>
826<li id="fn:1"><p>the footnote text.</p>
827
828<p>may be multiple paragraphs.</p>
829</li>
830<li id="fn:and-this-is-an-i">and this is an inline footnote</li>
831</ol>
832</div>
833`,
834
835 "empty footnote[^]\n\n[^]: fn text",
836 "<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",
837}
838
839func TestFootnotes(t *testing.T) {
840 doTestsInlineParam(t, footnoteTests, EXTENSION_FOOTNOTES, 0, HtmlRendererParameters{})
841}
842
843func TestFootnotesWithParameters(t *testing.T) {
844 tests := make([]string, len(footnoteTests))
845
846 prefix := "testPrefix"
847 returnText := "ret"
848 re := regexp.MustCompile(`(?ms)<li id="fn:(\S+?)">(.*?)</li>`)
849
850 // Transform the test expectations to match the parameters we're using.
851 for i, test := range footnoteTests {
852 if i%2 == 1 {
853 test = strings.Replace(test, "fn:", "fn:"+prefix, -1)
854 test = strings.Replace(test, "fnref:", "fnref:"+prefix, -1)
855 test = re.ReplaceAllString(test, `<li id="fn:$1">$2 <a class="footnote-return" href="#fnref:$1">ret</a></li>`)
856 }
857 tests[i] = test
858 }
859
860 params := HtmlRendererParameters{
861 FootnoteAnchorPrefix: prefix,
862 FootnoteReturnLinkContents: returnText,
863 }
864
865 doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, HTML_FOOTNOTE_RETURN_LINKS, params)
866}
867
868func TestSmartDoubleQuotes(t *testing.T) {
869 var tests = []string{
870 "this should be normal \"quoted\" text.\n",
871 "<p>this should be normal “quoted” text.</p>\n",
872 "this \" single double\n",
873 "<p>this “ single double</p>\n",
874 "two pair of \"some\" quoted \"text\".\n",
875 "<p>two pair of “some” quoted “text”.</p>\n"}
876
877 doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS, HtmlRendererParameters{})
878}
879
880func TestSmartAngledDoubleQuotes(t *testing.T) {
881 var tests = []string{
882 "this should be angled \"quoted\" text.\n",
883 "<p>this should be angled «quoted» text.</p>\n",
884 "this \" single double\n",
885 "<p>this « single double</p>\n",
886 "two pair of \"some\" quoted \"text\".\n",
887 "<p>two pair of «some» quoted «text».</p>\n"}
888
889 doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_ANGLED_QUOTES, HtmlRendererParameters{})
890}
891
892func TestSmartFractions(t *testing.T) {
893 var tests = []string{
894 "1/2, 1/4 and 3/4; 1/4th and 3/4ths\n",
895 "<p>½, ¼ and ¾; ¼th and ¾ths</p>\n",
896 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
897 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
898
899 doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS, HtmlRendererParameters{})
900
901 tests = []string{
902 "1/2, 2/3, 81/100 and 1000000/1048576.\n",
903 "<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",
904 "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n",
905 "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"}
906
907 doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_FRACTIONS, HtmlRendererParameters{})
908}