Unicode-escaping h() variant
December 22nd, 2005Put 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).