Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

string added to rails_helpers gets HTML escaped

#4695

If the first part of a string is a rails helper the rest of the string gets HTML escaped:

h('test')+'<br/>' # => wrong: test&lt;br&gt;
''+h('test')+'<br/>' # => ok: test<br/>
"#{h('test')}<br/>" # => ok: test<br/>

Reported by Fjan · May 25th, 2010 @ 08:46 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Neeraj Singh
    Neeraj Singh

    In my view it is a consistent behavior.

    >> ''+ERB::Util.html_escape('test') + '<br />'
    => "test<br />"
    
    >> ''.html_safe+ERB::Util.html_escape('test') + '<br />'
    => "test&lt;br /&gt;"
    
    >> ERB::Util.html_escape('test') + '<br />'.html_safe
    => "test<br />"
    

    In the first example given by the Fjan, not safe html is being added to safe html. Probably you already know the solution and that is to mark br as safe as I did in my last example.

    May 25th, 2010 @ 10:20 PM

  2. Fjan
    Fjan

    Yes, it is consistent, however it is also a bug since it is consistently different from Rails versions < 2.3.8 and therefore it breaks a lot of apps which is not allowed in a point release. (I had to fix around 20 occurrences in my apps)

    May 25th, 2010 @ 10:27 PM

  3. Neeraj Singh
  4. Fjan
    Fjan

    great :-)

    Well, I fixed everything by now. I actually amended the test helper to find all of them and it found a few more I hadn't caught before. It's a simple one-line addition to the test suite this, I'm going to leave it in:

      raise "Double escaped HTML in text (&#{$1};)" if @response.body =~ /&amp;(raquo|lt|gt|nbsp|amp)/
    

    May 25th, 2010 @ 10:56 PM

  5. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • Milestone set to 2.3.8
    • State changed from new to resolved

    In the meantime, you can apply the following monkey-patch:

    class ActiveSupport::SafeBuffer
      def concat(*args) super end
      alias << concat
    end
    

    This will remove any trace of html_safe concatenation.

    May 25th, 2010 @ 11:43 PM

  6. Jeremy Kemper
  7. Dan Nuttall
    Dan Nuttall

    This commit doesn't seem to have made it into the 2.3.8 tag:
    http://github.com/rails/rails/blob/v2.3.8/activesupport/lib/active_...

    Was this an oversight or was it left out for a reason?

    June 1st, 2010 @ 02:40 PM

  8. Matthew Horan
    Matthew Horan

    Anyone? Bueller? This has forced us to stick with 2.3.5.

    June 4th, 2010 @ 09:12 PM