I use Haml in a Rails 3 application and its new lines put me nuts! For instance,
%span foo
displayed as
<span> foo </span>
But I would really like to
<span>foo</foo>
The reason (besides pure XML) is that extra line breaks are a problem for my Selenium tests because they ruined the ability to execute XPath queries, such as "//span[.='foo']" . "//span[.='foo']" So I would have to write '\nfoo\n' instead of (ew!) Or use //span[contains(text(), 'foo')] , which matches too broadly.
I know that I could use alligator operators ("<" and ">") to separate spaces, but since I never had a case where I want newlines to appear on the output, I would just end up adding them mechanically at the end of each line. And it just seems very unfortunate.
So now I see two solutions:
- Force Haml will never produce newlines unless they receive a Ruby expression. I see some
nuke_inner_whitespace / nuke_outer_whitespace variables distributed around Haml code that can do the job, but I'm not sure how to change them without resorting to free monkey fixes. - Connect to Rails to apply
gsub!("\n", "") to all displayed HTML. (For textarea and pre's, I could still use ~ "foo\nbar" to make Haml emit foo
bar .) But where is the right place to connect to Rails? I lost a little code.
Any pointers or other suggestions appreciated!
Update: I used the Jason Monkey patch below for a while, and I'm starting to think it is not worth it. For instance. if I want to get <span>[del]</span> <span>foo</span> , itβs hard not to have a space between the core kernels. Even the following will display as [del]foo on the page:
%span = '[del] ' %span foo
So, I think I'll get back to adding alligator operators manually (see my own answer below). Live and learn.
Thanks again to Jason! :)
source share