remotipart icon indicating copy to clipboard operation
remotipart copied to clipboard

Response needs to be HTML escaped

Open mdesantis opened this issue 10 years ago • 7 comments

As I wrote in the issue #122, I have some response errors (no response execution, syntax errors, ...) due to the fact that the response contains a render partial: ..., and remotipart wraps response into a <textarea data-type="text/javascript">...</textarea>, so its content should be HTML escaped. I resolved turning the create.js.erb view from this:

<%- rendering = j render(partial: 'form_errors') %>
$('#form_errors').replaceWith('<%= rendering %>');

into this:

<%# .to_str turns an ActiveSupport::SafeBuffer instance into a String,
  which gets HTML escaped %>
<%- rendering = j render(partial: 'form_errors').to_str %>
$('#form_errors').replaceWith('<%= rendering %>');

I don't know whether this behaviour is expected or not, but I think it should at least documented in the Readme, since it leads to unexepcted errors.

mdesantis avatar Jan 12 '15 23:01 mdesantis

Had to do this recently, haven't had this problem in the past...

chrise86 avatar Aug 19 '15 09:08 chrise86

Thank you for raising this, spent a good few hours trying to work out this one. Particularly frustrating since remotipart also swallows ajax requests in the console. Agree that it should be documented.

tombeynon avatar Sep 27 '15 17:09 tombeynon

Thanks for sharing the solution @mdesantis , I was having the same issue here.

alexbrahastoll avatar Sep 30 '15 18:09 alexbrahastoll

You're welcome, happy to help

mdesantis avatar Oct 01 '15 01:10 mdesantis

There seems to be a difference from version 1.0 to 1.2 where the old code set the text like so:

responses = { text: type ? textarea.value : root ? root.innerHTML : null }

Whereas the new code does it like this:

content = {
                  html: root.innerHTML,
                  text: type ?
                    textarea.value :
                    root ? (root.textContent || root.innerText) : null
                };

So it's using root.textContent or root.innerText. I suspect root.textContent escapes any &#39; codes with single quotes. So then the javascript will be broken as it will have single quotes that are unescaped.

yads avatar Dec 23 '15 17:12 yads

Thanks @mdesantis as well. Your workaround made my day!

jloosfelt avatar Mar 10 '16 09:03 jloosfelt

Thanks @mdesantis, This works if I have remotipart data, but currently, we have a scenario where we use the same form for different cases. If remotipart is not being used in that, then this fails because the response is HTML escaped.

snkshukla avatar May 11 '17 12:05 snkshukla