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

Decoding removes £ entity when given a SafeBuffer #13

Closed
nathany opened this issue Oct 22, 2013 · 2 comments
Closed

Decoding removes £ entity when given a SafeBuffer #13

nathany opened this issue Oct 22, 2013 · 2 comments

Comments

@nathany
Copy link

nathany commented Oct 22, 2013

Use Case

The Rails number_to_currency helper returns currency symbols as HTML entities. When exporting an HTML report to CSV, we would like to use the UTF-8 symbol instead.

Versions

  • ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.5.0]
  • Rails 4.0.0 (same issue with 3.x)
  • htmlentities (4.3.1)

Steps to Reproduce

New rails app:

rails new testapp
cd testapp
echo "gem 'htmlentities'" >> Gemfile
bundle
rails c

In console:

HTMLEntities.new.decode("£12.34") # => "£12.34"
ActiveSupport::SafeBuffer.new("£12.34") # => "£12.34"
HTMLEntities.new.decode(ActiveSupport::SafeBuffer.new("£12.34")) # => "12.34"

It decodes the way I would want when not given a SafeBuffer, but removes the entity entirely when given a SafeBuffer.

Work around

Instead of using a nice generic solution like HTMLEntities, I end up using a few gsubs to get the job done:

str.to_s.gsub("£", "\u00a3").gsub("€", "\u20ac").gsub("č", "\u010d")

(which works, but then this list needs to be maintained).

@threedaymonk
Copy link
Owner

I've looked into this. In short, ActiveRecord::SafeBuffer breaks gsub.

"hello".gsub(/(.)/) { $1 } # => "hello"
ActiveSupport::SafeBuffer.new("hello").gsub(/(.)/) { $1 } # => "ooooo"

I can't find a generic way to fix this, but I can suggest a workaround: call to_str on the buffer:

HTMLEntities.new.decode(ActiveSupport::SafeBuffer.new("£12.34").to_str) # => "£12.34"

@nathany
Copy link
Author

nathany commented Oct 23, 2013

Thanks. I was looking for a work-around and never tried to_str (to_s didn't help).

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