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

Final newline is rendered #913

Closed
boardfish opened this issue May 6, 2021 · 8 comments · Fixed by #1207
Closed

Final newline is rendered #913

boardfish opened this issue May 6, 2021 · 8 comments · Fixed by #1207

Comments

@boardfish
Copy link
Collaborator

boardfish commented May 6, 2021

Reduced test case: https://github.com/boardfish/view_component-bug

Steps to reproduce

Render a component that uses a HTML element with display: inline by default, e.g. span or a, inline.

Expected behavior

There should be no trailing whitespace. i.e. I should be able to do this:

<%= render TestComponent.new %>,

and receive:

<a href="https://simon.fish">Add Test template here</a>,

as opposed to:

<a href="https://simon.fish">Add Test template here</a>
,

Actual behavior

It seems as though the final newline of the template file is rendered, even though this usually indicates the end of a file. This is interpreted by browsers to mean whitespace.

image

image

The newline preceding the comma is rendered from the component template. The following whitespace is part of the view template.

System configuration

Rails version: 6.1.3.2

Ruby version: 2.7.3

Gem version: 2.31.1

@BlakeWilliams
Copy link
Contributor

Thanks for opening this! We should definitely support this case.

Any interest in opening a pull request to fix this issue? Even if the PR only includes a failing test that would help us resolve the issue sooner.

@boardfish
Copy link
Collaborator Author

Yeah, I'd be happy to do that. I'll get on it right away ✨

@boardfish
Copy link
Collaborator Author

Alright, done 👍

@boardfish
Copy link
Collaborator Author

Raised rails/rails#42201, because this issue's really more in the remit of Action View. Whether it gets fixed by proxy of a change to Rails is something we can't yet be sure of, so I'd leave this open.

The failing test can probably also be taken out or adapted to document the fix if a change to Rails will fix it.

@boardfish
Copy link
Collaborator Author

That PR got merged, so I'll raise another one here to change those tests back to their original form. If the change to the ERB renderer fixed the issue here, then there shouldn't be a need for further work. Otherwise, I'll consider adding a config option to ViewComponent for this.

@boardfish
Copy link
Collaborator Author

boardfish commented Jun 10, 2021

ActionView::Template::Handlers::ERB.strip_trailing_newlines = true is the fix for this issue. Add it to your config/application.rb and it should work.

@boardfish
Copy link
Collaborator Author

I'm updating the tests in #958 - can I get some advice on how best to do this (if at all just yet) given that the functionality is only in edge Rails?

@boardfish
Copy link
Collaborator Author

boardfish commented Nov 5, 2021

Just encountered this again - if render_in is used, the final newline will still be rendered in spite of the setting. I imagine all that needs to be done is (pseudocode):

def render_in(view_context)
  output = super
  return output unless ActionView::Template::Handlers::ERB.strip_trailing_newlines

  output.chomp
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants