Sha256: 4630023bcfbd61dacbabae069d2ed03add0bffa00b7fd853f3bc48b07c83fb25

Contents?: true

Size: 1 KB

Versions: 7

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module Capybara
  class Server
    class AnimationDisabler
      def initialize(app)
        @app = app
      end

      def call(env)
        @status, @headers, @body = @app.call(env)
        return [@status, @headers, @body] unless html_content?
        response = Rack::Response.new([], @status, @headers)

        @body.each { |html| response.write insert_disable(html) }
        @body.close if @body.respond_to?(:close)

        response.finish
      end

    private

      def html_content?
        !!(@headers['Content-Type'] =~ /html/)
      end

      def insert_disable(html)
        html.sub(%r{(</head>)}, DISABLE_MARKUP + '\\1')
      end

      DISABLE_MARKUP = <<~HTML
        <script defer>(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);</script>
        <style>
          * {
             transition: none !important;
             animation-duration: 0s !important;
             animation-delay: 0s !important;
          }
        </style>
      HTML
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
tdiary-5.0.11 vendor/bundle/gems/capybara-3.6.0/lib/capybara/server/animation_disabler.rb
capybara-3.6.0 lib/capybara/server/animation_disabler.rb
capybara-3.5.1 lib/capybara/server/animation_disabler.rb
capybara-3.5.0 lib/capybara/server/animation_disabler.rb
capybara-3.4.2 lib/capybara/server/animation_disabler.rb
capybara-3.4.1 lib/capybara/server/animation_disabler.rb
capybara-3.4.0 lib/capybara/server/animation_disabler.rb