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

Missing escape of labels #54

Closed
jacobat opened this issue Dec 20, 2012 · 4 comments
Closed

Missing escape of labels #54

jacobat opened this issue Dec 20, 2012 · 4 comments

Comments

@jacobat
Copy link

jacobat commented Dec 20, 2012

The following piece of code will break when trying to run in the browser:

data_table = GoogleVisualr::DataTable.new
data_table.new_column('number', "It's my party" )
chart = GoogleVisualr::Interactive::SteppedAreaChart.new(data_table, {})
chart.to_js('div_id')

If I puts the to_js call I get:

<script type='text/javascript'>
  google.load('visualization','1', {packages: ['corechart'], callback: function() {
    var data_table = new google.visualization.DataTable();data_table.addColumn('number', 'It's my party');
    var chart = new google.visualization.SteppedAreaChart(document.getElementById('div_id'));
    chart.draw(data_table, {});
  }});
</script>

Where the problem is:

data_table.addColumn('number', 'It's my party');

The quote in It's is what's breaking the javascript.

@kandadaboggu
Copy link
Contributor

You should use render_chart helper method instead of calling to_js directly, i.e.

render_chart(chart, 'div_id')

The render_chart helper performs the proper html escaping before returning.

@winston
Copy link
Owner

winston commented Dec 21, 2012

@jacobat is right. I didn't escape the labels:

@cols.each do |column|
    js << "data_table.addColumn("
    if column[:role].nil?
      js << column.map{ |key, value| "'#{value}'" }.join(", ")
    else
      js << "{" + column.map{ |key, value| "#{key}: '#{value}'" }.join(", ") + "}"
    end
    js << ");"
end

@jacobat
Copy link
Author

jacobat commented Dec 23, 2012

Would it be possible to construct the JS strings with to_json instead?

Say:

      @cols.each do |column|
        js << "data_table.addColumn("
        js << column.to_json
        js << ");"
      end

winston added a commit that referenced this issue Jan 19, 2013
@winston
Copy link
Owner

winston commented Jan 19, 2013

Fixed. Thank you!

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

3 participants