Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip trailing whitespace in templates #1379

Merged
merged 13 commits into from
Jun 21, 2022
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ title: Changelog

## main

* Strip newlines from templates before compilation to avoid unwanted newlines.

*Cameron Dutro*

## 2.56.1

* Rename private accessor `rendered_component` to `rendered_content`.
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def variants_from_inline_calls(calls)

def compiled_template(file_path)
handler = ActionView::Template.handler_for_extension(File.extname(file_path).gsub(".", ""))
template = File.read(file_path)
template = File.read(file_path).rstrip
camertron marked this conversation as resolved.
Show resolved Hide resolved
camertron marked this conversation as resolved.
Show resolved Hide resolved

if handler.method(:call).parameters.length > 1
handler.call(component_class, template)
Expand Down
2 changes: 1 addition & 1 deletion test/sandbox/test/layouts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LayoutsTest < ActionDispatch::IntegrationTest
test "rendering with_content_for" do
get "/layout_with_content_for"
assert_response :success
assert_select 'body[data-layout="with_content_for"]', "Hello content for\n\n Foo: bar"
assert_select 'body[data-layout="with_content_for"]', "Hello content for\n Foo: bar"
camertron marked this conversation as resolved.
Show resolved Hide resolved
end
end
end