1 require File.dirname(__FILE__) + '/../test_helper'
 
   3 class RichTextTest < ActiveSupport::TestCase
 
   4   include ActionDispatch::Assertions::SelectorAssertions
 
   7     r = RichText.new("html", "foo http://example.com/ bar")
 
  10       assert_select "a[href='http://example.com/']", 1
 
  11       assert_select "a[rel='nofollow']", 1
 
  14     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
 
  17       assert_select "a[href='http://example.com/']", 1
 
  18       assert_select "a[rel='nofollow']", 1
 
  21     r = RichText.new("html", "foo example@example.com bar")
 
  26     r = RichText.new("html", "foo <a href='mailto:example@example.com'>bar</a> baz")
 
  29       assert_select "a[href='mailto:example@example.com']", 1
 
  30       assert_select "a[rel='nofollow']", 1
 
  33     r = RichText.new("html", "foo <div>bar</div> baz")
 
  35       assert_select "div", false
 
  36       assert_select "p", /^foo *bar *baz$/
 
  39     r = RichText.new("html", "foo <script>bar = 1;</script> baz")
 
  41       assert_select "script", false
 
  42       assert_select "p", /^foo *baz$/
 
  45     r = RichText.new("html", "foo <style>div { display: none; }</style> baz")
 
  47       assert_select "style", false
 
  48       assert_select "p", /^foo *baz$/
 
  52   def test_markdown_to_html
 
  53     r = RichText.new("markdown", "foo http://example.com/ bar")
 
  56       assert_select "a[href='http://example.com/']", 1
 
  57       assert_select "a[rel='nofollow']", 1
 
  60     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
 
  63       assert_select "a[href='http://example.com/']", 1
 
  64       assert_select "a[rel='nofollow']", 1
 
  67     r = RichText.new("markdown", "foo example@example.com bar")
 
  70       assert_select "a[href='mailto:example@example.com']", 1
 
  71       assert_select "a[rel='nofollow']", 1
 
  74     r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar")
 
  77       assert_select "a[href='mailto:example@example.com']", 1
 
  78       assert_select "a[rel='nofollow']", 1
 
  81     r = RichText.new("markdown", "foo  bar")
 
  83       assert_select "img", 1
 
  84       assert_select "img[alt='bar']", 1
 
  85       assert_select "img[src='http://example.com/example.png']", 1
 
  88     r = RichText.new("markdown", "# foo bar baz")
 
  90       assert_select "h1", "foo bar baz"
 
  93     r = RichText.new("markdown", "## foo bar baz")
 
  95       assert_select "h2", "foo bar baz"
 
  98     r = RichText.new("markdown", "### foo bar baz")
 
 100       assert_select "h3", "foo bar baz"
 
 103     r = RichText.new("markdown", "* foo bar baz")
 
 105       assert_select "ul" do
 
 106         assert_select "li", "foo bar baz"
 
 110     r = RichText.new("markdown", "1. foo bar baz")
 
 112       assert_select "ol" do
 
 113         assert_select "li", "foo bar baz"
 
 117     r = RichText.new("markdown", "foo *bar* _baz_ qux")
 
 119       assert_select "em", "bar"
 
 120       assert_select "em", "baz"
 
 123     r = RichText.new("markdown", "foo **bar** __baz__ qux")
 
 125       assert_select "strong", "bar"
 
 126       assert_select "strong", "baz"
 
 129     r = RichText.new("markdown", "foo `bar` baz")
 
 131       assert_select "code", "bar"
 
 134     r = RichText.new("markdown", "    foo bar baz")
 
 136       assert_select "pre", /^\s*foo bar baz\s*$/
 
 140   def test_text_to_html
 
 141     r = RichText.new("text", "foo http://example.com/ bar")
 
 144       assert_select "a[href='http://example.com/']", 1
 
 145       assert_select "a[rel='nofollow']", 1
 
 148     r = RichText.new("text", "foo example@example.com bar")
 
 153     r = RichText.new("text", "foo < bar & baz > qux")
 
 155       assert_select "p", "foo < bar & baz > qux"
 
 161   def assert_html(richtext, &block)
 
 162     html = richtext.to_html
 
 163     assert html.html_safe?
 
 164     root = HTML::Document.new(richtext.to_html, false, true).root
 
 165     assert_select root, "*" do