huacnlee / social-share-button

Helper for add social share feature in your Rails app. Twitter, Facebook, Weibo, Douban ...
MIT License
582 stars 311 forks source link

Escape html for description #129

Open beagleknight opened 7 years ago

beagleknight commented 7 years ago

Hi there!

We are currently using this gem in our project https://github.com/AjuntamentdeBarcelona/decidim and we found a bug when having descriptions with single quotes in the description like s'explicarà.

I checked your gem and I see you are using html_escape for the title but not for the description. I just want to know if this is intended or not 😄

Thanks!

jcuervo commented 5 years ago

Anybody who comes down this line trying to truncate description for whatever use and breaking the the layout for one way or another, the solution I implemented was via nokogiri:

def social_share_description(description)
  doc = Nokogiri::HTML::DocumentFragment.parse(description)

  doc.search('br').remove
  doc.to_html
end

This sample strips off all the br tags, irregardless if they are written in <br>, <br />, <br >. You might need to require 'nokogiri'.