inline_test.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 |
// // Blackfriday Markdown Processor // Available at http://github.com/russross/blackfriday // // Copyright © 2011 Russ Ross <russ@russross.com>. // Distributed under the Simplified BSD License. // See README.md for details. // // // Unit tests for inline parsing // package blackfriday import ( "regexp" "testing" "strings" ) func TestEmphasis(t *testing.T) { t.Parallel() var tests = []string{ "nothing inline\n", "<p>nothing inline</p>\n", "simple *inline* test\n", "<p>simple <em>inline</em> test</p>\n", "*at the* beginning\n", "<p><em>at the</em> beginning</p>\n", "at the *end*\n", "<p>at the <em>end</em></p>\n", "*try two* in *one line*\n", "<p><em>try two</em> in <em>one line</em></p>\n", "over *two\nlines* test\n", "<p>over <em>two\nlines</em> test</p>\n", "odd *number of* markers* here\n", "<p>odd <em>number of</em> markers* here</p>\n", "odd *number\nof* markers* here\n", "<p>odd <em>number\nof</em> markers* here</p>\n", "simple _inline_ test\n", "<p>simple <em>inline</em> test</p>\n", "_at the_ beginning\n", "<p><em>at the</em> beginning</p>\n", "at the _end_\n", "<p>at the <em>end</em></p>\n", "_try two_ in _one line_\n", "<p><em>try two</em> in <em>one line</em></p>\n", "over _two\nlines_ test\n", "<p>over <em>two\nlines</em> test</p>\n", "odd _number of_ markers_ here\n", "<p>odd <em>number of</em> markers_ here</p>\n", "odd _number\nof_ markers_ here\n", "<p>odd <em>number\nof</em> markers_ here</p>\n", "mix of *markers_\n", "<p>mix of *markers_</p>\n", "*What is A\\* algorithm?*\n", "<p><em>What is A* algorithm?</em></p>\n", } doTestsInline(t, tests) } func TestReferenceOverride(t *testing.T) { t.Parallel() var tests = []string{ "test [ref1][]\n", "<p>test <a href=\"http://www.ref1.com/\" title=\"Reference 1\">ref1</a></p>\n", "test [my ref][ref1]\n", "<p>test <a href=\"http://www.ref1.com/\" title=\"Reference 1\">my ref</a></p>\n", "test [ref2][]\n\n[ref2]: http://www.leftalone.com/ (Ref left alone)\n", "<p>test <a href=\"http://www.overridden.com/\" title=\"Reference Overridden\">ref2</a></p>\n", "test [ref3][]\n\n[ref3]: http://www.leftalone.com/ (Ref left alone)\n", "<p>test <a href=\"http://www.leftalone.com/\" title=\"Ref left alone\">ref3</a></p>\n", "test [ref4][]\n\n[ref4]: http://zombo.com/ (You can do anything)\n", "<p>test [ref4][]</p>\n", "test [!(*http.ServeMux).ServeHTTP][] complicated ref\n", "<p>test <a href=\"http://localhost:6060/pkg/net/http/#ServeMux.ServeHTTP\" title=\"ServeHTTP docs\">!(*http.ServeMux).ServeHTTP</a> complicated ref</p>\n", "test [ref5][]\n", "<p>test <a href=\"http://www.ref5.com/\" title=\"Reference 5\">Moo</a></p>\n", } doTestsInlineParam(t, tests, TestParams{ referenceOverride: func(reference string) (rv *Reference, overridden bool) { switch reference { case "ref1": // just an overridden reference exists without definition return &Reference{ Link: "http://www.ref1.com/", Title: "Reference 1"}, true case "ref2": // overridden exists and reference defined return &Reference{ Link: "http://www.overridden.com/", Title: "Reference Overridden"}, true case "ref3": // not overridden and reference defined return nil, false case "ref4": // overridden missing and defined return nil, true case "!(*http.ServeMux).ServeHTTP": return &Reference{ Link: "http://localhost:6060/pkg/net/http/#ServeMux.ServeHTTP", Title: "ServeHTTP docs"}, true case "ref5": return &Reference{ Link: "http://www.ref5.com/", Title: "Reference 5", Text: "Moo", }, true } return nil, false }, }) } func TestStrong(t *testing.T) { t.Parallel() var tests = []string{ "nothing inline\n", "<p>nothing inline</p>\n", "simple **inline** test\n", "<p>simple <strong>inline</strong> test</p>\n", "**at the** beginning\n", "<p><strong>at the</strong> beginning</p>\n", "at the **end**\n", "<p>at the <strong>end</strong></p>\n", "**try two** in **one line**\n", "<p><strong>try two</strong> in <strong>one line</strong></p>\n", "over **two\nlines** test\n", "<p>over <strong>two\nlines</strong> test</p>\n", "odd **number of** markers** here\n", "<p>odd <strong>number of</strong> markers** here</p>\n", "odd **number\nof** markers** here\n", "<p>odd <strong>number\nof</strong> markers** here</p>\n", "simple __inline__ test\n", "<p>simple <strong>inline</strong> test</p>\n", "__at the__ beginning\n", "<p><strong>at the</strong> beginning</p>\n", "at the __end__\n", "<p>at the <strong>end</strong></p>\n", "__try two__ in __one line__\n", "<p><strong>try two</strong> in <strong>one line</strong></p>\n", "over __two\nlines__ test\n", "<p>over <strong>two\nlines</strong> test</p>\n", "odd __number of__ markers__ here\n", "<p>odd <strong>number of</strong> markers__ here</p>\n", "odd __number\nof__ markers__ here\n", "<p>odd <strong>number\nof</strong> markers__ here</p>\n", "mix of **markers__\n", "<p>mix of **markers__</p>\n", "**`/usr`** : this folder is named `usr`\n", "<p><strong><code>/usr</code></strong> : this folder is named <code>usr</code></p>\n", "**`/usr`** :\n\n this folder is named `usr`\n", "<p><strong><code>/usr</code></strong> :</p>\n\n<p>this folder is named <code>usr</code></p>\n", } doTestsInline(t, tests) } func TestEmphasisMix(t *testing.T) { t.Parallel() var tests = []string{ "***triple emphasis***\n", "<p><strong><em>triple emphasis</em></strong></p>\n", "***triple\nemphasis***\n", "<p><strong><em>triple\nemphasis</em></strong></p>\n", "___triple emphasis___\n", "<p><strong><em>triple emphasis</em></strong></p>\n", "***triple emphasis___\n", "<p>***triple emphasis___</p>\n", "*__triple emphasis__*\n", "<p><em><strong>triple emphasis</strong></em></p>\n", "__*triple emphasis*__\n", "<p><strong><em>triple emphasis</em></strong></p>\n", "**improper *nesting** is* bad\n", "<p><strong>improper *nesting</strong> is* bad</p>\n", "*improper **nesting* is** bad\n", "<p>*improper <strong>nesting* is</strong> bad</p>\n", } doTestsInline(t, tests) } func TestEmphasisLink(t *testing.T) { t.Parallel() var tests = []string{ "[first](before) *text[second] (inside)text* [third](after)\n", "<p><a href=\"before\">first</a> <em>text<a href=\"inside\">second</a>text</em> <a href=\"after\">third</a></p>\n", "*incomplete [link] definition*\n", "<p><em>incomplete [link] definition</em></p>\n", "*it's [emphasis*] (not link)\n", "<p><em>it's [emphasis</em>] (not link)</p>\n", "*it's [emphasis*] and *[asterisk]\n", "<p><em>it's [emphasis</em>] and *[asterisk]</p>\n", } doTestsInline(t, tests) } func TestStrikeThrough(t *testing.T) { t.Parallel() var tests = []string{ "nothing inline\n", "<p>nothing inline</p>\n", "simple ~~inline~~ test\n", "<p>simple <del>inline</del> test</p>\n", "~~at the~~ beginning\n", "<p><del>at the</del> beginning</p>\n", "at the ~~end~~\n", "<p>at the <del>end</del></p>\n", "~~try two~~ in ~~one line~~\n", "<p><del>try two</del> in <del>one line</del></p>\n", "over ~~two\nlines~~ test\n", "<p>over <del>two\nlines</del> test</p>\n", "odd ~~number of~~ markers~~ here\n", "<p>odd <del>number of</del> markers~~ here</p>\n", "odd ~~number\nof~~ markers~~ here\n", "<p>odd <del>number\nof</del> markers~~ here</p>\n", } doTestsInline(t, tests) } func TestCodeSpan(t *testing.T) { t.Parallel() var tests = []string{ "`source code`\n", "<p><code>source code</code></p>\n", "` source code with spaces `\n", "<p><code>source code with spaces</code></p>\n", "` source code with spaces `not here\n", "<p><code>source code with spaces</code>not here</p>\n", "a `single marker\n", "<p>a `single marker</p>\n", "a single multi-tick marker with ``` no text\n", "<p>a single multi-tick marker with ``` no text</p>\n", "markers with ` ` a space\n", "<p>markers with a space</p>\n", "`source code` and a `stray\n", "<p><code>source code</code> and a `stray</p>\n", "`source *with* _awkward characters_ in it`\n", "<p><code>source *with* _awkward characters_ in it</code></p>\n", "`split over\ntwo lines`\n", "<p><code>split over\ntwo lines</code></p>\n", "```multiple ticks``` for the marker\n", "<p><code>multiple ticks</code> for the marker</p>\n", "```multiple ticks `with` ticks inside```\n", "<p><code>multiple ticks `with` ticks inside</code></p>\n", } doTestsInline(t, tests) } func TestLineBreak(t *testing.T) { t.Parallel() var tests = []string{ "this line \nhas a break\n", "<p>this line<br />\nhas a break</p>\n", "this line \ndoes not\n", "<p>this line\ndoes not</p>\n", "this line\\\ndoes not\n", "<p>this line\\\ndoes not</p>\n", "this line\\ \ndoes not\n", "<p>this line\\\ndoes not</p>\n", "this has an \nextra space\n", "<p>this has an<br />\nextra space</p>\n", } doTestsInline(t, tests) tests = []string{ "this line \nhas a break\n", "<p>this line<br />\nhas a break</p>\n", "this line \ndoes not\n", "<p>this line\ndoes not</p>\n", "this line\\\nhas a break\n", "<p>this line<br />\nhas a break</p>\n", "this line\\ \ndoes not\n", "<p>this line\\\ndoes not</p>\n", "this has an \nextra space\n", "<p>this has an<br />\nextra space</p>\n", } doTestsInlineParam(t, tests, TestParams{ extensions: BackslashLineBreak}) } func TestInlineLink(t *testing.T) { t.Parallel() var tests = []string{ "[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", "[foo with a title](/bar/ \"title\")\n", "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n", "[foo with a title](/bar/\t\"title\")\n", "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n", "[foo with a title](/bar/ \"title\" )\n", "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n", "[foo with a title](/bar/ title with no quotes)\n", "<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n", "[foo]()\n", "<p>[foo]()</p>\n", "![foo](/bar/)\n", "<p><img src=\"/bar/\" alt=\"foo\" /></p>\n", "![foo with a title](/bar/ \"title\")\n", "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" /></p>\n", "![foo with a title](/bar/\t\"title\")\n", "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" /></p>\n", "![foo with a title](/bar/ \"title\" )\n", "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" /></p>\n", "![foo with a title](/bar/ title with no quotes)\n", "<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" /></p>\n", "![](img.jpg)\n", "<p><img src=\"img.jpg\" alt=\"\" /></p>\n", "[link](url)\n", "<p><a href=\"url\">link</a></p>\n", "![foo]()\n", "<p>![foo]()</p>\n", "[a link]\t(/with_a_tab/)\n", "<p><a href=\"/with_a_tab/\">a link</a></p>\n", "[a link] (/with_spaces/)\n", "<p><a href=\"/with_spaces/\">a link</a></p>\n", "[text (with) [[nested] (brackets)]](/url/)\n", "<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n", "[text (with) [broken nested] (brackets)]](/url/)\n", "<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n", "[text\nwith a newline](/link/)\n", "<p><a href=\"/link/\">text\nwith a newline</a></p>\n", "[text in brackets] [followed](/by a link/)\n", "<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n", "[link with\\] a closing bracket](/url/)\n", "<p><a href=\"/url/\">link with] a closing bracket</a></p>\n", "[link with\\[ an opening bracket](/url/)\n", "<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n", "[link with\\) a closing paren](/url/)\n", "<p><a href=\"/url/\">link with) a closing paren</a></p>\n", "[link with\\( an opening paren](/url/)\n", "<p><a href=\"/url/\">link with( an opening paren</a></p>\n", "[link]( with whitespace)\n", "<p><a href=\"with whitespace\">link</a></p>\n", "[link]( with whitespace )\n", "<p><a href=\"with whitespace\">link</a></p>\n", "[![image](someimage)](with image)\n", "<p><a href=\"with image\"><img src=\"someimage\" alt=\"image\" /></a></p>\n", "[link](url \"one quote)\n", "<p><a href=\"url "one quote\">link</a></p>\n", "[link](url 'one quote)\n", "<p><a href=\"url 'one quote\">link</a></p>\n", "[link](<url>)\n", "<p><a href=\"url\">link</a></p>\n", "[link & ampersand](/url/)\n", "<p><a href=\"/url/\">link & ampersand</a></p>\n", "[link & ampersand](/url/)\n", "<p><a href=\"/url/\">link & ampersand</a></p>\n", "[link](/url/&query)\n", "<p><a href=\"/url/&query\">link</a></p>\n", "[[t]](/t)\n", "<p><a href=\"/t\">[t]</a></p>\n", "[link](</>)\n", "<p><a href=\"/\">link</a></p>\n", "[link](<./>)\n", "<p><a href=\"./\">link</a></p>\n", "[link](<../>)\n", "<p><a href=\"../\">link</a></p>\n", } doLinkTestsInline(t, tests) } func TestRelAttrLink(t *testing.T) { t.Parallel() var nofollowTests = []string{ "[foo](http://bar.com/foo/)\n", "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n", "[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", "[foo](/)\n", "<p><a href=\"/\">foo</a></p>\n", "[foo](./)\n", "<p><a href=\"./\">foo</a></p>\n", "[foo](../)\n", "<p><a href=\"../\">foo</a></p>\n", "[foo](../bar)\n", "<p><a href=\"../bar\">foo</a></p>\n", } doTestsInlineParam(t, nofollowTests, TestParams{ HTMLFlags: Safelink | NofollowLinks, }) var noreferrerTests = []string{ "[foo](http://bar.com/foo/)\n", "<p><a href=\"http://bar.com/foo/\" rel=\"noreferrer\">foo</a></p>\n", "[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", } doTestsInlineParam(t, noreferrerTests, TestParams{ HTMLFlags: Safelink | NoreferrerLinks, }) var noopenerTests = []string{ "[foo](http://bar.com/foo/)\n", "<p><a href=\"http://bar.com/foo/\" rel=\"noopener\">foo</a></p>\n", "[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", } doTestsInlineParam(t, noopenerTests, TestParams{ HTMLFlags: Safelink | NoopenerLinks, }) var nofollownoreferrernoopenerTests = []string{ "[foo](http://bar.com/foo/)\n", "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow noreferrer noopener\">foo</a></p>\n", "[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", } doTestsInlineParam(t, nofollownoreferrernoopenerTests, TestParams{ HTMLFlags: Safelink | NofollowLinks | NoreferrerLinks | NoopenerLinks, }) } func TestHrefTargetBlank(t *testing.T) { t.Parallel() var tests = []string{ // internal link "[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", "[foo](/)\n", "<p><a href=\"/\">foo</a></p>\n", "[foo](./)\n", "<p><a href=\"./\">foo</a></p>\n", "[foo](./bar)\n", "<p><a href=\"./bar\">foo</a></p>\n", "[foo](../)\n", "<p><a href=\"../\">foo</a></p>\n", "[foo](../bar)\n", "<p><a href=\"../bar\">foo</a></p>\n", "[foo](http://example.com)\n", "<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n", } doTestsInlineParam(t, tests, TestParams{ HTMLFlags: Safelink | HrefTargetBlank, }) } func TestSafeInlineLink(t *testing.T) { t.Parallel() var tests = []string{ "[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", "[foo](/)\n", "<p><a href=\"/\">foo</a></p>\n", "[foo](./)\n", "<p><a href=\"./\">foo</a></p>\n", "[foo](../)\n", "<p><a href=\"../\">foo</a></p>\n", "[foo](http://bar/)\n", "<p><a href=\"http://bar/\">foo</a></p>\n", "[foo](https://bar/)\n", "<p><a href=\"https://bar/\">foo</a></p>\n", "[foo](ftp://bar/)\n", "<p><a href=\"ftp://bar/\">foo</a></p>\n", "[foo](mailto://bar/)\n", "<p><a href=\"mailto://bar/\">foo</a></p>\n", // Not considered safe "[foo](baz://bar/)\n", "<p><tt>foo</tt></p>\n", } doSafeTestsInline(t, tests) } func TestReferenceLink(t *testing.T) { t.Parallel() var tests = []string{ "[link][ref]\n", "<p>[link][ref]</p>\n", "[link][ref]\n [ref]: /url/ \"title\"\n", "<p><a href=\"/url/\" title=\"title\">link</a></p>\n", "[link][ref]\n [ref]: /url/\n", "<p><a href=\"/url/\">link</a></p>\n", " [ref]: /url/\n", "", " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n", "", " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n [4spaces]: /url/\n", "<pre><code>[4spaces]: /url/\n</code></pre>\n", "[hmm](ref2)\n [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n", "<p><a href=\"ref2\">hmm</a></p>\n", "[ref]\n", "<p>[ref]</p>\n", "[ref]\n [ref]: /url/ \"title\"\n", "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n", "[ref]\n [ref]: ../url/ \"title\"\n", "<p><a href=\"../url/\" title=\"title\">ref</a></p>\n", "[link][ref]\n [ref]: /url/", "<p><a href=\"/url/\">link</a></p>\n", } doLinkTestsInline(t, tests) } func TestTags(t *testing.T) { t.Parallel() var tests = []string{ "a <span>tag</span>\n", "<p>a <span>tag</span></p>\n", "<span>tag</span>\n", "<p><span>tag</span></p>\n", "<span>mismatch</spandex>\n", "<p><span>mismatch</spandex></p>\n", "a <singleton /> tag\n", "<p>a <singleton /> tag</p>\n", } doTestsInline(t, tests) } func TestAutoLink(t *testing.T) { t.Parallel() var tests = []string{ "http://foo.com/\n", "<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n", "1 http://foo.com/\n", "<p>1 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n", "1http://foo.com/\n", "<p>1<a href=\"http://foo.com/\">http://foo.com/</a></p>\n", "1.http://foo.com/\n", "<p>1.<a href=\"http://foo.com/\">http://foo.com/</a></p>\n", "1. http://foo.com/\n", "<ol>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ol>\n", "-http://foo.com/\n", "<p>-<a href=\"http://foo.com/\">http://foo.com/</a></p>\n", "- http://foo.com/\n", "<ul>\n<li><a href=\"http://foo.com/\">http://foo.com/</a></li>\n</ul>\n", "_http://foo.com/\n", "<p>_<a href=\"http://foo.com/\">http://foo.com/</a></p>\n", "令狐http://foo.com/\n", "<p>令狐<a href=\"http://foo.com/\">http://foo.com/</a></p>\n", "令狐 http://foo.com/\n", "<p>令狐 <a href=\"http://foo.com/\">http://foo.com/</a></p>\n", "ahttp://foo.com/\n", "<p>ahttp://foo.com/</p>\n", ">http://foo.com/\n", "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n", "> http://foo.com/\n", "<blockquote>\n<p><a href=\"http://foo.com/\">http://foo.com/</a></p>\n</blockquote>\n", "go to <http://foo.com/>\n", "<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n", "a secure <https://link.org>\n", "<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n", "an email <mailto:some@one.com>\n", "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n", "an email <mailto://some@one.com>\n", "<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n", "an email <some@one.com>\n", "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n", "an ftp <ftp://old.com>\n", "<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n", "an ftp <ftp:old.com>\n", "<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n", "a link with <http://new.com?query=foo&bar>\n", "<p>a link with <a href=\"http://new.com?query=foo&bar\">" + "http://new.com?query=foo&bar</a></p>\n", "quotes mean a tag <http://new.com?query=\"foo\"&bar>\n", "<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n", "quotes mean a tag <http://new.com?query='foo'&bar>\n", "<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n", "unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n", "<p>unless escaped <a href=\"http://new.com?query="foo"&bar\">" + "http://new.com?query="foo"&bar</a></p>\n", "even a > can be escaped <http://new.com?q=\\>&etc>\n", "<p>even a > can be escaped <a href=\"http://new.com?q=>&etc\">" + "http://new.com?q=>&etc</a></p>\n", "<a href=\"http://fancy.com\">http://fancy.com</a>\n", "<p><a href=\"http://fancy.com\">http://fancy.com</a></p>\n", "<a href=\"http://fancy.com\">This is a link</a>\n", "<p><a href=\"http://fancy.com\">This is a link</a></p>\n", "<a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a>\n", "<p><a href=\"http://www.fancy.com/A_B.pdf\">http://www.fancy.com/A_B.pdf</a></p>\n", "(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (\n", "<p>(<a href=\"http://www.fancy.com/A_B\">http://www.fancy.com/A_B</a> (</p>\n", "(<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", "<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", "http://www.foo.com<br />\n", "<p><a href=\"http://www.foo.com\">http://www.foo.com</a><br /></p>\n", "http://foo.com/viewtopic.php?f=18&t=297", "<p><a href=\"http://foo.com/viewtopic.php?f=18&t=297\">http://foo.com/viewtopic.php?f=18&t=297</a></p>\n", "http://foo.com/viewtopic.php?param="18"zz", "<p><a href=\"http://foo.com/viewtopic.php?param="18"zz\">http://foo.com/viewtopic.php?param="18"zz</a></p>\n", "http://foo.com/viewtopic.php?param="18"", "<p><a href=\"http://foo.com/viewtopic.php?param="18"\">http://foo.com/viewtopic.php?param="18"</a></p>\n", "<a href=\"https://fancy.com\">https://fancy.com</a>\n", "<p><a href=\"https://fancy.com\">https://fancy.com</a></p>\n", } doLinkTestsInline(t, tests) } var footnoteTests = []string{ "testing footnotes.[^a]\n\n[^a]: This is the note\n", `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a href="#fn:a">1</a></sup></p> <div class="footnotes"> <hr /> <ol> <li id="fn:a">This is the note</li> </ol> </div> `, `testing long[^b] notes. [^b]: Paragraph 1 Paragraph 2 ` + "```\n\tsome code\n\t```" + ` Paragraph 3 No longer in the footnote `, `<p>testing long<sup class="footnote-ref" id="fnref:b"><a href="#fn:b">1</a></sup> notes.</p> <p>No longer in the footnote</p> <div class="footnotes"> <hr /> <ol> <li id="fn:b"><p>Paragraph 1</p> <p>Paragraph 2</p> <p><code> some code </code></p> <p>Paragraph 3</p></li> </ol> </div> `, `testing[^c] multiple[^d] notes. [^c]: this is [note] c omg [^d]: this is note d what happens here [note]: /link/c `, `<p>testing<sup class="footnote-ref" id="fnref:c"><a href="#fn:c">1</a></sup> multiple<sup class="footnote-ref" id="fnref:d"><a href="#fn:d">2</a></sup> notes.</p> <p>omg</p> <p>what happens here</p> <div class="footnotes"> <hr /> <ol> <li id="fn:c">this is <a href="/link/c">note</a> c</li> <li id="fn:d">this is note d</li> </ol> </div> `, "testing inline^[this is the note] notes.\n", `<p>testing inline<sup class="footnote-ref" id="fnref:this-is-the-note"><a href="#fn:this-is-the-note">1</a></sup> notes.</p> <div class="footnotes"> <hr /> <ol> <li id="fn:this-is-the-note">this is the note</li> </ol> </div> `, "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", `<p>testing multiple<sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup> types<sup class="footnote-ref" id="fnref:inline-note"><a href="#fn:inline-note">2</a></sup> of notes<sup class="footnote-ref" id="fnref:2"><a href="#fn:2">3</a></sup></p> <div class="footnotes"> <hr /> <ol> <li id="fn:1"><p>the first deferred note</p> <p>which happens to be a block</p></li> <li id="fn:inline-note">inline note</li> <li id="fn:2">the second deferred note</li> </ol> </div> `, `This is a footnote[^1]^[and this is an inline footnote] [^1]: the footnote text. may be multiple paragraphs. `, `<p>This is a footnote<sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup><sup class="footnote-ref" id="fnref:and-this-is-an-i"><a href="#fn:and-this-is-an-i">2</a></sup></p> <div class="footnotes"> <hr /> <ol> <li id="fn:1"><p>the footnote text.</p> <p>may be multiple paragraphs.</p></li> <li id="fn:and-this-is-an-i">and this is an inline footnote</li> </ol> </div> `, "empty footnote[^]\n\n[^]: fn text", "<p>empty footnote<sup class=\"footnote-ref\" id=\"fnref:\"><a href=\"#fn:\">1</a></sup></p>\n\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:\">fn text</li>\n</ol>\n\n</div>\n", "Some text.[^note1]\n\n[^note1]: fn1", "<p>Some text.<sup class=\"footnote-ref\" id=\"fnref:note1\"><a href=\"#fn:note1\">1</a></sup></p>\n\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:note1\">fn1</li>\n</ol>\n\n</div>\n", "Some text.[^note1][^note2]\n\n[^note1]: fn1\n[^note2]: fn2\n", "<p>Some text.<sup class=\"footnote-ref\" id=\"fnref:note1\"><a href=\"#fn:note1\">1</a></sup><sup class=\"footnote-ref\" id=\"fnref:note2\"><a href=\"#fn:note2\">2</a></sup></p>\n\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:note1\">fn1</li>\n\n<li id=\"fn:note2\">fn2</li>\n</ol>\n\n</div>\n", `Bla bla [^1] [WWW][w3] [^1]: This is a footnote [w3]: http://www.w3.org/ `, `<p>Bla bla <sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup> <a href="http://www.w3.org/">WWW</a></p> <div class="footnotes"> <hr /> <ol> <li id="fn:1">This is a footnote</li> </ol> </div> `, `This is exciting![^fn1] [^fn1]: Fine print `, `<p>This is exciting!<sup class="footnote-ref" id="fnref:fn1"><a href="#fn:fn1">1</a></sup></p> <div class="footnotes"> <hr /> <ol> <li id="fn:fn1">Fine print</li> </ol> </div> `, `This text does not reference a footnote. [^footnote]: But it has a footnote! And it gets omitted. `, "<p>This text does not reference a footnote.</p>\n", } func TestFootnotes(t *testing.T) { t.Parallel() doTestsInlineParam(t, footnoteTests, TestParams{ extensions: Footnotes, }) } func TestFootnotesWithParameters(t *testing.T) { t.Parallel() tests := make([]string, len(footnoteTests)) prefix := "testPrefix" returnText := "ret" re := regexp.MustCompile(`(?ms)<li id="fn:(\S+?)">(.*?)</li>`) // Transform the test expectations to match the parameters we're using. for i, test := range footnoteTests { if i%2 == 1 { test = strings.Replace(test, "fn:", "fn:"+prefix, -1) test = strings.Replace(test, "fnref:", "fnref:"+prefix, -1) test = re.ReplaceAllString(test, `<li id="fn:$1">$2 <a class="footnote-return" href="#fnref:$1">ret</a></li>`) } tests[i] = test } params := HTMLRendererParameters{ FootnoteAnchorPrefix: prefix, FootnoteReturnLinkContents: returnText, } doTestsInlineParam(t, tests, TestParams{ extensions: Footnotes, HTMLFlags: FootnoteReturnLinks, HTMLRendererParameters: params, }) } func TestNestedFootnotes(t *testing.T) { t.Parallel() var tests = []string{ `Paragraph.[^fn1] [^fn1]: Asterisk[^fn2] [^fn2]: Obelisk`, `<p>Paragraph.<sup class="footnote-ref" id="fnref:fn1"><a href="#fn:fn1">1</a></sup></p> <div class="footnotes"> <hr /> <ol> <li id="fn:fn1">Asterisk<sup class="footnote-ref" id="fnref:fn2"><a href="#fn:fn2">2</a></sup></li> <li id="fn:fn2">Obelisk</li> </ol> </div> `, } doTestsInlineParam(t, tests, TestParams{extensions: Footnotes}) } func TestInlineComments(t *testing.T) { t.Parallel() var tests = []string{ "Hello <!-- there ->\n", "<p>Hello <!— there –></p>\n", "Hello <!-- there -->\n", "<p>Hello <!-- there --></p>\n", "Hello <!-- there -->", "<p>Hello <!-- there --></p>\n", "Hello <!---->\n", "<p>Hello <!----></p>\n", "Hello <!-- there -->\na", "<p>Hello <!-- there -->\na</p>\n", "* list <!-- item -->\n", "<ul>\n<li>list <!-- item --></li>\n</ul>\n", "<!-- Front --> comment\n", "<p><!-- Front --> comment</p>\n", "blahblah\n<!--- foo -->\nrhubarb\n", "<p>blahblah\n<!--- foo -->\nrhubarb</p>\n", } doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsDashes}) } func TestSmartDoubleQuotes(t *testing.T) { t.Parallel() var tests = []string{ "this should be normal \"quoted\" text.\n", "<p>this should be normal “quoted” text.</p>\n", "this \" single double\n", "<p>this “ single double</p>\n", "two pair of \"some\" quoted \"text\".\n", "<p>two pair of “some” quoted “text”.</p>\n"} doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants}) } func TestSmartDoubleQuotesNBSP(t *testing.T) { t.Parallel() var tests = []string{ "this should be normal \"quoted\" text.\n", "<p>this should be normal “ quoted ” text.</p>\n", "this \" single double\n", "<p>this “ single double</p>\n", "two pair of \"some\" quoted \"text\".\n", "<p>two pair of “ some ” quoted “ text ”.</p>\n"} doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsQuotesNBSP}) } func TestSmartAngledDoubleQuotes(t *testing.T) { t.Parallel() var tests = []string{ "this should be angled \"quoted\" text.\n", "<p>this should be angled «quoted» text.</p>\n", "this \" single double\n", "<p>this « single double</p>\n", "two pair of \"some\" quoted \"text\".\n", "<p>two pair of «some» quoted «text».</p>\n"} doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsAngledQuotes}) } func TestSmartAngledDoubleQuotesNBSP(t *testing.T) { t.Parallel() var tests = []string{ "this should be angled \"quoted\" text.\n", "<p>this should be angled « quoted » text.</p>\n", "this \" single double\n", "<p>this « single double</p>\n", "two pair of \"some\" quoted \"text\".\n", "<p>two pair of « some » quoted « text ».</p>\n"} doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsAngledQuotes | SmartypantsQuotesNBSP}) } func TestSmartFractions(t *testing.T) { t.Parallel() var tests = []string{ "1/2, 1/4 and 3/4; 1/4th and 3/4ths\n", "<p>½, ¼ and ¾; ¼th and ¾ths</p>\n", "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n", "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"} doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants}) tests = []string{ "1/2, 2/3, 81/100 and 1000000/1048576.\n", "<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", "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n", "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"} doTestsInlineParam(t, tests, TestParams{HTMLFlags: Smartypants | SmartypantsFractions}) } func TestDisableSmartDashes(t *testing.T) { t.Parallel() doTestsInlineParam(t, []string{ "foo - bar\n", "<p>foo - bar</p>\n", "foo -- bar\n", "<p>foo -- bar</p>\n", "foo --- bar\n", "<p>foo --- bar</p>\n", }, TestParams{}) doTestsInlineParam(t, []string{ "foo - bar\n", "<p>foo – bar</p>\n", "foo -- bar\n", "<p>foo — bar</p>\n", "foo --- bar\n", "<p>foo —– bar</p>\n", }, TestParams{HTMLFlags: Smartypants | SmartypantsDashes}) doTestsInlineParam(t, []string{ "foo - bar\n", "<p>foo - bar</p>\n", "foo -- bar\n", "<p>foo – bar</p>\n", "foo --- bar\n", "<p>foo — bar</p>\n", }, TestParams{HTMLFlags: Smartypants | SmartypantsLatexDashes | SmartypantsDashes}) doTestsInlineParam(t, []string{ "foo - bar\n", "<p>foo - bar</p>\n", "foo -- bar\n", "<p>foo -- bar</p>\n", "foo --- bar\n", "<p>foo --- bar</p>\n", }, TestParams{HTMLFlags: Smartypants | SmartypantsLatexDashes}) } func TestSkipLinks(t *testing.T) { t.Parallel() doTestsInlineParam(t, []string{ "[foo](gopher://foo.bar)", "<p><tt>foo</tt></p>\n", "[foo](mailto://bar/)\n", "<p><tt>foo</tt></p>\n", }, TestParams{ HTMLFlags: SkipLinks, }) } func TestSkipImages(t *testing.T) { t.Parallel() doTestsInlineParam(t, []string{ "![foo](/bar/)\n", "<p></p>\n", }, TestParams{ HTMLFlags: SkipImages, }) } func TestUseXHTML(t *testing.T) { t.Parallel() doTestsParam(t, []string{ "---", "<hr>\n", }, TestParams{}) doTestsParam(t, []string{ "---", "<hr />\n", }, TestParams{HTMLFlags: UseXHTML}) } func TestSkipHTML(t *testing.T) { t.Parallel() doTestsParam(t, []string{ "<div class=\"foo\"></div>\n\ntext\n\n<form>the form</form>", "<p>text</p>\n\n<p>the form</p>\n", "text <em>inline html</em> more text", "<p>text inline html more text</p>\n", }, TestParams{HTMLFlags: SkipHTML}) } func BenchmarkSmartDoubleQuotes(b *testing.B) { params := TestParams{HTMLFlags: Smartypants} params.extensions |= Autolink | Strikethrough params.HTMLFlags |= UseXHTML for i := 0; i < b.N; i++ { runMarkdown("this should be normal \"quoted\" text.\n", params) } } |