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

Move inline JS to rendered files to support CSP #561

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/controllers/teaspoon/suite_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def show
@suite = Teaspoon::Suite.new(params)
end

def js
jstype = params.extract!(:jstype).fetch(:jstype, 'vanilla')
@suite = Teaspoon::Suite.new(params)
render "teaspoon/suite/#{jstype}", :content_type => 'application/javascript'
end

def hook
hooks = Teaspoon::Suite.new(params).hooks[params[:hook].to_s]

Expand Down
4 changes: 1 addition & 3 deletions app/views/teaspoon/suite/_boot.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
<%= javascript_include_tag *@suite.spec_assets, debug: @suite.config.expand_assets, allow_non_precompiled: true %>
<script type="text/javascript">
Teaspoon.onWindowLoad(Teaspoon.execute);
</script>
<script src="<%= "#{Teaspoon.configuration.mount_at}/js/#{@suite.name}.vanilla.js" %>"></script>
20 changes: 1 addition & 19 deletions app/views/teaspoon/suite/_boot_require_js.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,2 @@
<%
rails_config = Rails.application.config
require_options = {baseUrl: rails_config.assets.prefix}
require_options.merge!(urlArgs: "instrument=1", waitSeconds: 0) if Teaspoon.configuration.use_coverage
require_options.merge!(rails_config.requirejs.user_config) if rails_config.respond_to?(:requirejs)
specs = @suite.spec_assets(false).map{ |s| "#{s.gsub(/\.js.*$/, "")}" }
%>

<%= javascript_include_tag @suite.helper %>
<script type="text/javascript">
Teaspoon.onWindowLoad(function () {
// setup the Teaspoon path prefix to load /assets
require.config(<%= require_options.to_json.html_safe %>);

// require specs by striping off the .js file extension
require(<%= specs.to_json.html_safe %>, function() {
Teaspoon.execute();
});
});
</script>
<script src="<%= "#{Teaspoon.configuration.mount_at}/js/#{@suite.name}.require.js" %>"></script>
2 changes: 2 additions & 0 deletions app/views/teaspoon/suite/config.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Teaspoon.version = <%= Teaspoon::VERSION.to_json.html_safe %>;
Teaspoon.suites = <%= {all: Teaspoon.configuration.suite_configs.keys, active: @suite.name}.to_json.html_safe %>;
17 changes: 17 additions & 0 deletions app/views/teaspoon/suite/require.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%
rails_config = Rails.application.config
require_options = {baseUrl: rails_config.assets.prefix}
require_options.merge!(urlArgs: "instrument=1", waitSeconds: 0) if Teaspoon.configuration.use_coverage
require_options.merge!(rails_config.requirejs.user_config) if rails_config.respond_to?(:requirejs)
specs = @suite.spec_assets(false).map{ |s| "#{s.gsub(/\.js.*$/, "")}" }
%>

Teaspoon.onWindowLoad(function () {
// setup the Teaspoon path prefix to load /assets
require.config(<%= require_options.to_json.html_safe %>);

// require specs by striping off the .js file extension
require(<%= specs.to_json.html_safe %>, function() {
Teaspoon.execute();
});
});
5 changes: 1 addition & 4 deletions app/views/teaspoon/suite/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

<%= stylesheet_link_tag *@suite.stylesheets %>
<%= javascript_include_tag *@suite.javascripts %>
<script type="text/javascript">
Teaspoon.version = <%= Teaspoon::VERSION.to_json.html_safe %>;
Teaspoon.suites = <%= {all: Teaspoon.configuration.suite_configs.keys, active: @suite.name}.to_json.html_safe %>;
</script>
<script src="<%= "#{Teaspoon.configuration.mount_at}/js/#{@suite.name}.config.js" %>"></script>
<%= render @suite.boot_partial %>
</head>
<body data-no-turbolink>
Expand Down
1 change: 1 addition & 0 deletions app/views/teaspoon/suite/vanilla.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Teaspoon.onWindowLoad(Teaspoon.execute);
1 change: 1 addition & 0 deletions lib/teaspoon/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Engine < ::Rails::Engine
routes do
root to: "suite#index"
match "/fixtures/*filename", to: "suite#fixtures", via: :get, as: "fixture"
match "/js/:suite.:jstype.js", to: "suite#js", via: :get, defaults: { suite: "default", jstype: "vanilla" }
match "/:suite", to: "suite#show", via: :get, as: "suite", defaults: { suite: "default" }
match "/:suite/:hook", to: "suite#hook", via: :post, defaults: { suite: "default", hook: "default" }
end
Expand Down