k0kubun / hamlit

High Performance Haml Implementation
https://rubygems.org/gems/hamlit
Other
981 stars 59 forks source link

Missing newline when using link_to with blocks #65

Closed harmdewit closed 8 years ago

harmdewit commented 8 years ago

When using a rails helper like link_to, there is a difference in output when using a block or not. For example:


%div
  %a{href: '#'} This is a link
  %a{href: '#'} This is a link
%div
  %a{href: '#'}
    This is a link
  %a{href: '#'}
    This is a link
%div
  = link_to 'This is a link', '#'
  = link_to 'This is a link', '#'
%div
  = link_to '#' do
    This is a link
  = link_to '#' do
    This is a link

Result:

<div>
<a href="#">This is a link</a>
<a href="#">This is a link</a>
</div>
<div>
<a href="#">
This is a link
</a>
<a href="#">
This is a link
</a>
</div>
<div>
<a href="#">This is a link</a>
<a href="#">This is a link</a>
</div>
<div>
<a href="#">This is a link
</a><a href="#">This is a link
</a></div>

All variations produce newlines between the <a>'s, except for the last link_to with block. This results in the same missing margins as seen in this issue.

Maybe this is caused by the 'ugly-only' support, which i'm ok with. But having different results caused by using a block or not seems confusing.

k0kubun commented 8 years ago

Thank you for using Hamlit and reporting this.

Maybe this is caused by the 'ugly-only' support, which i'm ok with. But having different results caused by using a block or not seems confusing.

Actually this happens on both pretty mode and ugly mode of original Haml.

input

%div
  %a{ href: '#' }
    a
  = link_to '#' do
    b

output

haml, RAILS_ENV=development (pretty mode)

<div>
  <a href='#'>
    a
  </a>
  <a href="#">b
  </a>
</div>

haml, RAILS_ENV=production (ugly mode)

<div>
<a href='#'>
a
</a>
<a href="#">b
</a></div>

hamlit

<div>
<a href='#'>
a
</a>
<a href="#">b
</a></div>

Since Hamlit is aimed to be similar to Haml's ugly mode as much as possible, at least this is not a bug in Hamlit.

In both %a and link_to, "This is a link\n" is the block's content and they're not different. The different things are the fact that nested %a prepends "\n" to the nested content and the one that link_to with block does not. It's the issue of link_to helper with block and out-of-scope for Hamlit.

In addition, if you want to trim newline inside link_to's block, you can use < operator.

%div<
  = link_to '#' do
    This is a link

%div<
  %a{ href: '#' }<
    This is a link

is rendered to:

<div><a href="#">This is a link</a></div>
<div><a href='#'>This is a link</a></div>
harmdewit commented 8 years ago

Thanks for the explanation. Just seems strange that a normal content_tag without block has a preceding \n and a content_tag without block doesn't. But if it's internal to rails and standard haml works the same way, then i agree it should be kept as it is.