vega 0.2.2 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 455e0c4c1814906c620cc31ddd7ded69330f51b8722abfe23f895ab0bd0e47b0
4
- data.tar.gz: 2264a6053aa5588decd30420935452f9c7426d707f5564ff05644e473a463296
3
+ metadata.gz: 12a40c67ddedf70381a6a20c4224bf028910fbf92e6c44c13ca31e5a2465d1a9
4
+ data.tar.gz: 1e33341d2b573078e6ee30984b8c7ba15638a26d1678c426eaf5ddb248796b8f
5
5
  SHA512:
6
- metadata.gz: f08e7e57fc295a1f43c5ea0766c69582e6a4ece29dec97beefce16ace22665a3b09d26b9c0c1166628a0454d67f2f0e4f12a0e64d8edf2963c7980f9cb10dc09
7
- data.tar.gz: 9e0f2da88f1fe97adb15d8135fd66b3b55782e2c103c7f26cd0290fddc0383ff3b7b4911240da7ce6dbd72da2ad06d6e544c4d512444bccd25f3edb1121b3009
6
+ metadata.gz: f3f04599a9a099d2572b0778ef6d7043993eb15bb46412ef8c31e6e2dff2bd6188ca20851969fd16d9dd1203c7ca6d36ef5ba437ab5e138344ba9564878aa813
7
+ data.tar.gz: caab809560db84c55fc8015804468e97b5bf5c7a89139436c9bc70b0229775f2b39db5c31109b783ab02e31a1fb0c27b5769e0d70165c005ddf594ff88e63495
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## 0.2.6 (2022-02-02)
2
+
3
+ - Added `vega_chart` helper
4
+ - Added `nonce` option
5
+ - Added `to_html` method
6
+
7
+ ## 0.2.5 (2022-01-22)
8
+
9
+ - Updated Vega-Lite to 5.2.0 and Vega-Embed to 6.20.5
10
+
11
+ ## 0.2.4 (2022-01-15)
12
+
13
+ - Support for `importmap-rails` is no longer experimental
14
+ - Fixed asset precompilation error with `importmap-rails`
15
+
16
+ ## 0.2.3 (2021-11-15)
17
+
18
+ - Updated Vega-Embed to 6.20.1 to fix spec version warnings
19
+ - Fixed error with iRuby
20
+
1
21
  ## 0.2.2 (2021-11-06)
2
22
 
3
23
  - Added experimental support for exporting charts
data/README.md CHANGED
@@ -13,12 +13,13 @@ Works with Rails, iRuby, and other frameworks
13
13
  Add this line to your application’s Gemfile:
14
14
 
15
15
  ```ruby
16
- gem 'vega'
16
+ gem "vega"
17
17
  ```
18
18
 
19
- The follow the instructions for how you plan to use it:
19
+ Then follow the instructions for how you plan to use it:
20
20
 
21
- - [Rails 7 / Importmap](#rails-7--importmap) (experimental)
21
+ - [Rails 7 / Importmap](#rails-7--importmap)
22
+ - [Rails 7 / esbuild or Webpack](#rails-7--esbuild-or-webpack)
22
23
  - [Rails 6 / Webpacker](#rails-6--webpacker)
23
24
  - [Rails 5 / Sprockets](#rails-5--sprockets)
24
25
  - [iRuby](#iruby)
@@ -41,7 +42,24 @@ import "vega"
41
42
  import "vega-lite"
42
43
  import "vega-embed"
43
44
 
44
- window.dispatchEvent(new Event("vega:load"));
45
+ window.dispatchEvent(new Event("vega:load"))
46
+ ```
47
+
48
+ ### Rails 7 / esbuild or Webpack
49
+
50
+ Run:
51
+
52
+ ```sh
53
+ yarn add vega vega-lite vega-embed
54
+ ```
55
+
56
+ And add to `app/javascript/application.js`:
57
+
58
+ ```js
59
+ import embed from "vega-embed"
60
+
61
+ window.vegaEmbed = embed
62
+ window.dispatchEvent(new Event("vega:load"))
45
63
  ```
46
64
 
47
65
  ### Rails 6 / Webpacker
@@ -74,12 +92,12 @@ No additional set up is needed.
74
92
 
75
93
  ### Other
76
94
 
77
- For Sinatra and other web frameworks, include the Vega JavaScript files on pages with charts:
95
+ For Sinatra and other web frameworks, download [Vega](https://cdn.jsdelivr.net/npm/vega@5), [Vega-Lite](https://cdn.jsdelivr.net/npm/vega-lite@5), and [Vega-Embed](https://cdn.jsdelivr.net/npm/vega-embed@6) and include them on pages with charts:
78
96
 
79
97
  ```html
80
- <script src="https://cdn.jsdelivr.net/npm/vega@5.21.0"></script>
81
- <script src="https://cdn.jsdelivr.net/npm/vega-lite@5.1.1"></script>
82
- <script src="https://cdn.jsdelivr.net/npm/vega-embed@6.19.1"></script>
98
+ <script src="vega.js"></script>
99
+ <script src="vega-lite.js"></script>
100
+ <script src="vega-embed.js"></script>
83
101
  ```
84
102
 
85
103
  ## Getting Started
@@ -324,7 +342,7 @@ spec = {
324
342
  And render it in Rails
325
343
 
326
344
  ```erb
327
- <%= Vega.render(spec) %>
345
+ <%= vega_chart spec %>
328
346
  ```
329
347
 
330
348
  Or display it in iRuby
@@ -367,6 +385,29 @@ File.binwrite("chart.pdf", chart.to_pdf)
367
385
 
368
386
  ## Content Security Policy (CSP)
369
387
 
388
+ ### Styles and Frames
389
+
390
+ Enable unsafe inline styles and blob frames on actions that have charts
391
+
392
+ ```ruby
393
+ class ChartsController < ApplicationController
394
+ content_security_policy only: :index do |policy|
395
+ policy.style_src :self, :unsafe_inline
396
+ policy.frame_src :blob
397
+ end
398
+ end
399
+ ```
400
+
401
+ ### Nonce
402
+
403
+ Automatically add a nonce when configured in Rails with:
404
+
405
+ ```erb
406
+ <%= vega_chart chart %>
407
+ ```
408
+
409
+ ### Interpreter
410
+
370
411
  By default, the Vega parser uses the Function constructor, which [can cause issues with CSP](https://vega.github.io/vega/usage/interpreter/).
371
412
 
372
413
  For Rails 7 / Importmap, add to `config/importmap.rb`:
@@ -18,14 +18,20 @@ module Vega
18
18
  end
19
19
  immutable_method :embed_options
20
20
 
21
- def to_s
22
- Spec.new(spec).to_s
21
+ def to_html(nonce: nil)
22
+ Spec.new(spec).to_html(nonce: nonce)
23
23
  end
24
+ alias_method :to_s, :to_html
24
25
 
25
26
  def to_iruby
26
27
  Spec.new(spec).to_iruby
27
28
  end
28
29
 
30
+ # for https://github.com/SciRuby/iruby/issues/314
31
+ def to_iruby_mimebundle(**)
32
+ [[to_iruby].to_h, {}]
33
+ end
34
+
29
35
  def to_json
30
36
  spec.to_json
31
37
  end
data/lib/vega/engine.rb CHANGED
@@ -3,8 +3,8 @@ module Vega
3
3
  # for assets
4
4
 
5
5
  # for importmap
6
- if defined?(Importmap)
7
- initializer "vega.importmap", after: "importmap" do |app|
6
+ initializer "vega.importmap" do |app|
7
+ if defined?(Importmap)
8
8
  app.config.assets.precompile << "vega-embed.js"
9
9
  app.config.assets.precompile << "vega-interpreter.js"
10
10
  app.config.assets.precompile << "vega-lite.js"
@@ -0,0 +1,29 @@
1
+ module Vega
2
+ module Helper
3
+ def vega_chart(chart, nonce: true)
4
+ unless chart.is_a?(Vega::BaseChart) || chart.is_a?(Hash)
5
+ raise TypeError, "expected Vega chart or spec"
6
+ end
7
+
8
+ if nonce == true
9
+ # Secure Headers also defines content_security_policy_nonce but it takes an argument
10
+ # Rails 5.2 overrides this method, but earlier versions do not
11
+ if respond_to?(:content_security_policy_nonce) && (content_security_policy_nonce rescue nil)
12
+ # Rails 5.2
13
+ nonce = content_security_policy_nonce
14
+ elsif respond_to?(:content_security_policy_script_nonce)
15
+ # Secure Headers
16
+ nonce = content_security_policy_script_nonce
17
+ else
18
+ nonce = nil
19
+ end
20
+ end
21
+
22
+ if chart.is_a?(Hash)
23
+ Vega.render(chart, nonce: nonce)
24
+ else
25
+ chart.to_html(nonce: nonce)
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/vega/spec.rb CHANGED
@@ -6,11 +6,12 @@ module Vega
6
6
  @spec = spec.transform_keys!(&:to_s)
7
7
  end
8
8
 
9
- def to_s
9
+ def to_html(nonce: nil)
10
10
  html, js = generate_output
11
+ nonce_html = nonce ? " nonce=\"#{ERB::Util.html_escape(nonce)}\"" : nil
11
12
  output = <<~EOS
12
13
  #{html}
13
- <script>
14
+ <script#{nonce_html}>
14
15
  (function() {
15
16
  var createChart = function() { #{js} };
16
17
  if ("vegaEmbed" in window) {
@@ -23,6 +24,7 @@ module Vega
23
24
  EOS
24
25
  output.respond_to?(:html_safe) ? output.html_safe : output
25
26
  end
27
+ alias_method :to_s, :to_html
26
28
 
27
29
  # TODO only load vega-lite if $schema requires it
28
30
  def to_iruby
@@ -34,8 +36,8 @@ module Vega
34
36
  paths: {
35
37
  'vega': 'https://cdn.jsdelivr.net/npm/vega@5.21.0/build/vega.min',
36
38
  'vega-util': 'https://cdn.jsdelivr.net/npm/vega-util@1.17.0/build/vega-util.min',
37
- 'vega-lite': 'https://cdn.jsdelivr.net/npm/vega-lite@5.1.1/build/vega-lite.min',
38
- 'vega-embed': 'https://cdn.jsdelivr.net/npm/vega-embed@6.19.1/build/vega-embed.min'
39
+ 'vega-lite': 'https://cdn.jsdelivr.net/npm/vega-lite@5.2.0/build/vega-lite.min',
40
+ 'vega-embed': 'https://cdn.jsdelivr.net/npm/vega-embed@6.20.5/build/vega-embed.min'
39
41
  }
40
42
  });
41
43
  require(['vega', 'vega-util', 'vega-lite', 'vega-embed'], function(vega, vegaUtil, vegaLite, vegaEmbed) {
data/lib/vega/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vega
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.6"
3
3
  end
data/lib/vega.rb CHANGED
@@ -9,6 +9,7 @@ require "vega/base_chart"
9
9
  require "vega/chart"
10
10
  require "vega/lite_chart"
11
11
  require "vega/spec"
12
+ require "vega/helper"
12
13
  require "vega/version"
13
14
 
14
15
  # integrations
@@ -25,8 +26,8 @@ module Vega
25
26
  LiteChart.new
26
27
  end
27
28
 
28
- def render(spec)
29
- Spec.new(spec).to_s
29
+ def render(spec, nonce: nil)
30
+ Spec.new(spec).to_html(nonce: nonce)
30
31
  end
31
32
 
32
33
  def display(spec)
@@ -34,3 +35,9 @@ module Vega
34
35
  end
35
36
  end
36
37
  end
38
+
39
+ if defined?(ActiveSupport.on_load)
40
+ ActiveSupport.on_load(:action_view) do
41
+ include Vega::Helper
42
+ end
43
+ end