winston / google_visualr

A Ruby Gem for the Google Visualization API. Write Ruby code. Generate Javascript. Display a Google Chart.

Home Page:http://googlevisualr.heroku.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with escaping apostrophes

ghiculescu opened this issue · comments

Similar to #54. I'm making an organisation chart, based on the code at http://googlevisualr.herokuapp.com/examples/interactive/org_chart, but the users are being set dynamically so I'm using string interpolation. This means I have to use apostrophes to set attributes on the HTML tags, though.

org_chart = GoogleVisualr::DataTable.new
org_chart.new_column('string', 'Name')
org_chart.new_column('string', 'Boss')
rows = []
rows << [{v: "Bob", f: 'Bob<div style="color:red; font-style:italic">CEO</div>'}, '']
[{name: "Fred", role: "VP"}, {name: "John", role: "VP"}, {name: "Dave", role: "VP"}].each do |data|
  rows << [{v: data[:name], f: "#{data[:name]}<div style='color:red; font-style:italic'>#{data[:role]}</div>"}, 'Bob']
  # the row above is the culprit
end
org_chart.add_rows(rows)
opts   = { :allowHtml => true }
@chart = GoogleVisualr::Interactive::OrgChart.new(org_chart, opts)

In Chrome, this throws a Uncaught SyntaxError: Unexpected identifier. The javascript looks like this:

Screen Shot 2013-02-12 at 11 49 04 PM

If I add .gsub("'", '"') to the row in question, or if I escape the quotation marks myself, everything renders fine, but it's not very elegant.

I'm using v2.1.6 of the gem.