Haml: Extra whitespace modifies multiline value of input field

Created on 21 Jan 2015  路  6Comments  路  Source: haml/haml

I've run into a quirk using HAML 4.0.6 and Rails 4.1.8 and Rails 4.2.0, when the ugly option is set to false, like in my development environment.

It involves a multiline string value for an <input> tag getting padded, and modifying the original value.

I find it kind of hard to explain, so I created a small repo that has this behavior.

I've got a multiline string:
https://github.com/kyletolle/haml_playground/blob/master/app/models/tester.rb#L3

<<eos
This is a
multiline string,
which is helpful

for testing!
eos

That I use as the value for a hidden input field:
https://github.com/kyletolle/haml_playground/blob/master/app/views/testers/_form.html.haml#L13

In my development environment, HAML has the ugly option set to false, which means it pads all the output with whitespace for readability.

Except, it also pads lines which are part of the value attribute, when it's a multiline string. So, in effect, it ends up modifying the value that I'm putting into the input:
haml-quirk

You can see the actual output and my expected output here:
https://gist.github.com/kyletolle/b878f2e6de3100be9379

I have the example running on Heroku too, so you can see it in progress.
https://haml-playground.herokuapp.com/testers/new

To help highlight the problem, I take the value of that hidden input field and then put it into a text area. In the textarea, you can see the extra spaces. So, the string I expect to be output in the textarea is not what's actually there, because of this extra whitespace.

This isn't an issue in production, since it doesn't pad the markup for readability. The ugly option is set to true. But I do run into it while developing.

Is this expected behavior? Or is this a bug? Feels like a bug, since it's actually modifying data.

Also, is there a workaround? I like having the readable markup, so I'd like to keep ugly equal to false. But I wonder if there might be another way to keep the multiline string value from being padded with spaces?

I have seen #643, which seems to be in the same space as this.

If I can provide any more information, please let me know.

I appreciate all the work you've done with HAML!

Most helpful comment

I stumbled over this as well - what fixed it for me is using ~ instead of =. Eg:

.col-md-2~ f.text_area :script, class: "form-control"

instead of

.col-md-2= f.text_area :script, class: "form-control"

I found the info in the FAQ.

All 6 comments

Here's an additional screenshot of what the extra space looks like in the text area:

screenshot 2015-01-20 17 43 40

I've the same issue.
My solution is change the text field to text area and set its class to hidden.

this is a variation of bug#506 and dunno why this isn't fixed for development mode? it should at least be document. Should a add a line to the readme?

I had a similar problem (reported here http://stackoverflow.com/questions/34804880/weird-behavior-on-form-data-signature-with-a-get-request/34805154). This helped me solve my issue. Thanks

I stumbled over this as well - what fixed it for me is using ~ instead of =. Eg:

.col-md-2~ f.text_area :script, class: "form-control"

instead of

.col-md-2= f.text_area :script, class: "form-control"

I found the info in the FAQ.

This may be resolved by Haml 5, which doesn't have ugly: false. Please try Haml 5.0.0.beta.2.

And some operators described in https://github.com/haml/haml/issues/828#issuecomment-217467178 might be helpful.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tisba picture tisba  路  10Comments

elia picture elia  路  3Comments

ollym picture ollym  路  42Comments

mattwildig picture mattwildig  路  9Comments

modsognir picture modsognir  路  6Comments