-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restoring the '%' trim mode for ERb templates, allowing for a leading…
… percent sign on a line to indicate non-inserted Ruby code.
- Loading branch information
Showing
3 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require "abstract_unit" | ||
|
||
class HandlersTest < ActiveSupport::TestCase | ||
HANDLER = ActionView::Template::Handlers::ERB | ||
Template = Struct.new(:source) | ||
|
||
extend ActiveSupport::Testing::Declarative | ||
|
||
test "content is not trimmed without a trim mode" do | ||
with_erb_trim_mode nil do | ||
assert_equal(" \ntest", render(" <% 'IGNORED' %> \ntest")) | ||
end | ||
end | ||
|
||
test "content around tags is trimmed if the trim mode includes a dash" do | ||
with_erb_trim_mode '-' do | ||
assert_equal("test", render(" <% 'IGNORED' %> \ntest")) | ||
end | ||
end | ||
|
||
test "percent lines are normal content without a trim mode" do | ||
with_erb_trim_mode nil do | ||
assert_equal( "% if false\noops\n% end\n", | ||
render("% if false\noops\n% end\n") ) | ||
end | ||
end | ||
|
||
test "percent lines count as ruby if trim mode includes a percent" do | ||
with_erb_trim_mode "%" do | ||
assert_equal("", render("% if false\noops\n% end\n")) | ||
end | ||
end | ||
|
||
test "both trim modes can be used at the same time" do | ||
with_erb_trim_mode "%-" do | ||
assert_equal( "test", render( "% if false\noops\n% end\n" + | ||
" <% 'IGNORED' %> \ntest" ) ) | ||
end | ||
end | ||
|
||
private | ||
|
||
def with_erb_trim_mode(mode) | ||
@old_erb_trim_mode = HANDLER.erb_trim_mode | ||
HANDLER.erb_trim_mode = mode | ||
yield | ||
ensure | ||
HANDLER.erb_trim_mode = @old_erb_trim_mode | ||
end | ||
|
||
def render(template) | ||
eval("output_buffer = nil; " + HANDLER.call(Template.new(template))) | ||
end | ||
end |