haml 5.0.1 → 5.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,262 +0,0 @@
1
- require 'test_helper'
2
-
3
- class FiltersTest < Haml::TestCase
4
- test "should be registered as filters when including Haml::Filters::Base" do
5
- begin
6
- refute Haml::Filters.defined.has_key? "bar"
7
- Module.new {def self.name; "Foo::Bar"; end; include Haml::Filters::Base}
8
- assert Haml::Filters.defined.has_key? "bar"
9
- ensure
10
- Haml::Filters.remove_filter "Bar"
11
- end
12
- end
13
-
14
- test "should raise error when attempting to register a defined Tilt filter" do
15
- begin
16
- assert_raises RuntimeError do
17
- 2.times do
18
- Haml::Filters.register_tilt_filter "Foo"
19
- end
20
- end
21
- ensure
22
- Haml::Filters.remove_filter "Foo"
23
- end
24
- end
25
-
26
- test "should raise error when a Tilt filters dependencies are unavailable for extension" do
27
- begin
28
- assert_raises Haml::Error do
29
- # ignore warnings from Tilt
30
- silence_warnings do
31
- Haml::Filters.register_tilt_filter "Textile"
32
- Haml::Filters.defined["textile"].template_class
33
- end
34
- end
35
- ensure
36
- Haml::Filters.remove_filter "Textile"
37
- end
38
- end
39
-
40
- test "should raise error when a Tilt filters dependencies are unavailable for filter without extension" do
41
- begin
42
- assert_raises Haml::Error do
43
- Haml::Filters.register_tilt_filter "Maruku"
44
- Haml::Filters.defined["maruku"].template_class
45
- end
46
- ensure
47
- Haml::Filters.remove_filter "Maruku"
48
- end
49
- end
50
-
51
- test "should raise informative error about Maruku being moved to haml-contrib" do
52
- begin
53
- render(":maruku\n # foo")
54
- flunk("Should have raised error with message about the haml-contrib gem.")
55
- rescue Haml::Error => e
56
- assert_equal e.message, Haml::Error.message(:install_haml_contrib, "maruku")
57
- end
58
- end
59
-
60
- test "should raise informative error about Textile being moved to haml-contrib" do
61
- begin
62
- render(":textile\n h1. foo")
63
- flunk("Should have raised error with message about the haml-contrib gem.")
64
- rescue Haml::Error => e
65
- assert_equal e.message, Haml::Error.message(:install_haml_contrib, "textile")
66
- end
67
- end
68
-
69
- test "should respect escaped newlines and interpolation" do
70
- html = "\\n\n\n"
71
- haml = ":plain\n \\n\#{""}"
72
- assert_equal(html, render(haml))
73
- end
74
-
75
- test "should process an filter with no content" do
76
- assert_equal("\n", render(':plain'))
77
- end
78
-
79
- test ":plain with content" do
80
- expectation = "foo\n"
81
- assert_equal(expectation, render(":plain\n foo"))
82
- end
83
-
84
- test "should pass options to Tilt filters that precompile" do
85
- begin
86
- orig_erb_opts = Haml::Filters::Erb.options
87
- haml = ":erb\n <%= 'foo' %>"
88
- refute_match('test_var', Haml::Engine.new(haml).compiler.precompiled)
89
- Haml::Filters::Erb.options = {:outvar => 'test_var'}
90
- assert_match('test_var', Haml::Engine.new(haml).compiler.precompiled)
91
- ensure
92
- Haml::Filters::Erb.options = orig_erb_opts
93
- end
94
- end
95
-
96
- test "should pass options to Tilt filters that don't precompile" do
97
- begin
98
- filter = Class.new(Tilt::Template) do
99
- def self.name
100
- "Foo"
101
- end
102
-
103
- def prepare
104
- @engine = {:data => data, :options => options}
105
- end
106
-
107
- def evaluate(scope, locals, &block)
108
- @output = @engine[:options].to_a.join
109
- end
110
- end
111
- Haml::Filters.register_tilt_filter "Foo", :template_class => filter
112
- Haml::Filters::Foo.options[:foo] = "bar"
113
- haml = ":foo"
114
- assert_equal "foobar\n", render(haml)
115
- ensure
116
- Haml::Filters.remove_filter "Foo"
117
- end
118
- end
119
-
120
- test "interpolated code should be escaped if escape_html is set" do
121
- assert_equal "&lt;script&gt;evil&lt;/script&gt;\n\n",
122
- render(":plain\n \#{'<script>evil</script>'}", :escape_html => true)
123
- end
124
-
125
- end
126
-
127
- class ErbFilterTest < Haml::TestCase
128
- test "multiline expressions should work" do
129
- html = "foobarbaz\n\n"
130
- haml = %Q{:erb\n <%= "foo" +\n "bar" +\n "baz" %>}
131
- assert_equal(html, render(haml))
132
- end
133
-
134
- test "should evaluate in the same context as Haml" do
135
- haml = ":erb\n <%= foo %>"
136
- html = "bar\n\n"
137
- scope = Object.new.instance_eval {foo = "bar"; nil if foo; binding}
138
- assert_equal(html, render(haml, :scope => scope))
139
- end
140
-
141
- end
142
-
143
- class JavascriptFilterTest < Haml::TestCase
144
- test "should interpolate" do
145
- scope = Object.new.instance_eval {foo = "bar"; nil if foo; binding}
146
- haml = ":javascript\n \#{foo}"
147
- html = render(haml, :scope => scope)
148
- assert_match(/bar/, html)
149
- end
150
-
151
- test "should never HTML-escape non-interpolated ampersands" do
152
- html = "<script>\n & < > &amp;\n</script>\n"
153
- haml = %Q{:javascript\n & < > \#{"&"}}
154
- assert_equal(html, render(haml, :escape_html => true))
155
- end
156
-
157
- test "should not include type in HTML 5 output" do
158
- html = "<script>\n foo bar\n</script>\n"
159
- haml = ":javascript\n foo bar"
160
- assert_equal(html, render(haml, :format => :html5))
161
- end
162
-
163
- test "should always include CDATA when format is xhtml" do
164
- html = "<script type='text/javascript'>\n //<![CDATA[\n foo bar\n //]]>\n</script>\n"
165
- haml = ":javascript\n foo bar"
166
- assert_equal(html, render(haml, :format => :xhtml, :cdata => false))
167
- end
168
-
169
- test "should omit CDATA when cdata option is false" do
170
- html = "<script>\n foo bar\n</script>\n"
171
- haml = ":javascript\n foo bar"
172
- assert_equal(html, render(haml, :format => :html5, :cdata => false))
173
- end
174
-
175
- test "should include CDATA when cdata option is true" do
176
- html = "<script>\n //<![CDATA[\n foo bar\n //]]>\n</script>\n"
177
- haml = ":javascript\n foo bar"
178
- assert_equal(html, render(haml, :format => :html5, :cdata => true))
179
- end
180
-
181
- test "should default to no CDATA when format is html5" do
182
- haml = ":javascript\n foo bar"
183
- out = render(haml, :format => :html5)
184
- refute_match('//<![CDATA[', out)
185
- refute_match('//]]>', out)
186
- end
187
- end
188
-
189
- class CSSFilterTest < Haml::TestCase
190
- test "should wrap output in CDATA and a CSS tag when output is XHTML" do
191
- html = "<style type='text/css'>\n /*<![CDATA[*/\n foo\n /*]]>*/\n</style>\n"
192
- haml = ":css\n foo"
193
- assert_equal(html, render(haml, :format => :xhtml))
194
- end
195
-
196
- test "should not include type in HTML 5 output" do
197
- html = "<style>\n foo bar\n</style>\n"
198
- haml = ":css\n foo bar"
199
- assert_equal(html, render(haml, :format => :html5))
200
- end
201
-
202
- test "should always include CDATA when format is xhtml" do
203
- html = "<style type='text/css'>\n /*<![CDATA[*/\n foo bar\n /*]]>*/\n</style>\n"
204
- haml = ":css\n foo bar"
205
- assert_equal(html, render(haml, :format => :xhtml, :cdata => false))
206
- end
207
-
208
- test "should omit CDATA when cdata option is false" do
209
- html = "<style>\n foo bar\n</style>\n"
210
- haml = ":css\n foo bar"
211
- assert_equal(html, render(haml, :format => :html5, :cdata => false))
212
- end
213
-
214
- test "should include CDATA when cdata option is true" do
215
- html = "<style>\n /*<![CDATA[*/\n foo bar\n /*]]>*/\n</style>\n"
216
- haml = ":css\n foo bar"
217
- assert_equal(html, render(haml, :format => :html5, :cdata => true))
218
- end
219
-
220
- test "should default to no CDATA when format is html5" do
221
- haml = ":css\n foo bar"
222
- out = render(haml, :format => :html5)
223
- refute_match('<![CDATA[', out)
224
- refute_match(']]>', out)
225
- end
226
- end
227
-
228
- class CDATAFilterTest < Haml::TestCase
229
- test "should wrap output in CDATA tag" do
230
- html = "<![CDATA[\n foo\n]]>\n"
231
- haml = ":cdata\n foo"
232
- assert_equal(html, render(haml))
233
- end
234
- end
235
-
236
- class EscapedFilterTest < Haml::TestCase
237
- test "should escape ampersands" do
238
- html = "&amp;\n"
239
- haml = ":escaped\n &"
240
- assert_equal(html, render(haml))
241
- end
242
- end
243
-
244
- class RubyFilterTest < Haml::TestCase
245
- test "can write to haml_io" do
246
- haml = ":ruby\n haml_io.puts 'hello'\n"
247
- html = "hello\n"
248
- assert_equal(html, render(haml))
249
- end
250
-
251
- test "haml_io appends to output" do
252
- haml = "hello\n:ruby\n haml_io.puts 'hello'\n"
253
- html = "hello\nhello\n"
254
- assert_equal(html, render(haml))
255
- end
256
-
257
- test "can create local variables" do
258
- haml = ":ruby\n a = 7\n=a"
259
- html = "7\n"
260
- assert_equal(html, render(haml))
261
- end
262
- end
@@ -1,695 +0,0 @@
1
- require 'test_helper'
2
- require "active_model/naming"
3
-
4
- class FormModel
5
- extend ActiveModel::Naming
6
- end
7
-
8
- class HelperTest < Haml::TestCase
9
- TEXT_AREA_CONTENT_REGEX = /<(textarea)[^>]*>\n(.*?)<\/\1>/im
10
-
11
- Post = Struct.new('Post', :body, :error_field, :errors)
12
- class PostErrors
13
- def on(name)
14
- return unless name == 'error_field'
15
- ["Really bad error"]
16
- end
17
- alias_method :full_messages, :on
18
-
19
- def [](name)
20
- on(name) || []
21
- end
22
- end
23
-
24
- def setup
25
- @base = Class.new(ActionView::Base) {
26
- def nested_tag
27
- content_tag(:span) {content_tag(:div) {"something"}}
28
- end
29
-
30
- def wacky_form
31
- form_tag("/foo") {"bar"}
32
- end
33
- }.new
34
- @base.controller = ActionController::Base.new
35
- @base.view_paths << File.expand_path("../templates", __FILE__)
36
- @base.instance_variable_set(:@post, Post.new("Foo bar\nbaz", nil, PostErrors.new))
37
- end
38
-
39
- def render(text, options = {})
40
- return @base.render :inline => text, :type => :haml if options == :action_view
41
- super
42
- end
43
-
44
- def test_rendering_with_escapes
45
- def @base.render_something_with_haml_concat
46
- haml_concat "<p>"
47
- end
48
- def @base.render_something_with_haml_tag_and_concat
49
- haml_tag 'p' do
50
- haml_concat '<foo>'
51
- end
52
- end
53
-
54
- output = render(<<-HAML, :action_view)
55
- - render_something_with_haml_concat
56
- - render_something_with_haml_tag_and_concat
57
- - render_something_with_haml_concat
58
- HAML
59
- assert_equal("&lt;p&gt;\n<p>\n &lt;foo&gt;\n</p>\n&lt;p&gt;\n", output)
60
- end
61
-
62
- def test_with_raw_haml_concat
63
- haml = <<HAML
64
- - with_raw_haml_concat do
65
- - haml_concat "<>&"
66
- HAML
67
- assert_equal("<>&\n", render(haml, :action_view))
68
- end
69
-
70
- def test_flatten
71
- assert_equal("FooBar", Haml::Helpers.flatten("FooBar"))
72
-
73
- assert_equal("FooBar", Haml::Helpers.flatten("Foo\rBar"))
74
-
75
- assert_equal("Foo&#x000A;Bar", Haml::Helpers.flatten("Foo\nBar"))
76
-
77
- assert_equal("Hello&#x000A;World!&#x000A;YOU ARE FLAT?&#x000A;OMGZ!",
78
- Haml::Helpers.flatten("Hello\nWorld!\nYOU ARE \rFLAT?\n\rOMGZ!"))
79
- end
80
-
81
- def test_list_of_should_render_correctly
82
- assert_equal("<li>1</li>\n<li>2</li>", render("= list_of([1, 2]) do |i|\n = i"))
83
- assert_equal("<li>[1]</li>", render("= list_of([[1]]) do |i|\n = i.inspect"))
84
- assert_equal("<li>\n <h1>Fee</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fi</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fo</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fum</h1>\n <p>A word!</p>\n</li>",
85
- render("= list_of(['Fee', 'Fi', 'Fo', 'Fum']) do |title|\n %h1= title\n %p A word!"))
86
- assert_equal("<li c='3'>1</li>\n<li c='3'>2</li>", render("= list_of([1, 2], {:c => 3}) do |i|\n = i"))
87
- assert_equal("<li c='3'>[1]</li>", render("= list_of([[1]], {:c => 3}) do |i|\n = i.inspect"))
88
- assert_equal("<li c='3'>\n <h1>Fee</h1>\n <p>A word!</p>\n</li>\n<li c='3'>\n <h1>Fi</h1>\n <p>A word!</p>\n</li>\n<li c='3'>\n <h1>Fo</h1>\n <p>A word!</p>\n</li>\n<li c='3'>\n <h1>Fum</h1>\n <p>A word!</p>\n</li>",
89
- render("= list_of(['Fee', 'Fi', 'Fo', 'Fum'], {:c => 3}) do |title|\n %h1= title\n %p A word!"))
90
- end
91
-
92
- def test_buffer_access
93
- assert(render("= buffer") =~ /#<Haml::Buffer:0x[a-z0-9]+>/)
94
- assert_equal(render("= (buffer == _hamlout)"), "true\n")
95
- end
96
-
97
- def test_tabs
98
- assert_equal("foo\nbar\nbaz\n", render("foo\n- tab_up\nbar\n- tab_down\nbaz"))
99
- assert_equal("<p>tabbed</p>\n", render("- buffer.tabulation=5\n%p tabbed"))
100
- end
101
-
102
- def test_with_tabs
103
- assert_equal(<<HTML, render(<<HAML))
104
- Foo
105
- Bar
106
- Baz
107
- Baz
108
- HTML
109
- Foo
110
- - with_tabs 2 do
111
- = "Bar\\nBaz"
112
- Baz
113
- HAML
114
- end
115
-
116
- def test_helpers_dont_leak
117
- # Haml helpers shouldn't be accessible from ERB
118
- render("foo")
119
- proper_behavior = false
120
-
121
- begin
122
- ActionView::Base.new.render(:inline => "<%= flatten('Foo\\nBar') %>")
123
- rescue NoMethodError, ActionView::Template::Error
124
- proper_behavior = true
125
- end
126
- assert(proper_behavior)
127
-
128
- begin
129
- ActionView::Base.new.render(:inline => "<%= concat('foo') %>")
130
- rescue ArgumentError, NameError
131
- proper_behavior = true
132
- end
133
- assert(proper_behavior)
134
- end
135
-
136
- def test_action_view_included
137
- assert(Haml::Helpers.action_view?)
138
- end
139
-
140
- def test_form_tag
141
- def @base.protect_against_forgery?; false; end
142
- rendered = render(<<HAML, :action_view)
143
- = form_tag 'foo' do
144
- %p bar
145
- %strong baz
146
- HAML
147
- fragment = Nokogiri::HTML.fragment(rendered)
148
- assert_equal 'foo', fragment.css('form').first.attributes['action'].to_s
149
- assert_equal 'bar', fragment.css('form p').first.text.strip
150
- assert_equal 'baz', fragment.css('form strong').first.text.strip
151
- end
152
-
153
- def test_form_for
154
- # FIXME: current HAML doesn't do proper indentation with form_for (it's the capture { output } in #form_for).
155
- def @base.protect_against_forgery?; false; end
156
- rendered = render(<<HAML, :action_view)
157
- = form_for OpenStruct.new, url: 'foo', as: :post do |f|
158
- = f.text_field :name
159
- HAML
160
- assert_match(/<(form|div)[^>]+><input/, rendered)
161
- end
162
-
163
- def test_pre
164
- assert_equal(%(<pre>Foo bar&#x000A; baz</pre>\n),
165
- render('= content_tag "pre", "Foo bar\n baz"', :action_view))
166
- end
167
-
168
- def test_text_area_tag
169
- output = render('= text_area_tag "body", "Foo\nBar\n Baz\n Boom"', :action_view)
170
- match_data = output.match(TEXT_AREA_CONTENT_REGEX)
171
- assert_equal "Foo&#x000A;Bar&#x000A; Baz&#x000A; Boom", match_data[2]
172
- end
173
-
174
- def test_text_area
175
- output = render('= text_area :post, :body', :action_view)
176
- match_data = output.match(TEXT_AREA_CONTENT_REGEX)
177
- assert_equal "Foo bar&#x000A;baz", match_data[2]
178
- end
179
-
180
- def test_partials_should_not_cause_textareas_to_be_indented
181
- # non-indentation of textareas rendered inside partials
182
- @base.instance_variable_set(:@post, Post.new("Foo", nil, PostErrors.new))
183
- output = render(".foo\n .bar\n = render '/text_area_helper'", :action_view)
184
- match_data = output.match(TEXT_AREA_CONTENT_REGEX)
185
- assert_equal 'Foo', match_data[2]
186
- end
187
-
188
- def test_textareas_should_preserve_leading_whitespace
189
- # leading whitespace preservation
190
- @base.instance_variable_set(:@post, Post.new(" Foo", nil, PostErrors.new))
191
- output = render(".foo\n = text_area :post, :body", :action_view)
192
- match_data = output.match(TEXT_AREA_CONTENT_REGEX)
193
- assert_equal '&#x0020; Foo', match_data[2]
194
- end
195
-
196
- def test_textareas_should_preserve_leading_whitespace_in_partials
197
- # leading whitespace in textareas rendered inside partials
198
- @base.instance_variable_set(:@post, Post.new(" Foo", nil, PostErrors.new))
199
- output = render(".foo\n .bar\n = render '/text_area_helper'", :action_view)
200
- match_data = output.match(TEXT_AREA_CONTENT_REGEX)
201
- assert_equal '&#x0020; Foo', match_data[2]
202
- end
203
-
204
- def test_capture_haml
205
- assert_equal(<<HTML, render(<<HAML))
206
- "<p>13</p>\\n"
207
- HTML
208
- - (foo = capture_haml(13) do |a|
209
- %p= a
210
- - end)
211
- = foo.inspect
212
- HAML
213
- end
214
-
215
- def test_content_tag_block
216
- assert_equal(<<HTML.strip, render(<<HAML, :action_view).strip)
217
- <div><p>bar</p>
218
- <strong>bar</strong>
219
- </div>
220
- HTML
221
- = content_tag :div do
222
- %p bar
223
- %strong bar
224
- HAML
225
- end
226
-
227
- def test_content_tag_error_wrapping
228
- def @base.protect_against_forgery?; false; end
229
- output = render(<<HAML, :action_view)
230
- = form_for @post, :as => :post, :html => {:class => nil, :id => nil}, :url => '' do |f|
231
- = f.label 'error_field'
232
- HAML
233
- fragment = Nokogiri::HTML.fragment(output)
234
- refute_nil fragment.css('form div.field_with_errors label[for=post_error_field]').first
235
- end
236
-
237
- def test_form_tag_in_helper_with_string_block
238
- def @base.protect_against_forgery?; false; end
239
- rendered = render('= wacky_form', :action_view)
240
- fragment = Nokogiri::HTML.fragment(rendered)
241
- assert_equal 'bar', fragment.text.strip
242
- assert_equal '/foo', fragment.css('form').first.attributes['action'].to_s
243
- end
244
-
245
- def test_haml_tag_name_attribute_with_id
246
- assert_equal("<p id='some_id'></p>\n", render("- haml_tag 'p#some_id'"))
247
- end
248
-
249
- def test_haml_tag_name_attribute_with_colon_id
250
- assert_equal("<p id='some:id'></p>\n", render("- haml_tag 'p#some:id'"))
251
- end
252
-
253
- def test_haml_tag_without_name_but_with_id
254
- assert_equal("<div id='some_id'></div>\n", render("- haml_tag '#some_id'"))
255
- end
256
-
257
- def test_haml_tag_without_name_but_with_class
258
- assert_equal("<div class='foo'></div>\n", render("- haml_tag '.foo'"))
259
- end
260
-
261
- def test_haml_tag_without_name_but_with_colon_class
262
- assert_equal("<div class='foo:bar'></div>\n", render("- haml_tag '.foo:bar'"))
263
- end
264
-
265
- def test_haml_tag_name_with_id_and_class
266
- assert_equal("<p class='foo' id='some_id'></p>\n", render("- haml_tag 'p#some_id.foo'"))
267
- end
268
-
269
- def test_haml_tag_name_with_class
270
- assert_equal("<p class='foo'></p>\n", render("- haml_tag 'p.foo'"))
271
- end
272
-
273
- def test_haml_tag_name_with_class_and_id
274
- assert_equal("<p class='foo' id='some_id'></p>\n", render("- haml_tag 'p.foo#some_id'"))
275
- end
276
-
277
- def test_haml_tag_name_with_id_and_multiple_classes
278
- assert_equal("<p class='foo bar' id='some_id'></p>\n", render("- haml_tag 'p#some_id.foo.bar'"))
279
- end
280
-
281
- def test_haml_tag_name_with_multiple_classes_and_id
282
- assert_equal("<p class='foo bar' id='some_id'></p>\n", render("- haml_tag 'p.foo.bar#some_id'"))
283
- end
284
-
285
- def test_haml_tag_name_and_attribute_classes_merging_with_id
286
- assert_equal("<p class='bar foo' id='some_id'></p>\n", render("- haml_tag 'p#some_id.foo', :class => 'bar'"))
287
- end
288
-
289
- def test_haml_tag_name_and_attribute_classes_merging
290
- assert_equal("<p class='bar foo'></p>\n", render("- haml_tag 'p.foo', :class => 'bar'"))
291
- end
292
-
293
- def test_haml_tag_name_merges_id_and_attribute_id
294
- assert_equal("<p id='foo_bar'></p>\n", render("- haml_tag 'p#foo', :id => 'bar'"))
295
- end
296
-
297
- def test_haml_tag_attribute_html_escaping
298
- assert_equal("<p id='foo&amp;bar'>baz</p>\n", render("%p{:id => 'foo&bar'} baz", :escape_html => true))
299
- end
300
-
301
- def test_haml_tag_autoclosed_tags_are_closed_xhtml
302
- assert_equal("<br class='foo' />\n", render("- haml_tag :br, :class => 'foo'", :format => :xhtml))
303
- end
304
-
305
- def test_haml_tag_autoclosed_tags_are_closed_html
306
- assert_equal("<br class='foo'>\n", render("- haml_tag :br, :class => 'foo'", :format => :html5))
307
- end
308
-
309
- def test_haml_tag_with_class_array
310
- assert_equal("<p class='a b'>foo</p>\n", render("- haml_tag :p, 'foo', :class => %w[a b]"))
311
- assert_equal("<p class='a b c d'>foo</p>\n", render("- haml_tag 'p.c.d', 'foo', :class => %w[a b]"))
312
- end
313
-
314
- def test_haml_tag_with_id_array
315
- assert_equal("<p id='a_b'>foo</p>\n", render("- haml_tag :p, 'foo', :id => %w[a b]"))
316
- assert_equal("<p id='c_a_b'>foo</p>\n", render("- haml_tag 'p#c', 'foo', :id => %w[a b]"))
317
- end
318
-
319
- def test_haml_tag_with_data_hash
320
- assert_equal("<p data-baz data-foo='bar'>foo</p>\n",
321
- render("- haml_tag :p, 'foo', :data => {:foo => 'bar', :baz => true}"))
322
- end
323
-
324
- def test_haml_tag_non_autoclosed_tags_arent_closed
325
- assert_equal("<p></p>\n", render("- haml_tag :p"))
326
- end
327
-
328
- def test_haml_tag_renders_text_on_a_single_line
329
- assert_equal("<p>#{'a' * 100}</p>\n", render("- haml_tag :p, 'a' * 100"))
330
- end
331
-
332
- def test_haml_tag_raises_error_for_multiple_content
333
- assert_raises(Haml::Error) { render("- haml_tag :p, 'foo' do\n bar") }
334
- end
335
-
336
- def test_haml_tag_flags
337
- assert_equal("<p />\n", render("- haml_tag :p, :/", :format => :xhtml))
338
- assert_equal("<p>\n", render("- haml_tag :p, :/", :format => :html5))
339
- assert_equal("<p>kumquat</p>\n", render("- haml_tag :p, :< do\n kumquat"))
340
-
341
- assert_raises(Haml::Error) { render("- haml_tag :p, 'foo', :/") }
342
- assert_raises(Haml::Error) { render("- haml_tag :p, :/ do\n foo") }
343
- end
344
-
345
- def test_haml_tag_error_return
346
- assert_raises(Haml::Error) { render("= haml_tag :p") }
347
- end
348
-
349
- def test_haml_tag_with_multiline_string
350
- assert_equal(<<HTML, render(<<HAML))
351
- <p>
352
- foo
353
- bar
354
- baz
355
- </p>
356
- HTML
357
- - haml_tag :p, "foo\\nbar\\nbaz"
358
- HAML
359
- end
360
-
361
- def test_haml_concat_inside_haml_tag_escaped_with_xss
362
- assert_equal("<p>\n &lt;&gt;&amp;\n</p>\n", render(<<HAML, :action_view))
363
- - haml_tag :p do
364
- - haml_concat "<>&"
365
- HAML
366
- end
367
-
368
- def test_haml_concat_with_multiline_string
369
- assert_equal(<<HTML, render(<<HAML))
370
- <p>
371
- foo
372
- bar
373
- baz
374
- </p>
375
- HTML
376
- %p
377
- - haml_concat "foo\\nbar\\nbaz"
378
- HAML
379
- end
380
-
381
- def test_haml_tag
382
- assert_equal(<<HTML, render(<<HAML))
383
- <p>
384
- <strong>Hi!</strong>
385
- </p>
386
- HTML
387
- - haml_tag :p do
388
- - haml_tag :strong, "Hi!"
389
- HAML
390
- end
391
-
392
- def test_haml_tag_if_positive
393
- assert_equal(<<HTML, render(<<HAML))
394
- <div class='conditional'>
395
- <p>A para</p>
396
- </div>
397
- HTML
398
- - haml_tag_if true, '.conditional' do
399
- %p A para
400
- HAML
401
- end
402
-
403
- def test_haml_tag_if_positive_with_attributes
404
- assert_equal(<<HTML, render(<<HAML))
405
- <div class='conditional' foo='bar'>
406
- <p>A para</p>
407
- </div>
408
- HTML
409
- - haml_tag_if true, '.conditional', {:foo => 'bar'} do
410
- %p A para
411
- HAML
412
- end
413
-
414
- def test_haml_tag_if_negative
415
- assert_equal(<<HTML, render(<<HAML))
416
- <p>A para</p>
417
- HTML
418
- - haml_tag_if false, '.conditional' do
419
- %p A para
420
- HAML
421
- end
422
-
423
- def test_haml_tag_if_error_return
424
- assert_raises(Haml::Error) { render("= haml_tag_if false, '.conditional' do\n %p Hello") }
425
- end
426
-
427
- def test_is_haml
428
- assert(!ActionView::Base.new.is_haml?)
429
- assert_equal("true\n", render("= is_haml?"))
430
- assert_equal("true\n", render("= is_haml?", :action_view))
431
- assert_equal("false", @base.render(:inline => '<%= is_haml? %>'))
432
- assert_equal("false\n", render("= render :inline => '<%= is_haml? %>'", :action_view))
433
- end
434
-
435
- def test_page_class
436
- controller = Struct.new(:controller_name, :action_name).new('troller', 'tion')
437
- scope = Struct.new(:controller).new(controller)
438
- result = render("%div{:class => page_class} MyDiv", :scope => scope)
439
- expected = "<div class='troller tion'>MyDiv</div>\n"
440
- assert_equal expected, result
441
- end
442
-
443
- def test_indented_capture
444
- assert_equal(" Foo\n ", @base.render(:inline => " <% res = capture do %>\n Foo\n <% end %><%= res %>"))
445
- end
446
-
447
- def test_capture_deals_properly_with_collections
448
- obj = Object.new
449
- def obj.trc(collection, &block)
450
- collection.each do |record|
451
- haml_concat capture_haml(record, &block)
452
- end
453
- end
454
-
455
- assert_equal("1\n\n2\n\n3\n\n", render("- trc([1, 2, 3]) do |i|\n = i.inspect", scope: obj))
456
- end
457
-
458
- def test_capture_with_string_block
459
- assert_equal("foo\n", render("= capture { 'foo' }", :action_view))
460
- end
461
-
462
- def test_capture_with_non_string_value_reurns_nil
463
- def @base.check_capture_returns_nil(&block)
464
- contents = capture(&block)
465
-
466
- contents << "ERROR" if contents
467
- end
468
-
469
- assert_equal("\n", render("= check_capture_returns_nil { 2 }", :action_view))
470
- end
471
-
472
-
473
- class HomemadeViewContext
474
- include ActionView::Context
475
- include ActionView::Helpers::FormHelper
476
-
477
- def initialize
478
- _prepare_context
479
- end
480
-
481
- def url_for(*)
482
- "/"
483
- end
484
-
485
- def dom_class(*)
486
- end
487
-
488
- def dom_id(*)
489
- end
490
-
491
- def m # I have to inject the model into the view using an instance method, using locals doesn't work.
492
- FormModel.new
493
- end
494
-
495
- def protect_against_forgery?
496
- end
497
-
498
- # def capture(*args, &block)
499
- # capture_haml(*args, &block)
500
- # end
501
- end
502
-
503
- def test_form_for_with_homemade_view_context
504
- handler = ActionView::Template.handler_for_extension("haml")
505
- template = ActionView::Template.new(<<HAML, "inline template", handler, {})
506
- = form_for(m, :url => "/") do
507
- %b Bold!
508
- HAML
509
-
510
- # see if Bold is within form tags:
511
- assert_match(/<form.*>.*<b>Bold!<\/b>.*<\/form>/m, template.render(HomemadeViewContext.new, {}))
512
- end
513
-
514
- def test_find_and_preserve_with_block
515
- assert_equal("<pre>Foo&#x000A;Bar</pre>\nFoo\nBar\n",
516
- render("= find_and_preserve do\n %pre\n Foo\n Bar\n Foo\n Bar"))
517
- end
518
-
519
- def test_find_and_preserve_with_block_and_tags
520
- assert_equal("<pre>Foo\nBar</pre>\nFoo\nBar\n",
521
- render("= find_and_preserve([]) do\n %pre\n Foo\n Bar\n Foo\n Bar"))
522
- end
523
-
524
- def test_preserve_with_block
525
- assert_equal("<pre>Foo&#x000A;Bar</pre>&#x000A;Foo&#x000A;Bar",
526
- render("= preserve do\n %pre\n Foo\n Bar\n Foo\n Bar"))
527
- end
528
-
529
- def test_init_haml_helpers
530
- context = Object.new
531
- class << context
532
- include Haml::Helpers
533
- end
534
- context.init_haml_helpers
535
-
536
- result = context.capture_haml do
537
- context.haml_tag :p, :attr => "val" do
538
- context.haml_concat "Blah"
539
- end
540
- end
541
-
542
- assert_equal("<p attr='val'>\n Blah\n</p>\n", result)
543
- end
544
-
545
- def test_non_haml
546
- assert_equal("false\n", render("= non_haml { is_haml? }"))
547
- end
548
-
549
- def test_content_tag_nested
550
- assert_equal "<span><div>something</div></span>", render("= nested_tag", :action_view).strip
551
- end
552
-
553
- def test_error_return
554
- assert_raises(Haml::Error, <<MESSAGE) {render("= haml_concat 'foo'")}
555
- haml_concat outputs directly to the Haml template.
556
- Disregard its return value and use the - operator,
557
- or use capture_haml to get the value as a String.
558
- MESSAGE
559
- end
560
-
561
- def test_error_return_line
562
- render("%p foo\n= haml_concat('foo').to_s\n%p bar")
563
- assert false, "Expected Haml::Error"
564
- rescue Haml::Error => e
565
- assert_equal 2, e.backtrace[0].scan(/:(\d+)/).first.first.to_i
566
- end
567
-
568
- def test_error_return_line_in_helper
569
- obj = Object.new
570
- def obj.something_that_uses_haml_concat
571
- haml_concat('foo').to_s
572
- end
573
-
574
- render("- something_that_uses_haml_concat", scope: obj)
575
- assert false, "Expected Haml::Error"
576
- rescue Haml::Error => e
577
- assert_equal __LINE__ - 6, e.backtrace[0].scan(/:(\d+)/).first.first.to_i
578
- end
579
-
580
- class ActsLikeTag
581
- # We want to be able to have people include monkeypatched ActionView helpers
582
- # without redefining is_haml?.
583
- # This is accomplished via Object#is_haml?, and this is a test for it.
584
- include ActionView::Helpers::TagHelper
585
- def to_s
586
- content_tag :p, 'some tag content'
587
- end
588
- end
589
-
590
- def test_random_class_includes_tag_helper
591
- assert_equal "<p>some tag content</p>", ActsLikeTag.new.to_s
592
- end
593
-
594
- def test_capture_with_nuke_outer
595
- assert_equal "<div></div>\n*<div>hi there!</div>\n", render(<<HAML)
596
- %div
597
- = precede("*") do
598
- %div> hi there!
599
- HAML
600
-
601
- assert_equal "<div></div>\n*<div>hi there!</div>\n", render(<<HAML)
602
- %div
603
- = precede("*") do
604
- = " "
605
- %div> hi there!
606
- HAML
607
- end
608
-
609
- def test_html_escape
610
- assert_equal "&quot;&gt;&lt;&amp;", Haml::Helpers.html_escape('"><&')
611
- end
612
-
613
- def test_html_escape_should_work_on_frozen_strings
614
- begin
615
- assert Haml::Helpers.html_escape('foo'.freeze)
616
- rescue => e
617
- flunk e.message
618
- end
619
- end
620
-
621
- def test_html_escape_encoding
622
- old_stderr, $stderr = $stderr, StringIO.new
623
- string = "\"><&\u00e9" # if you're curious, u00e9 is "LATIN SMALL LETTER E WITH ACUTE"
624
- assert_equal "&quot;&gt;&lt;&amp;\u00e9", Haml::Helpers.html_escape(string)
625
- assert $stderr.string == "", "html_escape shouldn't generate warnings with UTF-8 strings: #{$stderr.string}"
626
- ensure
627
- $stderr = old_stderr
628
- end
629
-
630
- def test_html_escape_non_string
631
- assert_equal('4.58', Haml::Helpers.html_escape(4.58))
632
- assert_equal('4.58', Haml::Helpers.html_escape_without_haml_xss(4.58))
633
- end
634
-
635
- def test_escape_once
636
- assert_equal "&quot;&gt;&lt;&amp;", Haml::Helpers.escape_once('"><&')
637
- end
638
-
639
- def test_escape_once_leaves_entity_references
640
- assert_equal "&quot;&gt;&lt;&amp; &nbsp;", Haml::Helpers.escape_once('"><& &nbsp;')
641
- end
642
-
643
- def test_escape_once_leaves_numeric_references
644
- assert_equal "&quot;&gt;&lt;&amp; &#160;", Haml::Helpers.escape_once('"><& &#160;') #decimal
645
- assert_equal "&quot;&gt;&lt;&amp; &#x00a0;", Haml::Helpers.escape_once('"><& &#x00a0;') #hexadecimal
646
- end
647
-
648
- def test_escape_once_encoding
649
- old_stderr, $stderr = $stderr, StringIO.new
650
- string = "\"><&\u00e9 &nbsp;"
651
- assert_equal "&quot;&gt;&lt;&amp;\u00e9 &nbsp;", Haml::Helpers.escape_once(string)
652
- assert $stderr.string == "", "html_escape shouldn't generate warnings with UTF-8 strings: #{$stderr.string}"
653
- ensure
654
- $stderr = old_stderr
655
- end
656
-
657
- def test_html_attrs_xhtml
658
- assert_equal("<html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'></html>\n",
659
- render("%html{html_attrs}", :format => :xhtml))
660
- end
661
-
662
- def test_html_attrs_html4
663
- assert_equal("<html lang='en-US'></html>\n",
664
- render("%html{html_attrs}", :format => :html4))
665
- end
666
-
667
- def test_html_attrs_html5
668
- assert_equal("<html lang='en-US'></html>\n",
669
- render("%html{html_attrs}", :format => :html5))
670
- end
671
-
672
- def test_html_attrs_xhtml_other_lang
673
- assert_equal("<html lang='es-AR' xml:lang='es-AR' xmlns='http://www.w3.org/1999/xhtml'></html>\n",
674
- render("%html{html_attrs('es-AR')}", :format => :xhtml))
675
- end
676
-
677
- def test_html_attrs_html4_other_lang
678
- assert_equal("<html lang='es-AR'></html>\n",
679
- render("%html{html_attrs('es-AR')}", :format => :html4))
680
- end
681
-
682
- def test_html_attrs_html5_other_lang
683
- assert_equal("<html lang='es-AR'></html>\n",
684
- render("%html{html_attrs('es-AR')}", :format => :html5))
685
- end
686
-
687
- def test_escape_once_should_work_on_frozen_strings
688
- begin
689
- Haml::Helpers.escape_once('foo'.freeze)
690
- rescue => e
691
- flunk e.message
692
- end
693
- end
694
-
695
- end