all repos — grayfriday @ b79e720a36b6805be26938b176bb44cdabaace5d

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