all repos — grayfriday @ 375cae5dbea8edc3c30439522c2c72f5e0c2b566

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