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

Pre-populating textarea adds extra newline characters #68

Closed
DanielNill opened this issue Apr 30, 2013 · 1 comment
Closed

Pre-populating textarea adds extra newline characters #68

DanielNill opened this issue Apr 30, 2013 · 1 comment

Comments

@DanielNill
Copy link

Assuming that @model.get("message") is a string with newline characters \n\r in it

%textarea.msgpost{:id => "post_#{@model.id}", :name => 'message', :placeholder => 'Add a comment, image or pdf...', :maxlength => '2000' }= @model.get("message")

returns a textarea with double the number of newline characters originally in the string.

However

%textarea.msgpost{:id => "post_#{@model.id}", :name => 'message', :placeholder => 'Add a comment, image or pdf...', :maxlength => '2000' }
    = @model.get("message")

does not insert extra newline characters.

Any ideas.

@netzpirat
Copy link
Owner

I made a little template for Ruby Haml:

- message = "hoho\n\rHehe"
- id = 12

%textarea.msgpost{:id => "post_#{id}", :name => 'message', :placeholder => 'Add a comment, image or pdf...', :maxlength => '2000' }= message

%textarea.msgpost{:id => "post_#{id}", :name => 'message', :placeholder => 'Add a comment, image or pdf...', :maxlength => '2000' }
  = message

and the output is

<textarea class='msgpost' id='post_12' maxlength='2000' name='message' placeholder='Add a comment, image or pdf...'>hoho&#x000A;Hehe</textarea>
<textarea class='msgpost' id='post_12' maxlength='2000' name='message' placeholder='Add a comment, image or pdf...'>hoho
Hehe</textarea>

So Ruby Haml does only whitespace preservation when adding the content directly to the textarea and not as child. Haml Coffee does the same:

- message = "hoho\n\rHehe"
- id = 12
%textarea.msgpost{:id => "post_#{id}", :name => 'message', :placeholder => 'Add a comment, image or pdf...', :maxlength => '2000' }= message

%textarea.msgpost{:id => "post_#{id}", :name => 'message', :placeholder => 'Add a comment, image or pdf...', :maxlength => '2000' }
  = message

and the output is:

<textarea class='msgpost' id='post_12' name='message' placeholder='Add a comment, image or pdf...' maxlength='2000'>hoho&#x000A;
Hehe</textarea>
<textarea class='msgpost' id='post_12' name='message' placeholder='Add a comment, image or pdf...' maxlength='2000'>hoho

Hehe</textarea>

Again, whitespace preservation is only done in the first example, so far so good, but the difference is that Ruby Haml converts CRLF (\r\n) to CR (\n), which Haml-Coffee does not. So this is a bug!

But the bug is only visible because I put the data directly within the template and with a real Backbone model the textarea preservation will not work because it's dynamic. In this case you anyway need to make use of ~:

%textarea.msgpost{:id => "post_#{id}", :name => 'message', :placeholder => 'Add a comment, image or pdf...', :maxlength => '2000' }~ message

%textarea.msgpost{:id => "post_#{id}", :name => 'message', :placeholder => 'Add a comment, image or pdf...', :maxlength => '2000' }
  ~ message

otherwise the find a preserve helper will not be called.

Bugs to be fixed:

  • Convert CRLF to CR when reading a template source.
  • The preserve helper needs to delete \r
  • The findAndPreserve helper needs to delete \r (HamlCoffeeAssets)

Thanks a lot for reporting.

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

No branches or pull requests

2 participants