Special Functions for dealing with Whitespace in HAML

Sometimes HAML will generate an unwanted space in your HTML, especially when you mix HAML markup and rails helpers.

For example, this piece of HAML:

.form-group
  By clicking Login, you agree to our
  = link_to "Terms of Service", "http://www.example.com"
  \.

Will generate this HTML:

<div class="form-group">
  By clicking Login, you agree to our
  <a href="http://www.example.com">Terms of Service</a>
  .
</div>

This markup results in a space between the ‘Terms of Service’ link and the period at the end. Note that I also had to escape the period in the HAML markup, otherwise the HAML compiler will throw a syntax error.

An easy way to fix this is to use the succeed method.

.form-group
  By clicking Login, you agree to our
  = succeed "." do
    = link_to "Terms of Service", "http://www.example.com"

There are also precede and surround methods which do what you would expect.

Happy coding.