Thomas Fuchs
Hi, I'm Thomas Fuchs. I'm the author of Zepto.js, of script.aculo.us, and I'm a Ruby on Rails core alumnus. With Amy Hoy I'm building cheerful software, like Noko Time Tracking and Every Time Zone and write books like Retinafy.me.
   Want me to speak at your conference? Contact me!

Unicode-escaping h() variant

December 22nd, 2005

Put this in application_helper.rb:


def u(html)
  h(html).gsub(/([^x00-x9f])/u) { |s|
    "&#x%x;" % $1.unpack('U')[0] }
end

This will automatically output 香-style HTML entities where needed.

I’m using it for escaping HTML inside of Edge Rails .rjs templates, to ensure compatibility across browsers (especially Safari seems to be a bit buggy here with support for UTF-8 in Ajax calls).