From cae4eeb474a9742605384fa0c7a14ff265357095 Mon Sep 17 00:00:00 2001 From: Tang Rufus Date: Wed, 30 Dec 2015 07:21:25 +0800 Subject: [PATCH 1/2] Add skip_display_none option --- .rubocop.yml | 3 +-- README.md | 0 app/helpers/react_on_rails_helper.rb | 2 +- .../base/base/config/initializers/react_on_rails.rb | 2 ++ lib/react_on_rails/configuration.rb | 10 +++++++--- spec/dummy/spec/helpers/react_on_rails_helper_spec.rb | 0 6 files changed, 11 insertions(+), 6 deletions(-) mode change 100644 => 100755 README.md mode change 100644 => 100755 app/helpers/react_on_rails_helper.rb mode change 100644 => 100755 lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb mode change 100644 => 100755 lib/react_on_rails/configuration.rb mode change 100644 => 100755 spec/dummy/spec/helpers/react_on_rails_helper_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index 0f536fb45..9ee935980 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -43,7 +43,7 @@ Lint/HandleExceptions: # Offense count: 1 Metrics/AbcSize: - Max: 23 + Max: 24 # Offense count: 1 # Configuration parameters: CountComments. @@ -69,4 +69,3 @@ Metrics/ModuleLength: Style/GlobalVars: Exclude: - 'spec/dummy/config/environments/development.rb' - diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/app/helpers/react_on_rails_helper.rb b/app/helpers/react_on_rails_helper.rb old mode 100644 new mode 100755 index 6d4654afc..953bf1053 --- a/app/helpers/react_on_rails_helper.rb +++ b/app/helpers/react_on_rails_helper.rb @@ -61,7 +61,7 @@ def react_component(component_name, props = {}, options = {}) content_tag(:div, "", class: "js-react-on-rails-component", - style: "display:none", + style: ReactOnRails.configuration.skip_display_none ? nil : "display:none", data: data) # Create the HTML rendering part diff --git a/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb b/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb old mode 100644 new mode 100755 index 6327d3e3d..fda79b4e6 --- a/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +++ b/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb @@ -23,4 +23,6 @@ config.prerender = false # Default is true for development, off otherwise config.trace = Rails.env.development? + # Default is false, enable if your content security policy doesn't include `style-src: 'unsafe-inline'` + config.skip_display_none = false end diff --git a/lib/react_on_rails/configuration.rb b/lib/react_on_rails/configuration.rb old mode 100644 new mode 100755 index b4cd6de1c..3cddda898 --- a/lib/react_on_rails/configuration.rb +++ b/lib/react_on_rails/configuration.rb @@ -14,19 +14,22 @@ def self.configuration trace: Rails.env.development?, development_mode: Rails.env.development?, server_renderer_pool_size: 1, - server_renderer_timeout: 20) + server_renderer_timeout: 20, + skip_display_none: false) end class Configuration attr_accessor :server_bundle_js_file, :prerender, :replay_console, :trace, :development_mode, :logging_on_server, :server_renderer_pool_size, - :server_renderer_timeout, :raise_on_prerender_error + :server_renderer_timeout, :raise_on_prerender_error, + :skip_display_none def initialize(server_bundle_js_file: nil, prerender: nil, replay_console: nil, trace: nil, development_mode: nil, logging_on_server: nil, server_renderer_pool_size: nil, - server_renderer_timeout: nil, raise_on_prerender_error: nil) + server_renderer_timeout: nil, raise_on_prerender_error: nil, + skip_display_none: nil) if File.exist?(server_bundle_js_file) self.server_bundle_js_file = server_bundle_js_file else @@ -43,6 +46,7 @@ def initialize(server_bundle_js_file: nil, prerender: nil, replay_console: nil, end self.trace = trace.nil? ? Rails.env.development? : trace self.raise_on_prerender_error = raise_on_prerender_error + self.skip_display_none = skip_display_none # Server rendering: self.server_renderer_pool_size = self.development_mode ? 1 : server_renderer_pool_size diff --git a/spec/dummy/spec/helpers/react_on_rails_helper_spec.rb b/spec/dummy/spec/helpers/react_on_rails_helper_spec.rb old mode 100644 new mode 100755 From 22f0815466ffd6b2c73ba462a2914ea309bd4e67 Mon Sep 17 00:00:00 2001 From: Aaron Van Bokhoven Date: Thu, 21 Jan 2016 20:58:08 -1000 Subject: [PATCH 2/2] Add tests for skip_display_none config option. --- README.md | 0 app/helpers/react_on_rails_helper.rb | 0 .../config/initializers/react_on_rails.rb | 0 lib/react_on_rails/configuration.rb | 0 .../config/initializers/react_on_rails.rb | 3 ++ .../helpers/react_on_rails_helper_spec.rb | 31 +++++++++++++++++++ spec/react_on_rails/configuration_spec.rb | 13 ++++++++ 7 files changed, 47 insertions(+) mode change 100755 => 100644 README.md mode change 100755 => 100644 app/helpers/react_on_rails_helper.rb mode change 100755 => 100644 lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb mode change 100755 => 100644 lib/react_on_rails/configuration.rb mode change 100755 => 100644 spec/dummy/spec/helpers/react_on_rails_helper_spec.rb diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/app/helpers/react_on_rails_helper.rb b/app/helpers/react_on_rails_helper.rb old mode 100755 new mode 100644 diff --git a/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb b/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb old mode 100755 new mode 100644 diff --git a/lib/react_on_rails/configuration.rb b/lib/react_on_rails/configuration.rb old mode 100755 new mode 100644 diff --git a/spec/dummy/config/initializers/react_on_rails.rb b/spec/dummy/config/initializers/react_on_rails.rb index 666fce86c..20c0f2216 100644 --- a/spec/dummy/config/initializers/react_on_rails.rb +++ b/spec/dummy/config/initializers/react_on_rails.rb @@ -18,4 +18,7 @@ # Server rendering only (not for render_component helper) config.server_renderer_pool_size = 1 # increase if you're on JRuby config.server_renderer_timeout = 20 # seconds + + # Default is false, enable if your content security policy doesn't include `style-src: 'unsafe-inline'` + config.skip_display_none = false end diff --git a/spec/dummy/spec/helpers/react_on_rails_helper_spec.rb b/spec/dummy/spec/helpers/react_on_rails_helper_spec.rb old mode 100755 new mode 100644 index 67444fa33..64eba76c4 --- a/spec/dummy/spec/helpers/react_on_rails_helper_spec.rb +++ b/spec/dummy/spec/helpers/react_on_rails_helper_spec.rb @@ -74,6 +74,37 @@ it { is_expected.not_to include react_component_div } it { is_expected.to include react_definition_div } end + + context "with skip_display_none option true" do + before { ReactOnRails.configuration.skip_display_none = true } + + let(:react_definition_div_skip_display_none_true) do + "
".squish + end + + it { is_expected.to include react_definition_div_skip_display_none_true } + end + + context "with skip_display_none option false" do + before { ReactOnRails.configuration.skip_display_none = false } + + let(:react_definition_div_skip_display_none_false) do + "
".squish + end + + it { is_expected.to include react_definition_div_skip_display_none_false } + end end describe "#server_render_js" do diff --git a/spec/react_on_rails/configuration_spec.rb b/spec/react_on_rails/configuration_spec.rb index bf9a0e6b8..779c2a789 100644 --- a/spec/react_on_rails/configuration_spec.rb +++ b/spec/react_on_rails/configuration_spec.rb @@ -21,5 +21,18 @@ module ReactOnRails expect(ReactOnRails.configuration.server_bundle_js_file).to eq("client/dist/something.js") expect(ReactOnRails.configuration.prerender).to eq(true) end + + context "skip display: none" do + it "will default false" do + expect(ReactOnRails.configuration.skip_display_none).to eq(false) + end + + it "will be true if set to true" do + ReactOnRails.configure do |config| + config.skip_display_none = true + end + expect(ReactOnRails.configuration.skip_display_none).to eq(true) + end + end end end