Class: Capybara::Server::AnimationDisabler Private
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/capybara/server/animation_disabler.rb |
Constant Summary
-
DISABLE_CSS_MARKUP_TEMPLATE =
# File 'lib/capybara/server/animation_disabler.rb', line 64<<~CSS %<selector>s, %<selector>s::before, %<selector>s::after { transition: none !important; animation-duration: 0s !important; animation-delay: 0s !important; scroll-behavior: auto !important; } CSS
-
DISABLE_JS_MARKUP_TEMPLATE =
# File 'lib/capybara/server/animation_disabler.rb', line 73<<~SCRIPT //<![CDATA[ (typeof jQuery !== 'undefined') && (jQuery.fx.off = true); //]]> SCRIPT
Class Method Summary
- .new(app) ⇒ AnimationDisabler constructor Internal use only
- .selector_for(css_or_bool) Internal use only
Instance Attribute Summary
- #disable_css_markup readonly private Internal use only
- #disable_js_markup readonly private Internal use only
Instance Method Summary
- #call(env) Internal use only
- #directive_nonces(headers) private Internal use only
- #html_content?(headers) ⇒ Boolean private Internal use only
- #insert_disable(html, nonces) private Internal use only
Constructor Details
.new(app) ⇒ AnimationDisabler
# File 'lib/capybara/server/animation_disabler.rb', line 17
def initialize(app) @app = app @disable_css_markup = format(DISABLE_CSS_MARKUP_TEMPLATE, selector: self.class.selector_for(Capybara.disable_animation)) @disable_js_markup = +DISABLE_JS_MARKUP_TEMPLATE end
Class Method Details
.selector_for(css_or_bool)
[ GitHub ]# File 'lib/capybara/server/animation_disabler.rb', line 6
def self.selector_for(css_or_bool) case css_or_bool when String css_or_bool when true '*' else raise CapybaraError, 'Capybara.disable_animation supports either a String (the css selector to disable) or a boolean' end end
Instance Attribute Details
#disable_css_markup (readonly, private)
[ GitHub ]# File 'lib/capybara/server/animation_disabler.rb', line 39
attr_reader :disable_css_markup, :disable_js_markup
#disable_js_markup (readonly, private)
[ GitHub ]# File 'lib/capybara/server/animation_disabler.rb', line 39
attr_reader :disable_css_markup, :disable_js_markup
Instance Method Details
#call(env)
[ GitHub ]# File 'lib/capybara/server/animation_disabler.rb', line 24
def call(env) status, headers, body = @app.call(env) return [status, headers, body] unless html_content?(headers) nonces = directive_nonces(headers).transform_values { |nonce| "nonce=\"#{nonce}\"" if nonce && !nonce.empty? } response = Rack::Response.new([], status, headers) body.each { |html| response.write insert_disable(html, nonces) } body.close if body.respond_to?(:close) response.finish end
#directive_nonces(headers) (private)
[ GitHub ]# File 'lib/capybara/server/animation_disabler.rb', line 50
def directive_nonces(headers) headers.fetch('Content-Security-Policy', '') .split(';') .map(&:split) # rubocop:disable Style/MapToHash .to_h do |s| [ s[0], s[1..].filter_map do |value| /^'nonce-(?<nonce>.+)'/ =~ value nonce end[0] ] end end
#html_content?(headers) ⇒ Boolean
(private)
# File 'lib/capybara/server/animation_disabler.rb', line 41
def html_content?(headers) /html/.match?(headers['Content-Type']) # rubocop:todo Performance/StringInclude end
#insert_disable(html, nonces) (private)
[ GitHub ]# File 'lib/capybara/server/animation_disabler.rb', line 45
def insert_disable(html, nonces) html.sub(%r{(</head>)}, "<style #{nonces['style-src']}>#{disable_css_markup}</style>\\1") .sub(%r{(</body>)}, "<script #{nonces['script-src']}>#{disable_js_markup}</script>\\1") end