all repos — grayfriday @ 55cde00c8ab66a2037b219eb4ae96d8e30e272fe

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