Skip to content

Commit

Permalink
Merge pull request #2498 from javierm/animation_disabler_invalid_html
Browse files Browse the repository at this point in the history
Fix invalid HTML generated by AnimationDisabler
  • Loading branch information
twalpole authored Oct 3, 2021
2 parents 0468de5 + cad1efc commit 5e51607
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/capybara/server/animation_disabler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def self.selector_for(css_or_bool)

def initialize(app)
@app = app
@disable_markup = format(DISABLE_MARKUP_TEMPLATE, selector: self.class.selector_for(Capybara.disable_animation))
@disable_css_markup = format(DISABLE_CSS_MARKUP_TEMPLATE,
selector: self.class.selector_for(Capybara.disable_animation))
@disable_js_markup = format(DISABLE_JS_MARKUP_TEMPLATE,
selector: self.class.selector_for(Capybara.disable_animation))
end

def call(env)
Expand All @@ -33,22 +36,17 @@ def call(env)

private

attr_reader :disable_markup
attr_reader :disable_css_markup, :disable_js_markup

def html_content?
/html/.match?(@headers['Content-Type'])
end

def insert_disable(html)
html.sub(%r{(</body>)}, "#{disable_markup}\\1")
html.sub(%r{(</head>)}, "#{disable_css_markup}\\1").sub(%r{(</body>)}, "#{disable_js_markup}\\1")
end

DISABLE_MARKUP_TEMPLATE = <<~HTML
<script>
//<![CDATA[
(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);
//]]>
</script>
DISABLE_CSS_MARKUP_TEMPLATE = <<~HTML
<style>
%<selector>s, %<selector>s::before, %<selector>s::after {
transition: none !important;
Expand All @@ -58,6 +56,14 @@ def insert_disable(html)
}
</style>
HTML

DISABLE_JS_MARKUP_TEMPLATE = <<~HTML
<script>
//<![CDATA[
(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);
//]]>
</script>
HTML
end
end
end
6 changes: 6 additions & 0 deletions spec/shared_selenium_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@
@animation_session = Capybara::Session.new(session.mode, TestApp.new)
end

it 'should add CSS to the <head> element' do
@animation_session.visit('with_animation')

expect(@animation_session).to have_selector(:css, 'head > style', text: 'transition: none', visible: :hidden)
end

it 'should disable CSS transitions' do
@animation_session.visit('with_animation')
@animation_session.click_link('transition me away')
Expand Down

0 comments on commit 5e51607

Please sign in to comment.