You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1154 lines
32 KiB

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