all repos — grayfriday @ d5a8df164b001f7f60e314b2d90aec3927b5819c

blackfriday fork with a few changes

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	"testing"
 18)
 19
 20func runMarkdownInline(input string, extensions, htmlFlags int) string {
 21	extensions |= EXTENSION_AUTOLINK
 22	extensions |= EXTENSION_STRIKETHROUGH
 23
 24	htmlFlags |= HTML_USE_XHTML
 25
 26	renderer := HtmlRenderer(htmlFlags, "", "")
 27
 28	return string(Markdown([]byte(input), renderer, extensions))
 29}
 30
 31func doTestsInline(t *testing.T, tests []string) {
 32	doTestsInlineParam(t, tests, 0, 0)
 33}
 34
 35func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int) {
 36	// catch and report panics
 37	var candidate string
 38	defer func() {
 39		if err := recover(); err != nil {
 40			t.Errorf("\npanic while processing [%#v]\n", candidate)
 41		}
 42	}()
 43
 44	for i := 0; i+1 < len(tests); i += 2 {
 45		input := tests[i]
 46		candidate = input
 47		expected := tests[i+1]
 48		actual := runMarkdownInline(candidate, extensions, htmlFlags)
 49		if actual != expected {
 50			t.Errorf("\nInput   [%#v]\nExpected[%#v]\nActual  [%#v]",
 51				candidate, expected, actual)
 52		}
 53
 54		// now test every substring to stress test bounds checking
 55		if !testing.Short() {
 56			for start := 0; start < len(input); start++ {
 57				for end := start + 1; end <= len(input); end++ {
 58					candidate = input[start:end]
 59					_ = runMarkdownInline(candidate, extensions, htmlFlags)
 60				}
 61			}
 62		}
 63	}
 64}
 65
 66func TestRawHtmlTag(t *testing.T) {
 67	tests := []string{
 68		"zz <style>p {}</style>\n",
 69		"<p>zz p {}</p>\n",
 70	}
 71	doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE)
 72}
 73
 74func TestEmphasis(t *testing.T) {
 75	var tests = []string{
 76		"nothing inline\n",
 77		"<p>nothing inline</p>\n",
 78
 79		"simple *inline* test\n",
 80		"<p>simple <em>inline</em> test</p>\n",
 81
 82		"*at the* beginning\n",
 83		"<p><em>at the</em> beginning</p>\n",
 84
 85		"at the *end*\n",
 86		"<p>at the <em>end</em></p>\n",
 87
 88		"*try two* in *one line*\n",
 89		"<p><em>try two</em> in <em>one line</em></p>\n",
 90
 91		"over *two\nlines* test\n",
 92		"<p>over <em>two\nlines</em> test</p>\n",
 93
 94		"odd *number of* markers* here\n",
 95		"<p>odd <em>number of</em> markers* here</p>\n",
 96
 97		"odd *number\nof* markers* here\n",
 98		"<p>odd <em>number\nof</em> markers* here</p>\n",
 99
100		"simple _inline_ test\n",
101		"<p>simple <em>inline</em> test</p>\n",
102
103		"_at the_ beginning\n",
104		"<p><em>at the</em> beginning</p>\n",
105
106		"at the _end_\n",
107		"<p>at the <em>end</em></p>\n",
108
109		"_try two_ in _one line_\n",
110		"<p><em>try two</em> in <em>one line</em></p>\n",
111
112		"over _two\nlines_ test\n",
113		"<p>over <em>two\nlines</em> test</p>\n",
114
115		"odd _number of_ markers_ here\n",
116		"<p>odd <em>number of</em> markers_ here</p>\n",
117
118		"odd _number\nof_ markers_ here\n",
119		"<p>odd <em>number\nof</em> markers_ here</p>\n",
120
121		"mix of *markers_\n",
122		"<p>mix of *markers_</p>\n",
123	}
124	doTestsInline(t, tests)
125}
126
127func TestStrong(t *testing.T) {
128	var tests = []string{
129		"nothing inline\n",
130		"<p>nothing inline</p>\n",
131
132		"simple **inline** test\n",
133		"<p>simple <strong>inline</strong> test</p>\n",
134
135		"**at the** beginning\n",
136		"<p><strong>at the</strong> beginning</p>\n",
137
138		"at the **end**\n",
139		"<p>at the <strong>end</strong></p>\n",
140
141		"**try two** in **one line**\n",
142		"<p><strong>try two</strong> in <strong>one line</strong></p>\n",
143
144		"over **two\nlines** test\n",
145		"<p>over <strong>two\nlines</strong> test</p>\n",
146
147		"odd **number of** markers** here\n",
148		"<p>odd <strong>number of</strong> markers** here</p>\n",
149
150		"odd **number\nof** markers** here\n",
151		"<p>odd <strong>number\nof</strong> markers** here</p>\n",
152
153		"simple __inline__ test\n",
154		"<p>simple <strong>inline</strong> test</p>\n",
155
156		"__at the__ beginning\n",
157		"<p><strong>at the</strong> beginning</p>\n",
158
159		"at the __end__\n",
160		"<p>at the <strong>end</strong></p>\n",
161
162		"__try two__ in __one line__\n",
163		"<p><strong>try two</strong> in <strong>one line</strong></p>\n",
164
165		"over __two\nlines__ test\n",
166		"<p>over <strong>two\nlines</strong> test</p>\n",
167
168		"odd __number of__ markers__ here\n",
169		"<p>odd <strong>number of</strong> markers__ here</p>\n",
170
171		"odd __number\nof__ markers__ here\n",
172		"<p>odd <strong>number\nof</strong> markers__ here</p>\n",
173
174		"mix of **markers__\n",
175		"<p>mix of **markers__</p>\n",
176	}
177	doTestsInline(t, tests)
178}
179
180func TestEmphasisMix(t *testing.T) {
181	var tests = []string{
182		"***triple emphasis***\n",
183		"<p><strong><em>triple emphasis</em></strong></p>\n",
184
185		"***triple\nemphasis***\n",
186		"<p><strong><em>triple\nemphasis</em></strong></p>\n",
187
188		"___triple emphasis___\n",
189		"<p><strong><em>triple emphasis</em></strong></p>\n",
190
191		"***triple emphasis___\n",
192		"<p>***triple emphasis___</p>\n",
193
194		"*__triple emphasis__*\n",
195		"<p><em><strong>triple emphasis</strong></em></p>\n",
196
197		"__*triple emphasis*__\n",
198		"<p><strong><em>triple emphasis</em></strong></p>\n",
199
200		"**improper *nesting** is* bad\n",
201		"<p><strong>improper *nesting</strong> is* bad</p>\n",
202
203		"*improper **nesting* is** bad\n",
204		"<p><em>improper **nesting</em> is** bad</p>\n",
205	}
206	doTestsInline(t, tests)
207}
208
209func TestStrikeThrough(t *testing.T) {
210	var tests = []string{
211		"nothing inline\n",
212		"<p>nothing inline</p>\n",
213
214		"simple ~~inline~~ test\n",
215		"<p>simple <del>inline</del> test</p>\n",
216
217		"~~at the~~ beginning\n",
218		"<p><del>at the</del> beginning</p>\n",
219
220		"at the ~~end~~\n",
221		"<p>at the <del>end</del></p>\n",
222
223		"~~try two~~ in ~~one line~~\n",
224		"<p><del>try two</del> in <del>one line</del></p>\n",
225
226		"over ~~two\nlines~~ test\n",
227		"<p>over <del>two\nlines</del> test</p>\n",
228
229		"odd ~~number of~~ markers~~ here\n",
230		"<p>odd <del>number of</del> markers~~ here</p>\n",
231
232		"odd ~~number\nof~~ markers~~ here\n",
233		"<p>odd <del>number\nof</del> markers~~ here</p>\n",
234	}
235	doTestsInline(t, tests)
236}
237
238func TestCodeSpan(t *testing.T) {
239	var tests = []string{
240		"`source code`\n",
241		"<p><code>source code</code></p>\n",
242
243		"` source code with spaces `\n",
244		"<p><code>source code with spaces</code></p>\n",
245
246		"` source code with spaces `not here\n",
247		"<p><code>source code with spaces</code>not here</p>\n",
248
249		"a `single marker\n",
250		"<p>a `single marker</p>\n",
251
252		"a single multi-tick marker with ``` no text\n",
253		"<p>a single multi-tick marker with ``` no text</p>\n",
254
255		"markers with ` ` a space\n",
256		"<p>markers with  a space</p>\n",
257
258		"`source code` and a `stray\n",
259		"<p><code>source code</code> and a `stray</p>\n",
260
261		"`source *with* _awkward characters_ in it`\n",
262		"<p><code>source *with* _awkward characters_ in it</code></p>\n",
263
264		"`split over\ntwo lines`\n",
265		"<p><code>split over\ntwo lines</code></p>\n",
266
267		"```multiple ticks``` for the marker\n",
268		"<p><code>multiple ticks</code> for the marker</p>\n",
269
270		"```multiple ticks `with` ticks inside```\n",
271		"<p><code>multiple ticks `with` ticks inside</code></p>\n",
272	}
273	doTestsInline(t, tests)
274}
275
276func TestLineBreak(t *testing.T) {
277	var tests = []string{
278		"this line  \nhas a break\n",
279		"<p>this line<br />\nhas a break</p>\n",
280
281		"this line \ndoes not\n",
282		"<p>this line\ndoes not</p>\n",
283
284		"this has an   \nextra space\n",
285		"<p>this has an<br />\nextra space</p>\n",
286	}
287	doTestsInline(t, tests)
288}
289
290func TestInlineLink(t *testing.T) {
291	var tests = []string{
292		"[foo](/bar/)\n",
293		"<p><a href=\"/bar/\">foo</a></p>\n",
294
295		"[foo with a title](/bar/ \"title\")\n",
296		"<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
297
298		"[foo with a title](/bar/\t\"title\")\n",
299		"<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
300
301		"[foo with a title](/bar/ \"title\"  )\n",
302		"<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
303
304		"[foo with a title](/bar/ title with no quotes)\n",
305		"<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n",
306
307		"[foo]()\n",
308		"<p>[foo]()</p>\n",
309
310		"![foo](/bar/)\n",
311		"<p><img src=\"/bar/\" alt=\"foo\" />\n</p>\n",
312
313		"![foo with a title](/bar/ \"title\")\n",
314		"<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
315
316		"![foo with a title](/bar/\t\"title\")\n",
317		"<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
318
319		"![foo with a title](/bar/ \"title\"  )\n",
320		"<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
321
322		"![foo with a title](/bar/ title with no quotes)\n",
323		"<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" />\n</p>\n",
324
325		"![foo]()\n",
326		"<p>![foo]()</p>\n",
327
328		"[a link]\t(/with_a_tab/)\n",
329		"<p><a href=\"/with_a_tab/\">a link</a></p>\n",
330
331		"[a link]  (/with_spaces/)\n",
332		"<p><a href=\"/with_spaces/\">a link</a></p>\n",
333
334		"[text (with) [[nested] (brackets)]](/url/)\n",
335		"<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n",
336
337		"[text (with) [broken nested] (brackets)]](/url/)\n",
338		"<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n",
339
340		"[text\nwith a newline](/link/)\n",
341		"<p><a href=\"/link/\">text\nwith a newline</a></p>\n",
342
343		"[text in brackets] [followed](/by a link/)\n",
344		"<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n",
345
346		"[link with\\] a closing bracket](/url/)\n",
347		"<p><a href=\"/url/\">link with] a closing bracket</a></p>\n",
348
349		"[link with\\[ an opening bracket](/url/)\n",
350		"<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n",
351
352		"[link with\\) a closing paren](/url/)\n",
353		"<p><a href=\"/url/\">link with) a closing paren</a></p>\n",
354
355		"[link with\\( an opening paren](/url/)\n",
356		"<p><a href=\"/url/\">link with( an opening paren</a></p>\n",
357
358		"[link](  with whitespace)\n",
359		"<p><a href=\"with whitespace\">link</a></p>\n",
360
361		"[link](  with whitespace   )\n",
362		"<p><a href=\"with whitespace\">link</a></p>\n",
363
364		"[link](url \"one quote)\n",
365		"<p><a href=\"url &quot;one quote\">link</a></p>\n",
366
367		"[link](url 'one quote)\n",
368		"<p><a href=\"url 'one quote\">link</a></p>\n",
369
370		"[link](<url>)\n",
371		"<p><a href=\"url\">link</a></p>\n",
372
373		"[link & ampersand](/url/)\n",
374		"<p><a href=\"/url/\">link &amp; ampersand</a></p>\n",
375
376		"[link &amp; ampersand](/url/)\n",
377		"<p><a href=\"/url/\">link &amp; ampersand</a></p>\n",
378
379		"[link](/url/&query)\n",
380		"<p><a href=\"/url/&amp;query\">link</a></p>\n",
381	}
382	doTestsInline(t, tests)
383}
384
385func TestReferenceLink(t *testing.T) {
386	var tests = []string{
387		"[link][ref]\n",
388		"<p>[link][ref]</p>\n",
389
390		"[link][ref]\n   [ref]: /url/ \"title\"\n",
391		"<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
392
393		"[link][ref]\n   [ref]: /url/\n",
394		"<p><a href=\"/url/\">link</a></p>\n",
395
396		"   [ref]: /url/\n",
397		"",
398
399		"   [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
400		"",
401
402		"   [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n    [4spaces]: /url/\n",
403		"<pre><code>[4spaces]: /url/\n</code></pre>\n",
404
405		"[hmm](ref2)\n   [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
406		"<p><a href=\"ref2\">hmm</a></p>\n",
407
408		"[ref]\n",
409		"<p>[ref]</p>\n",
410
411		"[ref]\n   [ref]: /url/ \"title\"\n",
412		"<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
413	}
414	doTestsInline(t, tests)
415}
416
417func TestTags(t *testing.T) {
418	var tests = []string{
419		"a <span>tag</span>\n",
420		"<p>a <span>tag</span></p>\n",
421
422		"<span>tag</span>\n",
423		"<p><span>tag</span></p>\n",
424
425		"<span>mismatch</spandex>\n",
426		"<p><span>mismatch</spandex></p>\n",
427
428		"a <singleton /> tag\n",
429		"<p>a <singleton /> tag</p>\n",
430	}
431	doTestsInline(t, tests)
432}
433
434func TestAutoLink(t *testing.T) {
435	var tests = []string{
436		"go to <http://foo.com/>\n",
437		"<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
438
439		"a secure <https://link.org>\n",
440		"<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
441
442		"an email <mailto:some@one.com>\n",
443		"<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
444
445		"an email <mailto://some@one.com>\n",
446		"<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
447
448		"an email <some@one.com>\n",
449		"<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
450
451		"an ftp <ftp://old.com>\n",
452		"<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
453
454		"an ftp <ftp:old.com>\n",
455		"<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
456
457		"a link with <http://new.com?query=foo&bar>\n",
458		"<p>a link with <a href=\"http://new.com?query=foo&amp;bar\">" +
459			"http://new.com?query=foo&amp;bar</a></p>\n",
460
461		"quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
462		"<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
463
464		"quotes mean a tag <http://new.com?query='foo'&bar>\n",
465		"<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
466
467		"unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
468		"<p>unless escaped <a href=\"http://new.com?query=&quot;foo&quot;&amp;bar\">" +
469			"http://new.com?query=&quot;foo&quot;&amp;bar</a></p>\n",
470
471		"even a > can be escaped <http://new.com?q=\\>&etc>\n",
472		"<p>even a &gt; can be escaped <a href=\"http://new.com?q=&gt;&amp;etc\">" +
473			"http://new.com?q=&gt;&amp;etc</a></p>\n",
474	}
475	doTestsInline(t, tests)
476}