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,22 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Haml
4
- class OptionsTest < Haml::TestCase
5
- def test_buffer_defaults_have_only_buffer_option_keys
6
- assert_equal(
7
- Haml::Options.buffer_option_keys.sort,
8
- Haml::Options.buffer_defaults.keys.sort,
9
- )
10
- end
11
-
12
- def test_buffer_defaults_values_are_the_same_as_rails_defaults
13
- rails_defaults = Haml::Options.defaults.merge(Haml::Template.options)
14
- Haml::Options.buffer_option_keys.each do |key|
15
- assert_equal(
16
- rails_defaults[key],
17
- Haml::Options.buffer_defaults[key], "key: #{key}"
18
- )
19
- end
20
- end
21
- end
22
- end
@@ -1,172 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Haml
4
- class ParserTest < Haml::TestCase
5
-
6
- test "should raise error for 'else' at wrong indent level" do
7
- begin
8
- parse("- if true\n #first\n text\n - else\n #second")
9
- flunk("Should have raised a Haml::SyntaxError")
10
- rescue SyntaxError => e
11
- assert_equal Error.message(:bad_script_indent, 'else', 0, 1), e.message
12
- end
13
- end
14
-
15
- test "should raise error for 'elsif' at wrong indent level" do
16
- begin
17
- parse("- if true\n #first\n text\n - elsif false\n #second")
18
- flunk("Should have raised a Haml::SyntaxError")
19
- rescue SyntaxError => e
20
- assert_equal Error.message(:bad_script_indent, 'elsif', 0, 1), e.message
21
- end
22
- end
23
-
24
- test "should raise error for 'else' at wrong indent level after unless" do
25
- begin
26
- parse("- unless true\n #first\n text\n - else\n #second")
27
- flunk("Should have raised a Haml::SyntaxError")
28
- rescue SyntaxError => e
29
- assert_equal Error.message(:bad_script_indent, 'else', 0, 1), e.message
30
- end
31
- end
32
-
33
- test "should raise syntax error for else with no if" do
34
- begin
35
- parse("- else\n 'foo'")
36
- flunk("Should have raised a Haml::SyntaxError")
37
- rescue SyntaxError => e
38
- assert_equal Error.message(:missing_if, 'else'), e.message
39
- end
40
- end
41
-
42
- test "should raise syntax error for nested else with no" do
43
- begin
44
- parse("#foo\n - else\n 'foo'")
45
- flunk("Should have raised a Haml::SyntaxError")
46
- rescue SyntaxError => e
47
- assert_equal Error.message(:missing_if, 'else'), e.message
48
- end
49
- end
50
-
51
- test "else after if containing case is accepted" do
52
- # see issue 572
53
- begin
54
- parse "- if true\n - case @foo\n - when 1\n bar\n- else\n bar"
55
- assert true
56
- rescue SyntaxError
57
- flunk 'else clause after if containing case should be accepted'
58
- end
59
- end
60
-
61
- test "else after if containing unless is accepted" do
62
- begin
63
- parse "- if true\n - unless @foo\n bar\n- else\n bar"
64
- assert true
65
- rescue SyntaxError
66
- flunk 'else clause after if containing unless should be accepted'
67
- end
68
- end
69
-
70
- test "loud script with else is accepted" do
71
- begin
72
- parse "= if true\n - 'A'\n-else\n - 'B'"
73
- assert true
74
- rescue SyntaxError
75
- flunk 'loud script (=) should allow else'
76
- end
77
- end
78
-
79
- test "else after nested loud script is accepted" do
80
- begin
81
- parse "-if true\n =if true\n - 'A'\n-else\n B"
82
- assert true
83
- rescue SyntaxError
84
- flunk 'else after nested loud script should be accepted'
85
- end
86
- end
87
-
88
- test "case with indented whens should allow else" do
89
- begin
90
- parse "- foo = 1\n-case foo\n -when 1\n A\n -else\n B"
91
- assert true
92
- rescue SyntaxError
93
- flunk 'case with indented whens should allow else'
94
- end
95
- end
96
-
97
- test "revealed conditional comments are detected" do
98
- text = "some revealed text"
99
- cond = "[cond]"
100
-
101
- node = parse("/!#{cond} #{text}").children[0]
102
-
103
- assert_equal text, node.value[:text]
104
- assert_equal cond, node.value[:conditional]
105
- assert node.value[:revealed]
106
- end
107
-
108
- test "hidden conditional comments are detected" do
109
- text = "some revealed text"
110
- cond = "[cond]"
111
-
112
- node = parse("/#{cond} #{text}").children[0]
113
-
114
- assert_equal text, node.value[:text]
115
- assert_equal cond, node.value[:conditional]
116
- refute node.value[:revealed]
117
- end
118
-
119
- test "only script lines are checked for continuation keywords" do
120
- haml = "- if true\n setup\n- else\n else\n"
121
- node = parse(haml).children[0]
122
- assert_equal(3, node.children.size)
123
- end
124
-
125
- # see #830. Strictly speaking the pipe here is not necessary, but there
126
- # shouldn't be an error if it is there.
127
- test "multiline Ruby with extra trailing pipe doesn't raise error" do
128
- haml = "%p= foo bar, |\n baz"
129
- begin
130
- parse haml
131
- rescue Haml::SyntaxError
132
- flunk "Should not have raised SyntaxError"
133
- end
134
- end
135
-
136
- test "empty filter doesn't hide following lines" do
137
- root = parse "%p\n :plain\n %p\n"
138
- p_element = root.children[0]
139
- assert_equal 2, p_element.children.size
140
- assert_equal :filter, p_element.children[0].type
141
- assert_equal :tag, p_element.children[1].type
142
- end
143
-
144
- # Previously blocks under a haml_comment would be rejected if any line was
145
- # indented by a value that wasn't a multiple of the document indentation.
146
- test "haml_comment accepts any indentation in content" do
147
- begin
148
- parse "-\#\n Indented two spaces\n Indented three spaces"
149
- rescue Haml::SyntaxError
150
- flunk "haml_comment should accept any combination of indentation"
151
- end
152
- end
153
-
154
- test "block haml_comment includes text" do
155
- root = parse "-#\n Hello\n Hello\n"
156
- assert_equal "Hello\n Hello\n", root.children[0].value[:text]
157
- end
158
-
159
- test "block haml_comment includes first line if present" do
160
- root = parse "-# First line\n Hello\n Hello\n"
161
- assert_equal " First line\nHello\n Hello\n", root.children[0].value[:text]
162
- end
163
-
164
- private
165
-
166
- def parse(haml, options = nil)
167
- options ||= Options.new
168
- parser = Parser.new(options)
169
- parser.call(haml)
170
- end
171
- end
172
- end
@@ -1,332 +0,0 @@
1
- require 'test_helper'
2
- require 'mocks/article'
3
-
4
- require 'action_pack/version'
5
- require 'template_test_helper'
6
-
7
- class TemplateTest < Haml::TestCase
8
- TEMPLATES = %w{ very_basic standard helpers
9
- whitespace_handling original_engine list helpful
10
- silent_script tag_parsing just_stuff partials
11
- nuke_outer_whitespace nuke_inner_whitespace bemit
12
- render_layout partial_layout partial_layout_erb}
13
-
14
- def setup
15
- @base = create_base
16
-
17
- # filters template uses :sass
18
- # Sass::Plugin.options.update(:line_comments => true, :style => :compact)
19
- end
20
-
21
- def create_base
22
- vars = { 'article' => Article.new, 'foo' => 'value one' }
23
-
24
- base = ActionView::Base.new(TemplateTestHelper::TEMPLATE_PATH, vars)
25
-
26
- # This is needed by RJS in (at least) Rails 3
27
- base.instance_variable_set(:@template, base)
28
-
29
- # This is used by form_for.
30
- # It's usually provided by ActionController::Base.
31
- def base.protect_against_forgery?; false; end
32
-
33
- base
34
- end
35
-
36
- def render(text, options = {})
37
- return @base.render(:inline => text, :type => :haml) if options == :action_view
38
- options = options.merge(:format => :xhtml)
39
- super(text, options, @base)
40
- end
41
-
42
- def load_result(name)
43
- @result = ''
44
- File.new(File.dirname(__FILE__) + "/results/#{name}.xhtml").each_line { |l| @result += l }
45
- @result
46
- end
47
-
48
- def assert_renders_correctly(name, &render_method)
49
- old_options = Haml::Template.options.dup
50
- Haml::Template.options[:escape_html] = false
51
- render_method ||= proc { |n| @base.render(:file => n) }
52
-
53
- silence_warnings do
54
- load_result(name).split("\n").zip(render_method[name].split("\n")).each_with_index do |pair, line|
55
- message = "template: #{name}\nline: #{line}"
56
- assert_equal(pair.first, pair.last, message)
57
- end
58
- end
59
- rescue ActionView::Template::Error => e
60
- if e.message =~ /Can't run [\w:]+ filter; required (one of|file) ((?:'\w+'(?: or )?)+)(, but none were found| not found)/
61
- puts "\nCouldn't require #{$2}; skipping a test."
62
- else
63
- raise e
64
- end
65
- ensure
66
- Haml::Template.options = old_options
67
- end
68
-
69
- def test_empty_render_should_remain_empty
70
- assert_equal('', render(''))
71
- end
72
-
73
- TEMPLATES.each do |template|
74
- define_method "test_template_should_render_correctly [template: #{template}]" do
75
- assert_renders_correctly template
76
- end
77
- end
78
-
79
- def test_render_method_returning_null
80
- @base.instance_eval do
81
- def empty
82
- nil
83
- end
84
- def render_something(&block)
85
- capture(self, &block)
86
- end
87
- end
88
-
89
- content_to_render = "%h1 This is part of the broken view.\n= render_something do |thing|\n = thing.empty do\n = 'test'"
90
- result = render(content_to_render)
91
- expected_result = "<h1>This is part of the broken view.</h1>\n"
92
- assert_equal(expected_result, result)
93
- end
94
-
95
- def test_simple_rendering
96
- content_to_render = "%p test\n= capture { 'foo' }"
97
- result = render(content_to_render)
98
- expected_result = "<p>test</p>\nfoo\n"
99
- assert_equal(expected_result, result)
100
- end
101
-
102
- def test_templates_should_render_correctly_with_render_proc
103
- assert_renders_correctly("standard") do |name|
104
- engine = Haml::Engine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"), format: :xhtml)
105
- engine.render_proc(@base).call
106
- end
107
- end
108
-
109
- def test_templates_should_render_correctly_with_def_method
110
- assert_renders_correctly("standard") do |name|
111
- engine = Haml::Engine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"), format: :xhtml)
112
- engine.def_method(@base, "render_standard")
113
- @base.render_standard
114
- end
115
- end
116
-
117
- def test_instance_variables_should_work_inside_templates
118
- @base.instance_variable_set(:@content_for_layout, 'something')
119
- assert_equal("<p>something</p>", render("%p= @content_for_layout").chomp)
120
-
121
- @base.instance_eval("@author = 'Hampton Catlin'")
122
- assert_equal("<div class='author'>Hampton Catlin</div>", render(".author= @author").chomp)
123
-
124
- @base.instance_eval("@author = 'Hampton'")
125
- assert_equal("Hampton", render("= @author").chomp)
126
-
127
- @base.instance_eval("@author = 'Catlin'")
128
- assert_equal("Catlin", render("= @author").chomp)
129
- end
130
-
131
- def test_instance_variables_should_work_inside_attributes
132
- @base.instance_eval("@author = 'hcatlin'")
133
- assert_equal("<p class='hcatlin'>foo</p>", render("%p{:class => @author} foo").chomp)
134
- end
135
-
136
- def test_template_renders_should_eval
137
- assert_equal("2\n", render("= 1+1"))
138
- end
139
-
140
- def test_haml_options
141
- old_options = Haml::Template.options.dup
142
- Haml::Template.options[:suppress_eval] = true
143
- old_base, @base = @base, create_base
144
- assert_renders_correctly("eval_suppressed")
145
- ensure
146
- @base = old_base
147
- Haml::Template.options = old_options
148
- end
149
-
150
- def test_with_output_buffer
151
- assert_equal(<<HTML, render(<<HAML))
152
- <p>
153
- foo
154
- baz
155
- </p>
156
- HTML
157
- %p
158
- foo
159
- -# Parenthesis required due to Rails 3.0 deprecation of block helpers
160
- -# that return strings.
161
- - (with_output_buffer do
162
- bar
163
- = "foo".gsub(/./) do |s|
164
- - "flup"
165
- - end)
166
- baz
167
- HAML
168
- end
169
-
170
- def test_exceptions_should_work_correctly
171
- begin
172
- render("- raise 'oops!'")
173
- rescue Exception => e
174
- assert_equal("oops!", e.message)
175
- assert_match(/^\(haml\):1/, e.backtrace[0])
176
- else
177
- assert false
178
- end
179
-
180
- template = <<END
181
- %p
182
- %h1 Hello!
183
- = "lots of lines"
184
- = "even more!"
185
- - raise 'oh no!'
186
- %p
187
- this is after the exception
188
- %strong yes it is!
189
- ho ho ho.
190
- END
191
-
192
- begin
193
- render(template.chomp)
194
- rescue Exception => e
195
- assert_match(/^\(haml\):5/, e.backtrace[0])
196
- else
197
- assert false
198
- end
199
- end
200
-
201
- def test_form_builder_label_with_block
202
- output = render(<<HAML, :action_view)
203
- = form_for @article, :as => :article, :html => {:class => nil, :id => nil}, :url => '' do |f|
204
- = f.label :title do
205
- Block content
206
- HAML
207
- fragment = Nokogiri::HTML.fragment output
208
- assert_equal "Block content", fragment.css('form label').first.content.strip
209
- end
210
-
211
- ## XSS Protection Tests
212
-
213
- def test_escape_html_option_set
214
- assert Haml::Template.options[:escape_html]
215
- end
216
-
217
- def test_xss_protection
218
- assert_equal("Foo &amp; Bar\n", render('= "Foo & Bar"', :action_view))
219
- end
220
-
221
- def test_xss_protection_with_safe_strings
222
- assert_equal("Foo & Bar\n", render('= Haml::Util.html_safe("Foo & Bar")', :action_view))
223
- end
224
-
225
- def test_xss_protection_with_bang
226
- assert_equal("Foo & Bar\n", render('!= "Foo & Bar"', :action_view))
227
- end
228
-
229
- def test_xss_protection_in_interpolation
230
- assert_equal("Foo &amp; Bar\n", render('Foo #{"&"} Bar', :action_view))
231
- end
232
-
233
- def test_xss_protection_in_attributes
234
- assert_equal("<div data-html='&lt;foo&gt;bar&lt;/foo&gt;'></div>\n", render('%div{ "data-html" => "<foo>bar</foo>" }', :action_view))
235
- end
236
-
237
- def test_xss_protection_in_attributes_with_safe_strings
238
- assert_equal("<div data-html='<foo>bar</foo>'></div>\n", render('%div{ "data-html" => "<foo>bar</foo>".html_safe }', :action_view))
239
- end
240
-
241
- def test_xss_protection_with_bang_in_interpolation
242
- assert_equal("Foo & Bar\n", render('! Foo #{"&"} Bar', :action_view))
243
- end
244
-
245
- def test_xss_protection_with_safe_strings_in_interpolation
246
- assert_equal("Foo & Bar\n", render('Foo #{Haml::Util.html_safe("&")} Bar', :action_view))
247
- end
248
-
249
- def test_xss_protection_with_mixed_strings_in_interpolation
250
- assert_equal("Foo & Bar &amp; Baz\n", render('Foo #{Haml::Util.html_safe("&")} Bar #{"&"} Baz', :action_view))
251
- end
252
-
253
- def test_xss_protection_with_erb_filter
254
- if defined?(Haml::SafeErubiTemplate)
255
- assert_equal("&lt;img&gt;\n\n", render(":erb\n <%= '<img>' %>", :action_view))
256
- assert_equal("<img>\n\n", render(":erb\n <%= '<img>'.html_safe %>", :action_view))
257
- else # For Haml::SafeErubisTemplate
258
- assert_equal("&lt;img&gt;\n", render(":erb\n <%= '<img>' %>", :action_view))
259
- assert_equal("<img>\n", render(":erb\n <%= '<img>'.html_safe %>", :action_view))
260
- end
261
- end
262
-
263
- def test_rendered_string_is_html_safe
264
- assert(render("Foo").html_safe?)
265
- end
266
-
267
- def test_rendered_string_is_html_safe_with_action_view
268
- assert(render("Foo", :action_view).html_safe?)
269
- end
270
-
271
- def test_xss_html_escaping_with_non_strings
272
- assert_equal("4\n", render("= html_escape(4)"))
273
- end
274
-
275
- def test_xss_protection_with_concat
276
- assert_equal("Foo &amp; Bar", render('- concat "Foo & Bar"', :action_view))
277
- end
278
-
279
- def test_xss_protection_with_concat_with_safe_string
280
- assert_equal("Foo & Bar", render('- concat(Haml::Util.html_safe("Foo & Bar"))', :action_view))
281
- end
282
-
283
- def test_xss_protection_with_safe_concat
284
- assert_equal("Foo & Bar", render('- safe_concat "Foo & Bar"', :action_view))
285
- end
286
-
287
- ## Regression
288
-
289
- def test_xss_protection_with_nested_haml_tag
290
- assert_equal(<<HTML, render(<<HAML, :action_view))
291
- <div>
292
- <ul>
293
- <li>Content!</li>
294
- </ul>
295
- </div>
296
- HTML
297
- - haml_tag :div do
298
- - haml_tag :ul do
299
- - haml_tag :li, "Content!"
300
- HAML
301
- end
302
-
303
- if defined?(ActionView::Helpers::PrototypeHelper)
304
- def test_rjs
305
- assert_equal(<<HTML, render(<<HAML, :action_view))
306
- window.location.reload();
307
- HTML
308
- = update_page do |p|
309
- - p.reload
310
- HAML
311
- end
312
- end
313
-
314
- def test_cache
315
- @base.controller = ActionController::Base.new
316
- @base.controller.perform_caching = false
317
- assert_equal(<<HTML, render(<<HAML, :action_view))
318
- Test
319
- HTML
320
- - cache do
321
- Test
322
- HAML
323
- end
324
-
325
- class ::TosUnsafeObject; def to_s; '<hr>'; end; end
326
- class ::TosSafeObject; def to_s; '<hr>'.html_safe; end; end
327
-
328
- def test_object_that_returns_safe_buffer
329
- assert_equal("<hr>\n", render('= ::TosSafeObject.new', escape_html: true))
330
- assert_equal("&lt;hr&gt;\n", render('= ::TosUnsafeObject.new', escape_html: true))
331
- end
332
- end