3 class RichTextTest < ActiveSupport::TestCase
4 include Rails::Dom::Testing::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 noopener noreferrer']", 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 noopener noreferrer']", 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 noopener noreferrer']", 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$/
51 r = RichText.new("html", "<table><tr><td>column</td></tr></table>")
53 assert_select "table[class='table table-sm w-auto']"
58 r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
59 assert_equal "foo <a href='http://example.com/'>bar</a> baz", r.to_text
62 def test_html_spam_score
63 r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
64 assert_equal 55, r.spam_score.round
67 def test_markdown_to_html
68 r = RichText.new("markdown", "foo http://example.com/ bar")
71 assert_select "a[href='http://example.com/']", 1
72 assert_select "a[rel='nofollow noopener noreferrer']", 1
75 r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
78 assert_select "a[href='http://example.com/']", 1
79 assert_select "a[rel='nofollow noopener noreferrer']", 1
82 r = RichText.new("markdown", "foo example@example.com bar")
85 assert_select "a[href='mailto:example@example.com']", 1
86 assert_select "a[rel='nofollow noopener noreferrer']", 1
89 r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar")
92 assert_select "a[href='mailto:example@example.com']", 1
93 assert_select "a[rel='nofollow noopener noreferrer']", 1
96 r = RichText.new("markdown", "foo  bar")
98 assert_select "img", 1
99 assert_select "img[alt='bar']", 1
100 assert_select "img[src='http://example.com/example.png']", 1
103 r = RichText.new("markdown", "# foo bar baz")
105 assert_select "h1", "foo bar baz"
108 r = RichText.new("markdown", "## foo bar baz")
110 assert_select "h2", "foo bar baz"
113 r = RichText.new("markdown", "### foo bar baz")
115 assert_select "h3", "foo bar baz"
118 r = RichText.new("markdown", "* foo bar baz")
120 assert_select "ul" do
121 assert_select "li", "foo bar baz"
125 r = RichText.new("markdown", "1. foo bar baz")
127 assert_select "ol" do
128 assert_select "li", "foo bar baz"
132 r = RichText.new("markdown", "foo *bar* _baz_ qux")
134 assert_select "em", "bar"
135 assert_select "em", "baz"
138 r = RichText.new("markdown", "foo **bar** __baz__ qux")
140 assert_select "strong", "bar"
141 assert_select "strong", "baz"
144 r = RichText.new("markdown", "foo `bar` baz")
146 assert_select "code", "bar"
149 r = RichText.new("markdown", " foo bar baz")
151 assert_select "pre", /^\s*foo bar baz\s*$/
154 r = RichText.new("markdown", "|column|column")
156 assert_select "table[class='table table-sm w-auto']"
160 def test_markdown_to_text
161 r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
162 assert_equal "foo [bar](http://example.com/) baz", r.to_text
165 def test_markdown_spam_score
166 r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
167 assert_equal 50, r.spam_score.round
170 def test_text_to_html
171 r = RichText.new("text", "foo http://example.com/ bar")
174 assert_select "a[href='http://example.com/']", 1
175 assert_select "a[rel='nofollow noopener noreferrer']", 1
178 r = RichText.new("text", "foo example@example.com bar")
183 r = RichText.new("text", "foo < bar & baz > qux")
185 assert_select "p", "foo < bar & baz > qux"
189 def test_text_to_text
190 r = RichText.new("text", "foo http://example.com/ bar")
191 assert_equal "foo http://example.com/ bar", r.to_text
194 def test_text_spam_score
195 r = RichText.new("text", "foo http://example.com/ bar")
196 assert_equal 141, r.spam_score.round
201 def assert_html(richtext, &block)
202 html = richtext.to_html
203 assert html.html_safe?
204 root = Nokogiri::HTML::DocumentFragment.parse(html)
205 assert_select root, "*" do