]> git.openstreetmap.org Git - rails.git/blob - test/lib/rich_text_test.rb
9ba0f7dcb352b8cc51ae291d7bdffd9c655eb1ca
[rails.git] / test / lib / rich_text_test.rb
1 require "test_helper"
2
3 class RichTextTest < ActiveSupport::TestCase
4   include Rails::Dom::Testing::Assertions::SelectorAssertions
5
6   def test_html_to_html
7     r = RichText.new("html", "foo http://example.com/ bar")
8     assert_html r do
9       assert_select "a", 1
10       assert_select "a[href='http://example.com/']", 1
11       assert_select "a[rel='nofollow']", 1
12     end
13
14     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
15     assert_html r do
16       assert_select "a", 1
17       assert_select "a[href='http://example.com/']", 1
18       assert_select "a[rel='nofollow']", 1
19     end
20
21     r = RichText.new("html", "foo example@example.com bar")
22     assert_html r do
23       assert_select "a", 0
24     end
25
26     r = RichText.new("html", "foo <a href='mailto:example@example.com'>bar</a> baz")
27     assert_html r do
28       assert_select "a", 1
29       assert_select "a[href='mailto:example@example.com']", 1
30       assert_select "a[rel='nofollow']", 1
31     end
32
33     r = RichText.new("html", "foo <div>bar</div> baz")
34     assert_html r do
35       assert_select "div", false
36       assert_select "p", /^foo *bar *baz$/
37     end
38
39     r = RichText.new("html", "foo <script>bar = 1;</script> baz")
40     assert_html r do
41       assert_select "script", false
42       assert_select "p", /^foo *baz$/
43     end
44
45     r = RichText.new("html", "foo <style>div { display: none; }</style> baz")
46     assert_html r do
47       assert_select "style", false
48       assert_select "p", /^foo *baz$/
49     end
50   end
51
52   def test_markdown_to_html
53     r = RichText.new("markdown", "foo http://example.com/ bar")
54     assert_html r do
55       assert_select "a", 1
56       assert_select "a[href='http://example.com/']", 1
57       assert_select "a[rel='nofollow']", 1
58     end
59
60     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
61     assert_html r do
62       assert_select "a", 1
63       assert_select "a[href='http://example.com/']", 1
64       assert_select "a[rel='nofollow']", 1
65     end
66
67     r = RichText.new("markdown", "foo example@example.com bar")
68     assert_html r do
69       assert_select "a", 1
70       assert_select "a[href='mailto:example@example.com']", 1
71       assert_select "a[rel='nofollow']", 1
72     end
73
74     r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar")
75     assert_html r do
76       assert_select "a", 1
77       assert_select "a[href='mailto:example@example.com']", 1
78       assert_select "a[rel='nofollow']", 1
79     end
80
81     r = RichText.new("markdown", "foo ![bar](http://example.com/example.png) bar")
82     assert_html r do
83       assert_select "img", 1
84       assert_select "img[alt='bar']", 1
85       assert_select "img[src='http://example.com/example.png']", 1
86     end
87
88     r = RichText.new("markdown", "# foo bar baz")
89     assert_html r do
90       assert_select "h1", "foo bar baz"
91     end
92
93     r = RichText.new("markdown", "## foo bar baz")
94     assert_html r do
95       assert_select "h2", "foo bar baz"
96     end
97
98     r = RichText.new("markdown", "### foo bar baz")
99     assert_html r do
100       assert_select "h3", "foo bar baz"
101     end
102
103     r = RichText.new("markdown", "* foo bar baz")
104     assert_html r do
105       assert_select "ul" do
106         assert_select "li", "foo bar baz"
107       end
108     end
109
110     r = RichText.new("markdown", "1. foo bar baz")
111     assert_html r do
112       assert_select "ol" do
113         assert_select "li", "foo bar baz"
114       end
115     end
116
117     r = RichText.new("markdown", "foo *bar* _baz_ qux")
118     assert_html r do
119       assert_select "em", "bar"
120       assert_select "em", "baz"
121     end
122
123     r = RichText.new("markdown", "foo **bar** __baz__ qux")
124     assert_html r do
125       assert_select "strong", "bar"
126       assert_select "strong", "baz"
127     end
128
129     r = RichText.new("markdown", "foo `bar` baz")
130     assert_html r do
131       assert_select "code", "bar"
132     end
133
134     r = RichText.new("markdown", "    foo bar baz")
135     assert_html r do
136       assert_select "pre", /^\s*foo bar baz\s*$/
137     end
138   end
139
140   def test_text_to_html
141     r = RichText.new("text", "foo http://example.com/ bar")
142     assert_html r do
143       assert_select "a", 1
144       assert_select "a[href='http://example.com/']", 1
145       assert_select "a[rel='nofollow']", 1
146     end
147
148     r = RichText.new("text", "foo example@example.com bar")
149     assert_html r do
150       assert_select "a", 0
151     end
152
153     r = RichText.new("text", "foo < bar & baz > qux")
154     assert_html r do
155       assert_select "p", "foo < bar & baz > qux"
156     end
157   end
158
159   private
160
161   def assert_html(richtext, &block)
162     html = richtext.to_html
163     assert html.html_safe?
164     root = Nokogiri::HTML::DocumentFragment.parse(html)
165     assert_select root, "*" do
166       yield block
167     end
168   end
169 end