# coding: UTF-8 require 'test_helper' class HTMLRenderTest < Redcarpet::TestCase def setup @markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML) @rndr = { :no_html => Redcarpet::Render::HTML.new(:filter_html => true), :no_images => Redcarpet::Render::HTML.new(:no_images => true), :no_links => Redcarpet::Render::HTML.new(:no_links => true), :safe_links => Redcarpet::Render::HTML.new(:safe_links_only => true), :escape_html => Redcarpet::Render::HTML.new(:escape_html => true), :hard_wrap => Redcarpet::Render::HTML.new(:hard_wrap => true), :toc_data => Redcarpet::Render::HTML.new(:with_toc_data => true), :prettify => Redcarpet::Render::HTML.new(:prettify => true) } end def render_with(rndr, text) Redcarpet::Markdown.new(rndr).render(text) end # Hint: overrides filter_html, no_images and no_links def test_that_escape_html_works source = <NO EOS expected = <Through <em>NO</em> <script>DOUBLE NO</script>

<script>BAD</script>

<img src="/favicon.ico" />

EOE markdown = render_with(@rndr[:escape_html], source) html_equal expected, markdown end def test_that_filter_html_works markdown = render_with(@rndr[:no_html], 'Through NO ') html_equal "

Through NO DOUBLE NO

\n", markdown end def test_filter_html_doesnt_break_two_space_hard_break markdown = render_with(@rndr[:no_html], "Lorem, \nipsum\n") html_equal "

Lorem,
\nipsum

\n", markdown end def test_that_no_image_flag_works rd = render_with(@rndr[:no_images], %(![dust mite](http://dust.mite/image.png) )) assert rd !~ /links)) assert rd !~ /[IRC](irc://chat.freenode.org/#freenode)

\n", rd end def test_that_hard_wrap_works rd = render_with(@rndr[:hard_wrap], </ end def test_that_link_attributes_work rndr = Redcarpet::Render::HTML.new(:link_attributes => {:rel => 'blank'}) md = Redcarpet::Markdown.new(rndr) assert md.render('This is a [simple](http://test.com) test.').include?('rel="blank"') end def test_that_link_works_with_quotes rd = render_with(Redcarpet::Render::HTML.new, %([This'link"is](http://example.net/))) assert_equal "

This'link"is

\n", rd rd = render_with(@rndr[:escape_html], %([This'link"is](http://example.net/))) assert_equal "

This'link"is

\n", rd end def test_that_code_emphasis_work markdown = <<-MD This should be **`a bold codespan`** However, this should be *`an emphasised codespan`* * **`ABC`** or **`DEF`** * Foo bar MD html = <This should be a bold codespan However, this should be an emphasised codespan

HTML output = render_with(Redcarpet::Render::HTML.new, markdown) assert_equal html, output end def test_that_parenthesis_are_handled_into_links markdown = "Hey have a look at the [bash man page](man:bash(1))!" html = "

Hey have a look at the bash man page!

\n" output = render_with(Redcarpet::Render::HTML.new, markdown) assert_equal html, output end def test_autolinking_works_as_expected markdown = "Example of uri ftp://user:pass@example.com/. Email foo@bar.com and link http://bar.com" renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true) output = renderer.render(markdown) assert output.include? 'ftp://user:pass@example.com/' assert output.include? 'mailto:foo@bar.com' assert output.include? '' end def test_that_footnotes_work markdown = <<-MD This is a footnote.[^1] [^1]: It provides additional information. MD html = <This is a footnote.1


  1. It provides additional information. 

HTML renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :footnotes => true) output = renderer.render(markdown) assert_equal html, output end def test_footnotes_enabled_but_missing_marker markdown = <Some text without a marker

[^1] And a trailing definition

HTML renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :footnotes => true) output = renderer.render(markdown) assert_equal html, output end def test_footnotes_enabled_but_missing_definition markdown = "Some text with a marker[^1] but no definition." html = "

Some text with a marker[^1] but no definition.

\n" renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :footnotes => true) output = renderer.render(markdown) assert_equal html, output end def test_autolink_short_domains markdown = "Example of uri ftp://auto/short/domains. Email auto@l.n and link http://a/u/t/o/s/h/o/r/t" renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true) output = renderer.render(markdown) assert output.include? 'ftp://auto/short/domains' assert output.include? 'mailto:auto@l.n' assert output.include? 'http://a/u/t/o/s/h/o/r/t' end def test_toc_heading_id markdown = "# First level heading\n## Second level heading" output = render_with(@rndr[:toc_data], markdown) assert_match /

/, output assert_match /

/, output end def test_that_prettify_works text = <<-Markdown Foo ~~~ruby some code ~~~ Bar Markdown renderer = Redcarpet::Markdown.new(@rndr[:prettify], fenced_code_blocks: true) output = renderer.render(text) assert output.include?("") end def test_safe_links_only_with_anchors markdown = "An [anchor link](#anchor) on a page." renderer = Redcarpet::Markdown.new(@rndr[:safe_links]) output = renderer.render(markdown) assert_match %r{anchor link}, output end def test_autolink_with_link_attributes render = Redcarpet::Render::HTML.new(link_attributes: {rel: "nofollow"}) parser = Redcarpet::Markdown.new(render, autolink: true) output = parser.render("https://github.com/") assert_match %r{rel="nofollow"}, output end end