1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
# Explicitly requiring rails_template because rails initializers is not executed here.
require 'haml/rails_template'
describe Haml::RailsTemplate do
def render(haml)
ActionView::Template.register_template_handler(:haml, Haml::RailsTemplate.new)
base = Class.new(ActionView::Base) do
def compiled_method_container
self.class
end
end.new(ActionView::LookupContext.new(''), {}, ActionController::Base.new)
base.render(inline: haml, type: :haml)
end
specify 'html escape' do
assert_equal %Q|<script>alert("a");</script>\n|, render(<<-HAML.unindent)
= '<script>alert("a");</script>'
HAML
assert_equal %Q|<script>alert("a");</script>\n|, render(<<-HAML.unindent)
= '<script>alert("a");</script>'.html_safe
HAML
skip 'escape is not working well in truffleruby' if RUBY_ENGINE == 'truffleruby'
assert_equal %Q|<script>alert("a");</script>\n|, render(<<-'HAML'.unindent)
#{'<script>alert("a");</script>'}
HAML
assert_equal %Q|<script>alert("a");</script>\n|, render(<<-'HAML'.unindent)
#{'<script>alert("a");</script>'.html_safe}
HAML
end
specify 'attribute escape' do
assert_equal %Q|<a href='<script>alert("a");</script>'></a>\n|, render(<<-HAML.unindent)
%a{ href: '<script>alert("a");</script>' }
HAML
assert_equal %Q|<a href='<script>'></a>\n|, render(<<-HAML.unindent)
%a{ href: '<script>'.html_safe }
HAML
end
it 'forces to escape html_safe attributes' do
assert_equal <<-'HTML'.unindent, render(<<-HAML.unindent)
<meta content=''"'>
<meta content=''"'>
<meta content=''"'>
HTML
%meta{ content: %{'"}.html_safe }
- val = %{'"}.html_safe
%meta{ content: val }
- hash = { content: val }
%meta{ hash }
HAML
end
specify 'boolean attributes' do
assert_equal %Q|<span checked='no' disabled></span>\n|, render(<<-HAML.unindent)
- val = 'no'
%span{ disabled: true, checked: val, autoplay: false }
HAML
end
specify 'link_to' do
assert_equal %Q|<a class="bar" href="#">foo</a>\n|, render(%q|= link_to 'foo', '#', class: 'bar'|)
end
specify 'content_tag' do
assert_equal <<-HTML.unindent.strip, render(<<-HAML.unindent)
<div>text
</div>
HTML
= content_tag :div do
text
HAML
assert_equal <<-HTML.unindent.strip, render(<<-HAML.unindent)
<div>text
</div><div>text
</div><div>text
</div>
HTML
- 3.times do
= content_tag :div do
text
HAML
end
specify 'find_and_preserve' do
assert_equal <<-HTML.unindent, render(<<-'HAML'.unindent)
Foo bar
<pre>foo bar</pre>
<pre>foo&#x000A;bar</pre>
HTML
= find_and_preserve("Foo bar")
= find_and_preserve("<pre>foo bar</pre>")
= find_and_preserve("<pre>foo\nbar</pre>")
HAML
end
specify 'capture_haml' do
assert_equal <<-HTML.unindent, render(<<-'HAML'.unindent)
<div class='capture'><span>
<p>Capture</p>
</span>
</div>
HTML
- html = capture_haml do
%span
%p Capture
.capture= html
HAML
end
specify 'preserve' do
assert_equal %q|Foo
Bar|, render(<<-'HAML'.unindent)
= preserve do
Foo
Bar
HAML
assert_equal %q|<div />|, render(<<-'HAML'.unindent)
= preserve do
<div />
HAML
end
specify 'succeed' do
assert_equal %Q|<i>succeed</i>&\n|, render(<<-'HAML'.unindent)
= succeed '&' do
%i succeed
HAML
end
specify 'precede' do
assert_equal %Q|&<i>precede</i>\n|, render(<<-'HAML'.unindent)
= precede '&' do
%i precede
HAML
end
specify 'surround' do
assert_equal %Q|&<i>surround</i>&\n|, render(<<-'HAML'.unindent)
= surround '&' do
%i surround
HAML
end
specify 'object which returns SafeBuffer for to_s (like kaminari)' do
class ::TosUnsafeObject; def to_s; "<hr>"; end; end
class ::TosSafeObject; def to_s; "<hr>".html_safe; end; end
assert_equal %Q|<hr>\n|, render(%q|= ::TosSafeObject.new|)
assert_equal %Q|<hr>\n|, render(%q|= ::TosUnsafeObject.new|)
assert_equal %Q|<p><hr></p>\n|, render(%Q|%p= ::TosSafeObject.new|)
assert_equal %Q|<p>\n<hr>\n</p>\n|, render(%Q|%p\n = ::TosSafeObject.new|)
end
specify 'encoding' do
assert_equal Encoding::UTF_8, render('Test').encoding
end
specify '.set_options' do
original = Haml::RailsTemplate.options[:use_html_safe]
begin
Haml::RailsTemplate.set_options(use_html_safe: !original)
assert_equal !original, Haml::RailsTemplate.options[:use_html_safe]
ensure
Haml::RailsTemplate.set_options(use_html_safe: original)
end
end
end
|