Skip to content

Instantly share code, notes, and snippets.

@rilian
Forked from keithtom/no_animations.rb
Last active October 5, 2019 07:49
Show Gist options
  • Select an option

  • Save rilian/c259360c3ffa9562b29ec552c6b2b6c6 to your computer and use it in GitHub Desktop.

Select an option

Save rilian/c259360c3ffa9562b29ec552c6b2b6c6 to your computer and use it in GitHub Desktop.

Revisions

  1. rilian revised this gist May 18, 2017. 2 changed files with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions config-environments-test.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    Rails.application.configure do
    # ...

    require_relative '../../spec/support/no_animations'
    config.middleware.use Rack::NoAnimations
    end
    File renamed without changes.
  2. @keithtom keithtom revised this gist Feb 2, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion no_animations.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    module Rack
    # disable CSS3 and jQuery animations in test mode for speed and consistency. avoid timing issues.
    # disable CSS3 and jQuery animations in test mode for speed, consistency and avoiding timing issues.
    # Usage for Rails:
    # in config/environments/test.rb
    # config.middleware.use Rack::NoAnimations
    class NoAnimations
    def initialize(app, options = {})
    @app = app
  3. @keithtom keithtom created this gist Feb 2, 2014.
    51 changes: 51 additions & 0 deletions no_animations.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    module Rack
    # disable CSS3 and jQuery animations in test mode for speed and consistency. avoid timing issues.
    class NoAnimations
    def initialize(app, options = {})
    @app = app
    end

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

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

    response.finish
    end

    private

    def html?
    @headers["Content-Type"] =~ /html/
    end

    def inject(fragment)
    disable_animations = <<-EOF
    <script type="text/javascript">(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);</script>
    <style>
    * {
    -o-transition: none !important;
    -moz-transition: none !important;
    -ms-transition: none !important;
    -webkit-transition: none !important;
    transition: none !important;
    -o-transform: none !important;
    -moz-transform: none !important;
    -ms-transform: none !important;
    -webkit-transform: none !important;
    transform: none !important;
    -webkit-animation: none !important;
    -moz-animation: none !important;
    -o-animation: none !important;
    -ms-animation: none !important;
    animation: none !important;
    }
    </style>
    EOF
    fragment.gsub(%r{</head>}, disable_animations + "</head>")
    end
    end
    end