Skip to content

Commit e85199b

Browse files
worldwise001mathieujobin
authored andcommitted
Move inline JS to rendered files to support CSP
1 parent 1339817 commit e85199b

File tree

8 files changed

+30
-26
lines changed

8 files changed

+30
-26
lines changed

app/controllers/teaspoon/suite_controller.rb

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def show
1616
@suite = Teaspoon::Suite.new(params)
1717
end
1818

19+
def js
20+
jstype = params.extract!(:jstype).fetch(:jstype, 'vanilla')
21+
@suite = Teaspoon::Suite.new(params)
22+
render "teaspoon/suite/#{jstype}", :content_type => 'application/javascript'
23+
end
24+
1925
def hook
2026
hooks = Teaspoon::Suite.new(params).hooks[params[:hook].to_s]
2127

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
<%= javascript_include_tag *@suite.spec_assets, debug: @suite.config.expand_assets %>
2-
<script type="text/javascript">
3-
Teaspoon.onWindowLoad(Teaspoon.execute);
4-
</script>
2+
<script src="<%= "#{Teaspoon.configuration.mount_at}/js/#{@suite.name}.vanilla.js" %>"></script>
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,2 @@
1-
<%
2-
rails_config = Rails.application.config
3-
require_options = {baseUrl: rails_config.assets.prefix}
4-
require_options.merge!(urlArgs: "instrument=1", waitSeconds: 0) if Teaspoon.configuration.use_coverage
5-
require_options.merge!(rails_config.requirejs.user_config) if rails_config.respond_to?(:requirejs)
6-
specs = @suite.spec_assets(false).map{ |s| "#{s.gsub(/\.js.*$/, "")}" }
7-
%>
8-
91
<%= javascript_include_tag @suite.helper %>
10-
<script type="text/javascript">
11-
Teaspoon.onWindowLoad(function () {
12-
// setup the Teaspoon path prefix to load /assets
13-
require.config(<%= require_options.to_json.html_safe %>);
14-
15-
// require specs by striping off the .js file extension
16-
require(<%= specs.to_json.html_safe %>, function() {
17-
Teaspoon.execute();
18-
});
19-
});
20-
</script>
2+
<script src="<%= "#{Teaspoon.configuration.mount_at}/js/#{@suite.name}.require.js" %>"></script>
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Teaspoon.version = <%= Teaspoon::VERSION.to_json.html_safe %>;
2+
Teaspoon.suites = <%= {all: Teaspoon.configuration.suite_configs.keys, active: @suite.name}.to_json.html_safe %>;
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<%
2+
rails_config = Rails.application.config
3+
require_options = {baseUrl: rails_config.assets.prefix}
4+
require_options.merge!(urlArgs: "instrument=1", waitSeconds: 0) if Teaspoon.configuration.use_coverage
5+
require_options.merge!(rails_config.requirejs.user_config) if rails_config.respond_to?(:requirejs)
6+
specs = @suite.spec_assets(false).map{ |s| "#{s.gsub(/\.js.*$/, "")}" }
7+
%>
8+
9+
Teaspoon.onWindowLoad(function () {
10+
// setup the Teaspoon path prefix to load /assets
11+
require.config(<%= require_options.to_json.html_safe %>);
12+
13+
// require specs by striping off the .js file extension
14+
require(<%= specs.to_json.html_safe %>, function() {
15+
Teaspoon.execute();
16+
});
17+
});

app/views/teaspoon/suite/show.html.erb

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
<%= stylesheet_link_tag *@suite.stylesheets %>
99
<%= javascript_include_tag *@suite.javascripts %>
10-
<script type="text/javascript">
11-
Teaspoon.version = <%= Teaspoon::VERSION.to_json.html_safe %>;
12-
Teaspoon.suites = <%= {all: Teaspoon.configuration.suite_configs.keys, active: @suite.name}.to_json.html_safe %>;
13-
</script>
10+
<script src="<%= "#{Teaspoon.configuration.mount_at}/js/#{@suite.name}.config.js" %>"></script>
1411
<%= render @suite.boot_partial %>
1512
</head>
1613
<body data-no-turbolink>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Teaspoon.onWindowLoad(Teaspoon.execute);

lib/teaspoon/engine.rb

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Engine < ::Rails::Engine
99
routes do
1010
root to: "suite#index"
1111
match "/fixtures/*filename", to: "suite#fixtures", via: :get, as: "fixture"
12+
match "/js/:suite.:jstype.js", to: "suite#js", via: :get, defaults: { suite: "default", jstype: "vanilla" }
1213
match "/:suite", to: "suite#show", via: :get, as: "suite", defaults: { suite: "default" }
1314
match "/:suite/:hook", to: "suite#hook", via: :post, defaults: { suite: "default", hook: "default" }
1415
end

0 commit comments

Comments
 (0)