require "test_helper" class HTML::Pipeline::MentionFilterTest < Test::Unit::TestCase def filter(html, base_url='/', info_url=nil) HTML::Pipeline::MentionFilter.call(html, :base_url => base_url, :info_url => info_url) end def test_filtering_a_documentfragment body = "

@kneath: check it out.

" doc = Nokogiri::HTML::DocumentFragment.parse(body) res = filter(doc, '/') assert_same doc, res link = "@kneath" assert_equal "

#{link}: check it out.

", res.to_html end def test_filtering_plain_text body = "

@kneath: check it out.

" res = filter(body, '/') link = "@kneath" assert_equal "

#{link}: check it out.

", res.to_html end def test_not_replacing_mentions_in_pre_tags body = "
@kneath: okay
" assert_equal body, filter(body).to_html end def test_not_replacing_mentions_in_code_tags body = "

@kneath: okay

" assert_equal body, filter(body).to_html end def test_not_replacing_mentions_in_links body = "

@kneath okay

" assert_equal body, filter(body).to_html end def test_entity_encoding_and_whatnot body = "

@kneath what's up

" link = "@kneath" assert_equal "

#{link} what's up

", filter(body, '/').to_html end def test_html_injection body = "

@kneath <script>alert(0)</script>

" link = "@kneath" assert_equal "

#{link} <script>alert(0)</script>

", filter(body, '/').to_html end def test_links_to_nothing_when_no_info_url_given body = "

How do I @mention someone?

" assert_equal "

How do I @mention someone?

", filter(body, '/').to_html end def test_links_to_more_info_when_info_url_given body = "

How do I @mention someone?

" link = "@mention" assert_equal "

How do I #{link} someone?

", filter(body, '/', 'https://github.com/blog/821').to_html end MarkdownPipeline = HTML::Pipeline.new [ HTML::Pipeline::MarkdownFilter, HTML::Pipeline::MentionFilter ] def mentioned_usernames result = {} MarkdownPipeline.call(@body, {}, result) html = result[:output].to_html users = html.scan(/user-mention">@(.+?)