html2fortitude 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,73 @@
1
+ describe "html2fortitude 'needs' conversion" do
2
+ it "should produce no needs if there are no needs" do
3
+ result = h2f("hello, world")
4
+ expect(result.needs).to eq({ })
5
+ expect(result.content_text).to eq("text \"hello, world\"")
6
+ end
7
+
8
+ it "should produce a single need, mapped to nil, if there's just one" do
9
+ result = h2f("hello, <%= @foo %>")
10
+ expect(result.needs).to eq({ ":foo" => "nil" })
11
+ expect(result.content_text).to eq(%{text "hello, "
12
+ text(foo)})
13
+ end
14
+
15
+ it "should produce multiple needs, mapped to nil, if there are multiple" do
16
+ result = h2f("hello, <%= @foo + @bar %>, <%= @baz %>")
17
+ expect(result.needs).to eq({
18
+ ":foo" => "nil", ":bar" => "nil", ":baz" => "nil"
19
+ })
20
+ expect(result.content_text).to eq(%{text "hello, "
21
+ text(foo + bar)
22
+ text ", "
23
+ text(baz)})
24
+ end
25
+
26
+ it "should produce needs with no mapping if asked to" do
27
+ result = h2f("hello, <%= @foo %>", :assigns => :required_needs)
28
+ expect(result.needs).to eq({ ":foo" => nil })
29
+ expect(result.content_text).to eq(%{text "hello, "
30
+ text(foo)})
31
+ end
32
+
33
+ it "should produce multiple needs with no mapping if asked to" do
34
+ result = h2f("hello, <%= @foo + @bar %>", :assigns => :required_needs)
35
+ expect(result.needs).to eq({ ":foo" => nil, ":bar" => nil })
36
+ expect(result.content_text).to eq(%{text "hello, "
37
+ text(foo + bar)})
38
+ end
39
+
40
+ it "should turn off needs entirely if asked to" do
41
+ result = h2f("hello, <%= @foo + @bar %>", :assigns => :no_needs)
42
+ expect(result.needs).to eq({ })
43
+ expect(result.content_text).to eq(%{text "hello, "
44
+ text(foo + bar)})
45
+ end
46
+
47
+ it "should produce instance variables in text if asked to" do
48
+ result = h2f("hello, <%= @foo + @bar %>", :assigns => :instance_variables)
49
+ expect(result.needs).to eq({ ":foo" => "nil", ":bar" => "nil" })
50
+ expect(result.content_text).to eq(%{text "hello, "
51
+ text(@foo + @bar)})
52
+ end
53
+
54
+ it "should extract 'needs' from loud ERb" do
55
+ result = h2f("hello, <%= @foo %>")
56
+ expect(result.needs).to eq({ ":foo" => "nil" })
57
+ expect(result.content_text).to eq(%{text "hello, "
58
+ text(foo)})
59
+ end
60
+
61
+ it "should extract 'needs' from silent ERb" do
62
+ result = h2f("hello, <% @foo %>")
63
+ expect(result.needs).to eq({ ":foo" => "nil" })
64
+ expect(result.content_text).to eq(%{text "hello, "
65
+ foo})
66
+ end
67
+
68
+ it "should extract 'needs' from code used as a direct argument to a tag" do
69
+ result = h2f("<p><%= @foo %></p>")
70
+ expect(result.needs).to eq({ ":foo" => "nil" })
71
+ expect(result.content_text).to eq(%{p(foo)})
72
+ end
73
+ end
@@ -0,0 +1,43 @@
1
+ describe "html2fortitude options support" do
2
+ it "should generate a class descending from Fortitude::Widget::Html5 by default" do
3
+ result = h2f("hello, world", :class_name => "Some::Class::Name")
4
+ expect(result.class_name).to eq("Some::Class::Name")
5
+ expect(result.superclass).to eq("Fortitude::Widget::Html5")
6
+ end
7
+
8
+ it "should generate a class inheriting from the given class if asked to" do
9
+ result = h2f("hello, world", :class_name => "Some::Other::ClassName", :superclass => "My::Base")
10
+ expect(result.class_name).to eq("Some::Other::ClassName")
11
+ expect(result.superclass).to eq("My::Base")
12
+ end
13
+
14
+ it "should generate a method named 'content' by default" do
15
+ result = h2f("hello, world")
16
+ expect(result.method_name).to eq("content")
17
+ end
18
+
19
+ it "should generate a method with a different name if asked to" do
20
+ result = h2f("hello, world", :method => "foobar")
21
+ expect(result.method_name).to eq("foobar")
22
+ end
23
+
24
+ it "should use { ... } for tag content by default" do
25
+ expect(h2f_content("<p><span>hi</span></p>")).to eq(%{p {
26
+ span("hi")
27
+ }})
28
+ end
29
+
30
+ it "should use do ... end instead of { ... } for tag content if asked to" do
31
+ expect(h2f_content("<p><span>hi</span></p>", :do_end => true)).to eq(%{p do
32
+ span("hi")
33
+ end})
34
+ end
35
+
36
+ it "should use Ruby 1.8-style Hashes by default" do
37
+ expect(h2f_content("<p class=\"foo\"/>")).to eq(%{p(:class => "foo")})
38
+ end
39
+
40
+ it "should use Ruby 1.9-style Hashes if asked to" do
41
+ expect(h2f_content("<p class=\"foo\"/>", :new_style_hashes => true)).to eq(%{p(class: "foo")})
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ describe "html2fortitude 'other stuff' support" do
2
+ it "should emit XML Processing Instructions correctly" do
3
+ expect(h2f_content("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")).to eq(
4
+ %{rawtext("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")})
5
+ end
6
+
7
+ it "should emit CDATA correctly" do
8
+ expect(h2f_content(%{hello <![CDATA[
9
+ foo
10
+ bar
11
+ ]]>
12
+ world})).to eq(%{text "hello "
13
+ cdata <<-END_OF_CDATA_CONTENT
14
+ foo
15
+ bar
16
+ END_OF_CDATA_CONTENT
17
+ text "world"})
18
+ end
19
+
20
+ it "should emit DTDs correctly" do
21
+ expect(h2f_content(%{<!DOCTYPE html>})).to eq(%{doctype!})
22
+ end
23
+
24
+ it "should emit comments correctly" do
25
+ expect(h2f_content(%{hello <!-- something here --> world})).to eq(%{text "hello "
26
+ comment "something here"
27
+ text " world"})
28
+ end
29
+
30
+ it "should emit multiline comments correctly" do
31
+ expect(h2f_content(%{hello <!-- something
32
+ here
33
+ yo --> world})).to eq(%{text "hello "
34
+ comment %{something
35
+ here
36
+ yo}
37
+ text " world"})
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ describe "html2fortitude Rails support" do
2
+ it "should output calls to Rails helpers normally" do
3
+ expect(h2f_content(%{<%= distance_of_time_in_words 5.minutes.from_now %>})).to eq(%{text(distance_of_time_in_words 5.minutes.from_now)})
4
+ end
5
+
6
+ it "should not output calls to Rails helpers that are transformed to output already" do
7
+ expect(h2f_content(%{<%= image_tag 'foo' %>})).to eq(%{image_tag 'foo'})
8
+ end
9
+ end
@@ -0,0 +1,252 @@
1
+ describe "html2fortitude tags handling" do
2
+ it "should render a very simple tag with just its name" do
3
+ expect(h2f_content("<p/>")).to eq(%{p})
4
+ expect(h2f_content("<p></p>")).to eq(%{p})
5
+ end
6
+
7
+ it "should render a very simple tag with attributes" do
8
+ expect(h2f_content(%{<p foo="bar"/>})).to eq(%{p(:foo => "bar")})
9
+ end
10
+
11
+ it "should render several tags back-to-back correctly" do
12
+ expect(h2f_content(%{<p/><br/><p/>})).to eq(%{p
13
+ br
14
+ p})
15
+ end
16
+
17
+ it "should render several tags with attributes back-to-back correctly" do
18
+ expect(h2f_content(%{<p foo="bar"/><br bar="foo"/><p bar="baz"/>})).to eq(%{p(:foo => "bar")
19
+ br(:bar => "foo")
20
+ p(:bar => "baz")})
21
+ end
22
+
23
+ it "should render multiple attributes properly" do
24
+ result = h2f_content(%{<p foo="bar" bar="baz"/>})
25
+
26
+ expect(result).to match(%r{^p\(:[a-z].*"\)$})
27
+ expect(result).to match(%r{:foo => "bar"})
28
+ expect(result).to match(%r{:bar => "baz"})
29
+ end
30
+
31
+ it "should render attributes that aren't simple Symbols properly" do
32
+ expect(h2f_content(%{<p foo-bar="baz"/>})).to eq(%{p("foo-bar" => "baz")})
33
+ end
34
+
35
+ it "should render attributes that have dynamic values properly" do
36
+ expect(h2f_content(%{<p foo="<%= bar %>"})).to eq(%{p(:foo => bar)})
37
+ end
38
+
39
+ it "should render attributes that have partially dynamic values properly" do
40
+ expect(h2f_content(%{<p foo="baz <%= bar %> quux"})).to eq(%{p(:foo => "baz \#{bar} quux")})
41
+ end
42
+
43
+ it "should render attributes that have a mix of dynamic and static content for their values properly" do
44
+ expect(h2f_content(%{<p foo="bar <%= baz %> quux"/>})).to eq(%{p(:foo => "bar \#{baz} quux")})
45
+ end
46
+
47
+ it "should render attributes that have a dynamic key properly" do
48
+ pending "This doesn't work; html2haml's <haml_loud> system fouls up Nokogiri too badly here"
49
+ expect(h2f_content(%{<p <%= foo %>="bar"/>})).to eq(%{p foo => "bar"})
50
+ end
51
+
52
+ it "should render attributes that have a mix of dynamic and static content for their keys properly" do
53
+ pending "This doesn't work; html2haml's <haml_loud> system fouls up Nokogiri too badly here"
54
+ expect(h2f_content(%{<p foo<%= bar %>baz="bar"/>})).to eq(%{p "foo\#{bar}baz" => "bar"})
55
+ end
56
+
57
+ it "should render a tag containing another tag correctly" do
58
+ expect(h2f_content(%{<p><br/></p>})).to eq(%{p {
59
+ br
60
+ }})
61
+ end
62
+
63
+ it "should render a tag containing text correctly" do
64
+ expect(h2f_content(%{<p>hello, world</p>})).to eq(%{p("hello, world")})
65
+ end
66
+
67
+ it "should render a tag containing text with newlines correctly" do
68
+ expect(h2f_content(%{<p>hello,
69
+ world</p>})).to eq(%{p(%{hello,
70
+ world})})
71
+ end
72
+
73
+ it "should render a tag with a single child of loud output correctly" do
74
+ expect(h2f_content(%{<p><%= foo %></p>})).to eq(%{p(foo)})
75
+ end
76
+
77
+ it "should render a tag with multiple children of loud output correctly" do
78
+ expect(h2f_content(%{<p><%= foo %><%= bar %></p>})).to eq(%{p {
79
+ text(foo)
80
+ text(bar)
81
+ }})
82
+ end
83
+
84
+ it "should render a tag with a single child of multiline loud output correctly" do
85
+ expect(h2f_content(%{<p><%=
86
+ x = 123
87
+ x += 200
88
+ x
89
+ %></p>})).to eq(%{p {
90
+ x = 123
91
+ x += 200
92
+ text(x)
93
+ }})
94
+ end
95
+
96
+ it "should render a tag with a single child of loud output with a semicolon correctly" do
97
+ expect(h2f_content(%{<p><%= x = 123; x += 200; x %></p>})).to eq(%{p {
98
+ text(x = 123; x += 200; x)
99
+ }})
100
+ end
101
+
102
+ it "should render a tag with a single child of loud output and attributes correctly" do
103
+ expect(h2f_content(%{<p aaa="bbb" ccc="ddd"><%= foo %></p>})).to eq(%{p(foo, :aaa => "bbb", :ccc => "ddd")})
104
+ end
105
+
106
+ it "should render a tag with a single child of loud output that needs parentheses and attributes correctly" do
107
+ expect(h2f_content(%{<p aaa="bbb" ccc="ddd"><%= foo :bar, :baz %></p>})).to eq(%{p((foo :bar, :baz), :aaa => "bbb", :ccc => "ddd")})
108
+ end
109
+
110
+ it "should render a tag with a single child of quiet output correctly" do
111
+ expect(h2f_content(%{<p><% foo %></p>})).to eq(%{p {
112
+ foo
113
+ }})
114
+ end
115
+
116
+ it "should render a tag with multiple children of quiet output correctly" do
117
+ expect(h2f_content(%{<p><% foo %><% bar %></p>})).to eq(%{p {
118
+ foo
119
+ bar
120
+ }})
121
+ end
122
+
123
+ it "should render a tag with intermixed quiet and loud output correctly" do
124
+ expect(h2f_content(%{<p><% foo %><%= bar %><% baz %><%= quux %></p>})).to eq(%{p {
125
+ foo
126
+ text(bar)
127
+ baz
128
+ text(quux)
129
+ }})
130
+ end
131
+
132
+ it "should turn <script type=\"text/javascript\"> into javascript <<-EOJC ... EOJC" do
133
+ expect(h2f_content(%{<script type="text/javascript">
134
+ foo
135
+ bar
136
+ baz
137
+ </script>})).to eq(%{javascript <<-END_OF_JAVASCRIPT_CONTENT
138
+ foo
139
+ bar
140
+ baz
141
+ END_OF_JAVASCRIPT_CONTENT})
142
+ end
143
+
144
+ it "should remove CDATA from within 'javascript'" do
145
+ expect(h2f_content(%{<script type="text/javascript">
146
+ <![CDATA[
147
+ foo
148
+ bar
149
+ baz
150
+ ]]>
151
+ </script>})).to eq(%{javascript <<-END_OF_JAVASCRIPT_CONTENT
152
+ foo
153
+ bar
154
+ baz
155
+ END_OF_JAVASCRIPT_CONTENT})
156
+ end
157
+
158
+ it "should turn <script language=\"javascript\"> into javascript <<-EOJC ... EOJC" do
159
+ expect(h2f_content(%{<script language="javascript">
160
+ foo
161
+ bar
162
+ baz
163
+ </script>})).to eq(%{javascript <<-END_OF_JAVASCRIPT_CONTENT
164
+ foo
165
+ bar
166
+ baz
167
+ END_OF_JAVASCRIPT_CONTENT})
168
+ end
169
+
170
+ it "should turn <script type=\"text/javascript\"> with additional attributes into javascript <<-EOJC ... EOJC with those attributes" do
171
+ expect(h2f_content(%{<script type="text/javascript" id="bar">
172
+ foo
173
+ bar
174
+ baz
175
+ </script>})).to eq(%{javascript <<-END_OF_JAVASCRIPT_CONTENT, :id => "bar"
176
+ foo
177
+ bar
178
+ baz
179
+ END_OF_JAVASCRIPT_CONTENT})
180
+ end
181
+
182
+ it "should turn other <script> blocks into just script <<-EOSC ... EOSC" do
183
+ expect(h2f_content(%{<script type="text/vbscript">
184
+ foo
185
+ bar
186
+ baz
187
+ </script>})).to eq(%{script <<-END_OF_SCRIPT_CONTENT, :type => "text/vbscript"
188
+ foo
189
+ bar
190
+ baz
191
+ END_OF_SCRIPT_CONTENT})
192
+ end
193
+
194
+ it "should include attributes in other <script> blocks" do
195
+ result = h2f_content(%{<script type="text/vbscript" id="bar">
196
+ foo
197
+ bar
198
+ baz
199
+ </script>})
200
+
201
+ if result =~ /^script <<-END_OF_SCRIPT_CONTENT, (.*)$/
202
+ attributes = $1
203
+
204
+ expect([
205
+ %{:type => "text/vbscript", :id => "bar"},
206
+ %{:id => "bar", :type => "text/vbscript"}
207
+ ]).to be_include(attributes)
208
+ else
209
+ raise "No match: #{script}"
210
+ end
211
+
212
+ expect(result).to match(/^script <<-END_OF_SCRIPT_CONTENT, .*\nfoo\nbar\nbaz\nEND_OF_SCRIPT_CONTENT$/mi)
213
+ end
214
+
215
+ it "should turn <style> tags into style <<-EOSC ... EOSC" do
216
+ expect(h2f_content(%{<style>
217
+ foo
218
+ bar
219
+ baz
220
+ </style>})).to eq(%{style <<-END_OF_STYLE_CONTENT
221
+ foo
222
+ bar
223
+ baz
224
+ END_OF_STYLE_CONTENT})
225
+ end
226
+
227
+ it "should remove CDATA from within 'style'" do
228
+ expect(h2f_content(%{<style type="text/css">
229
+ <![CDATA[
230
+ foo
231
+ bar
232
+ baz
233
+ ]]>
234
+ </style>})).to eq(%{style <<-END_OF_STYLE_CONTENT, :type => "text/css"
235
+ foo
236
+ bar
237
+ baz
238
+ END_OF_STYLE_CONTENT})
239
+ end
240
+
241
+ it "should turn <style> tags with an attribute into style <<-EOSC, <attribute> ... EOSC" do
242
+ expect(h2f_content(%{<style id="foo">
243
+ foo
244
+ bar
245
+ baz
246
+ </style>})).to eq(%{style <<-END_OF_STYLE_CONTENT, :id => "foo"
247
+ foo
248
+ bar
249
+ baz
250
+ END_OF_STYLE_CONTENT})
251
+ end
252
+ end
@@ -0,0 +1,106 @@
1
+ describe "html2fortitude text translation" do
2
+ it "should render simple text with #text" do
3
+ expect(h2f_content("hello, world")).to eq(%{text "hello, world"})
4
+ end
5
+
6
+ it "should escape '\#{' sequences in its input" do
7
+ expect(h2f_content("hello, \#{world}")).to eq(%{text "hello, \\\#{world}"})
8
+ end
9
+
10
+ it "should properly escape text that requires escaping" do
11
+ expect(h2f_content("hello, \"world")).to eq(%{text "hello, \\\"world"})
12
+ end
13
+
14
+ it "should not escape text that doesn't require escaping" do
15
+ expect(h2f_content("hello, \'world")).to eq(%{text "hello, 'world"})
16
+ end
17
+
18
+ it "should properly handle multiline text" do
19
+ expect(h2f_content(%{hello
20
+
21
+ world})).to eq(%{text %{hello
22
+
23
+ world}})
24
+ end
25
+
26
+ it "should properly handle multiline text with a } in it" do
27
+ result = h2f_content(<<-EOS)
28
+ hello
29
+ }
30
+ world
31
+ EOS
32
+ expect(result).to eq(<<-EOS.rstrip)
33
+ text %{hello
34
+ \\}
35
+ world}
36
+ EOS
37
+ end
38
+
39
+ it "should not output empty space" do
40
+ expect(h2f_content("")).to eq("")
41
+ expect(h2f_content(" ")).to eq("")
42
+ end
43
+
44
+ it "should allow dynamic content in text" do
45
+ expect(h2f_content("hello, <% abc %> world")).to eq(
46
+ %{text "hello, "
47
+ abc
48
+ text " world"})
49
+ end
50
+
51
+ it "should allow loud dynamic content in text" do
52
+ expect(h2f_content("hello, <%= abc %> world")).to eq(%{text "hello, "
53
+ text(abc)
54
+ text " world"})
55
+ end
56
+
57
+ it "should allow multiline dynamic content in text" do
58
+ expect(h2f_content(%{hello, <% abc
59
+ def(a, "b")
60
+ ghi %>
61
+ world})).to eq(%{text "hello, "
62
+ abc
63
+ def(a, "b")
64
+ ghi
65
+
66
+ text "world"})
67
+ end
68
+
69
+ it "should allow loud multiline dynamic content in text" do
70
+ expect(h2f_content(%{hello, <%= abc
71
+ def(a, "b")
72
+ ghi %>
73
+ world})).to eq(%{text "hello, "
74
+ abc
75
+ def(a, "b")
76
+ text(ghi)
77
+
78
+ text "world"})
79
+ end
80
+
81
+ it "should eliminate whitespace before a tag" do
82
+ expect(h2f_content(%{bar
83
+
84
+
85
+ <p/>})).to eq(%{text "bar"
86
+
87
+
88
+
89
+ p})
90
+ end
91
+
92
+
93
+ it "should eliminate whitespace after a tag" do
94
+ expect(h2f_content(%{<p/>
95
+
96
+
97
+ bar})).to eq(%{p
98
+
99
+
100
+ text "bar"})
101
+ end
102
+
103
+ it "should skip 'text'/'rawtext' when calling 'render'" do
104
+ expect(h2f_content(%{<%= render :partial => 'foo/bar' %>})).to eq(%{render :partial => 'foo/bar'})
105
+ end
106
+ end